PierreLeveau commited on
Commit
09e128c
1 Parent(s): 11d0de1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
+
5
+ # Images
6
+ torch.hub.download_url_to_file('https://storage.googleapis.com/kili-datasets-public/plastic-in-river/ckze0btj10ejf0lyy1imtdy7o.jpg', 'bottles1.jpg')
7
+ torch.hub.download_url_to_file('https://storage.googleapis.com/kili-datasets-public/plastic-in-river/ckze0btj10ejd0lyyfzm85k9u.jpg', 'bottles2.jpg')
8
+
9
+ # Model
10
+ model = torch.hub.load_state_dict_from_url("gs://kili-datasets-public/plastic_in_river/model/best.pt", force_reload=True) # force_reload=True to update
11
+ # -> load the model from HF models.
12
+
13
+ def yolo(im, size=640):
14
+ g = (size / max(im.size)) # gain
15
+ im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize
16
+
17
+ results = model(im) # inference
18
+ results.render() # updates results.imgs with boxes and labels
19
+ return Image.fromarray(results.imgs[0])
20
+
21
+
22
+ inputs = gr.inputs.Image(type='pil', label="Original Image")
23
+ outputs = gr.outputs.Image(type="pil", label="Output Image")
24
+
25
+ title = "YOLOv5"
26
+ description = "YOLOv5 Gradio demo for object detection. Upload an image or click an example image to use."
27
+ article = "<p style='text-align: center'>YOLOv5 is a family of compound-scaled object detection models trained on the COCO dataset, and includes simple functionality for Test Time Augmentation (TTA), model ensembling, hyperparameter evolution, and export to ONNX, CoreML and TFLite. <a href='https://github.com/ultralytics/yolov5'>Source code</a> |<a href='https://apps.apple.com/app/id1452689527'>iOS App</a> | <a href='https://pytorch.org/hub/ultralytics_yolov5'>PyTorch Hub</a></p>"
28
+
29
+ examples = [['bottles1.jpg'], ['bottles2.jpg']]
30
+ gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples, theme="huggingface").launch(
31
+ debug=True)