Spaces:
Running
Running
Flavio de Oliveira
commited on
Commit
•
be7bcc5
1
Parent(s):
1b337ee
Create resize function
Browse files
app.py
CHANGED
@@ -5,9 +5,26 @@ import tempfile
|
|
5 |
import os
|
6 |
import yaml
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def predict(input_image: Image.Image):
|
9 |
|
10 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# Used as a context manager. Takes care of cleaning up the directory.
|
12 |
# Even if an error is raised within the with block, the directory is removed.
|
13 |
# No finally block needed
|
|
|
5 |
import os
|
6 |
import yaml
|
7 |
|
8 |
+
def resize_image(image, base_height):
|
9 |
+
|
10 |
+
# Calculate aspect ratio
|
11 |
+
w_percent = base_height / float(image.size[1])
|
12 |
+
w_size = int(float(image.size[0]) * float(w_percent))
|
13 |
+
|
14 |
+
# Resize the image
|
15 |
+
return image.resize((w_size, base_height), Image.ANTIALIAS)
|
16 |
+
|
17 |
def predict(input_image: Image.Image):
|
18 |
|
19 |
try:
|
20 |
+
|
21 |
+
# Try to resize the image to a fixed height of 128 pixels
|
22 |
+
try:
|
23 |
+
input_image = resize_image(input_image, 128)
|
24 |
+
except Exception as e:
|
25 |
+
print(f"Image resizing failed: {e}")
|
26 |
+
return f"Image resizing failed: {e}"
|
27 |
+
|
28 |
# Used as a context manager. Takes care of cleaning up the directory.
|
29 |
# Even if an error is raised within the with block, the directory is removed.
|
30 |
# No finally block needed
|