Update app.py
Browse files
app.py
CHANGED
|
@@ -36,21 +36,58 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 36 |
)
|
| 37 |
|
| 38 |
|
| 39 |
-
def
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
def imageRotation(image):
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
|
| 39 |
+
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
|
| 40 |
+
best_ratio_diff = float('inf')
|
| 41 |
+
best_ratio = (1, 1)
|
| 42 |
+
area = width * height
|
| 43 |
+
for ratio in target_ratios:
|
| 44 |
+
target_aspect_ratio = ratio[0] / ratio[1]
|
| 45 |
+
ratio_diff = abs(aspect_ratio - target_aspect_ratio)
|
| 46 |
+
if ratio_diff < best_ratio_diff:
|
| 47 |
+
best_ratio_diff = ratio_diff
|
| 48 |
+
best_ratio = ratio
|
| 49 |
+
elif ratio_diff == best_ratio_diff:
|
| 50 |
+
if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
|
| 51 |
+
best_ratio = ratio
|
| 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 |
+
# calculate the existing image aspect ratio
|
| 59 |
+
target_ratios = set(
|
| 60 |
+
(i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
|
| 61 |
+
i * j <= max_num and i * j >= min_num)
|
| 62 |
+
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
|
| 63 |
+
|
| 64 |
+
# find the closest aspect ratio to the target
|
| 65 |
+
target_aspect_ratio = find_closest_aspect_ratio(
|
| 66 |
+
aspect_ratio, target_ratios, orig_width, orig_height, image_size)
|
| 67 |
+
|
| 68 |
+
# calculate the target width and height
|
| 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 |
+
# resize the image
|
| 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 |
+
# split the image
|
| 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):
|