aruco_ar_demo.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import numpy as np
  2. import cv2 as cv
  3. # aruco
  4. adict = cv.aruco.Dictionary_get(cv.aruco.DICT_4X4_50)
  5. cv.imshow("marker", cv.aruco.drawMarker(adict, 0, 400))
  6. # random calibration data. your mileage may vary.
  7. imsize = (800, 600)
  8. K = cv.getDefaultNewCameraMatrix(np.diag([800, 800, 1]), imsize, True)
  9. # AR scene
  10. cv.ovis.addResourceLocation("packs/Sinbad.zip") # shipped with Ogre
  11. win = cv.ovis.createWindow("arucoAR", imsize, flags=0)
  12. win.setCameraIntrinsics(K, imsize)
  13. win.createEntity("figure", "Sinbad.mesh", (0, 0, 5), (1.57, 0, 0))
  14. win.createLightEntity("sun", (0, 0, 100))
  15. # video capture
  16. cap = cv.VideoCapture(0)
  17. cap.set(cv.CAP_PROP_FRAME_WIDTH, imsize[0])
  18. cap.set(cv.CAP_PROP_FRAME_HEIGHT, imsize[1])
  19. while cv.ovis.waitKey(1) != 27:
  20. img = cap.read()[1]
  21. win.setBackground(img)
  22. corners, ids = cv.aruco.detectMarkers(img, adict)[:2]
  23. cv.waitKey(1)
  24. if ids is None:
  25. continue
  26. rvecs, tvecs = cv.aruco.estimatePoseSingleMarkers(corners, 5, K, None)[:2]
  27. win.setCameraPose(tvecs[0].ravel(), rvecs[0].ravel(), invert=True)