Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,48 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from PIL import Image
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import hf_hub_download
|
3 |
from PIL import Image
|
4 |
+
import yolov5
|
5 |
+
|
6 |
+
# Downloading the model from HuggingFace
|
7 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='myYOLO/best.pt', force_reload=True)
|
8 |
+
|
9 |
+
|
10 |
+
# Prediction function
|
11 |
+
def predict(im, threshold=0.50):
|
12 |
+
"""
|
13 |
+
Performs prediction using Datacat YOLOv5 model
|
14 |
+
"""
|
15 |
+
|
16 |
+
# We could resize the image, but the application handles high definition for now
|
17 |
+
# g = (size / max(im.size)) # gain
|
18 |
+
# im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS)
|
19 |
+
|
20 |
+
# initializing confidence threshold
|
21 |
+
model.conf = threshold
|
22 |
+
# inference
|
23 |
+
results = model(im)
|
24 |
+
numpy_image = results.render()[0]
|
25 |
+
output_image = Image.fromarray(numpy_image)
|
26 |
+
|
27 |
+
return output_image
|
28 |
+
|
29 |
+
title = "YOLOv5 - Auction sale catalogues layout analysis"
|
30 |
+
|
31 |
+
description = "<p style='text-align: center'>YOLOv5 Gradio demo for auction sales catalogues layout analysis. Detecting titles and catalogues entries.</p>"
|
32 |
+
|
33 |
+
article = "<p style='text-align: center'>YOLOv5 source code : <a href='https://github.com/ultralytics/yolov5'>Source code</a> | <a href='https://pytorch.org/hub/ultralytics_yolov5'>PyTorch Hub</a></p>"
|
34 |
+
|
35 |
+
|
36 |
+
demo=gr.Interface(fn=predict,
|
37 |
+
inputs=[gr.Image(type="pil", label="document image"), gr.Slider(maximum=1, step=0.01, value=0.50)],
|
38 |
+
outputs=gr.Image(type="pil", label="annotated document").style(height=700),
|
39 |
+
title=title,
|
40 |
+
description=description,
|
41 |
+
article=article,
|
42 |
+
theme="huggingface")
|
43 |
+
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
demo.launch(debug=True)
|
47 |
+
|
48 |
+
|