-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking_marker_directly.py
executable file
·86 lines (73 loc) · 2.53 KB
/
tracking_marker_directly.py
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
79
80
81
82
83
84
85
86
# Module to track fiducial markers and output their pose
import numpy as np
import cv2
import json
import math
import time
import csv
import sys
MAX_BOTS = 10
VIDEO_SOURCE_ID = 0
class Marker():
def __init__(self):
# position is (x,y)
self._position = [0,0]
# orientation is theta
self._orientation = 0
def update_pose(self, position, orientation):
self._position = position
self._orientation = orientation
class Tracker():
def __init__(self):
self._cap = cv2.VideoCapture(VIDEO_SOURCE_ID)
self._dictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_50)
self._gray2 = create_blank(1920, 1080, rgb_color=(0, 0, 0))
def track_every_frame(self):
ret, frame = self._cap.read()
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
cv2.resizeWindow('frame', 1280, 960)
gray = frame
self._detected_markers = cv2.aruco.detectMarkers(gray, self._dictionary)
if len(self._detected_markers[0]) > 0:
for (fids, index) in zip(self._detected_markers[0], self._detected_markers[1]):
for pt in fids:
try:
if (int(index[0])==0):
ll = ((pt[0] +pt[1] +pt[2] +pt[3])/4)
cv2.circle(gray,(ll[0],ll[1]), 15, (0,0,255), -1)
cv2.circle(self._gray2,(ll[0],ll[1]), 15, (0,0,255), -1)
elif (int(index[0])==1):
ll = ((pt[0] +pt[1] +pt[2] +pt[3])/4)
cv2.circle(gray,(ll[0],ll[1]), 15, (0,255,0), -1)
cv2.circle(self._gray2,(ll[0],ll[1]), 15, (0,255,0), -1)
elif (int(index[0])==3):
ll = ((pt[0] +pt[1] +pt[2] +pt[3])/4)
cv2.circle(gray,(ll[0],ll[1]), 15, (255,255,0), -1)
elif (int(index[0])==2):
ll = ((pt[0] +pt[1] +pt[2] +pt[3])/4)
cv2.circle(gray,(ll[0],ll[1]), 15, (255,0,0), -1)
cv2.circle(self._gray2,(ll[0],ll[1]), 15, (255,0,0), -1)
except IndexError:
pass
if len(self._detected_markers[0]) > 0:
cv2.aruco.drawDetectedMarkers(gray, self._detected_markers[0], self._detected_markers[1])
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
# When everything done, release the capture
self._cap.release()
cv2.destroyAllWindows()
sys.exit()
# for creating a blank image
def create_blank(width, height, rgb_color=(0, 0, 0)):
"""Create new image(numpy array) filled with certain color in RGB"""
# Create black blank image
image = np.zeros((height, width, 3), np.uint8)
# Since OpenCV uses BGR, convert the color first
color = tuple(reversed(rgb_color))
# Fill image with color
image[:] = color
return image
if __name__ == "__main__":
watch_dogs = Tracker()
while (True):
watch_dogs.track_every_frame()