distance_single.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import cv2
  2. from cv2 import aruco
  3. import numpy as np
  4. import os
  5. import sys
  6. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
  7. from default_config import *
  8. visualzation = True
  9. calib_data_path = f"camera_calibration/calib_data/{set_resolution}/MultiMatrix.npz"
  10. calib_data = np.load(calib_data_path)
  11. print('resolution:', set_resolution)
  12. cam_mat = calib_data["camMatrix"]
  13. dist_coef = calib_data["distCoef"]
  14. marker_dict = aruco.getPredefinedDictionary(aruco.DICT_7X7_50)
  15. param_markers = aruco.DetectorParameters()
  16. cap = cv2.VideoCapture(0)
  17. # Thay đổi độ phân giải phù hợp với yêu cầu ban đầu
  18. new_height = RESOLUTION_STANDARDS[set_resolution]
  19. new_width, new_height = change_resolution(cap,new_height)
  20. print(f'Resolution camera {new_width}x{new_height}')
  21. fps = cap.get(cv2.CAP_PROP_FPS)
  22. print(f"Frames per second: {fps}")
  23. while True:
  24. ret, frame = cap.read()
  25. if not ret:
  26. break
  27. gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  28. detector = cv2.aruco.ArucoDetector(marker_dict, param_markers)
  29. marker_corners, marker_IDs, reject = detector.detectMarkers(gray_frame)
  30. if marker_corners:
  31. rVec, tVec, _ = aruco.estimatePoseSingleMarkers(
  32. marker_corners, MARKER_SIZE, cam_mat, dist_coef
  33. )
  34. total_markers = range(0, marker_IDs.size)
  35. for ids, corners, i in zip(marker_IDs, marker_corners, total_markers):
  36. if visualzation:
  37. cv2.polylines(
  38. frame, [corners.astype(np.int32)], True, (0, 255, 255), 4, cv2.LINE_AA
  39. )
  40. corners = corners.reshape(4, 2)
  41. corners = corners.astype(int)
  42. top_right = corners[0].ravel()
  43. top_left = corners[1].ravel()
  44. bottom_right = corners[2].ravel()
  45. bottom_left = corners[3].ravel()
  46. distance = np.linalg.norm(tVec[i][0])
  47. x = tVec[i][0][0]
  48. y = tVec[i][0][1]
  49. z = tVec[i][0][2]
  50. # Draw the pose of the marker
  51. if visualzation:
  52. point = cv2.drawFrameAxes(frame, cam_mat, dist_coef, rVec[i], tVec[i], 4, 4)
  53. cv2.putText(
  54. frame,
  55. f"id: {ids[0]} Dist: {distance: .2f}",
  56. top_right,
  57. cv2.FONT_HERSHEY_PLAIN,
  58. 1.3,
  59. (0, 0, 255),
  60. 2,
  61. cv2.LINE_AA,
  62. )
  63. cv2.putText(
  64. frame,
  65. f"x:{x: .1f} y: {y: .1f} ",
  66. bottom_right,
  67. cv2.FONT_HERSHEY_PLAIN,
  68. 1.0,
  69. (0, 0, 255),
  70. 2,
  71. cv2.LINE_AA,
  72. )
  73. print(f"Marker {ids[0]}: x = {x: .1f}, y = {y: .1f} , z = {z: .1f}, distance = {distance: .2f}")
  74. if visualzation:
  75. cv2.imshow("frame", frame)
  76. key = cv2.waitKey(1)
  77. if key == ord("q"):
  78. break
  79. cap.release()
  80. if visualzation:
  81. cv2.destroyAllWindows()