syedfaisalabrar commited on
Commit
8334105
·
verified ·
1 Parent(s): 708bb4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -60
app.py CHANGED
@@ -36,67 +36,33 @@ tokenizer = AutoTokenizer.from_pretrained(
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
- # 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
 
 
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