标签:树莓 循迹 cx import cv2 跑道 camera output GPIO
# import the necessary packages from picamera.array import PiRGBArray from picamera import PiCamera import RPi.GPIO as GPIO import time import cv2 # initialize the camera and grab a reference to the raw camera capture GPIO.setmode(GPIO.BOARD) INT1 = 11 INT2 = 12 INT3 = 13 INT4 = 15 ENA = 16 ENB = 18 GPIO.setup(INT1,GPIO.OUT) GPIO.setup(INT2,GPIO.OUT) GPIO.setup(INT3,GPIO.OUT) GPIO.setup(INT4,GPIO.OUT) GPIO.setup(ENA,GPIO.OUT) GPIO.setup(ENB,GPIO.OUT) pwma = GPIO.PWM(16,80) pwmb = GPIO.PWM(18,80) pwma.start(90) pwmb.start(90) GPIO.output(INT1,GPIO.HIGH) GPIO.output(INT2,GPIO.LOW) GPIO.output(INT3,GPIO.HIGH) GPIO.output(INT4,GPIO.LOW) def right(): pwma.ChangeDutyCycle(90) pwmb.ChangeDutyCycle(20) def left(): pwma.ChangeDutyCycle(20) pwmb.ChangeDutyCycle(90) def stop(): GPIO.output(INT1,GPIO.LOW) GPIO.output(INT2,GPIO.LOW) GPIO.output(ENA,GPIO.HIGH) time.sleep(1) GPIO.output(INT3,GPIO.LOW) GPIO.output(INT4,GPIO.LOW) GPIO.output(ENB,GPIO.HIGH) GPIO.cleanup() camera = PiCamera() camera.resolution = (640, 480) camera.framerate = 32 camera.hflip = True camera.vflip = True rawCapture = PiRGBArray(camera, size=(640, 480)) # allow the camera to warmup time.sleep(0.1) # capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # grab the raw NumPy array representing the image, then initialize the timestamp # and occupied/unoccupied text crop_img = frame.array gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (15, 15), 0) ret, thresh1 = cv2.threshold(blur, 100, 255, cv2.THRESH_BINARY) mask = cv2.erode(thresh1, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) cnts = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) contours = cnts[0] if len(contours) > 0: c = max(contours, key=cv2.contourArea) M = cv2.moments(c) # 求取中心点坐标(cx,cy) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) cv2.line(crop_img, (cx, 0), (cx, 720), (255, 0, 0), 1) cv2.line(crop_img, (0, cy), (1280, cy), (255, 0, 0), 1) # 绘制轮廓图 cv2.drawContours(crop_img, contours, -1, (0, 255, 0), 1) if cx>360: print("right") right() elif cx<360: print("left") left() print(cx) # show the frame cv2.imshow("Frame", crop_img) key = cv2.waitKey(1) & 0xFF # clear the stream in preparation for the next frame rawCapture.truncate(0) # if the `q` key was pressed, break from the loop if key == ord("q"): stop() break
标签:树莓,循迹,cx,import,cv2,跑道,camera,output,GPIO 来源: https://www.cnblogs.com/bigtwetwet/p/15743659.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。