Stereo0001 commited on
Commit
9a485fd
1 Parent(s): 8904372

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -1
app.py CHANGED
@@ -1,4 +1,4 @@
1
-
2
  from cv2 import threshold
3
  import keras_ocr
4
  import cv2
@@ -71,3 +71,73 @@ iface = gr.Interface(fn=RemoveText,
71
  title="Remove Text for Image")
72
  iface.launch(debug=True)
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
  from cv2 import threshold
3
  import keras_ocr
4
  import cv2
 
71
  title="Remove Text for Image")
72
  iface.launch(debug=True)
73
 
74
+ '''
75
+
76
+
77
+
78
+
79
+ import cv2
80
+ import numpy as np
81
+ import keras_ocr
82
+
83
+ def detect_and_draw_lines(img, rho=1, theta=1):
84
+ """Detect lines in the image and draw them."""
85
+ edges = cv2.Canny(img, 50, 150, apertureSize=3)
86
+ lines = cv2.HoughLines(edges, rho, theta, 100)
87
+ if lines is not None:
88
+ for line in lines:
89
+ for x1, y1, x2, y2 in line:
90
+ cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
91
+ return img
92
+
93
+ def inpaint_lines(img):
94
+ """Inpaint the detected lines."""
95
+ # Create a mask where lines are white and the rest is black
96
+ mask = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
97
+ mask[mask > 0] = 255
98
+ # Inpaint using the mask
99
+ inpainted_img = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
100
+ return inpainted_img
101
+
102
+ def inpaint_text(img_path, pipeline):
103
+ # Read the image
104
+ img = keras_ocr.tools.read(img_path)
105
+ # Detect and draw lines
106
+ img_with_lines = detect_and_draw_lines(img)
107
+ # Inpaint the lines
108
+ inpainted_img = inpaint_lines(img_with_lines)
109
+ # Recognize text
110
+ prediction_groups = pipeline.recognize([inpainted_img])
111
+ # Define the mask for inpainting text
112
+ mask = np.zeros(inpainted_img.shape[:2], dtype="uint8")
113
+ for box in prediction_groups[0]:
114
+ x0, y0, x1, y1, x2, y2, x3, y3 = box[1]
115
+ cv2.line(mask, (x0, y0), (x1, y1), 255, 10)
116
+ cv2.line(mask, (x2, y2), (x3, y3), 255, 10)
117
+ # Inpaint text
118
+ inpainted_img = cv2.inpaint(inpainted_img, mask, 3, cv2.INPAINT_TELEA)
119
+ return inpainted_img
120
+
121
+ # keras-ocr will automatically download pretrained weights for the detector and recognizer.
122
+ pipeline = keras_ocr.pipeline.Pipeline()
123
+ def RemoveTextAndLines(image):
124
+ img_text_and_lines_removed = inpaint_text(image, pipeline)
125
+ return cv2.cvtColor(img_text_and_lines_removed, cv2.COLOR_BGR2RGB)
126
+
127
+ # Example usage with Gradio interface (assuming you have Gradio installed)
128
+ import gradio as gr
129
+
130
+ iface = gr.Interface(
131
+ fn=RemoveTextAndLines,
132
+ inputs=gr.inputs.Image(label="Image to Remove Text and Lines From", type="numpy"),
133
+ outputs="image",
134
+ examples=[["1.jpg"]],
135
+ title="Remove Text and Lines for Image"
136
+ )
137
+ iface.launch(debug=True)
138
+
139
+
140
+
141
+
142
+
143
+