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
| import cv2 import numpy as np import os
from Communications import SelfSerial from SplitInt import get_high_low_data from loguru import logger
os.system('echo rock | sudo -S chmod 777 /dev/ttyUSB0')
red_lower = np.array([0, 43, 46]) red_upper = np.array([10, 255, 255])
blue_lower = np.array([100, 43, 46]) blue_upper = np.array([124, 255, 255])
def find_largest_color_blob(image, color_lower, color_upper,min_area = 500):
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) color_mask = cv2.inRange(hsv_image, color_lower, color_upper) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7)) color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_OPEN, kernel) contours, _ = cv2.findContours(color_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) largest_contour = None largest_area = 0 for contour in contours: area = cv2.contourArea(contour) if area > largest_area and area > min_area: largest_area = area largest_contour = contour if largest_contour is not None: M = cv2.moments(largest_contour) if M["m00"] > 0: center_x = int(M["m10"] / M["m00"]) center_y = int(M["m01"] / M["m00"]) return center_x, center_y
return None
cap = cv2.VideoCapture(1)
cv2.namedWindow('camera', cv2.WINDOW_AUTOSIZE)
self_serial = SelfSerial('/dev/ttyUSB0')
mode = 0
logger.info("System Starting")
while True: ret, frame = cap.read()
if ret: mode = self_serial.uart_read_mode(mode)
if mode == 99: pass if mode == 1: if frame is not None: try: red_center = find_largest_color_blob(frame, red_lower, red_upper) blue_center = find_largest_color_blob(frame, blue_lower, blue_upper) if red_center is not None: cv2.circle(frame, red_center, 5, (0, 0, 255), -1) x = int(red_center[0]) y = int(red_center[1]) print(x, y) msg = get_high_low_data(x) + get_high_low_data(y) else: if blue_center is not None: cv2.circle(frame, blue_center, 5, (255, 0, 0), -1) x = int(red_center[0]) y = int(red_center[1]) print(x, y) msg = get_high_low_data(x) + get_high_low_data(y) except: x = 0 y = 0 print(x, y) msg = get_high_low_data(x) + get_high_low_data(y) cv2.imshow('camera', frame) cv2.waitKey(1) self_serial.uart_send_msg(0x01, msg) if mode == 2 : if frame is not None: print("请输入三个x坐标和y坐标") x1 = int(input()) y1 = int(input()) x2 = int(input()) y2 = int(input())
print(x1,y1) print(x2,y2) msg = get_high_low_data(x1) + get_high_low_data(y1) + get_high_low_data(x2) + get_high_low_data(y2) self_serial.uart_send_msg(0x02, msg) mode = 99 cv2.imshow('camera', frame) cv2.waitKey(1)
|