Spaces:
Configuration error
Configuration error
Danila-Pechenev
commited on
Commit
•
f023709
1
Parent(s):
105b241
Delete unnecessary type hints
Browse files- app/app.py +4 -4
- app/model.py +16 -18
- 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
|
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
|
32 |
placeholder.info("The image is being processed. It may take some time. Wait, please...")
|
33 |
-
image
|
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 |
-
|
15 |
-
width
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
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 |
-
|
29 |
-
|
30 |
-
|
31 |
if not os.path.exists("user_data"):
|
32 |
os.makedirs("user_data")
|
33 |
-
|
34 |
-
return
|
|
|
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
|
8 |
|
9 |
|
10 |
-
def template(filename: str)
|
11 |
-
image
|
12 |
-
width: int
|
13 |
-
height: int
|
14 |
width, height = image.size
|
15 |
-
image_bytes
|
16 |
image.save(image_bytes, format=image.format)
|
17 |
-
output_image
|
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]
|