File size: 4,370 Bytes
9a485fd
2ec286a
 
 
 
 
7d4cb84
2ec286a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d4cb84
 
 
2ec286a
9e0ee47
7d4cb84
 
 
 
 
2ec286a
9a485fd
 
 
 
 
 
bc3d87a
 
7a24815
9a485fd
bc3d87a
9a485fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc3d87a
 
 
9a485fd
bc3d87a
9a485fd
 
bc3d87a
9a485fd
bc3d87a
9a485fd
 
 
 
 
 
bc3d87a
9a485fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'''
from cv2 import threshold
import keras_ocr
import cv2
import math
import numpy as np
import gradio as gr

def midpoint(x1, y1, x2, y2):
    x_mid = int((x1 + x2)/2)
    y_mid = int((y1 + y2)/2)
    return (x_mid, y_mid)

def segment_img(img):
    hsv=cv2.cvtColor(img,cv2.COLOR_RGB2HSV)
    #mask
    mask=cv2.inRange(hsv,(40,25,25),(70,255,255))
    
    imask=mask>0
    threshold=np.zeros_like(img,np.uint8)
    threshold[imask]=img[imask]
    
    return threshold
#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) 
    
    #img=segment_img(img)
    
    
    # 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()
def RemoveText(image):
    img_text_removed = inpaint_text(image, pipeline)
    return  cv2.cvtColor(img_text_removed, cv2.COLOR_BGR2RGB)

iface = gr.Interface(fn=RemoveText,
             inputs=gr.inputs.Image(label="Image to Remove Text From", type="numpy"),
             outputs="image",
                    examples=[["1.jpg"]],
                    title="Remove Text for Image")
iface.launch(debug=True)

'''



import cv2
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras_ocr import pipeline  # 使用tf.keras_ocr

def detect_and_draw_lines(img, rho=1, theta=np.pi/180):
    """Detect lines in the image and draw them."""
    edges = cv2.Canny(img, 50, 150, apertureSize=3)
    lines = cv2.HoughLines(edges, rho, theta, 100)
    if lines is not None:
        for line in lines:
            for x1, y1, x2, y2 in line:
                cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
    return img

def inpaint_lines(img):
    """Inpaint the detected lines."""
    mask = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    mask[mask > 0] = 255
    inpainted_img = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
    return inpainted_img

def inpaint_text(img_path, pipeline):
    img = tf.keras.preprocessing.image.load_img(img_path, color_mode='rgb', target_size=(1024, 1024))
    img_array = tf.keras.preprocessing.image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    # Detect and draw lines
    img_with_lines = detect_and_draw_lines(img_array[0])
    # Inpaint the lines
    inpainted_img = inpaint_lines(img_with_lines)
    predictions = pipeline.recognize([inpainted_img])
    mask = np.zeros(inpainted_img.shape[:2], dtype="uint8")
    for box in predictions[0]:
        x0, y0, x1, y1, x2, y2, x3, y3 = box[1]
        cv2.line(mask, (x0, y0), (x1, y1), 255, 10)
        cv2.line(mask, (x2, y2), (x3, y3), 255, 10)
    inpainted_img = cv2.inpaint(inpainted_img, mask, 3, cv2.INPAINT_TELEA)
    return inpainted_img

pipeline = pipeline.Pipeline()
def RemoveTextAndLines(image):
    img_text_and_lines_removed = inpaint_text(image, pipeline)
    return cv2.cvtColor(img_text_and_lines_removed, cv2.COLOR_BGR2RGB)

# Example usage with Gradio interface (assuming you have Gradio installed)
import gradio as gr

iface = gr.Interface(
    fn=RemoveTextAndLines,
    inputs=gr.inputs.Image(label="Image to Remove Text and Lines From", type="numpy"),
    outputs="image",
    examples=[["1.jpg"]],
    title="Remove Text and Lines for Image"
)
iface.launch(debug=True)