-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVision.py
137 lines (116 loc) · 3.23 KB
/
Vision.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
import cv2
from networktables import NetworkTable
videoCapture = None
visionTable = None
#image size is 640x480
def main():
global videoCapture
videoCapture = cv2.VideoCapture()
setupVisionTable()
runVision()
def setupVisionTable():
global visionTable
NetworkTable.setIPAddress("10.46.69.21")
NetworkTable.setClientMode()
NetworkTable.initialize()
visionTable = NetworkTable.getTable("vision")
setRunVision(False)
turnOffLight()
def runVision():
global visionTable
while(True):
#print(visionTable.isConnected())
#print(getRunVision())
if visionTable.isConnected() and getRunVision():
for i in xrange(30):
frame1 = getCameraImage()
#cv2.namedWindow("frame1", cv2.WINDOW_NORMAL)
#cv2.imshow("frame1", frame1)
#cv2.imwrite("frame1.jpg", frame1)
turnOnLight()
cv2.waitKey(100)
for i in xrange(30):
frame2 = getCameraImage()
#cv2.namedWindow("frame2", cv2.WINDOW_NORMAL)
#cv2.imshow("frame2", frame2)
#cv2.imwrite("frame2.jpg", frame2)
cv2.waitKey(100)
turnOffLight()
processedFrame = threshold(getGrayscale(getDifference(frame1, frame2)))
hull = getConvexHull(getMaxContour(getContours(processedFrame)))
putValuesOnVisionTable(hull)
setRunVision(False)
def putValuesOnVisionTable(hull):
x,y,w,h = cv2.boundingRect(hull)
#print("Success")
print(x,y,w,h)
visionTable.putNumber("x", x)
visionTable.putNumber("y", y)
visionTable.putNumber("w", w)
visionTable.putNumber("h", h)
def setRunVision(b):
global visionTable
visionTable.putBoolean("runVision", b)
def getRunVision():
global visionTable
return visionTable.getBoolean("runVision", False)
def openCapture():
global videoCapture
while (not videoCapture.isOpened()):
print("Camera is NOT open")
videoCapture.open(0)
#print("Camera is open")
#videoCapture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1920)
#videoCapture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 1080)
def releaseCapture():
global videoCapture
videoCapture.release()
def turnOnLight():
global visionTable
visionTable.putBoolean("lightOn", True)
while(True):
if (getLightOnDone()):
return
def getLightOnDone():
global visionTable
return visionTable.getBoolean("lightOnDone", False)
def turnOffLight():
global visionTable
visionTable.putBoolean("lightOn", False)
while(True):
if(getLightOnDone() == False):
return
def getCameraImage():
global videoCapture
#videoCapture = cv2.VideoCapture()
openCapture()
retval, frame = videoCapture.read()
#releaseCapture()
return frame
def getDifference(frame1, frame2):
diff = cv2.subtract(frame2, frame1)
return diff
def getGrayscale(frame):
grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
return grayscale
def threshold(frame):
th, newFrame = cv2.threshold(frame, 50, 255, cv2.THRESH_BINARY)
return newFrame
def getContours(frame):
contoursList, hierarchy = cv2.findContours(frame, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
return contoursList
def getMaxContour(contoursList):
maxContour = None
if (len(contoursList)>0):
maxContour = contoursList[0]
maxArea = cv2.contourArea(maxContour)
for contour in contoursList:
area = cv2.contourArea(contour)
if (area>maxArea):
maxContour = contour
maxArea = area
return maxContour
def getConvexHull(contour):
hull = cv2.convexHull(contour)
return hull
main()