Mihao

Raspberry Pi Ball tracking

Jan 8th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. # USAGE
  2. # python ball_tracking.py --video ball_tracking_example.mp4
  3. # python ball_tracking.py
  4.  
  5. # import the necessary packages
  6. from collections import deque
  7. import numpy as np
  8. import argparse
  9. import imutils
  10. import cv2
  11.  
  12. # construct the argument parse and parse the arguments
  13. ap = argparse.ArgumentParser()
  14. ap.add_argument("-v", "--video",
  15. help="path to the (optional) video file")
  16. ap.add_argument("-b", "--buffer", type=int, default=64,
  17. help="max buffer size")
  18. args = vars(ap.parse_args())
  19.  
  20. # define the lower and upper boundaries of the "green"
  21. # ball in the HSV color space, then initialize the
  22. # list of tracked points
  23. greenLower = (29, 86, 6)
  24. greenUpper = (64, 255, 255)
  25. pts = deque(maxlen=args["buffer"])
  26.  
  27. # if a video path was not supplied, grab the reference
  28. # to the webcam
  29. if not args.get("video", False):
  30. camera = cv2.VideoCapture(0)
  31.  
  32. # otherwise, grab a reference to the video file
  33. else:
  34. camera = cv2.VideoCapture(args["video"])
  35.  
  36. # keep looping
  37. while True:
  38. # grab the current frame
  39. (grabbed, frame) = camera.read()
  40.  
  41. # if we are viewing a video and we did not grab a frame,
  42. # then we have reached the end of the video
  43. if args.get("video") and not grabbed:
  44. break
  45.  
  46. # resize the frame, blur it, and convert it to the HSV
  47. # color space
  48. frame = imutils.resize(frame, width=600)
  49. # blurred = cv2.GaussianBlur(frame, (11, 11), 0)
  50. hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  51.  
  52. # construct a mask for the color "green", then perform
  53. # a series of dilations and erosions to remove any small
  54. # blobs left in the mask
  55. mask = cv2.inRange(hsv, greenLower, greenUpper)
  56. mask = cv2.erode(mask, None, iterations=2)
  57. mask = cv2.dilate(mask, None, iterations=2)
  58.  
  59. # find contours in the mask and initialize the current
  60. # (x, y) center of the ball
  61. cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
  62. cv2.CHAIN_APPROX_SIMPLE)[-2]
  63. center = None
  64.  
  65. # only proceed if at least one contour was found
  66. if len(cnts) > 0:
  67. # find the largest contour in the mask, then use
  68. # it to compute the minimum enclosing circle and
  69. # centroid
  70. c = max(cnts, key=cv2.contourArea)
  71. ((x, y), radius) = cv2.minEnclosingCircle(c)
  72. M = cv2.moments(c)
  73. center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
  74.  
  75. # only proceed if the radius meets a minimum size
  76. if radius > 10:
  77. # draw the circle and centroid on the frame,
  78. # then update the list of tracked points
  79. cv2.circle(frame, (int(x), int(y)), int(radius),
  80. (0, 255, 255), 2)
  81. cv2.circle(frame, center, 5, (0, 0, 255), -1)
  82.  
  83. # update the points queue
  84. pts.appendleft(center)
  85.  
  86. # loop over the set of tracked points
  87. for i in xrange(1, len(pts)):
  88. # if either of the tracked points are None, ignore
  89. # them
  90. if pts[i - 1] is None or pts[i] is None:
  91. continue
  92.  
  93. # otherwise, compute the thickness of the line and
  94. # draw the connecting lines
  95. thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5)
  96. cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness)
  97.  
  98. # show the frame to our screen
  99. cv2.imshow("Frame", frame)
  100. key = cv2.waitKey(1) & 0xFF
  101.  
  102. # if the 'q' key is pressed, stop the loop
  103. if key == ord("q"):
  104. break
  105.  
  106. # cleanup the camera and close any open windows
  107. camera.release()
  108. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment