Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
78 lines (67 loc) · 2.71 KB
/
Copy pathexample.py
File metadata and controls
78 lines (67 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import sys
import cv2
print(cv2.__file__)
print(cv2.__version__)
import numpy as np
import aruco
print(aruco.__file__)
import pkg_resources # part of setuptools
if __name__ == '__main__':
# load board and camera parameters
#boardconfig = aruco.BoardConfiguration("chessboardinfo_small_meters.yml")
camparam = aruco.CameraParameters()
camparam.readFromXMLFile(os.path.join(os.path.dirname(__file__), "dfk72_6mm_param2.yml"))
# create detector and get parameters
detector = aruco.MarkerDetector()
params = detector.getParameters()
# print detector parameters
print("detector params:")
for val in dir(params):
if not val.startswith("__"):
print("\t{} : {}".format(val, params.__getattribute__(val)))
# load video
cap = cv2.VideoCapture(os.path.join(os.path.dirname(__file__), "example.mp4"))
ret, frame = cap.read()
if not ret:
print("can't open video!")
sys.exit(-1)
while ret:
markers = detector.detect(frame)
for marker in markers:
# print marker ID and point positions
print("Marker: {:d}".format(marker.id))
for i, point in enumerate(marker):
print("\t{:d} {}".format(i, str(point)))
marker.draw(frame, np.array([255, 255, 255]), 2)
# points3d = marker.get3DPoints()
# print("3D points: {:}".format(points3d))
# for p in points3d:
# print("\t{}".format(p))
print("center: {}".format(marker.getCenter()))
print("contour: {}".format(marker.contourPoints))
points3d = marker.get3DPoints()
print("3D points: {:}".format(points3d))
for p in points3d:
print("\t{}".format(p))
# calculate marker extrinsics for marker size of 3.5cm
marker.calculateExtrinsics(0.035, camparam)
# print("Marker extrinsics:\n{}\n{}".format(marker.Tvec, marker.Rvec))
aruco.CvDrawingUtils.draw3dAxis(frame, camparam, marker.Rvec, marker.Tvec, .1)
print("detected ids: {}".format(", ".join(str(m.id) for m in markers)))
# add aruco version to frame
y, x, c = frame.shape
text = "aruco {}".format(pkg_resources.require("aruco")[0].version)
font = cv2.FONT_HERSHEY_PLAIN
font_scale = 2
thickness = 2
cv2.putText(frame, text, (15, y - 15), font, font_scale, (255, 255, 255), thickness,
cv2.LINE_AA)
# show frame
if 'DISPLAY' in os.environ.keys():
cv2.imshow("frame", frame)
cv2.waitKey(10)
else:
print("No display!")
# read next frame
ret, frame = cap.read()