awacke1 commited on
Commit
926c0eb
·
1 Parent(s): 025cd8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -117
app.py CHANGED
@@ -1,118 +1,44 @@
1
- import pandas as pd
2
- import PIL
3
- from PIL import Image
4
- from PIL import ImageDraw
5
  import gradio as gr
6
- import torch
7
- import easyocr
8
-
9
-
10
- torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/20-Books.jpg', '20-Books.jpg')
11
- torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/21-Books.jpg', '21-Books.jpg')
12
- torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/22-Magazines.jpg', '22-Magazines.jpg')
13
- torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/23-Magazines.jpg', '23-Magazines.jpg')
14
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/24-Card-Games.jpg', '24-Card-Games.jpg')
15
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/25-Games.jpg', '25-Games.jpg')
16
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/26-Books.jpg', '26-Books.jpg')
17
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/27-Books.jpg', '27-Books.jpg')
18
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/28-Books.jpg', '28-Books.jpg')
19
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/27-Games.jpg', '27-Games.jpg')
20
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/28-Games.jpg', '28-Games.jpg')
21
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/29-Books-Science-Fiction.jpg', '29-Books-Science-Fiction.jpg')
22
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/30-Manga-Books.jpg', '30-Manga-Books.jpg')
23
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/31-Books.jpg', '31-Books.jpg')
24
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/32-Books.jpg', '32-Books.jpg')
25
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/33-Game-Night.jpg', '33-Game-Night.jpg')
26
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/34-Games.jpg', '34-Games.jpg')
27
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/35-Favorite-Games.jpg', '35-Favorite-Games.jpg')
28
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/36-Game-Night.jpg', '36-Game-Night.jpg')
29
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/38-Strategy-Games.jpg', '38-Strategy-Games.jpg')
30
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/39-Games.jpg', '39-Games.jpg')
31
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/40-Games.jpg', '40-Games.jpg')
32
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/41-Game-Rules.jpg', '41-Game-Rules.jpg')
33
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/42-Game-Rules.jpg', '42-Game-Rules.jpg')
34
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/43-Games-Pieces.jpg', '43-Games-Pieces.jpg')
35
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/44-Game-Pieces.jpg', '44-Game-Pieces.jpg')
36
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/45-Choose-Your-Own-Adventure.jpg', '45-Choose-Your-Own-Adventure.jpg')
37
- #torch.hub.download_url_to_file('https://github.com/AaronCWacker/Yggdrasil/blob/main/images/46-Choose-Your-Own-Adventure.jpg', '46-Choose-Your-Own-Adventure.jpg')
38
- #torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png', 'english.png')
39
- #torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/chinese.jpg', 'chinese.jpg')
40
- #torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/japanese.jpg', 'japanese.jpg')
41
- #torch.hub.download_url_to_file('https://i.imgur.com/mwQFd7G.jpeg', 'Hindi.jpeg')
42
-
43
- #,
44
- #['24-Card-Games.jpg',['en']],
45
- #['25-Games.jpg',['en']],
46
- #['26-Books.jpg',['en']],
47
- #['27-Books.jpg',['en']],
48
- #['28-Books.jpg',['en']],
49
- #['27-Games.jpg',['en']],
50
- #['28-Games.jpg',['en']]]
51
-
52
- #['29-Books-Science-Fiction.jpg',['en']],
53
- #['30-Manga-Books.jpg',['en']],
54
- #['31-Books.jpg',['en']],
55
- #['32-Books.jpg',['en']],
56
- #['33-Game-Night.jpg',['en']],
57
- #['34-Games.jpg',['en']]
58
- #['35-Favorite-Games.jpg',['en']],
59
- #['36-Game-Night.jpg',['en']],
60
- #['38-Strategy-Games.jpg',['en']],
61
- #['39-Games.jpg',['en']],
62
- #['40-Games.jpg',['en']],
63
- #['41-Game-Rules.jpg',['en']],
64
- #['42-Game-Rules.jpg',['en']],
65
- #['43-Games-Pieces.jpg',['en']],
66
- #['44-Game-Pieces.jpg',['en']],
67
- #['45-Choose-Your-Own-Adventure.jpg',['en']],
68
- #['46-Choose-Your-Own-Adventure.jpg',['en']]
69
- #]
70
-
71
- # Comment
72
- #['english.png',['en']],
73
- #['chinese.jpg',['ch_sim', 'en']],
74
- #['japanese.jpg',['ja', 'en']],
75
- #['Hindi.jpeg',['hi', 'en']]
76
-
77
- def draw_boxes(image, bounds, color='yellow', width=2):
78
- draw = ImageDraw.Draw(image)
79
- for bound in bounds:
80
- p0, p1, p2, p3 = bound[0]
81
- draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
82
- return image
83
-
84
- def inference(img, lang):
85
- reader = easyocr.Reader(lang)
86
- bounds = reader.readtext(img.name)
87
- im = PIL.Image.open(img.name)
88
- draw_boxes(im, bounds)
89
- im.save('result.jpg')
90
- return ['result.jpg', pd.DataFrame(bounds).iloc[: , 1:]]
91
-
92
- title = 'Image To Optical Character Recognition'
93
- description = 'Multilingual OCR which works conveniently on all devices in multiple languages.'
94
- article = "<p style='text-align: center'></p>"
95
- css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
96
- examples = [['20-Books.jpg',['en']],['21-Books.jpg',['en']],['22-Magazines.jpg',['en']],['23-Magazines.jpg',['en']]]
97
-
98
- choices = [
99
- "ch_sim",
100
- "ch_tra",
101
- "de",
102
- "en",
103
- "es",
104
- "ja",
105
- "hi",
106
- "ru"
107
- ]
108
- gr.Interface(
109
- inference,
110
- [gr.inputs.Image(type='file', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en'], label='language')],
111
- [gr.outputs.Image(type='file', label='Output'), gr.outputs.Dataframe(headers=['text', 'confidence'])],
112
- title=title,
113
- description=description,
114
- article=article,
115
- examples=examples,
116
- css=css,
117
- enable_queue=True
118
- ).launch(debug=True)
 
1
+ import torch
2
+ import re
 
 
3
  import gradio as gr
4
+ from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
5
+
6
+ device='cpu'
7
+ encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
8
+ decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
9
+ model_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
10
+ feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
11
+ tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
12
+ model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
13
+
14
+
15
+ def predict(image,max_length=64, num_beams=4):
16
+ image = image.convert('RGB')
17
+ image = feature_extractor(image, return_tensors="pt").pixel_values.to(device)
18
+ clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
19
+ caption_ids = model.generate(image, max_length = max_length)[0]
20
+ caption_text = clean_text(tokenizer.decode(caption_ids))
21
+ return caption_text
22
+
23
+
24
+
25
+ input = gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)
26
+ output = gr.outputs.Textbox(type="auto",label="Captions")
27
+ examples = [f"example{i}.jpg" for i in range(1,7)]
28
+
29
+ description= "Image captioning application made using transformers"
30
+ title = "Image Captioning 🖼️"
31
+
32
+ article = "Created By : Shreyas Dixit "
33
+
34
+ interface = gr.Interface(
35
+ fn=predict,
36
+ inputs = input,
37
+ theme="grass",
38
+ outputs=output,
39
+ examples = examples,
40
+ title=title,
41
+ description=description,
42
+ article = article,
43
+ )
44
+ interface.launch(debug=True)