Skip to content

Commit 7140dce

Browse files
authored
Add files via upload
1 parent a079b46 commit 7140dce

10 files changed

+1809
-0
lines changed

Aruco.py

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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()

Camera Data.csv

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
ros_x,ros_y,area,center_x,center_y
2+
-0.208,-0.43,2704,470,80
3+
-0.208,-0.41,2209,454,97
4+
-0.208,-0.39,1849,441,112
5+
-0.208,-0.37,1521,431,125
6+
-0.208,-0.35,1296,423,135
7+
-0.208,-0.33,1156,415,144
8+
-0.208,-0.31,1024,409,151
9+
-0.208,-0.29,900,403,158
10+
-0.208,-0.27,784,398,164
11+
-0.22,-0.43,2704,427,80
12+
-0.22,-0.41,2209,416,96
13+
-0.22,-0.39,1849,407,111
14+
-0.22,-0.37,1521,400,124
15+
-0.22,-0.35,1296,393,135
16+
-0.22,-0.33,1156,388,143
17+
-0.22,-0.31,1024,383,151
18+
-0.22,-0.29,900,379,158
19+
-0.22,-0.27,784,375,164
20+
-0.24,-0.43,2704,357,79
21+
-0.24,-0.41,2209,352,96
22+
-0.24,-0.39,1849,349,111
23+
-0.24,-0.37,1521,347,124
24+
-0.24,-0.35,1369,344,134
25+
-0.24,-0.33,1156,342,143
26+
-0.24,-0.31,961,341,151
27+
-0.24,-0.29,900,339,157
28+
-0.24,-0.27,784,338,164
29+
-0.26,-0.43,2704,285,79
30+
-0.26,-0.41,2209,288,96
31+
-0.26,-0.39,1849,291,110
32+
-0.26,-0.37,1521,293,124
33+
-0.26,-0.35,1296,295,134
34+
-0.26,-0.33,1156,297,143
35+
-0.26,-0.31,1024,298,150
36+
-0.26,-0.29,900,300,158
37+
-0.26,-0.27,784,300,164
38+
-0.28,-0.43,2704,214,78
39+
-0.28,-0.41,2209,224,95
40+
-0.28,-0.39,1849,233,110
41+
-0.28,-0.37,1521,240,123
42+
-0.28,-0.35,1296,246,134
43+
-0.28,-0.33,1156,251,142
44+
-0.28,-0.31,961,256,151
45+
-0.28,-0.29,900,259,157
46+
-0.28,-0.27,729,263,164

0 commit comments

Comments
 (0)