forked from bnsreenu/python_for_microscopists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTips_Tricks_42_How to remove text from images.py
69 lines (54 loc) · 2.33 KB
/
Tips_Tricks_42_How to remove text from images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# https://youtu.be/3RNPJbUHZKs
"""
Remove text from images
"""
import matplotlib.pyplot as plt
import keras_ocr
import cv2
import math
import numpy as np
#General Approach.....
#Use keras OCR to detect text, define a mask around the text, and inpaint the
#masked regions to remove the text.
#To apply the mask we need to provide the coordinates of the starting and
#the ending points of the line, and the thickness of the line
#The start point will be the mid-point between the top-left corner and
#the bottom-left corner of the box.
#the end point will be the mid-point between the top-right corner and the bottom-right corner.
#The following function does exactly that.
def midpoint(x1, y1, x2, y2):
x_mid = int((x1 + x2)/2)
y_mid = int((y1 + y2)/2)
return (x_mid, y_mid)
#Main function that detects text and inpaints.
#Inputs are the image path and kreas_ocr pipeline
def inpaint_text(img_path, pipeline):
# read the image
img = keras_ocr.tools.read(img_path)
# Recogize text (and corresponding regions)
# Each list of predictions in prediction_groups is a list of
# (word, box) tuples.
prediction_groups = pipeline.recognize([img])
#Define the mask for inpainting
mask = np.zeros(img.shape[:2], dtype="uint8")
for box in prediction_groups[0]:
x0, y0 = box[1][0]
x1, y1 = box[1][1]
x2, y2 = box[1][2]
x3, y3 = box[1][3]
x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)
#For the line thickness, we will calculate the length of the line between
#the top-left corner and the bottom-left corner.
thickness = int(math.sqrt( (x2 - x1)**2 + (y2 - y1)**2 ))
#Define the line and inpaint
cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255,
thickness)
inpainted_img = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)
return(inpainted_img)
# keras-ocr will automatically download pretrained
# weights for the detector and recognizer.
pipeline = keras_ocr.pipeline.Pipeline()
img_text_removed = inpaint_text('traffic-signs.jpg', pipeline)
plt.imshow(img_text_removed)
cv2.imwrite('text_removed_image.jpg', cv2.cvtColor(img_text_removed, cv2.COLOR_BGR2RGB))