File size: 1,616 Bytes
263ad4a
8282c39
902135a
8282c39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import hf_hub_download
from PIL import Image
import yolov5

# Downloading the model from HuggingFace
model = torch.hub.load('ultralytics/yolov5', 'custom', path='myYOLO/best.pt', force_reload=True)


# Prediction function
def predict(im, threshold=0.50):
    """
    Performs prediction using Datacat YOLOv5 model
    """

    # We could resize the image, but the application handles high definition for now
    # g = (size / max(im.size))  # gain
    # im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS)
    
    # initializing confidence threshold
    model.conf = threshold
    # inference
    results = model(im)
    numpy_image = results.render()[0]
    output_image = Image.fromarray(numpy_image)

    return output_image

title = "YOLOv5 - Auction sale catalogues layout analysis"

description = "<p style='text-align: center'>YOLOv5 Gradio demo for auction sales catalogues layout analysis. Detecting titles and catalogues entries.</p>"

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>"


demo=gr.Interface(fn=predict,
            inputs=[gr.Image(type="pil", label="document image"), gr.Slider(maximum=1, step=0.01, value=0.50)],
            outputs=gr.Image(type="pil", label="annotated document").style(height=700),
            title=title,
            description=description,
            article=article,
            theme="huggingface")


if __name__ == "__main__":
    demo.launch(debug=True)