def run(self):
bytes=''
while not self.thread_cancelled: ####see lines 18, 80, 88 ....
try:
bytes+=self.stream.raw.read(1024) ##limit max bytes read in 1 itteration? need to read more on this
a = bytes.find('\xff\xd8')##find start of stream of data
b = bytes.find('\xff\xd9')##find our end of data stream
if a!=-1 and b!=-1: ##so as long as we have a stream of data....do the following
jpg = bytes[a:b+2] ##converts to image or a specific variable...
bytes= bytes[b+2:]
img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR) ##decode the data
# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) ##converting color format for easier proccessing/ math
# define range of blue color in HSV
#lower_blue = np.array([self.L_RED, self.L_GREEN, self.L_BLUE], np.uint8)
#upper_blue = np.array([self.U_RED, self.U_GREEN, self.L_BLUE], np.uint8)
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, np.array([53,187,37]), np.array([97,244,153])) ##get colors in the range of these HSV values
# Bitwise-AND mask and original image
res = cv2.bitwise_and(img,img, mask= mask)
blurred = cv2.boxFilter(mask, 0, (7, 7), mask, (-1, -1), False, cv2.BORDER_DEFAULT) ##the next few line create outlines and
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1] ##remove any noise
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #find countors
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cv2.filterSpeckles(mask, 0, 100, 25) ##remove speckles aka random dots and white noise
for c in cnts:
M = cv2.moments(c)
if int(M["m00"]) != 0: ##Checks for division by zero
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
(cX, cY) = (0, 0)
cv2.drawContours(res, [c], -1, (0, 255, 0), 2) ##draw box/highlighting
cv2.circle(res, (cX, cY), 7, (255, 255, 255), 1) ##draw box/highlighting
##Try-Catch for appending cX to table
try:
self.table.putNumber('centerX', cX) ##Adds cX to the networktables
except KeyError:
print("centerX failed.")
cv2.imshow('img',img) ##display original image
cv2.imshow('mask',mask) ##display masked image
cv2.imshow('Final',res) ##show final image
cv2.imshow('cam',img) ##see line 71/comments
if cv2.waitKey(1) ==27: ##now we close if esc key is pressed
exit(0)
except ThreadError:
self.thread_cancelled = True
PythonVideoVision2017.py 文件源码
python
阅读 20
收藏 0
点赞 0
评论 0
评论列表
文章目录