File size: 3,720 Bytes
334adc2
 
9668774
334adc2
9668774
 
334adc2
ae0db67
0851882
f459ff6
0851882
1aa5d0c
9240006
 
eb90322
9240006
224c718
0851882
 
 
 
be299bc
85f0f8f
 
e795847
cd02c9c
 
2ad20b3
85f0f8f
e795847
93ce6b7
d289e37
f450985
cc6f2b9
d289e37
f450985
 
 
 
d289e37
 
 
 
 
 
df45ea5
32d36e6
be299bc
32d36e6
2f1cdb1
9668774
644d5b5
f4de4aa
644d5b5
f4de4aa
644d5b5
f4de4aa
20e28ec
c5c4e79
334adc2
 
 
f459ff6
d2b87eb
be299bc
137d498
be299bc
ba29956
e795847
be299bc
 
e795847
9668774
9240006
 
be299bc
8a2ee58
9240006
334adc2
20e28ec
 
 
f4de4aa
334adc2
9668774
 
2c4e7f5
93107a1
e795847
891aa18
9668774
 
 
6ad9946
9668774
 
 
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
import pandas as pd
import PIL
from PIL import Image
from PIL import ImageDraw
import gradio as gr
import torch
import easyocr
import cv2 as cv
import math
from numpy import asarray
import numpy as np

torch.hub.download_url_to_file('https://i.pinimg.com/736x/93/d3/54/93d354497ea26d5dc181055b9356cf79.jpg', 'korean.jpg')
torch.hub.download_url_to_file('https://64.media.tumblr.com/1a5796817934a179664508693cae82d3/67c2ea7948c385cc-1b/s1280x1920/a70d4fcc0a5528de415b024db4ee6036da9c4c35.jpg', 'chinese.jpg')
torch.hub.download_url_to_file('https://jtalkonline.com/wp-content/uploads/2014/06/ichigo-bleach-manga-japanese.jpg', 'japanese.jpg')
torch.hub.download_url_to_file('https://img.mghubcdn.com/file/imghub/moriarty-the-patriot/68/10.jpg', 'english.jpg')
    
def midpoint(x1, y1, x2, y2):
    x_mid = int((x1 + x2)/2)
    y_mid = int((y1 + y2)/2)
    return (x_mid, y_mid)

def draw_boxes(img, bounds, color='yellow', width=2):
    mask = np.zeros(img.shape[:2], dtype="uint8")
    for bound in bounds:
        pts = np.array([bound[0]], np.int32)
        pts = pts.reshape((-1,1,2))
        cv.fillPoly(image, pts, color =(255,255,255))
    return (mask)
    
def draw_mask(img, bounds):
    mask = np.zeros(img.shape[:2], dtype="uint8")
    for bound in bounds:
        box0, box1, box2, box3 = bound[0]
        
        x0, y0 = box0
        x1, y1 = box1 
        x2, y2 = box2
        x3, y3 = box3 
        
        x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
        x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)
        
        thickness = int(math.sqrt((x2 - x1)**2 + (y2 - y1)**2))
        
        cv.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255, thickness)
    img = cv.inpaint(img, mask, 1, cv.INPAINT_NS)
                 
    return(img)
       
def inference(img, lang):
    if lang == "english":
        lang = ['en']
    elif lang == "chinese":
        lang = ['ch_sim']
    elif lang == "korean":
        lang = ['ko']
    else:
        lang = ['ja']
    reader = easyocr.Reader(lang)
    bounds = reader.readtext(img.name)
    im = PIL.Image.open(img.name)
    img_array = np.array(im)
    img = draw_boxes(img_array, bounds)
    im = draw_mask(img_array, bounds)
    img = Image.fromarray(img)
    im = Image.fromarray(im, 'RGB')
    lang = ""
    img.save('box.jpg')
    im.save('result.jpg')
    
    return ['box.jpg', 'result.jpg', pd.DataFrame(bounds). iloc[: , 1:2]]

title = 'Manga Image Cleaner'
description = 'Image inpainting and text detection demo with the use of EasyOCR and CV2. To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to test the program.'
article = "<p style='text-align: center'> <a>Ready-to-use image inpainting with supported languages such as: Chinese, English, Japanese, and Korean</a> | <a href='https://github.com/JaidedAI/EasyOCR'>Github OCR Repo</a> | <a href='https://towardsdatascience.com/remove-text-from-images-using-cv2-and-keras-ocr-24e7612ae4f4'>CV2 Reference</a></p>"
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
examples = [['chinese.jpg',"chinese"], ['english.jpg',"english"], ['japanese.jpg',"japanese"], ['korean.jpg',"korean"]]
choices = [
    "chinese",
    "english",
    "japanese",
    "korean"
]
gr.Interface(
    inference,
    [gr.inputs.Image(type='file', label='Input'),gr.inputs.Dropdown(choices, type="value", default="korean", label='language')],
    [gr.outputs.Image(type='file', label='Output'), 
    gr.outputs.Image(type='file', label='Output'), 
    gr.outputs.Dataframe()],
    title=title,
    description=description,
    article=article,
    examples=examples,
    css=css,
    enable_queue=True
    ).launch(debug=True)