Update app.py
Browse files
app.py
CHANGED
@@ -52,42 +52,52 @@ def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_
|
|
52 |
return best_ratio
|
53 |
|
54 |
def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
|
|
|
|
|
|
|
|
|
55 |
orig_width, orig_height = image.size
|
56 |
aspect_ratio = orig_width / orig_height
|
57 |
|
58 |
-
#
|
59 |
target_ratios = set(
|
60 |
-
(i, j) for n in range(min_num, max_num + 1)
|
61 |
-
i
|
|
|
|
|
|
|
62 |
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
|
63 |
|
64 |
-
#
|
65 |
target_aspect_ratio = find_closest_aspect_ratio(
|
66 |
-
aspect_ratio, target_ratios, orig_width, orig_height, image_size
|
|
|
67 |
|
68 |
-
#
|
69 |
target_width = image_size * target_aspect_ratio[0]
|
70 |
target_height = image_size * target_aspect_ratio[1]
|
71 |
blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
|
72 |
|
73 |
-
#
|
74 |
resized_img = image.resize((target_width, target_height))
|
75 |
processed_images = []
|
76 |
for i in range(blocks):
|
|
|
77 |
box = (
|
78 |
(i % (target_width // image_size)) * image_size,
|
79 |
(i // (target_width // image_size)) * image_size,
|
80 |
((i % (target_width // image_size)) + 1) * image_size,
|
81 |
((i // (target_width // image_size)) + 1) * image_size
|
82 |
)
|
83 |
-
#
|
84 |
split_img = resized_img.crop(box)
|
85 |
processed_images.append(split_img)
|
86 |
assert len(processed_images) == blocks
|
87 |
if use_thumbnail and len(processed_images) != 1:
|
88 |
thumbnail_img = image.resize((image_size, image_size))
|
89 |
processed_images.append(thumbnail_img)
|
90 |
-
return processed_images
|
|
|
91 |
|
92 |
|
93 |
def imageRotation(image):
|
|
|
52 |
return best_ratio
|
53 |
|
54 |
def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
|
55 |
+
# Ensure the image is a PIL Image
|
56 |
+
if not isinstance(image, Image.Image):
|
57 |
+
image = Image.fromarray(image)
|
58 |
+
|
59 |
orig_width, orig_height = image.size
|
60 |
aspect_ratio = orig_width / orig_height
|
61 |
|
62 |
+
# Calculate the existing image aspect ratio
|
63 |
target_ratios = set(
|
64 |
+
(i, j) for n in range(min_num, max_num + 1)
|
65 |
+
for i in range(1, n + 1)
|
66 |
+
for j in range(1, n + 1)
|
67 |
+
if i * j <= max_num and i * j >= min_num
|
68 |
+
)
|
69 |
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
|
70 |
|
71 |
+
# Find the closest aspect ratio to the target
|
72 |
target_aspect_ratio = find_closest_aspect_ratio(
|
73 |
+
aspect_ratio, target_ratios, orig_width, orig_height, image_size
|
74 |
+
)
|
75 |
|
76 |
+
# Calculate the target width and height
|
77 |
target_width = image_size * target_aspect_ratio[0]
|
78 |
target_height = image_size * target_aspect_ratio[1]
|
79 |
blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
|
80 |
|
81 |
+
# Resize the image
|
82 |
resized_img = image.resize((target_width, target_height))
|
83 |
processed_images = []
|
84 |
for i in range(blocks):
|
85 |
+
# Calculate the crop box for each block
|
86 |
box = (
|
87 |
(i % (target_width // image_size)) * image_size,
|
88 |
(i // (target_width // image_size)) * image_size,
|
89 |
((i % (target_width // image_size)) + 1) * image_size,
|
90 |
((i // (target_width // image_size)) + 1) * image_size
|
91 |
)
|
92 |
+
# Split the image
|
93 |
split_img = resized_img.crop(box)
|
94 |
processed_images.append(split_img)
|
95 |
assert len(processed_images) == blocks
|
96 |
if use_thumbnail and len(processed_images) != 1:
|
97 |
thumbnail_img = image.resize((image_size, image_size))
|
98 |
processed_images.append(thumbnail_img)
|
99 |
+
return processed_images[0]
|
100 |
+
|
101 |
|
102 |
|
103 |
def imageRotation(image):
|