demo_camera.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import cv2
  2. import copy
  3. import numpy as np
  4. import torch
  5. from src import util
  6. from src.body import Body
  7. from src.hand import Hand
  8. body_estimation = Body('model/body_pose_model.pth')
  9. hand_estimation = Hand('model/hand_pose_model.pth')
  10. print(f"Torch device: {torch.cuda.get_device_name()}")
  11. cap = cv2.VideoCapture(0)
  12. print(cap.isOpened())
  13. cap.set(3, 640)
  14. cap.set(4, 480)
  15. while True:
  16. ret, oriImg = cap.read()
  17. candidate, subset = body_estimation(oriImg)
  18. canvas = copy.deepcopy(oriImg)
  19. canvas = util.draw_bodypose(canvas, candidate, subset)
  20. # detect hand
  21. hands_list = util.handDetect(candidate, subset, oriImg)
  22. all_hand_peaks = []
  23. for x, y, w, is_left in hands_list:
  24. peaks = hand_estimation(oriImg[y:y+w, x:x+w, :])
  25. peaks[:, 0] = np.where(peaks[:, 0]==0, peaks[:, 0], peaks[:, 0]+x)
  26. peaks[:, 1] = np.where(peaks[:, 1]==0, peaks[:, 1], peaks[:, 1]+y)
  27. all_hand_peaks.append(peaks)
  28. canvas = util.draw_handpose(canvas, all_hand_peaks)
  29. cv2.imshow('demo', canvas)#一个窗口用以显示原视频
  30. if cv2.waitKey(1) & 0xFF == ord('q'):
  31. break
  32. cap.release()
  33. cv2.destroyAllWindows()