Update app.py
Browse files
app.py
CHANGED
@@ -36,67 +36,33 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
36 |
)
|
37 |
|
38 |
|
39 |
-
def
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
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 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
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 |
|
|
|
36 |
)
|
37 |
|
38 |
|
39 |
+
def preprocessing(image, image_size=448):
|
40 |
+
"""
|
41 |
+
Apply enhancement filters and pad the image to match the target size while keeping full content.
|
42 |
+
"""
|
43 |
+
# Convert input to a PIL Image (if it isn’t already)
|
44 |
+
image = Image.fromarray(np.array(image))
|
45 |
+
|
46 |
+
# Apply enhancement filters
|
47 |
+
image = ImageEnhance.Sharpness(image).enhance(2.0) # Increase sharpness
|
48 |
+
image = ImageEnhance.Contrast(image).enhance(1.5) # Increase contrast
|
49 |
+
image = ImageEnhance.Brightness(image).enhance(0.8) # Reduce brightness
|
50 |
+
|
51 |
+
# Get original dimensions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
orig_width, orig_height = image.size
|
53 |
+
|
54 |
+
# Determine the padding needed to fit the image within a square of size `image_size`
|
55 |
+
pad_x = max(image_size - orig_width, 0)
|
56 |
+
pad_y = max(image_size - orig_height, 0)
|
57 |
+
|
58 |
+
# Create a new blank image with a white background
|
59 |
+
padded_image = Image.new("RGB", (orig_width + pad_x, orig_height + pad_y), (255, 255, 255))
|
60 |
+
|
61 |
+
# Paste the original image in the center
|
62 |
+
padded_image.paste(image, (pad_x // 2, pad_y // 2))
|
63 |
+
|
64 |
+
return padded_image
|
65 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
|
68 |
|