AlshimaaGamalAlsaied commited on
Commit
c0892d3
1 Parent(s): e56bf1e

Add application file

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import yolov7
4
+
5
+
6
+ from huggingface_hub import hf_hub_download
7
+
8
+ # Images
9
+ torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')
10
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/obss/sahi/main/tests/data/small-vehicles1.jpeg', 'small-vehicles1.jpeg')
11
+
12
+ def yolov7_inference(
13
+ image: gr.inputs.Image = None,
14
+ model_path: gr.inputs.Dropdown = None,
15
+ image_size: gr.inputs.Slider = 640,
16
+ conf_threshold: gr.inputs.Slider = 0.25,
17
+ iou_threshold: gr.inputs.Slider = 0.45,
18
+ ):
19
+ """
20
+ YOLOv7 inference function
21
+ Args:
22
+ image: Input image
23
+ model_path: Path to the model
24
+ image_size: Image size
25
+ conf_threshold: Confidence threshold
26
+ iou_threshold: IOU threshold
27
+ Returns:
28
+ Rendered image
29
+ """
30
+
31
+ model = yolov7.load(model_path, device="cpu", hf_model=True, trace=False)
32
+ model.conf = conf_threshold
33
+ model.iou = iou_threshold
34
+ results = model([image], size=image_size)
35
+ return results.render()[0]
36
+
37
+
38
+ inputs = [
39
+ gr.inputs.Image(type="pil", label="Input Image"),
40
+ gr.inputs.Dropdown(
41
+ choices=[
42
+ "kadirnar/yolov7-tiny-v0.1",
43
+ "kadirnar/yolov7-v0.1",
44
+ ],
45
+ default="kadirnar/yolov7-tiny-v0.1",
46
+ label="Model",
47
+ ),
48
+ gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
49
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
50
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
51
+ ]
52
+
53
+ outputs = gr.outputs.Image(type="filepath", label="Output Image")
54
+ title = "Yolov7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors"
55
+
56
+ examples = [['small-vehicles1.jpeg', 'kadirnar/yolov7-tiny-v0.1', 640, 0.25, 0.45], ['zidane.jpg', 'kadirnar/yolov7-v0.1', 640, 0.25, 0.45]]
57
+ demo_app = gr.Interface(
58
+ fn=yolov7_inference,
59
+ inputs=inputs,
60
+ outputs=outputs,
61
+ title=title,
62
+ examples=examples,
63
+ cache_examples=True,
64
+ theme='huggingface',
65
+ )
66
+ demo_app.launch(debug=True, enable_queue=True)