使用OpenCV,Python,Raspberry Pi 3的Ball Tracker

伊姆兰·伊布拉希米(Imran Ibrahimi)

我试图在Raspberry Pi上运行此脚本,但是我一直遇到属性错误。对于可能出现问题的任何帮助或指示,将不胜感激。

这是错误:

Traceback (most recent call last):
  File "/home/pi/ball-tracking/ball_tracking.py", line 48, 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'

这是我的代码:

# python ball_tracking.py --video ball_tracking_example.mp4
# python ball_tracking.py

# import the necessary packages
from collections import deque
import numpy as np
import argparse
import imutils
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
    help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=64,
    help="max buffer size")
args = vars(ap.parse_args())

# define the lower and upper boundaries of the "green"
# ball in the HSV color space, then initialize the
# list of tracked points
greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)
pts = deque(maxlen=args["buffer"])

# if a video path was not supplied, grab the reference
# to the webcam
if not args.get("video", False):
    camera = cv2.VideoCapture(0)

# otherwise, grab a reference to the video file
else:
    camera = cv2.VideoCapture(args["video"])

# keep looping
while True:
    # grab the current frame
    (grabbed, frame) = camera.read()

    # if we are viewing a video and we did not grab a frame,
    # then we have reached the end of the video
    if args.get("video") and not grabbed:
        break

    # resize the frame, blur it, and convert it to the HSV
    # color space
    frame = imutils.resize(frame, width=600)
    # blurred = cv2.GaussianBlur(frame, (11, 11), 0)
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # construct a mask for the color "green", then perform
    # a series of dilations and erosions to remove any small
    # blobs left in the mask
    mask = cv2.inRange(hsv, greenLower, greenUpper)
    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 ball
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)[-2]
    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)

    # update the points queue
    pts.appendleft(center)

    # loop over the set of tracked points
    for i in xrange(1, len(pts)):
        # if either of the tracked points are None, ignore
        # them
        if pts[i - 1] is None or pts[i] is None:
            continue

        # otherwise, compute the thickness of the line and
        # draw the 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 the frame to our screen
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF

    # if the 'q' key is pressed, stop the loop
    if key == ord("q"):
        break

# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()
马修斯·波特拉(Matheus Portela)

好像frameNone在此行中返回的,好像您的相机无法读取图像:

(grabbed, frame) = camera.read()

然后,在调整None对象大小时,程序会按照错误消息中的描述进行工作AttributeError: 'NoneType' object has no attribute 'shape'

frame = imutils.resize(frame, width=600)

本线程中所述,某些相机驱动程序可能会False, None在第一帧中返回可能的解决方法是验证是否grabbedFalse并忽略此帧。

while True:
    grabbed, frame = camera.read()

    if not grabbed:
        continue

    # the rest of the program

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Raspberry Pi 3,OpenCV和Python的运动跟踪器

来自分类Dev

使用OpenCV,Python和Raspberry Pi 3的球追踪器(带摄像头模块)

来自分类Dev

在Raspberry PI上使用Python和OpenCV进行图像处理

来自分类Dev

使用make install在Raspbian Raspberry Pi 3+中安装OpenCV 4时编译错误

来自分类Dev

Raspberry Pi 3上的OpenCV多个USB摄像头

来自分类Dev

Raspberry Pi Omxplayer OpenCV

来自分类Dev

Raspberry Pi 3 BLE扫描

来自分类Dev

Raspberry Pi 3 BLE扫描

来自分类Dev

使用Python 3在(Raspberry Pi的)X服务器上显示全屏jpeg

来自分类Dev

使用Python 3在(Raspberry Pi的)X服务器上显示全屏jpeg

来自分类Dev

在Raspberry Pi上使用Python 3,如何调用MPlayer并传递URL

来自分类Dev

程序与Raspberry Pi 3兼容但与Pi Zero不兼容吗?

来自分类Dev

Python,Windows 和 Raspberry pi 3 之间的套接字

