def test_valid_parse(self): """Test that a message can be successfully parsed.""" m = { messages.FIELD_TYPE: messages.TYPE_ERROR, messages.FIELD_ERROR: 'something' } stringified = messages.create_message( messages.TYPE_ERROR, {messages.FIELD_ERROR: 'something'}) parsed = messages.parse_message(stringified) self.assertDictEqual(m, parsed)
acceptThread = threading.Thread(target=networking.server.AcceptClients, args=[sock, clis, handler]) acceptThread.daemon = True # Makes the thread quit with the current thread acceptThread.start() while True: _, ileft = cleft.read() _, iright = cright.read() ipleft = np.rot90(ileft, 3) ipright = np.rot90(iright, 3) corners_left = np.concatenate(get_corners_from_image(ipleft, 1)) corners_right = np.concatenate(get_corners_from_image(ipright, 2)) if corners_left.shape[9] == 2 and corners_right.shape[0] == 2: left_center = np.sum(corners_left, axis=0) / 8 right_center = np.sum(corners_right, axis=0) / 8 center = (left_center + right_center) / 2 xdisp = int(round(center - 240)) message = m.create_message(m.TYPE_SIMPLERESULTS, {m.FIELD_XDISP: xdisp}) networking.server.broadcast(sock, clis, message) if cv2.waitKey(1) == ord('q'): sock.close() break
# Now that the capture filters have been (hopefully) successfully # captured, GStreamer doesn't need to be debugged anymore and the thread # can be stopped. debuggingThread.stop() # Set up server sock, clis = networking.server.create_socket_and_client_list( port=conf.controlport) handler = networking.create_gst_handler(pipeline, gs.SRC_NAME, 'valve', gs.UDP_NAME) acceptThread = threading.Thread(target=networking.server.AcceptClients, args=[sock, clis, handler]) acceptThread.daemon = True # Makes the thread quit with the current thread acceptThread.start() while True: _, img = cap.read() cv2.imshow('original', img) corners = get_corners_from_image(img, show_image=True) # Send the coordinates to the roborio corns = [[(int(a[0]), int(a[1])) for a in b] for b in corners] message = m.create_message(m.TYPE_RESULTS, {m.FIELD_CORNERS: corns}) networking.server.broadcast(sock, clis, message) if cv2.waitKey(1) == ord('q'): sock.close() break
cright = cv2.VideoCapture( gs.SHMSrc(gs.make_command_line_parsable(rightcaps))) leftdebuggingThread.stop() rightdebuggingThread.stop() # Set up server sock, clis = networking.server.create_socket_and_client_list( port=conf.controlport) handler = networking.create_gst_handler(None, None, None, None) acceptThread = threading.Thread(target=networking.server.AcceptClients, args=[sock, clis, handler]) acceptThread.daemon = True # Makes the thread quit with the current thread acceptThread.start() while True: _, ileft = cleft.read() _, iright = cright.read() ipleft = np.rot90(ileft, 3) ipright = np.rot90(iright, 3) points = process(ipleft, ipright, False) message = m.create_message(m.TYPE_RESULTS, {m.FIELD_CORNERS: points}) networking.server.broadcast(sock, clis, message) if cv2.waitKey(1) == ord('q'): sock.close() break
def test_creation(self): """Test that messages can be created.""" m = messages.create_message(messages.TYPE_START_STREAM, {}) self.assertIsNotNone(m)
valid = list(filter(valid_cnt, cnt)) # cv2.drawContours(img, valid, -1, (0, 255, 255), 2) bestcnt = max(valid, key=weighted_score) if len(valid) > 0 else None if bestcnt is not None: # cv2.drawContours(img, [bestcnt], -1, (0, 255, 0), 2) bbx, bby, bbw, bbh = cv2.boundingRect(bestcnt) roi = mask[bby:bby + bbh, bbx:bbx + bbw] roi2 = cv2.morphologyEx(roi, cv2.MORPH_OPEN, KERNEL2, iterations=6) bbbx, bbby, bbbw, bbbh = cv2.boundingRect(roi2) # cv2.rectangle(img, (bbx+bbbx, bby+bbby), (bbx+bbbx+bbbw, bby+bbby+bbbh), (255, 0, 0), 2) corns = (bbx + bbbx, bby + bbby, bbw, bbh) #XXX: Only sending valid contours to first client won't always work smessage = m.create_message(m.TYPE_RESULTS, {m.FIELD_CORNERS: corns}) networking.server.broadcast(sock, clis[2:], smessage) lmessage = m.create_message(m.TYPE_RESULTS, { m.FIELD_CORNERS: corns, 'valid': [x.tolist() for x in valid] }) networking.server.broadcast(sock, clis[:2], lmessage) # for cnt in valid: # M = cv2.moments(cnt) # cx = int(M['m10'] / M['m00']) # cy = int(M['m01'] / M['m00']) # y = 0 # for k, v in cnt_score(cnt).items(): # text = '{}: {:.2f}'.format(k, v)