1
+ import cv2
2
+ from cv2 import aruco
3
+ import numpy as np
4
+ from CameraCalibration import getCoords
5
+
6
+ def getCenterFromAruco ():
7
+ cen = np .array ([0 ,0 ])
8
+ area = 0
9
+
10
+ marker_dict = aruco .Dictionary_get (aruco .DICT_5X5_1000 )
11
+
12
+ param_markers = aruco .DetectorParameters_create ()
13
+
14
+ cap = cv2 .VideoCapture (0 )
15
+ #cap = cv2.VideoCapture(1, cv2.CAP_DSHOW)
16
+
17
+
18
+ while True :
19
+ ret , frame = cap .read ()
20
+ if not ret :
21
+ break
22
+ gray_frame = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
23
+ marker_corners , marker_IDs , reject = aruco .detectMarkers (
24
+ gray_frame , marker_dict , parameters = param_markers
25
+ )
26
+ if marker_corners :
27
+ for ids , corners in zip (marker_IDs , marker_corners ):
28
+ cv2 .polylines (
29
+ frame , [corners .astype (np .int32 )], True , (0 , 255 , 255 ), 4 , cv2 .LINE_AA
30
+ )
31
+ corners = corners .reshape (4 , 2 )
32
+ corners = corners .astype (int )
33
+ top_right = corners [0 ].ravel ()
34
+ top_left = corners [1 ].ravel ()
35
+ bottom_right = corners [2 ].ravel ()
36
+ bottom_left = corners [3 ].ravel ()
37
+ center = bottom_left
38
+ center [0 ] = (top_left [0 ] + bottom_right [0 ])/ 2
39
+ center [1 ] = (top_left [1 ] + bottom_right [1 ])/ 2
40
+ cv2 .putText (
41
+ frame ,
42
+ f"id: { ids [0 ]} " ,
43
+ top_right ,
44
+ cv2 .FONT_HERSHEY_PLAIN ,
45
+ 1.3 ,
46
+ (200 , 100 , 0 ),
47
+ 2 ,
48
+ cv2 .LINE_AA ,
49
+ )
50
+
51
+ if str (ids [0 ]) == "300" :
52
+ cen = center
53
+ area = (int (bottom_right [1 ]) - int (bottom_left [1 ]))** 2
54
+
55
+ with open (r"/home/ise.ros/Shyam/center_x.txt" ,'r+' ) as file :
56
+ file .truncate (0 )
57
+
58
+ file1 = open (r"/home/ise.ros/Shyam/center_x.txt" , "a" )
59
+ file1 .write (str (cen [0 ]))
60
+ file1 .close ()
61
+
62
+ with open (r"/home/ise.ros/Shyam/center_y.txt" ,'r+' ) as file :
63
+ file .truncate (0 )
64
+
65
+ file1 = open (r"/home/ise.ros/Shyam/center_y.txt" , "a" )
66
+ file1 .write (str (cen [1 ]))
67
+ file1 .close ()
68
+
69
+ with open (r"/home/ise.ros/Shyam/area.txt" ,'r+' ) as file :
70
+ file .truncate (0 )
71
+
72
+ file1 = open (r"/home/ise.ros/Shyam/area.txt" , "a" )
73
+ file1 .write (str (area ))
74
+ file1 .close ()
75
+ #return
76
+
77
+ cv2 .putText (
78
+ frame ,
79
+ f"Center: { center [0 ]} ,{ center [1 ]} " ,
80
+ center ,
81
+ cv2 .FONT_HERSHEY_PLAIN ,
82
+ 1.3 ,
83
+ (200 , 100 , 0 ),
84
+ 2 ,
85
+ cv2 .LINE_AA ,
86
+ )
87
+ cv2 .putText (
88
+ frame ,
89
+ f"Area: { (int (bottom_right [1 ]) - int (bottom_left [1 ]))** 2 } " ,
90
+ bottom_right ,
91
+ cv2 .FONT_HERSHEY_PLAIN ,
92
+ 1.3 ,
93
+ (200 , 100 , 0 ),
94
+ 2 ,
95
+ cv2 .LINE_AA ,
96
+ )
97
+ #print("Top Left: ", top_right)
98
+ #print("Top Right: ", top_left)
99
+ #print("Bottom Left: ", bottom_left)
100
+ #print("Bottom Right: ", bottom_right)
101
+ #print("Center Point: ", ((top_left[0] + bottom_right[0])/2 , (top_left[1] + bottom_right[1])/2))
102
+ if ids == 3 :
103
+ print (ids , " " , corners )
104
+
105
+ getCoords ()
106
+
107
+ '''blur = cv2.GaussianBlur(gray_frame,(5,5),0)
108
+ ret, thresh_img = cv2.threshold(blur,91,255,cv2.THRESH_BINARY)
109
+ contours = cv2.findContours(thresh_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[-2]
110
+ for c in contours :
111
+ #cv2.drawContours(frame, [c], -1, (0,255,0), 3)
112
+ if cv2.contourArea(c) > 1500 and cv2.contourArea(c) < 1600:
113
+ approx = cv2.approxPolyDP(c, 0.009 * cv2.arcLength(c, True), True)
114
+ cv2.drawContours(frame, [approx], 0, (0, 0, 255), 5)
115
+ #cv2.drawContours(frame, [c], -1, (0,255,0), 3)
116
+ n = approx.ravel()
117
+ i = 0
118
+
119
+ for j in n :
120
+ if(i % 2 == 0):
121
+ x = n[i]
122
+ y = n[i + 1]
123
+
124
+ # String containing the co-ordinates.
125
+ string = str(x) + " " + str(y)
126
+
127
+ if(i == 0):
128
+ # text on topmost co-ordinate.
129
+ cv2.putText(frame, "Arrow tip", (x, y),
130
+ cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 0, 0))
131
+ else:
132
+ # text on remaining co-ordinates.
133
+ cv2.putText(frame, string, (x, y),
134
+ cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0))
135
+
136
+ i = i + 1'''
137
+ #cv2.line(frame, (arucoCenter[0], arucoCenter[1]), (x, y), (255,0,0), 4)
138
+
139
+ cv2 .imshow ("frame" , frame )
140
+ key = cv2 .waitKey (1 )
141
+ if key == ord ("q" ):
142
+ break
143
+ cap .release ()
144
+ cv2 .destroyAllWindows ()
145
+ return
146
+
147
+
148
+ if __name__ == "__main__" :
149
+ getCenterFromAruco ()
150
+ #getCoords()
0 commit comments