syedfaisalabrar commited on
Commit
e6a4a0d
·
verified ·
1 Parent(s): 3d14c4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -36,10 +36,6 @@ tokenizer = AutoTokenizer.from_pretrained(
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
 
@@ -119,11 +115,25 @@ def vision_ai_api(image, doc_type):
119
 
120
  return results
121
 
122
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  def predict(image):
124
  """Pipeline: Preprocess -> Detect -> Crop -> Vision AI API."""
125
  processed_image = preprocessing(image)
126
- rotated_image = imageRotation(processed_image) # Placeholder for rotation
127
  detected_image, labels, bounding_boxes = detect_document(rotated_image)
128
 
129
  cropped_images = crop_image(rotated_image, bounding_boxes)
 
36
  )
37
 
38
 
 
 
 
 
39
  def preprocessing(image):
40
  """Apply three enhancement filters without resizing or cropping."""
41
 
 
115
 
116
  return results
117
 
118
+ def ensure_numpy(image):
119
+ """Ensure image is a valid NumPy array."""
120
+ if isinstance(image, torch.Tensor):
121
+ # Convert PyTorch tensor to NumPy array
122
+ image = image.permute(1, 2, 0).cpu().numpy()
123
+ elif isinstance(image, Image.Image):
124
+ # Convert PIL image to NumPy array
125
+ image = np.array(image)
126
+
127
+ if len(image.shape) == 2:
128
+ # Convert grayscale to 3-channel image
129
+ image = np.stack([image] * 3, axis=-1)
130
+
131
+ return image
132
+
133
  def predict(image):
134
  """Pipeline: Preprocess -> Detect -> Crop -> Vision AI API."""
135
  processed_image = preprocessing(image)
136
+ rotated_image = ensure_numpy(rotated_image)
137
  detected_image, labels, bounding_boxes = detect_document(rotated_image)
138
 
139
  cropped_images = crop_image(rotated_image, bounding_boxes)