awacke1 commited on
Commit
162b4b7
1 Parent(s): 7f8d20e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -23
app.py CHANGED
@@ -5,7 +5,10 @@ from PIL import ImageDraw
5
  import gradio as gr
6
  import torch
7
  import easyocr
8
- import webbrowser
 
 
 
9
 
10
  torch.hub.download_url_to_file('https://raw.githubusercontent.com/AaronCWacker/Yggdrasil/master/images/20-Books.jpg','20-Books.jpg')
11
  torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png', 'COVID.png')
@@ -24,26 +27,20 @@ def inference(img, lang):
24
  reader = easyocr.Reader(lang)
25
  bounds = reader.readtext(img.name)
26
  im = PIL.Image.open(img.name)
27
- im_with_boxes = draw_boxes(im, bounds)
28
- im_with_boxes.save('result.jpg')
29
- dataframe = pd.DataFrame(bounds, columns=['text', 'confidence'])
30
- dataframe['link'] = dataframe['text'].apply(lambda x: f"<a href='https://en.wikipedia.org/wiki/{x.replace(' ', '_')}'>{x}</a>")
31
- return ['result.jpg', dataframe[['link', 'confidence']]]
32
 
33
  title = '🖼️Image to Multilingual OCR👁️Gradio'
34
  description = 'Multilingual OCR which works conveniently on all devices in multiple languages.'
35
  article = "<p style='text-align: center'></p>"
36
 
37
  examples = [
38
- ['20-Books.jpg',['en']],
39
- ['COVID.png',['en']],
40
- ['chinese.jpg',['ch_sim', 'en']],
41
- ['japanese.jpg',['ja', 'en']],
42
- ['Hindi.jpeg',['hi', 'en']]
43
  ]
44
 
45
  css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
46
-
47
  choices = [
48
  "ch_sim",
49
  "ch_tra",
@@ -54,23 +51,14 @@ choices = [
54
  "hi",
55
  "ru"
56
  ]
57
-
58
- def open_link(link):
59
- webbrowser.open_new_tab(link)
60
-
61
- output = gr.outputs.Dataframe(headers=['text', 'confidence'])
62
-
63
  gr.Interface(
64
  inference,
65
  [gr.inputs.Image(type='file', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en'], label='language')],
66
- [gr.outputs.Image(type='file', label='Output'), output],
67
  title=title,
68
  description=description,
69
  article=article,
70
  examples=examples,
71
  css=css,
72
  enable_queue=True
73
- ).launch(debug=True)
74
-
75
- # Launch webbrowser on clicking the link in dataframe
76
- output.df_click = open_link
 
5
  import gradio as gr
6
  import torch
7
  import easyocr
8
+
9
+ #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/BeautyIsTruthTruthisBeauty.JPG', 'BeautyIsTruthTruthisBeauty.JPG')
10
+ #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/PleaseRepeatLouder.jpg', 'PleaseRepeatLouder.jpg')
11
+ #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/ProhibitedInWhiteHouse.JPG', 'ProhibitedInWhiteHouse.JPG')
12
 
13
  torch.hub.download_url_to_file('https://raw.githubusercontent.com/AaronCWacker/Yggdrasil/master/images/20-Books.jpg','20-Books.jpg')
14
  torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png', 'COVID.png')
 
27
  reader = easyocr.Reader(lang)
28
  bounds = reader.readtext(img.name)
29
  im = PIL.Image.open(img.name)
30
+ draw_boxes(im, bounds)
31
+ im.save('result.jpg')
32
+ return ['result.jpg', pd.DataFrame(bounds).iloc[: , 1:]]
 
 
33
 
34
  title = '🖼️Image to Multilingual OCR👁️Gradio'
35
  description = 'Multilingual OCR which works conveniently on all devices in multiple languages.'
36
  article = "<p style='text-align: center'></p>"
37
 
38
  examples = [
39
+ #['PleaseRepeatLouder.jpg',['ja']],['ProhibitedInWhiteHouse.JPG',['en']],['BeautyIsTruthTruthisBeauty.JPG',['en']],
40
+ ['20-Books.jpg',['en']],['COVID.png',['en']],['chinese.jpg',['ch_sim', 'en']],['japanese.jpg',['ja', 'en']],['Hindi.jpeg',['hi', 'en']]
 
 
 
41
  ]
42
 
43
  css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
 
44
  choices = [
45
  "ch_sim",
46
  "ch_tra",
 
51
  "hi",
52
  "ru"
53
  ]
 
 
 
 
 
 
54
  gr.Interface(
55
  inference,
56
  [gr.inputs.Image(type='file', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en'], label='language')],
57
+ [gr.outputs.Image(type='file', label='Output'), gr.outputs.Dataframe(headers=['text', 'confidence'])],
58
  title=title,
59
  description=description,
60
  article=article,
61
  examples=examples,
62
  css=css,
63
  enable_queue=True
64
+ ).launch(debug=True)