badru commited on
Commit
dbfe32a
·
verified ·
1 Parent(s): 3e85cab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -16,7 +16,7 @@ def load_model():
16
  processor, model = load_model()
17
 
18
  # Helper function to preprocess the image and detect lines
19
- def detect_lines(image):
20
  # Convert the PIL image to a NumPy array
21
  image_np = np.array(image)
22
 
@@ -26,23 +26,33 @@ def detect_lines(image):
26
  # Apply binary thresholding
27
  _, binary = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
28
 
 
 
 
 
29
  # Find contours
30
- contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
31
 
32
  # Sort contours top-to-bottom
33
  bounding_boxes = [cv2.boundingRect(c) for c in contours]
34
  bounding_boxes = sorted(bounding_boxes, key=lambda b: b[1]) # Sort by y-coordinate
35
 
 
 
 
 
 
 
 
36
  line_images = []
37
- for (x, y, w, h) in bounding_boxes:
38
- # Extract each line as a separate image
39
  line = image_np[y:y+h, x:x+w]
40
  line_images.append(line)
41
 
42
  return line_images
43
 
44
  # Streamlit app
45
- st.title("MMSai Meeeting Image Tools 1.0")
46
  uploaded_file = st.file_uploader("Upload an Image (JPG, JPEG, PNG)", type=["jpg", "jpeg", "png"])
47
 
48
  if uploaded_file is not None:
@@ -54,7 +64,7 @@ if uploaded_file is not None:
54
  st.write("Processing the image...")
55
 
56
  # Detect lines in the image
57
- line_images = detect_lines(image)
58
  st.write(f"Detected {len(line_images)} lines in the image.")
59
 
60
  # Perform OCR on each detected line
 
16
  processor, model = load_model()
17
 
18
  # Helper function to preprocess the image and detect lines
19
+ def detect_lines(image, min_height=20, min_width=100):
20
  # Convert the PIL image to a NumPy array
21
  image_np = np.array(image)
22
 
 
26
  # Apply binary thresholding
27
  _, binary = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
28
 
29
+ # Dilate to merge nearby text
30
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
31
+ dilated = cv2.dilate(binary, kernel, iterations=1)
32
+
33
  # Find contours
34
+ contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
35
 
36
  # Sort contours top-to-bottom
37
  bounding_boxes = [cv2.boundingRect(c) for c in contours]
38
  bounding_boxes = sorted(bounding_boxes, key=lambda b: b[1]) # Sort by y-coordinate
39
 
40
+ # Filter out small contours and merge nearby ones
41
+ filtered_boxes = []
42
+ for x, y, w, h in bounding_boxes:
43
+ if h >= min_height and w >= min_width: # Filter small boxes
44
+ filtered_boxes.append((x, y, w, h))
45
+
46
+ # Extract individual lines as images
47
  line_images = []
48
+ for (x, y, w, h) in filtered_boxes:
 
49
  line = image_np[y:y+h, x:x+w]
50
  line_images.append(line)
51
 
52
  return line_images
53
 
54
  # Streamlit app
55
+ st.title("Multiline Handwritten OCR with Hugging Face and OpenCV")
56
  uploaded_file = st.file_uploader("Upload an Image (JPG, JPEG, PNG)", type=["jpg", "jpeg", "png"])
57
 
58
  if uploaded_file is not None:
 
64
  st.write("Processing the image...")
65
 
66
  # Detect lines in the image
67
+ line_images = detect_lines(image, min_height=30, min_width=100)
68
  st.write(f"Detected {len(line_images)} lines in the image.")
69
 
70
  # Perform OCR on each detected line