syedfaisalabrar commited on
Commit
974149d
·
verified ·
1 Parent(s): 6c705c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -21
app.py CHANGED
@@ -36,32 +36,27 @@ tokenizer = AutoTokenizer.from_pretrained(
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
 
 
36
  )
37
 
38
 
39
+ from PIL import Image, ImageEnhance
40
+ import numpy as np
41
+ import torch
 
 
 
 
 
 
 
 
42
 
43
+ def preprocessing(image):
44
+ """Apply three enhancement filters without resizing or cropping."""
45
+
46
+ # Ensure the image is a PIL Image
47
+ if not isinstance(image, Image.Image):
48
+ image = Image.fromarray(np.array(image))
49
 
50
+ # Apply enhancements
51
+ image = ImageEnhance.Sharpness(image).enhance(2.0) # Increase sharpness
52
+ image = ImageEnhance.Contrast(image).enhance(1.5) # Increase contrast
53
+ image = ImageEnhance.Brightness(image).enhance(0.8) # Reduce brightness
54
 
55
+ # Convert to tensor without resizing
56
+ image_tensor = torch.tensor(np.array(image)).permute(2, 0, 1).float() / 255.0 # Shape: [C, H, W]
57
 
58
+ return image_tensor
 
59
 
 
60
 
61
 
62