File size: 3,647 Bytes
334adc2
 
9668774
334adc2
9668774
 
334adc2
ae0db67
0851882
 
1aa5d0c
d5653bd
dc72617
224c718
6c26fa2
224c718
 
 
 
 
0851882
 
 
 
334adc2
93ce6b7
d289e37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93107a1
 
899dbbf
93107a1
2f1cdb1
9668774
644d5b5
f4de4aa
644d5b5
f4de4aa
644d5b5
f4de4aa
20e28ec
c5c4e79
334adc2
 
 
224c718
d7cf1c9
224c718
1b1fded
d289e37
 
ae0db67
899dbbf
d289e37
c0d4465
ba29956
9b0f70e
d5653bd
d289e37
ae0db67
224c718
 
 
ae0db67
8eab7a1
f16ccdd
ae0db67
93a5f8a
ff8025f
224c718
d289e37
9668774
334adc2
 
 
9668774
d5653bd
334adc2
20e28ec
 
 
f4de4aa
334adc2
9668774
 
c5c4e79
93107a1
d289e37
ff8025f
93107a1
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
import numpy as np

torch.hub.download_url_to_file('https://i.pinimg.com/originals/45/d0/30/45d03054e15f4be731781eecba7458a4.jpg', 'korean.jpg')

def convert(img):
    pil_im = Image.fromarray(img)
    b = io.BytesIO()
    pil_im.save(b, 'jpeg')
    im_bytes = b.getvalue()
    return im_bytes
    
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_mask(img, bounds):
    mask = np.zeros(img.shape[:2], dtype="uint8")
    for box in bounds:
        
        x0, y0 = box[0]
        x1, y1 = box[1] 
        x2, y2 = box[2]
        x3, y3 = box[3] 
        
        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))
        
        cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255, thickness)
        img = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)
                 
    return(img)
    
def masking(img, mask):
    #img = cv.inpaint(img, mask, 3, cv.INPAINT_TELEA)
    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)
    
    #mask = PIL.Image.open(img.name)
    #mask = Image.new("L", im.size, 0)
    draw_mask(img_array, bounds)
    #
    #masking(img_array, mask)
    #img_inpainted = cv.inpaint(img_array, mask, 7, cv.INPAINT_NS)
    #remove_text(im, mask, bounds)
    lang = ""
    #cv.imshow('dst', dst)
    im.save('result.jpg')
    #mask.save('mask.png')
    
    #img = cv.imread('result.jpg')
    #mask = cv.imread('mask2.png',0)
    #dst = cv.inpaint(img,mask,3,cv.INPAINT_TELEA)
    
    #final.save('final.png')
    #img_inpainted.save('inpaint.jpg')
    #dst = cv2.inpaint(np.float32(img),np.float32(mask),3,cv2.INPAINT_TELEA)
    #img_inpainted = cv2.inpaint(im, mask, 7, cv2.INPAINT_TELEA)
    #cv2.imwrite('dst.png', dst)
    #cv.imwrite('result.jpg',dst)
    return ['result.jpg', pd.DataFrame(bounds). iloc[: , 1:2]]

title = 'EasyOCR'
description = 'Gradio demo for EasyOCR. EasyOCR demo supports 80+ languages.To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.'
article = "<p style='text-align: center'><a href='https://www.jaided.ai/easyocr/'>Ready-to-use OCR with 80+ supported languages and all popular writing scripts including Latin, Chinese, Arabic, Devanagari, Cyrillic and etc.</a> | <a href='https://github.com/JaidedAI/EasyOCR'>Github Repo</a></p>"
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
examples = [['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.Image(type='file', label='Output'), 
    gr.outputs.Dataframe()],
    title=title,
    description=description,
    article=article,
    examples=examples,
    css=css,
    enable_queue=True
    ).launch(debug=True)