Danila-Pechenev commited on
Commit
f023709
1 Parent(s): 105b241

Delete unnecessary type hints

Browse files
Files changed (3) hide show
  1. app/app.py +4 -4
  2. app/model.py +16 -18
  3. test/test_model.py +5 -17
app/app.py CHANGED
@@ -18,19 +18,19 @@ def describe_service():
18
 
19
 
20
  @st.experimental_memo
21
- def call_model(uploaded_file):
22
  return model.run_model(uploaded_file, st.session_state["model"])
23
 
24
 
25
  def process_image():
26
- uploaded_file: io.BytesIO = st.file_uploader(
27
  label="Choose a file (you can upload new files without refreshing the page)",
28
  type=["png", "jpg", "jpeg"],
29
  )
30
  if uploaded_file:
31
- placeholder: st.delta_generator.DeltaGenerator = st.empty()
32
  placeholder.info("The image is being processed. It may take some time. Wait, please...")
33
- image: Image.Image = call_model(uploaded_file)
34
  placeholder.empty()
35
  placeholder.image(image)
36
  with open("user_data/output.jpg", "rb") as file:
 
18
 
19
 
20
  @st.experimental_memo
21
+ def call_model(uploaded_file: io.BytesIO) -> Image.Image:
22
  return model.run_model(uploaded_file, st.session_state["model"])
23
 
24
 
25
  def process_image():
26
+ uploaded_file = st.file_uploader(
27
  label="Choose a file (you can upload new files without refreshing the page)",
28
  type=["png", "jpg", "jpeg"],
29
  )
30
  if uploaded_file:
31
+ placeholder = st.empty()
32
  placeholder.info("The image is being processed. It may take some time. Wait, please...")
33
+ image = call_model(uploaded_file)
34
  placeholder.empty()
35
  placeholder.image(image)
36
  with open("user_data/output.jpg", "rb") as file:
app/model.py CHANGED
@@ -11,24 +11,22 @@ def create_model() -> keras.Model:
11
 
12
 
13
  def run_model(image_bytes: io.BytesIO, model: keras.Model) -> Image.Image:
14
- image1: Image.Image = Image.open(image_bytes)
15
- width: int
16
- height: int
17
- width, height = image1.size
18
- image2: Image.Image = image1.resize((960, 640))
19
- image_array1: np.ndarray = keras.utils.img_to_array(image2)
20
- image_array2: np.ndarray = image_array1.astype("float32") / 255.0
21
- image_array3: np.ndarray = np.expand_dims(image_array2, axis=0)
22
- output: np.ndarray = model.predict(image_array3)
23
- output_image_array1: np.ndarray = output[0] * 255.0
24
- output_image_array2: np.ndarray = output_image_array1.clip(0, 255)
25
- output_image_array3: np.ndarray = output_image_array2.reshape(
26
- (np.shape(output_image_array2)[0], np.shape(output_image_array2)[1], 3)
27
  )
28
- output_image_array5: np.ndarray = output_image_array3.astype(np.uint8)
29
- output_image1: Image.Image = Image.fromarray(output_image_array5)
30
- output_image2: Image.Image = output_image1.resize((width, height))
31
  if not os.path.exists("user_data"):
32
  os.makedirs("user_data")
33
- output_image2.save("user_data/output.jpg")
34
- return output_image2
 
11
 
12
 
13
  def run_model(image_bytes: io.BytesIO, model: keras.Model) -> Image.Image:
14
+ image = Image.open(image_bytes)
15
+ width, height = image.size
16
+ image = image.resize((960, 640))
17
+ image_array = keras.utils.img_to_array(image)
18
+ image_array = image_array.astype("float32") / 255.0
19
+ image_array = np.expand_dims(image_array, axis=0)
20
+ output = model.predict(image_array)
21
+ output_image_array = output[0] * 255.0
22
+ output_image_array = output_image_array.clip(0, 255)
23
+ output_image_array = output_image_array.reshape(
24
+ (np.shape(output_image_array)[0], np.shape(output_image_array)[1], 3)
 
 
25
  )
26
+ output_image_array = output_image_array.astype(np.uint8)
27
+ output_image = Image.fromarray(output_image_array)
28
+ output_image = output_image.resize((width, height))
29
  if not os.path.exists("user_data"):
30
  os.makedirs("user_data")
31
+ output_image.save("user_data/output.jpg")
32
+ return output_image
test/test_model.py CHANGED
@@ -1,27 +1,21 @@
1
  from PIL import Image
2
- from tensorflow import keras
3
  import io
4
  import os
5
  from ..app import model
6
 
7
- model_instance: keras.Model = model.create_model()
8
 
9
 
10
- def template(filename: str) -> tuple[int, int, Image.Image]:
11
- image: Image.Image = Image.open(filename)
12
- width: int
13
- height: int
14
  width, height = image.size
15
- image_bytes: io.BytesIO = io.BytesIO()
16
  image.save(image_bytes, format=image.format)
17
- output_image: Image.Image = model.run_model(image_bytes, model_instance)
18
  return width, height, output_image
19
 
20
 
21
  def test_image_jpg():
22
- width: int
23
- height: int
24
- output_image: Image.Image
25
  width, height, output_image = template(os.path.join(os.getcwd(), "test/test_images/test1.jpg"))
26
 
27
  assert width == output_image.size[0]
@@ -29,9 +23,6 @@ def test_image_jpg():
29
 
30
 
31
  def test_image_png():
32
- width: int
33
- height: int
34
- output_image: Image.Image
35
  width, height, output_image = template(os.path.join(os.getcwd(), "test/test_images/test2.png"))
36
 
37
  assert width == output_image.size[0]
@@ -39,9 +30,6 @@ def test_image_png():
39
 
40
 
41
  def test_image_jpeg():
42
- width: int
43
- height: int
44
- output_image: Image.Image
45
  width, height, output_image = template(os.path.join(os.getcwd(), "test/test_images/test3.jpeg"))
46
 
47
  assert width == output_image.size[0]
 
1
  from PIL import Image
 
2
  import io
3
  import os
4
  from ..app import model
5
 
6
+ model_instance = model.create_model()
7
 
8
 
9
+ def template(filename: str):
10
+ image = Image.open(filename)
 
 
11
  width, height = image.size
12
+ image_bytes = io.BytesIO()
13
  image.save(image_bytes, format=image.format)
14
+ output_image = model.run_model(image_bytes, model_instance)
15
  return width, height, output_image
16
 
17
 
18
  def test_image_jpg():
 
 
 
19
  width, height, output_image = template(os.path.join(os.getcwd(), "test/test_images/test1.jpg"))
20
 
21
  assert width == output_image.size[0]
 
23
 
24
 
25
  def test_image_png():
 
 
 
26
  width, height, output_image = template(os.path.join(os.getcwd(), "test/test_images/test2.png"))
27
 
28
  assert width == output_image.size[0]
 
30
 
31
 
32
  def test_image_jpeg():
 
 
 
33
  width, height, output_image = template(os.path.join(os.getcwd(), "test/test_images/test3.jpeg"))
34
 
35
  assert width == output_image.size[0]