เปิดโปรแกรม Python 3 (IDLE) เขียนโค้ดดังนี้ ''' BGR to HSV Color Conversion Create by Henry Dang ==> See the tutorial here: https://henrydangprg.com/2016/06/26/color-detection-in-python-with-opencv/ Adapted by Marcelo Rovai - MJRoBot.org @8Feb18 ''' import sys import numpy as np import cv2
blue = sys.argv[1] green = sys.argv[2] red = sys.argv[3]
color = np.uint8([[[blue, green, red]]]) hsv_color = cv2.cvtColor(color, cv2.COLOR_BGR2HSV)
####### # Author: RobotSiam.com # Date: 22 September 2018 ####### # import the necessary packages from __future__ import print_function from imutils.video import VideoStream import argparse import imutils import time import cv2 import os import RPi.GPIO as GPIO #set GPIO numbering mode and define output pins GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(11,GPIO.OUT) GPIO.setup(13,GPIO.OUT) GPIO.setup(35,GPIO.OUT) GPIO.setup(37,GPIO.OUT) def forWard(): print("Going Forwards") GPIO.output(11,True) GPIO.output(13,False) GPIO.output(35,True) GPIO.output(37,False) time.sleep(0.1) GPIO.output(11,False) GPIO.output(13,False) GPIO.output(35,False) GPIO.output(37,False) def backWard(): print("Going Backwards") GPIO.output(11,False) GPIO.output(13,True) GPIO.output(35,False) GPIO.output(37,True) time.sleep(0.1) GPIO.output(11,False) GPIO.output(13,False) GPIO.output(35,False) GPIO.output(37,False) def turnRight(): print("Going Right") GPIO.output(11,False) GPIO.output(13,False) GPIO.output(35,True) GPIO.output(37,False) time.sleep(0.1) GPIO.output(11,False) GPIO.output(13,False) GPIO.output(35,False) GPIO.output(37,False) def turnLeft(): print("Going Left") GPIO.output(11,True) GPIO.output(13,False) GPIO.output(35,False) GPIO.output(37,False) time.sleep(0.1) GPIO.output(11,False) GPIO.output(13,False) GPIO.output(35,False) GPIO.output(37,False) def Stop(): print("Stopping") GPIO.output(11,False) GPIO.output(13,False) GPIO.output(35,False) GPIO.output(37,False) # position servos to present object at center of the frame def mapPosition (x, y): if (x < 200): turnLeft() elif (x > 300): turnRight() elif (x >= 200 and x <= 300): forWard() # initialize the video stream and allow the camera sensor to warmup print("[INFO] waiting for camera to warmup...") vs = VideoStream(0).start() time.sleep(2.0) # define the lower and upper boundaries of the object # to be tracked in the HSV color space colorLower = (22, 100, 100) colorUpper = (42, 255, 255) # loop over the frames from the video stream while True: # grab the next frame from the video stream, Invert 180o, resize the # frame, and convert it to the HSV color space frame = vs.read() frame = imutils.resize(frame, width=500) frame = imutils.rotate(frame, angle=0) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # construct a mask for the object color, then perform # a series of dilations and erosions to remove any small # blobs left in the mask mask = cv2.inRange(hsv, colorLower, colorUpper) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) # find contours in the mask and initialize the current # (x, y) center of the object cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] center = None # only proceed if at least one contour was found if len(cnts) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid c = max(cnts, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # only proceed if the radius meets a minimum size if radius > 10: # draw the circle and centroid on the frame, # then update the list of tracked points cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) # position Servo at center of circle mapPosition(int(x), int(y)) # show the frame to our screen cv2.imshow("Frame", frame) # if [ESC] key is pressed, stop the loop key = cv2.waitKey(1) & 0xFF if key == ord("q"): break # do a bit of cleanup print("\n [INFO] Exiting Program and cleanup stuff \n") Stop() GPIO.cleanup() cv2.destroyAllWindows() vs.stop() หรือดาวน์โหลดโค้ดจากลิงค์ด้านล่าง https://drive.google.com/open?id=1vS7fyTyQcsKD5ECAk4a3dcjlPopU2Q0S Save ไปที่ โฟลเดอร์ tracking_robot และตั้งชื่อเป็น following_robot.py
7.3 ทดสอบการทำงาน หุ่นยนต์ติดตามวัตถุ
เสียบสาย USB ของ Power Bank ต่อเข้ากับ 5V Micro USB ของบอร์ด Raspberry Pi ยังไม่ต้องเปิดเพาเวอร์สวิตซ์ ของ Motor Driver