shriarul5273 commited on
Commit
dd13bc1
1 Parent(s): ef2207d
Files changed (2) hide show
  1. .github/workflows/huggingface.yml +2 -2
  2. app.py +12 -7
.github/workflows/huggingface.yml CHANGED
@@ -17,9 +17,9 @@ jobs:
17
  env:
18
  HF: ${{secrets.HF_TOKEN }}
19
  HF_USER: ${{secrets.HF_USER }}
20
- run: git remote add space https://$HF_USER:$HF@huggingface.co/spaces/$HF_USER/yolov7
21
  - name: Push to hub
22
  env:
23
  HF: ${{ secrets.HF_TOKEN}}
24
  HF_USER: ${{secrets.HF_USER }}
25
- run: git push --force https://$HF_USER:$HF@huggingface.co/spaces/$HF_USER/yolov7
 
17
  env:
18
  HF: ${{secrets.HF_TOKEN }}
19
  HF_USER: ${{secrets.HF_USER }}
20
+ run: git remote add space https://$HF_USER:$HF@huggingface.co/spaces/$HF_USER/Yolov7
21
  - name: Push to hub
22
  env:
23
  HF: ${{ secrets.HF_TOKEN}}
24
  HF_USER: ${{secrets.HF_USER }}
25
+ run: git push --force https://$HF_USER:$HF@huggingface.co/spaces/$HF_USER/Yolov7
app.py CHANGED
@@ -9,7 +9,7 @@ from utils.general import check_img_size, non_max_suppression, \
9
  scale_coords
10
  from utils.plots import plot_one_box
11
  from utils.torch_utils import time_synchronized
12
-
13
 
14
 
15
 
@@ -78,8 +78,10 @@ def detect(img,model,device,iou_threshold=0.45,confidence_threshold=0.25):
78
 
79
  # Inference
80
  t1 = time_synchronized()
 
81
  with torch.no_grad(): # Calculating gradients would cause a GPU memory leak
82
  pred = model(img,augment=True)[0]
 
83
  t2 = time_synchronized()
84
 
85
  # Apply NMS
@@ -97,7 +99,7 @@ def detect(img,model,device,iou_threshold=0.45,confidence_threshold=0.25):
97
  label = f'{names[int(cls)]} {conf:.2f}'
98
  plot_one_box(xyxy, imgs, label=label, color=colors[int(cls)], line_thickness=2)
99
 
100
- return imgs
101
 
102
  def inference(img,model_link,iou_threshold,confidence_threshold):
103
  print(model_link)
@@ -118,16 +120,17 @@ def inference2(video,model_link,iou_threshold,confidence_threshold):
118
  fps = frames.get(cv2.CAP_PROP_FPS)
119
  image_size = (int(frames.get(cv2.CAP_PROP_FRAME_WIDTH)),int(frames.get(cv2.CAP_PROP_FRAME_HEIGHT)))
120
  finalVideo = cv2.VideoWriter('output.mp4',cv2.VideoWriter_fourcc(*'VP90'), fps, image_size)
121
- p = 1
122
  while frames.isOpened():
123
  ret,frame = frames.read()
124
  if not ret:
125
  break
126
- frame = detect(frame,model,device,iou_threshold,confidence_threshold)
 
127
  finalVideo.write(frame)
128
  frames.release()
129
  finalVideo.release()
130
- return 'output.mp4'
131
 
132
 
133
 
@@ -145,6 +148,7 @@ with gr.Blocks() as demo:
145
  with gr.Row():
146
  image_input = gr.Image(type='pil', label="Input Image", source="upload")
147
  image_output = gr.Image(type='pil', label="Output Image", source="upload")
 
148
  image_drop = gr.Dropdown(choices=models,value=models[0])
149
  image_iou_threshold = gr.Slider(label="IOU Threshold",interactive=True, minimum=0.0, maximum=1.0, value=0.45)
150
  image_conf_threshold = gr.Slider(label="Confidence Threshold",interactive=True, minimum=0.0, maximum=1.0, value=0.25)
@@ -155,6 +159,7 @@ with gr.Blocks() as demo:
155
  with gr.Row():
156
  video_input = gr.Video(type='pil', label="Input Image", source="upload")
