BilalSardar commited on
Commit
2ec286a
1 Parent(s): 7b89005

Upload 3 files

Browse files
Files changed (3) hide show
  1. 1.jpg +0 -0
  2. RemoveText.py +70 -0
  3. text_removed_image.jpg +0 -0
1.jpg ADDED
RemoveText.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ from cv2 import threshold
4
+ import matplotlib.pyplot as plt
5
+ import keras_ocr
6
+ import cv2
7
+ import math
8
+ import numpy as np
9
+
10
+
11
+ def midpoint(x1, y1, x2, y2):
12
+ x_mid = int((x1 + x2)/2)
13
+ y_mid = int((y1 + y2)/2)
14
+ return (x_mid, y_mid)
15
+
16
+ def segment_img(img):
17
+ hsv=cv2.cvtColor(img,cv2.COLOR_RGB2HSV)
18
+ #mask
19
+ mask=cv2.inRange(hsv,(40,25,25),(70,255,255))
20
+
21
+ imask=mask>0
22
+ threshold=np.zeros_like(img,np.uint8)
23
+ threshold[imask]=img[imask]
24
+
25
+ return threshold
26
+ #Main function that detects text and inpaints.
27
+ #Inputs are the image path and kreas_ocr pipeline
28
+ def inpaint_text(img_path, pipeline):
29
+ # read the image
30
+ img = keras_ocr.tools.read(img_path)
31
+
32
+ #img=segment_img(img)
33
+
34
+
35
+ # Recogize text (and corresponding regions)
36
+ # Each list of predictions in prediction_groups is a list of
37
+ # (word, box) tuples.
38
+ prediction_groups = pipeline.recognize([img])
39
+
40
+ #Define the mask for inpainting
41
+ mask = np.zeros(img.shape[:2], dtype="uint8")
42
+ for box in prediction_groups[0]:
43
+ x0, y0 = box[1][0]
44
+ x1, y1 = box[1][1]
45
+ x2, y2 = box[1][2]
46
+ x3, y3 = box[1][3]
47
+
48
+ x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
49
+ x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)
50
+
51
+ #For the line thickness, we will calculate the length of the line between
52
+ #the top-left corner and the bottom-left corner.
53
+ thickness = int(math.sqrt( (x2 - x1)**2 + (y2 - y1)**2 ))
54
+
55
+ #Define the line and inpaint
56
+ cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255,
57
+ thickness)
58
+ inpainted_img = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)
59
+
60
+ return (inpainted_img)
61
+
62
+ # keras-ocr will automatically download pretrained
63
+ # weights for the detector and recognizer.
64
+ pipeline = keras_ocr.pipeline.Pipeline()
65
+
66
+ img_text_removed = inpaint_text('1.jpg', pipeline)
67
+
68
+ plt.imshow(img_text_removed)
69
+
70
+ cv2.imwrite('text_removed_image.jpg', cv2.cvtColor(img_text_removed, cv2.COLOR_BGR2RGB))
text_removed_image.jpg ADDED