nickgambirasi
commited on
Commit
•
e4fcc21
1
Parent(s):
33f23b3
upload app file and requirements
Browse files- app.py +59 -0
- packages.txt +1 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import pytesseract
|
4 |
+
|
5 |
+
import yolov5
|
6 |
+
|
7 |
+
# Pytesseract init
|
8 |
+
choices = pytesseract.get_languages(config='')
|
9 |
+
|
10 |
+
def text_inference(filepath, languages):
|
11 |
+
|
12 |
+
return pytesseract.image_to_string(Image.open(filepath), lang=', '.join(languages))
|
13 |
+
|
14 |
+
# YOLOv5 Init
|
15 |
+
model = yolov5.load('nickgambirasi/yolov5s-recycling')
|
16 |
+
|
17 |
+
model.conf = 0.2
|
18 |
+
model.iou = 0.45
|
19 |
+
model.agnostic = False
|
20 |
+
model.multi_label = False
|
21 |
+
model.max_det = 1000
|
22 |
+
|
23 |
+
def yolo_inference(image):
|
24 |
+
|
25 |
+
results = model(Image.open(image), size=640)
|
26 |
+
predictions = results.pred[0]
|
27 |
+
|
28 |
+
scores = predictions[:, 4]
|
29 |
+
|
30 |
+
return scores
|
31 |
+
|
32 |
+
def full_inference(image, languages):
|
33 |
+
|
34 |
+
text_infer = text_inference(image, languages)
|
35 |
+
symbol_infer = yolo_inference(image)
|
36 |
+
|
37 |
+
output = {}
|
38 |
+
|
39 |
+
if text_infer:
|
40 |
+
|
41 |
+
output.update({'text': 'all_pass'})
|
42 |
+
|
43 |
+
if any(symbol_infer > model.conf):
|
44 |
+
|
45 |
+
output.update({'symbols': 'all_pass'})
|
46 |
+
|
47 |
+
return output
|
48 |
+
|
49 |
+
title = "Hyperintelligent Label Parser"
|
50 |
+
description = "Gradio deployment of models for text and symbol detection on product labels, powered by Tesseract and YOLOv5"
|
51 |
+
gr.Interface(
|
52 |
+
full_inference,
|
53 |
+
[gr.inputs.Image(type="filepath", label="Upload Your Label"), gr.inputs.CheckboxGroup(choices, type="value", default=['eng'], label="Language of Text")],
|
54 |
+
'text',
|
55 |
+
title=title,
|
56 |
+
description=description,
|
57 |
+
article=None,
|
58 |
+
examples=None
|
59 |
+
).launch(enable_queue=True)
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
tesseract-ocr-all
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
jinja2
|
3 |
+
pytesseract
|