nihalbaig commited on
Commit
26bd4c9
1 Parent(s): ee9e047

Create app.py

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