Spaces:
Configuration error
Configuration error
Danila-Pechenev
commited on
Commit
•
6b89d2d
1
Parent(s):
f023709
Improve code in `model.py`
Browse files- app/model.py +6 -9
app/model.py
CHANGED
@@ -14,19 +14,16 @@ 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 |
-
|
27 |
-
|
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
|
|
|
14 |
image = Image.open(image_bytes)
|
15 |
width, height = image.size
|
16 |
image = image.resize((960, 640))
|
17 |
+
image_array = np.expand_dims(keras.utils.img_to_array(image).astype("float32") / 255.0, axis=0)
|
|
|
|
|
18 |
output = model.predict(image_array)
|
19 |
+
output_image_array = (output[0] * 255.0).clip(0, 255)
|
|
|
20 |
output_image_array = output_image_array.reshape(
|
21 |
(np.shape(output_image_array)[0], np.shape(output_image_array)[1], 3)
|
22 |
+
).astype(np.uint8)
|
23 |
+
output_image = Image.fromarray(output_image_array).resize((width, height))
|
24 |
+
|
|
|
25 |
if not os.path.exists("user_data"):
|
26 |
os.makedirs("user_data")
|
27 |
output_image.save("user_data/output.jpg")
|
28 |
+
|
29 |
return output_image
|