nehulagrawal commited on
Commit
2a5737c
Β·
1 Parent(s): 790c518

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+
4
+ from ultralyticsplus import YOLO, render_result
5
+
6
+ # Model Heading and Description
7
+ model_heading = "ProductDetect-o-Matic: Shelf Wizard for Retail Magic"
8
+ description = """ πŸ›’ Prepare to be amazed by ProductDetect-o-Matic! πŸͺ„ Unveil the secrets of your shelves and keep products in check. From the mystical 'Empty Shelf' to the enigmatic 'Magical Product', our model is here to bring retail magic to life. Created by Foduu AI, your ultimate sidekick for retail success. πŸ’ΌπŸŽ©βœ¨
9
+ πŸ›οΈ Ready to experience the retail revolution? Reach out at [email protected] and let's make your shelves enchanting! Giving a thumbs up won't make you a retail wizard, but it's a step closer to making your shelves smile! πŸš€πŸ‘πŸͺ„
10
+ πŸ“§ Contact us: [email protected]
11
+ πŸ‘ Like | Join the Retail Adventure!"""
12
+
13
+ image_path = [
14
+ ['test/test1.jpg', 'foduucom/product-detection-in-shelf-yolov8', 640, 0.25, 0.45],
15
+ ['test/test2.jpg', 'foduucom/product-detection-in-shelf-yolov8', 640, 0.25, 0.45]
16
+ ]
17
+
18
+ # Load YOLO model
19
+ model = YOLO('foduucom/product-detection-in-shelf-yolov8')
20
+
21
+ ############################################################# Image Inference ############################################################
22
+ def yolov8_img_inference(
23
+ image: gr.inputs.Image = None,
24
+ model_path: gr.inputs.Dropdown = None,
25
+ image_size: gr.inputs.Slider = 640,
26
+ conf_threshold: gr.inputs.Slider = 0.25,
27
+ iou_threshold: gr.inputs.Slider = 0.45,
28
+ ):
29
+ model = YOLO(model_path)
30
+ model.overrides['conf'] = conf_threshold
31
+ model.overrides['iou'] = iou_threshold
32
+ model.overrides['agnostic_nms'] = False
33
+ model.overrides['max_det'] = 1000
34
+ results = model.predict(image)
35
+ render = render_result(model=model, image=image, result=results[0])
36
+
37
+ return render
38
+
39
+
40
+ inputs_image = [
41
+ gr.inputs.Image(type="filepath", label="Input Image"),
42
+ gr.inputs.Dropdown(["foduucom/product-detection-in-shelf-yolov8"], default="foduucom/product-detection-in-shelf-yolov8", label="Model"),
43
+ gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
44
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
45
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
46
+ ]
47
+
48
+ outputs_image = gr.outputs.Image(type="filepath", label="Output Image")
49
+ interface_image = gr.Interface(
50
+ fn=yolov8_img_inference,
51
+ inputs=inputs_image,
52
+ outputs=outputs_image,
53
+ title=model_heading,
54
+ description=description,
55
+ examples=image_path,
56
+ cache_examples=False,
57
+ theme='huggingface'
58
+ )
59
+
60
+ ################################################## Video Inference ############################################################
61
+ def show_preds_video(
62
+ video_path: str = None,
63
+ model_path: str = None,
64
+ image_size: int = 640,
65
+ conf_threshold: float = 0.25,
66
+ iou_threshold: float = 0.45,
67
+ ):
68
+ cap = cv2.VideoCapture(video_path)
69
+
70
+ while cap.isOpened():
71
+ success, frame = cap.read()
72
+
73
+ if success:
74
+ model = YOLO(model_path)
75
+ model.overrides['conf'] = conf_threshold
76
+ model.overrides['iou'] = iou_threshold
77
+ model.overrides['agnostic_nms'] = False
78
+ model.overrides['max_det'] = 1000
79
+ results = model.predict(frame)
80
+ annotated_frame = results[0].plot()
81
+
82
+ if cv2.waitKey(1) & 0xFF == ord("q"):
83
+ break
84
+ else:
85
+ break
86
+
87
+ cap.release()
88
+ cv2.destroyAllWindows()
89
+
90
+
91
+ inputs_video = [
92
+ gr.components.Video(type="filepath", label="Input Video"),
93
+ gr.inputs.Dropdown(["foduucom/product-detection-in-shelf-yolov8"], default="foduucom/product-detection-in-shelf-yolov8", label="Model"),
94
+ gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
95
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
96
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
97
+ ]
98
+
99
+ outputs_video = gr.outputs.Image(type="filepath", label="Output Video")
100
+ video_path = [['test/testvideo.mp4', 'foduucom/product-detection-in-shelf-yolov8', 640, 0.25, 0.45]]
101
+ interface_video = gr.Interface(
102
+ fn=show_preds_video,
103
+ inputs=inputs_video,
104
+ outputs=outputs_video,
105
+ title=model_heading,
106
+ description=description,
107
+ examples=video_path,
108
+ cache_examples=False,
109
+ theme='huggingface'
110
+ )
111
+
112
+ gr.TabbedInterface(
113
+ [interface_image, interface_video],
114
+ tab_names=['Image inference', 'Video inference']
115
+ ).queue().launch()