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