157
  video_output = gr.Video(type="pil", label="Output Image",format="mp4")
 
158
  video_drop = gr.Dropdown(choices=models,value=models[0])
159
  video_iou_threshold = gr.Slider(label="IOU Threshold",interactive=True, minimum=0.0, maximum=1.0, value=0.45)
160
  video_conf_threshold = gr.Slider(label="Confidence Threshold",interactive=True, minimum=0.0, maximum=1.0, value=0.25)
@@ -167,9 +172,9 @@ with gr.Blocks() as demo:
167
 
168
  text_button.click(inference, inputs=[image_input,image_drop,
169
  image_iou_threshold,image_conf_threshold],
170
- outputs=image_output)
171
  video_button.click(inference2, inputs=[video_input,video_drop,
172
  video_iou_threshold,video_conf_threshold],
173
- outputs=video_output)
174
 
175
  demo.launch()
 
9
  scale_coords
10
  from utils.plots import plot_one_box
11
  from utils.torch_utils import time_synchronized
12
+ import time
13
 
14
 
15
 
 
78
 
79
  # Inference
80
  t1 = time_synchronized()
81
+ start = time.time()
82
  with torch.no_grad(): # Calculating gradients would cause a GPU memory leak
83
  pred = model(img,augment=True)[0]
84
+ fps_inference = 1/(time.time()-start)
85
  t2 = time_synchronized()
86
 
87
  # Apply NMS
 
99
  label = f'{names[int(cls)]} {conf:.2f}'
100
  plot_one_box(xyxy, imgs, label=label, color=colors[int(cls)], line_thickness=2)
101
 
102
+ return imgs,fps_inference
103
 
104
  def inference(img,model_link,iou_threshold,confidence_threshold):
105
  print(model_link)
 
120
  fps = frames.get(cv2.CAP_PROP_FPS)
121
  image_size = (int(frames.get(cv2.CAP_PROP_FRAME_WIDTH)),int(frames.get(cv2.CAP_PROP_FRAME_HEIGHT)))
122
  finalVideo = cv2.VideoWriter('output.mp4',cv2.VideoWriter_fourcc(*'VP90'), fps, image_size)
123
+ fps_video = []
124
  while frames.isOpened():
125
  ret,frame = frames.read()
126
  if not ret:
127
  break
128
+ frame,fps = detect(frame,model,device,iou_threshold,confidence_threshold)
129
+ fps_video.append[fps]
130
  finalVideo.write(frame)
131
  frames.release()
132
  finalVideo.release()
133
+ return 'output.mp4',np.mean(fps_video)
134
 
135
 
136
 
 
148
  with gr.Row():
149
  image_input = gr.Image(type='pil', label="Input Image", source="upload")
150
  image_output = gr.Image(type='pil', label="Output Image", source="upload")
151
+ fps_image = gr.Number(0,label='FPS')
152
  image_drop = gr.Dropdown(choices=models,value=models[0])
153
  image_iou_threshold = gr.Slider(label="IOU Threshold",interactive=True, minimum=0.0, maximum=1.0, value=0.45)
154
  image_conf_threshold = gr.Slider(label="Confidence Threshold",interactive=True, minimum=0.0, maximum=1.0, value=0.25)
 
159
  with gr.Row():
160
  video_input = gr.Video(type='pil', label="Input Image", source="upload")
161
  video_output = gr.Video(type="pil", label="Output Image",format="mp4")
162
+ fps_video = gr.Number(0,label='FPS')
163
  video_drop = gr.Dropdown(choices=models,value=models[0])
164
  video_iou_threshold = gr.Slider(label="IOU Threshold",interactive=True, minimum=0.0, maximum=1.0, value=0.45)
165
  video_conf_threshold = gr.Slider(label="Confidence Threshold",interactive=True, minimum=0.0, maximum=1.0, value=0.25)
 
172
 
173
  text_button.click(inference, inputs=[image_input,image_drop,
174
  image_iou_threshold,image_conf_threshold],
175
+ outputs=[image_output,fps_image])
176
  video_button.click(inference2, inputs=[video_input,video_drop,
177
  video_iou_threshold,video_conf_threshold],
178
+ outputs=[video_output,fps_video])
179
 
180
  demo.launch()