SohomToom commited on
Commit
89815d3
·
verified ·
1 Parent(s): cd98f1b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -14
app.py CHANGED
@@ -39,20 +39,35 @@ def sample_border_color(image, box, padding=2):
39
  median_color = np.median(border_pixels, axis=0)
40
  return tuple(map(int, median_color))
41
 
42
- def detect_text_boxes(image):
43
- results = ocr.ocr(image, cls=True)
44
- if not results or not results[0]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  return []
46
- boxes = []
47
- for line in results[0]:
48
- box, (text, confidence) = line
49
- if text.strip():
50
- x_min = int(min(pt[0] for pt in box))
51
- x_max = int(max(pt[0] for pt in box))
52
- y_min = int(min(pt[1] for pt in box))
53
- y_max = int(max(pt[1] for pt in box))
54
- boxes.append(((x_min, y_min, x_max, y_max), text, confidence))
55
- return boxes
56
 
57
  def remove_text_dynamic_fill(img_path, output_path):
58
  image = cv2.imread(img_path)
@@ -106,8 +121,23 @@ def process_folder(input_files):
106
  output_path = os.path.join(temp_output, filename)
107
  remove_text_dynamic_fill(file.name, output_path)
108
 
109
- zip_path = shutil.make_archive(temp_output, 'zip', temp_output)
 
110
  return zip_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
  demo = gr.Interface(
113
  fn=process_folder,
 
39
  median_color = np.median(border_pixels, axis=0)
40
  return tuple(map(int, median_color))
41
 
42
+ def detect_text_boxes(image, max_dim=1280):
43
+ try:
44
+ # Resize large images to reduce memory load
45
+ height, width = image.shape[:2]
46
+ if max(height, width) > max_dim:
47
+ scale = max_dim / float(max(height, width))
48
+ image = cv2.resize(image, (int(width * scale), int(height * scale)))
49
+
50
+ results = PaddleOCR.ocr(image, cls=True)
51
+
52
+ if results is None or not results[0]:
53
+ print("No OCR results found or OCR returned None.")
54
+ return []
55
+
56
+ boxes = []
57
+ for line in results[0]:
58
+ box, (text, confidence) = line
59
+ if text.strip():
60
+ x_min = int(min(pt[0] for pt in box))
61
+ x_max = int(max(pt[0] for pt in box))
62
+ y_min = int(min(pt[1] for pt in box))
63
+ y_max = int(max(pt[1] for pt in box))
64
+ boxes.append(((x_min, y_min, x_max, y_max), text, confidence))
65
+ return boxes
66
+
67
+ except Exception as e:
68
+ print(f"OCR failed on image: {e}")
69
  return []
70
+
 
 
 
 
 
 
 
 
 
71
 
72
  def remove_text_dynamic_fill(img_path, output_path):
73
  image = cv2.imread(img_path)
 
121
  output_path = os.path.join(temp_output, filename)
122
  remove_text_dynamic_fill(file.name, output_path)
123
 
124
+ zip_path = "/tmp/cleaned_output.zip"
125
+ zip_folder(output_path, zip_path)
126
  return zip_path
127
+
128
+ import zipfile
129
+
130
+ def zip_folder(folder_path, output_zip):
131
+ with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
132
+ for root, _, files in os.walk(folder_path):
133
+ for file in files:
134
+ file_path = os.path.join(root, file)
135
+ arcname = os.path.relpath(file_path, folder_path)
136
+ zipf.write(file_path, arcname)
137
+
138
+
139
+
140
+
141
 
142
  demo = gr.Interface(
143
  fn=process_folder,