14
14
import torchvision
15
15
import cv2
16
16
import numpy as np
17
- import time
17
+ import os
18
18
19
19
import _init_paths
20
20
import models
@@ -211,16 +211,15 @@ def parse_args():
211
211
return args
212
212
213
213
214
- def main ( ):
214
+ def get_deepHRnet_keypoints ( video , output_dir = None , output_video = False , save_kpts = False ):
215
215
216
216
keypoints = None
217
217
# cudnn related setting
218
218
cudnn .benchmark = cfg .CUDNN .BENCHMARK
219
219
torch .backends .cudnn .deterministic = cfg .CUDNN .DETERMINISTIC
220
220
torch .backends .cudnn .enabled = cfg .CUDNN .ENABLED
221
221
222
- args = parse_args ()
223
- update_config (cfg , args )
222
+ #update_config(cfg, args)
224
223
225
224
box_model = torchvision .models .detection .fasterrcnn_resnet50_fpn (pretrained = True )
226
225
box_model .to (CTX )
@@ -245,16 +244,17 @@ def main():
245
244
pose_model .eval ()
246
245
247
246
# Loading an video or an video
248
- vidcap = cv2 .VideoCapture (args .video )
249
- save_path = args .output_dir + "/output.avi"
250
- fourcc = cv2 .VideoWriter_fourcc (* 'XVID' )
251
- vid_fps = vidcap .get (cv2 .CAP_PROP_FPS )
252
- out = cv2 .VideoWriter (save_path ,fourcc , vid_fps , (int (vidcap .get (3 )),int (vidcap .get (4 ))))
247
+ vidcap = cv2 .VideoCapture (video )
248
+ vid_name , vid_type = os .path .splitext (video )
249
+ if output_dir :
250
+ save_path = output_dir + f"/{ vid_name } _deephrnet_output.{ vid_type } "
251
+ fourcc = cv2 .VideoWriter_fourcc (* 'XVID' )
252
+ vid_fps = vidcap .get (cv2 .CAP_PROP_FPS )
253
+ out = cv2 .VideoWriter (save_path ,fourcc , vid_fps , (int (vidcap .get (3 )),int (vidcap .get (4 ))))
253
254
254
255
while True :
255
256
ret , image_bgr = vidcap .read ()
256
257
if ret :
257
- last_time = time .time ()
258
258
image = image_bgr [:, :, [2 , 1 , 0 ]]
259
259
260
260
input = []
@@ -291,25 +291,21 @@ def main():
291
291
else :
292
292
keypoints = np .append (keypoints , [[[np .nan , np .nan ]]* len (COCO_KEYPOINT_INDEXES )], axis = 0 )
293
293
294
- if args .showFps :
295
- fps = 1 / (time .time ()- last_time )
296
- img = cv2 .putText (image_bgr , 'fps: ' + "%.2f" % (fps ), (25 , 40 ), cv2 .FONT_HERSHEY_SIMPLEX , 1.2 , (0 , 255 , 0 ), 2 )
297
-
298
- if args .write :
294
+ if output_video :
299
295
out .write (image_bgr )
300
296
301
297
else :
302
298
print ('Video ended' )
303
299
break
300
+
301
+ if save_kpts :
302
+ np .save (f"{ output_dir } /keypoints" , keypoints )
303
+ print (f'keypoint saved to { output_dir } /keypoints.npy' )
304
304
305
- np .save (f"{ args .output_dir } /keypoints" , keypoints )
306
- print (f'keypoint saved to { args .output_dir } /keypoints.npy' )
307
305
cv2 .destroyAllWindows ()
308
306
vidcap .release ()
309
- if args . write :
307
+ if output_video :
310
308
print ('video has been saved as {}' .format (save_path ))
311
309
out .release ()
312
310
313
-
314
- if __name__ == '__main__' :
315
- main ()
311
+ return keypoints
0 commit comments