/src/01_body_from_image.py

https://github.com/burningion/dab-and-tpose-controlled-lights · Python · 72 lines · 48 code · 17 blank · 7 comment · 5 complexity · 67114e5b99ce6f9cafbcbc559e632b65 MD5 · raw file

  1. import cv2
  2. import pyopenpose as op
  3. from imutils import translate, rotate, resize
  4. import time
  5. import numpy as np
  6. # Custom Params (refer to include/openpose/flags.hpp for more parameters)
  7. params = dict()
  8. params["model_folder"] = "../../models/"
  9. vs = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,format=(string)NV12, framerate=(fraction)24/1 ! nvvidconv flip-method=0 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
  10. # Starting OpenPose
  11. opWrapper = op.WrapperPython()
  12. opWrapper.configure(params)
  13. opWrapper.start()
  14. datum = op.Datum()
  15. np.set_printoptions(precision=4)
  16. dabs = []
  17. tposes = []
  18. other = []
  19. fps_time = 0
  20. while True:
  21. ret_val, frame = vs.read()
  22. datum.cvInputData = frame
  23. opWrapper.emplaceAndPop([datum])
  24. # need to be able to see what's going on
  25. image = datum.cvOutputData
  26. cv2.putText(image,
  27. "FPS: %f" % (1.0 / (time.time() - fps_time)),
  28. (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
  29. (0, 255, 0), 2)
  30. cv2.imshow("Openpose", image)
  31. fps_time = time.time()
  32. # quit with a q keypress, b or m to save data
  33. key = cv2.waitKey(1) & 0xFF
  34. if key == ord("q"):
  35. break
  36. elif key == ord("b"):
  37. print("Dab: " + str(datum.poseKeypoints))
  38. dabs.append(datum.poseKeypoints[0])
  39. elif key == ord("m"):
  40. print("TPose: " + str(datum.poseKeypoints))
  41. tposes.append(datum.poseKeypoints[0])
  42. elif key == ord("/"):
  43. print("Other: " + str(datum.poseKeypoints))
  44. other.append(datum.poseKeypoints[0])
  45. # write our data as numpy binary files
  46. # for analysis later
  47. dabs = np.asarray(dabs)
  48. tposes = np.asarray(tposes)
  49. other = np.asarray(other)
  50. np.save('dabs.npy', dabs)
  51. np.save('tposes.npy', tposes)
  52. np.save('other.npy', other)
  53. # clean up after yourself
  54. vs.release()
  55. cv2.destroyAllWindows()