Stereo0001 commited on
Commit
bc3d87a
1 Parent(s): 15d5a22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -18
app.py CHANGED
@@ -75,12 +75,13 @@ iface.launch(debug=True)
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)
@@ -92,34 +93,29 @@ def detect_and_draw_lines(img, rho=1, theta=1):
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)
@@ -138,6 +134,3 @@ iface.launch(debug=True)
138
 
139
 
140
 
141
-
142
-
143
-
 
75
 
76
 
77
 
 
78
  import cv2
79
  import numpy as np
80
+ import tensorflow as tf
81
+ from tensorflow import keras
82
+ from tensorflow.keras_ocr import pipeline # 使用tf.keras_ocr
83
 
84
+ def detect_and_draw_lines(img, rho=1, theta=np.pi/180):
85
  """Detect lines in the image and draw them."""
86
  edges = cv2.Canny(img, 50, 150, apertureSize=3)
87
  lines = cv2.HoughLines(edges, rho, theta, 100)
 
93
 
94
  def inpaint_lines(img):
95
  """Inpaint the detected lines."""
 
96
  mask = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
97
  mask[mask > 0] = 255
 
98
  inpainted_img = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
99
  return inpainted_img
100
 
101
  def inpaint_text(img_path, pipeline):
102
+ img = tf.keras.preprocessing.image.load_img(img_path, color_mode='rgb', target_size=(1024, 1024))
103
+ img_array = tf.keras.preprocessing.image.img_to_array(img)
104
+ img_array = np.expand_dims(img_array, axis=0)
105
  # Detect and draw lines
106
+ img_with_lines = detect_and_draw_lines(img_array[0])
107
  # Inpaint the lines
108
  inpainted_img = inpaint_lines(img_with_lines)
109
+ predictions = pipeline.recognize([inpainted_img])
 
 
110
  mask = np.zeros(inpainted_img.shape[:2], dtype="uint8")
111
+ for box in predictions[0]:
112
  x0, y0, x1, y1, x2, y2, x3, y3 = box[1]
113
  cv2.line(mask, (x0, y0), (x1, y1), 255, 10)
114
  cv2.line(mask, (x2, y2), (x3, y3), 255, 10)
 
115
  inpainted_img = cv2.inpaint(inpainted_img, mask, 3, cv2.INPAINT_TELEA)
116
  return inpainted_img
117
 
118
+ pipeline = pipeline.Pipeline()
 
119
  def RemoveTextAndLines(image):
120
  img_text_and_lines_removed = inpaint_text(image, pipeline)
121
  return cv2.cvtColor(img_text_and_lines_removed, cv2.COLOR_BGR2RGB)
 
134
 
135
 
136