-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutreachVision.py
151 lines (99 loc) · 4.72 KB
/
OutreachVision.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import cv2
from pupil_apriltags import Detector
import keyboard
import math
import numpy as np
import ast
import re
import time
print("Initializing.")
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_EXPOSURE, -6)
#Scales resolution and window size
cameraScaler = .6 # Changes the resolution
cameraX = int(960 * cameraScaler)
cameraY = int(540 * cameraScaler)
at_detector = Detector(
families="tag16h5",
nthreads=1,
quad_decimate=0.3,
quad_sigma=0.35,
refine_edges=1,
decode_sharpening=0.25,
debug=0
)
cv2.namedWindow("Image", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("Image", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
print("Running loop.")
while True:
startTime = time.time()
if keyboard.is_pressed("z"):
exit()
_, img = cap.read()
img = cv2.resize(img, (cameraX, cameraY))
initImg1 = img
initImg2 = img
imgGrayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
apriltags = at_detector.detect(imgGrayscale)
apriltags = [x for x in apriltags if x.hamming == 0]
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
coneMask1 = cv2.inRange(img, (16, 0, 0), (25, 255, 255))
coneMask2 = cv2.inRange(img, (0, 180, 0), (180, 255, 255))
coneMask1 = cv2.medianBlur(coneMask1, 25)
coneMask2 = cv2.medianBlur(coneMask2, 25)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, ksize=(15, 15))
coneMask1 = cv2.erode(coneMask1, kernel)
coneMask2 = cv2.erode(coneMask2, kernel)
coneMask1 = cv2.dilate(coneMask1, kernel)
coneMask2 = cv2.dilate(coneMask2, kernel)
coneMask3 = cv2.bitwise_and(coneMask1, coneMask2)
coneMask3 = cv2.medianBlur(coneMask3, 25)
coneMask3 = cv2.erode(coneMask3, kernel)
coneMask3 = cv2.dilate(coneMask3, kernel)
coneCnts = cv2.findContours(coneMask3.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
coneCntsAreas = []
cubeMask1 = cv2.inRange(img, (115, 60, 0), (135, 230, 255))
cubeMask2 = cv2.inRange(img, (0, 60, 0), (180, 230, 255))
cubeMask1 = cv2.medianBlur(cubeMask1, 25)
cubeMask2 = cv2.medianBlur(cubeMask2, 25)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, ksize=(15, 15))
cubeMask1 = cv2.erode(cubeMask1, kernel)
cubeMask2 = cv2.erode(cubeMask2, kernel)
cubeMask1 = cv2.dilate(cubeMask1, kernel)
cubeMask2 = cv2.dilate(cubeMask2, kernel)
cubeMask3 = cv2.bitwise_and(cubeMask1, cubeMask2)
cubeMask3 = cv2.medianBlur(cubeMask3, 25)
cubeMask3 = cv2.erode(cubeMask3, kernel)
cubeMask3 = cv2.dilate(cubeMask3, kernel)
cubeCnts = cv2.findContours(cubeMask3.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cubeCntsAreas = []
for i in coneCnts[0]:
M = cv2.moments(i)
cX = int((M["m10"] / M["m00"]))
cY = int((M["m01"] / M["m00"]))
initImg2 = cv2.putText(initImg2, "Cone", (math.floor(cX) - 20, math.floor(cY) + 20), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 6, 2)
initImg2 = cv2.putText(initImg2, str(str(math.floor(cX)) + ", " + str(math.floor(cY))), (math.floor(cX) - 20, math.floor(cY) + 60), cv2.FONT_HERSHEY_SIMPLEX, 2 / 3, (0, 0, 255), 3, 2)
initImg2 = cv2.drawContours(initImg2, i, -1, (0, 255, 0), 3)
for i in cubeCnts[0]:
M = cv2.moments(i)
cX = int((M["m10"] / M["m00"]))
cY = int((M["m01"] / M["m00"]))
initImg2 = cv2.putText(initImg2, "Cube", (math.floor(cX) - 20, math.floor(cY) + 20), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 6, 2)
initImg2 = cv2.putText(initImg2, str(str(math.floor(cX)) + ", " + str(math.floor(cY))), (math.floor(cX) - 20, math.floor(cY) + 60), cv2.FONT_HERSHEY_SIMPLEX, 2 / 3, (0, 0, 255), 3, 2)
initImg2 = cv2.drawContours(initImg2, i, -1, (0, 255, 0), 3)
for i in range(len(apriltags)):
# Get the center from Apriltags
centerXY = ast.literal_eval((re.sub(" +", " ", ((str(apriltags[i].center).replace("[", "")).replace("]", "")).strip())).replace(" ", ", "))
centerXY = list(centerXY)
initImg2 = cv2.putText(initImg2, str(apriltags[i].tag_id), (int(centerXY[0]) - 20, int(centerXY[1]) + 20), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 6, 2)
initImg2 = cv2.putText(initImg2, str(int(centerXY[0])) + ", " + str(int(centerXY[1])), (int(centerXY[0]) - 20, int(centerXY[1]) + 60), cv2.FONT_HERSHEY_SIMPLEX, 2 / 3, (0, 0, 255), 3, 2)
coneMask3 = cv2.bitwise_and(initImg1, initImg1, mask=coneMask3)
cubeMask3 = cv2.bitwise_and(initImg1, initImg1, mask=cubeMask3)
maskedImgs = np.concatenate((cubeMask3, coneMask3), axis = 1)
unmaskedImgs = np.concatenate((initImg2, img), axis = 1)
finalImg = np.concatenate((unmaskedImgs, maskedImgs), axis = 0)
elapsedTime = time.time() - startTime
# FPS counter
finalImg = cv2.putText(finalImg, str(round(1/elapsedTime)) + " FPS", (int(4 * cameraScaler), int(45 * cameraScaler)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), int(5 * cameraScaler), 2)
cv2.imshow("Image", finalImg)
cv2.waitKey(5)