SHARE
TWEET

Untitled

a guest Feb 6th, 2018 136 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # import the necessary packages
  2. from __future__ import print_function
  3. from imutils.video import VideoStream
  4. import argparse
  5. import imutils
  6. import time
  7. import cv2
  8. import RPi.GPIO as GPIO
  9.  
  10. # initialize GPIO
  11. redLed = 21
  12. GPIO.setmode(GPIO.BCM)
  13. GPIO.setwarnings(False)
  14. GPIO.setup(redLed, GPIO.OUT)
  15.  
  16. # construct the argument parse and parse the arguments
  17. ap = argparse.ArgumentParser()
  18. ap.add_argument("-p", "--picamera", type=int, default=-1,
  19.     help="whether or not the Raspberry Pi camera should be used")
  20. args = vars(ap.parse_args())
  21.  
  22. # initialize the video stream and allow the camera sensor to warmup
  23. print("[INFO] waiting for camera to warmup...")
  24. vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
  25. time.sleep(2.0)
  26.  
  27. # define the lower and upper boundaries of the "green"
  28. # ball in the HSV color space
  29. greenLower = (29, 86, 6)
  30. greenUpper = (64, 255, 255)
  31.  
  32. # Start with LED off
  33. GPIO.output(redLed, GPIO.LOW)
  34. ledOn = False
  35.  
  36. # loop over the frames from the video stream
  37. while True:
  38.     # grab the next frame from the video stream, Invert 180o, resize the
  39.     # frame, and convert it to the HSV color space
  40.     frame = vs.read()
  41.     frame = imutils.resize(frame, width=500)
  42.     frame = imutils.rotate(frame, angle=180)
  43.     hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  44.  
  45.     # construct a mask for the color "green", then perform
  46.     # a series of dilations and erosions to remove any small
  47.     # blobs left in the mask
  48.     mask = cv2.inRange(hsv, greenLower, greenUpper)
  49.     mask = cv2.erode(mask, None, iterations=2)
  50.     mask = cv2.dilate(mask, None, iterations=2)
  51.  
  52.     # find contours in the mask and initialize the current
  53.     # (x, y) center of the ball
  54.     cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
  55.         cv2.CHAIN_APPROX_SIMPLE)
  56.     cnts = cnts[0] if imutils.is_cv2() else cnts[1]
  57.     center = None
  58.  
  59.     # only proceed if at least one contour was found
  60.     if len(cnts) > 0:
  61.         # find the largest contour in the mask, then use
  62.         # it to compute the minimum enclosing circle and
  63.         # centroid
  64.         c = max(cnts, key=cv2.contourArea)
  65.         ((x, y), radius) = cv2.minEnclosingCircle(c)
  66.         M = cv2.moments(c)
  67.         center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
  68.  
  69.         # only proceed if the radius meets a minimum size
  70.         if radius > 10:
  71.             # draw the circle and centroid on the frame,
  72.             # then update the list of tracked points
  73.             cv2.circle(frame, (int(x), int(y)), int(radius),
  74.                 (0, 255, 255), 2)
  75.             cv2.circle(frame, center, 5, (0, 0, 255), -1)
  76.  
  77.             # if the led is not already on, turn the LED on
  78.             if not ledOn:
  79.                 GPIO.output(redLed, GPIO.HIGH)
  80.                 ledOn = True
  81.  
  82.     # if the ball is not detected, turn the LED off
  83.     elif ledOn:
  84.         GPIO.output(redLed, GPIO.LOW)
  85.         ledOn = False
  86.  
  87.     # show the frame to our screen
  88.     cv2.imshow("Frame", frame)
  89.     key = cv2.waitKey(1) & 0xFF
  90.  
  91.     # if the 'q' key is pressed, stop the loop
  92.     if key == ord("q"):
  93.         break
  94.  
  95. # do a bit of cleanup
  96. GPIO.cleanup()
  97. cv2.destroyAllWindows()
  98. vs.stop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top