Advertisement
Guest User

Untitled

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