AlshimaaGamalAlsaied commited on
Commit
bc98293
1 Parent(s): 47843ee
Files changed (1) hide show
  1. app.py +79 -132
app.py CHANGED
@@ -16,7 +16,7 @@ import gradio as gr
16
  # o_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')
17
  # #torch.hub.download_url_to_file('https://raw.githubusercontent.com/obss/sahi/main/tests/data/small-vehicles1.jpeg', 'small-vehicles1.jpeg')
18
 
19
-
20
  def image_fn(
21
  image: gr.inputs.Image = None,
22
  model_path: gr.inputs.Dropdown = None,
@@ -42,18 +42,64 @@ def image_fn(
42
  results = model([image], size=image_size)
43
  return results.render()[0]
44
 
 
 
 
 
 
 
45
 
 
46
 
47
- demo_app = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  fn=image_fn,
49
  inputs=[
50
  gr.inputs.Image(type="pil", label="Input Image"),
51
  gr.inputs.Dropdown(
52
  choices=[
53
- "alshimaa/yolo5_epoch100",
54
  #"kadirnar/yolov7-v0.1",
55
  ],
56
- default="alshimaa/yolo5_epoch100",
57
  label="Model",
58
  )
59
  #gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size")
@@ -61,140 +107,41 @@ demo_app = gr.Interface(
61
  #gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold")
62
  ],
63
  outputs=gr.outputs.Image(type="filepath", label="Output Image"),
64
- title="Object Detector: Identify People Without Mask",
65
- examples=[['img1.png', 'alshimaa/yolo5_epoch100', 640, 0.25, 0.45], ['img2.png', 'alshimaa/yolo5_epoch100', 640, 0.25, 0.45], ['img3.png', 'alshimaa/yolo5_epoch100', 640, 0.25, 0.45]],
66
  cache_examples=True,
67
- live=True,
68
  theme='huggingface',
69
  )
70
- demo_app.launch(debug=True, enable_queue=True)
71
 
72
-
73
- # def image_fn(
74
- # image: gr.inputs.Image = None,
75
- # model_path: gr.inputs.Dropdown = None,
76
- # image_size: gr.inputs.Slider = 640,
77
- # conf_threshold: gr.inputs.Slider = 0.25,
78
- # iou_threshold: gr.inputs.Slider = 0.45,
79
- # ):
80
- # """
81
- # YOLOv7 inference function
82
- # Args:
83
- # image: Input image
84
- # model_path: Path to the model
85
- # image_size: Image size
86
- # conf_threshold: Confidence threshold
87
- # iou_threshold: IOU threshold
88
- # Returns:
89
- # Rendered image
90
- # """
91
-
92
- # model = yolov7.load(model_path, device="cpu", hf_model=True, trace=False)
93
- # model.conf = conf_threshold
94
- # model.iou = iou_threshold
95
- # results = model([image], size=image_size)
96
- # return results.render()[0]
97
-
98
-
99
-
100
- # def video_fn(model_path, video_file, conf_thres, iou_thres, start_sec, duration):
101
- # model = yolov7.load(model_path, device="cpu", hf_model=True, trace=False)
102
- # start_timestamp = time.strftime("%H:%M:%S", time.gmtime(start_sec))
103
- # end_timestamp = time.strftime("%H:%M:%S", time.gmtime(start_sec + duration))
104
-
105
- # suffix = Path(video_file).suffix
106
-
107
- # clip_temp_file = tempfile.NamedTemporaryFile(suffix=suffix)
108
- # subprocess.call(
109
- # f"ffmpeg -y -ss {start_timestamp} -i {video_file} -to {end_timestamp} -c copy {clip_temp_file.name}".split()
110
- # )
111
-
112
- # # Reader of clip file
113
- # cap = cv2.VideoCapture(clip_temp_file.name)
114
-
115
- # # This is an intermediary temp file where we'll write the video to
116
- # # Unfortunately, gradio doesn't play too nice with videos rn so we have to do some hackiness
117
- # # with ffmpeg at the end of the function here.
118
- # with tempfile.NamedTemporaryFile(suffix=".mp4") as temp_file:
119
- # out = cv2.VideoWriter(temp_file.name, cv2.VideoWriter_fourcc(*"MP4V"), 30, (1280, 720))
120
-
121
- # num_frames = 0
122
- # max_frames = duration * 30
123
- # while cap.isOpened():
124
- # try:
125
- # ret, frame = cap.read()
126
- # if not ret:
127
- # break
128
- # except Exception as e:
129
- # print(e)
130
- # continue
131
- # print("FRAME DTYPE", type(frame))
132
- # out.write(model([frame], conf_thres, iou_thres))
133
- # num_frames += 1
134
- # print("Processed {} frames".format(num_frames))
135
- # if num_frames == max_frames:
136
- # break
137
-
138
- # out.release()
139
-
140
- # # Aforementioned hackiness
141
- # out_file = tempfile.NamedTemporaryFile(suffix="out.mp4", delete=False)
142
- # subprocess.run(f"ffmpeg -y -loglevel quiet -stats -i {temp_file.name} -c:v libx264 {out_file.name}".split())
143
-
144
- # return out_file.name
145
-
146
- # image_interface = gr.Interface(
147
- # fn=image_fn,
148
- # inputs=[
149
- # gr.inputs.Image(type="pil", label="Input Image"),
150
- # gr.inputs.Dropdown(
151
- # choices=[
152
- # "alshimaa/SEE_model_yolo7",
153
- # #"kadirnar/yolov7-v0.1",
154
- # ],
155
- # default="alshimaa/SEE_model_yolo7",
156
- # label="Model",
157
- # )
158
- # #gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size")
159
- # #gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
160
- # #gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold")
161
- # ],
162
- # outputs=gr.outputs.Image(type="filepath", label="Output Image"),
163
- # title="Smart Environmental Eye (SEE)",
164
- # examples=[['image1.jpg', 'alshimaa/SEE_model_yolo7', 640, 0.25, 0.45], ['image2.jpg', 'alshimaa/SEE_model_yolo7', 640, 0.25, 0.45], ['image3.jpg', 'alshimaa/SEE_model_yolo7', 640, 0.25, 0.45]],
165
- # cache_examples=True,
166
- # theme='huggingface',
167
- # )
168
-
169
-
170
- # video_interface = gr.Interface(
171
- # fn=video_fn,
172
- # inputs=[
173
- # gr.inputs.Video(source = "upload", type = "mp4", label = "Input Video"),
174
- # gr.inputs.Dropdown(
175
- # choices=[
176
- # "alshimaa/SEE_model_yolo7",
177
- # #"kadirnar/yolov7-v0.1",
178
- # ],
179
- # default="alshimaa/SEE_model_yolo7",
180
- # label="Model",
181
- # ),
182
- # ],
183
- # outputs=gr.outputs.Video(type = "mp4", label = "Output Video"),
184
- # # examples=[
185
- # # ["video.mp4", 0.25, 0.45, 0, 2],
186
 
187
- # # ],
188
- # title="Smart Environmental Eye (SEE)",
189
- # cache_examples=True,
190
- # theme='huggingface',
191
 
192
- # )
193
 
194
- # if __name__ == "__main__":
195
- # gr.TabbedInterface(
196
- # [image_interface, video_interface],
197
- # ["Run on Images", "Run on Videos"],
198
- # ).launch()
199
 
200
 
 
16
  # o_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')
17
  # #torch.hub.download_url_to_file('https://raw.githubusercontent.com/obss/sahi/main/tests/data/small-vehicles1.jpeg', 'small-vehicles1.jpeg')
18
 
19
+
20
  def image_fn(
21
  image: gr.inputs.Image = None,
22
  model_path: gr.inputs.Dropdown = None,
 
42
  results = model([image], size=image_size)
43
  return results.render()[0]
44
 
45
+
46
+
47
+ def video_fn(model_path, video_file, conf_thres, iou_thres, start_sec, duration):
48
+ model = yolov7.load(model_path, device="cpu", hf_model=True, trace=False)
49
+ start_timestamp = time.strftime("%H:%M:%S", time.gmtime(start_sec))
50
+ end_timestamp = time.strftime("%H:%M:%S", time.gmtime(start_sec + duration))
51
 
52
+ suffix = Path(video_file).suffix
53
 
54
+ clip_temp_file = tempfile.NamedTemporaryFile(suffix=suffix)
55
+ subprocess.call(
56
+ f"ffmpeg -y -ss {start_timestamp} -i {video_file} -to {end_timestamp} -c copy {clip_temp_file.name}".split()
57
+ )
58
+
59
+ # Reader of clip file
60
+ cap = cv2.VideoCapture(clip_temp_file.name)
61
+
62
+ # This is an intermediary temp file where we'll write the video to
63
+ # Unfortunately, gradio doesn't play too nice with videos rn so we have to do some hackiness
64
+ # with ffmpeg at the end of the function here.
65
+ with tempfile.NamedTemporaryFile(suffix=".mp4") as temp_file:
66
+ out = cv2.VideoWriter(temp_file.name, cv2.VideoWriter_fourcc(*"MP4V"), 30, (1280, 720))
67
+
68
+ num_frames = 0
69
+ max_frames = duration * 30
70
+ while cap.isOpened():
71
+ try:
72
+ ret, frame = cap.read()
73
+ if not ret:
74
+ break
75
+ except Exception as e:
76
+ print(e)
77
+ continue
78
+ print("FRAME DTYPE", type(frame))
79
+ out.write(model([frame], conf_thres, iou_thres))
80
+ num_frames += 1
81
+ print("Processed {} frames".format(num_frames))
82
+ if num_frames == max_frames:
83
+ break
84
+
85
+ out.release()
86
+
87
+ # Aforementioned hackiness
88
+ out_file = tempfile.NamedTemporaryFile(suffix="out.mp4", delete=False)
89
+ subprocess.run(f"ffmpeg -y -loglevel quiet -stats -i {temp_file.name} -c:v libx264 {out_file.name}".split())
90
+
91
+ return out_file.name
92
+
93
+ image_interface = gr.Interface(
94
  fn=image_fn,
95
  inputs=[
96
  gr.inputs.Image(type="pil", label="Input Image"),
97
  gr.inputs.Dropdown(
98
  choices=[
99
+ "alshimaa/SEE_model_yolo7",
100
  #"kadirnar/yolov7-v0.1",
101
  ],
102
+ default="alshimaa/SEE_model_yolo7",
103
  label="Model",
104
  )
105
  #gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size")
 
107
  #gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold")
108
  ],
109
  outputs=gr.outputs.Image(type="filepath", label="Output Image"),
110
+ title="Smart Environmental Eye (SEE)",
111
+ examples=[['image1.jpg', 'alshimaa/SEE_model_yolo7', 640, 0.25, 0.45], ['image2.jpg', 'alshimaa/SEE_model_yolo7', 640, 0.25, 0.45], ['image3.jpg', 'alshimaa/SEE_model_yolo7', 640, 0.25, 0.45]],
112
  cache_examples=True,
 
113
  theme='huggingface',
114
  )
 
115
 
116
+
117
+ video_interface = gr.Interface(
118
+ fn=video_fn,
119
+ inputs=[
120
+ gr.inputs.Video(source = "upload", type = "mp4", label = "Input Video"),
121
+ gr.inputs.Dropdown(
122
+ choices=[
123
+ "alshimaa/SEE_model_yolo7",
124
+ #"kadirnar/yolov7-v0.1",
125
+ ],
126
+ default="alshimaa/SEE_model_yolo7",
127
+ label="Model",
128
+ ),
129
+ ],
130
+ outputs=gr.outputs.Video(type = "mp4", label = "Output Video"),
131
+ # examples=[
132
+ # ["video.mp4", 0.25, 0.45, 0, 2],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
+ # ],
135
+ title="Smart Environmental Eye (SEE)",
136
+ cache_examples=True,
137
+ theme='huggingface',
138
 
139
+ )
140
 
141
+ if __name__ == "__main__":
142
+ gr.TabbedInterface(
143
+ [image_interface, video_interface],
144
+ ["Run on Images", "Run on Videos"],
145
+ ).launch()
146
 
147