Python, OpenCV, Raspberry Pi-3 - Attribute Error - 'NoneType' object -
i trying track green ball v2 camera module using opencv , python (using virtualenv), keep encountering attributeerror: 'nonetype' object has no attribute 'shape'
any appreciated!
traceback (most recent call last):
file "/home/pi/ball-track/ball_tracking.py", line 52, in <module> frame = imutils.resize(frame, width=600) file "/usr/local/lib/python2.7/dist-packages/imutils/convenience.py", line 45, in resize (h, w) = image.shape[:2] attributeerror: 'nonetype' object has no attribute 'shape'
i have tried add bit of code in amend error not work:
while true: grabbed, frame = camera.read() if not grabbed: continue # rest of program
i have video file trying use within script , contains desired object (to tracked)
here code:
# python ball_tracking.py --video ball_tracking_example.mp4 # python ball_tracking.py # import necessary packages collections import deque import numpy np import argparse import imutils import cv2 # construct argument parse , parse arguments ap = argparse.argumentparser() ap.add_argument("-v", "--video", help="path (optional) video file") ap.add_argument("-b", "--buffer", type=int, default=64, help="max buffer size") args = vars(ap.parse_args()) # define lower , upper boundaries of "green" # ball in hsv color space, initialize # list of tracked points greenlower = (29, 86, 6) greenupper = (64, 255, 255) pts = deque(maxlen=args["buffer"]) # if video path not supplied, grab reference # webcam if not args.get("video", false): camera = cv2.videocapture(0) # otherwise, grab reference video file else: camera = cv2.videocapture(args["video"]) # keep looping while true: # grab current frame (grabbed, frame) = camera.read() # if viewing video , did not grab frame, # have reached end of video if args.get("video") , not grabbed: break # resize frame, blur it, , convert hsv # color space frame = imutils.resize(frame, width=600) # blurred = cv2.gaussianblur(frame, (11, 11), 0) hsv = cv2.cvtcolor(frame, cv2.color_bgr2hsv) # construct mask color "green", perform # series of dilations , erosions remove small # blobs left in mask mask = cv2.inrange(hsv, greenlower, greenupper) mask = cv2.erode(mask, none, iterations=2) mask = cv2.dilate(mask, none, iterations=2) # find contours in mask , initialize current # (x, y) center of ball cnts = cv2.findcontours(mask.copy(), cv2.retr_external, cv2.chain_approx_simple)[-2] center = none # proceed if @ least 1 contour found if len(cnts) > 0: # find largest contour in mask, use # compute minimum enclosing circle , # 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"])) # proceed if radius meets minimum size if radius > 10: # draw circle , centroid on frame, # update 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) # update points queue pts.appendleft(center) # loop on set of tracked points in xrange(1, len(pts)): # if either of tracked points none, ignore # them if pts[i - 1] none or pts[i] none: continue # otherwise, compute thickness of line , # draw connecting lines thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5) cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness) # show frame our screen cv2.imshow("frame", frame) key = cv2.waitkey(1) & 0xff # if 'q' key pressed, stop loop if key == ord("q"): break # cleanup camera , close open windows camera.release() cv2.destroyallwindows()
for experiencing same issue have resolved error typing command:
sudo modprobe bcm2835-v4l2
works charm!
Comments
Post a Comment