来自分类Dev

使用 Raspberry Pi 代替 Atmel SAMA5D3

来自分类Dev

从 UWP 应用(Raspberry PI 3)使用 WCF 服务时出错

来自分类Dev

使用Python键入Speed Tracker

来自分类Dev

使用Raspberry Pi和Android IP摄像机以及Python和OpenCV进行对象检测

来自分类Dev

Raspberry Pi 3的外部HDD 3.0

来自分类Dev

Raspberry Pi 3 *反向*远程桌面

来自分类Dev

新的 Raspberry PI 3 即将开始

来自分类Dev

Raspberry PI 3、SerialPort 和奇怪的响应

来自分类Dev

Ubuntu Core 旋转屏幕 Raspberry Pi 3

来自分类Dev

使用Qt和opencv交叉编译到Raspberry Pi

来自分类Dev

如何在 Raspberry PI3 上实时运行 openCV 算法

来自分类Dev

Python:OSError:[Errno -9985]在Raspberry PI 3B +上使用Snowboy和SpeechRecognition时设备不可用

来自分类Dev

如何使用c ++从我的Tracker类创建Tracker对象?

来自分类Dev

Raspberry pi3:C ++串行通信无法正常工作(Raspberry pi正在工作!)

来自分类Dev

Python Raspberry Pi GPIO错误

来自分类Dev

Raspberry Pi Python ValueError吗?

Related 相关文章

  1. 1

    使用Raspberry Pi 3,OpenCV和Python的运动跟踪器

  2. 2

    使用OpenCV,Python和Raspberry Pi 3的球追踪器(带摄像头模块)

  3. 3

    在Raspberry PI上使用Python和OpenCV进行图像处理

  4. 4

    使用make install在Raspbian Raspberry Pi 3+中安装OpenCV 4时编译错误

  5. 5

    Raspberry Pi 3上的OpenCV多个USB摄像头

  6. 6

    Raspberry Pi Omxplayer OpenCV

  7. 7

    Raspberry Pi 3 BLE扫描

  8. 8

    Raspberry Pi 3 BLE扫描

  9. 9

    使用Python 3在(Raspberry Pi的)X服务器上显示全屏jpeg

  10. 10

    使用Python 3在(Raspberry Pi的)X服务器上显示全屏jpeg

  11. 11

    在Raspberry Pi上使用Python 3,如何调用MPlayer并传递URL

  12. 12

    程序与Raspberry Pi 3兼容但与Pi Zero不兼容吗?

  13. 13

    Python,Windows 和 Raspberry pi 3 之间的套接字

  14. 14

    使用 Raspberry Pi 代替 Atmel SAMA5D3

  15. 15

    从 UWP 应用(Raspberry PI 3)使用 WCF 服务时出错

  16. 16

    使用Python键入Speed Tracker

  17. 17

    使用Raspberry Pi和Android IP摄像机以及Python和OpenCV进行对象检测

  18. 18

    Raspberry Pi 3的外部HDD 3.0

  19. 19

    Raspberry Pi 3 *反向*远程桌面

  20. 20

    新的 Raspberry PI 3 即将开始

  21. 21

    Raspberry PI 3、SerialPort 和奇怪的响应

  22. 22

    Ubuntu Core 旋转屏幕 Raspberry Pi 3

  23. 23

    使用Qt和opencv交叉编译到Raspberry Pi

  24. 24

    如何在 Raspberry PI3 上实时运行 openCV 算法

  25. 25

    Python:OSError:[Errno -9985]在Raspberry PI 3B +上使用Snowboy和SpeechRecognition时设备不可用

  26. 26

    如何使用c ++从我的Tracker类创建Tracker对象?

  27. 27

    Raspberry pi3:C ++串行通信无法正常工作(Raspberry pi正在工作!)

  28. 28

    Python Raspberry Pi GPIO错误

  29. 29

    Raspberry Pi Python ValueError吗?

热门标签

归档