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

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

我无法成功运行此脚本。我尝试通过命令行运行它,同时传递参数和不传递参数。我也试图在virtualenv中运行无济于事。我怀疑我缺少PiCamera的驱动程序

# 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`enter code here`

# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()

这是我的错误:

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"`enter code here`, line 45, in resize
    (h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'
伊姆兰·伊布拉希米(Imran Ibrahimi)

“ nonetype”对象是指未传递给resize方法的帧。

这可以通过在运行代码之前加载预安装的v4l2驱动程序来解决:

sudo modprobe bcm2835-v4l2

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

在Raspberry Pi和Python上控制USB网络摄像头照片捕获时间

来自分类Dev

在Raspberry Pi和Python上控制USB网络摄像头照片捕获时间

来自分类Dev

Raspberry Pi网络摄像头服务器是否通过IP流式传输?

来自分类Dev

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

来自分类Dev

Raspberry Pi 1B,mjpg流媒体和USB网络摄像头

来自分类Dev

Raspberry Pi 1B,mjpg流媒体和USB网络摄像头

来自分类Dev

在Raspberry PI上使用Python的433MHz发送器和接收器

来自分类Dev

在编码之前从Raspberry Pi上的摄像头捕获视频并在OpenGL中过滤

来自分类Dev

网络摄像头捕获API-如何使其在Raspberry Pi上运行?

来自分类Dev

无法在Raspberry Pi(Arch Linux)上获取网络摄像头图像

来自分类Dev

在Raspberry Pi上通过Node.js启动USB网络摄像头

来自分类Dev

我正在尝试在 Raspberry Pi 中连接两个网络摄像头

来自分类Dev

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

来自分类Dev

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

来自分类Dev

Raspberry PI 3、SerialPort 和奇怪的响应

来自分类Dev

Raspberry Pi Omxplayer OpenCV

来自分类Dev

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

来自分类Dev

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

来自分类Dev

具有GUI的Pi摄像机预览-Raspberry Pi

来自分类Dev

用于ARM处理器(Raspberry Pi)和python3的psutil的替代

来自分类Dev

Raspberry Pi 3 BLE扫描

来自分类Dev

Raspberry Pi 3 BLE扫描

来自分类Dev

Raspberry Pi和GitLab

来自分类Dev

NodeJS和Raspberry Pi

来自分类Dev

QEMU和Raspberry Pi

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

    在Raspberry Pi和Python上控制USB网络摄像头照片捕获时间

  6. 6

    在Raspberry Pi和Python上控制USB网络摄像头照片捕获时间

  7. 7

    Raspberry Pi网络摄像头服务器是否通过IP流式传输?

  8. 8

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

  9. 9

    Raspberry Pi 1B,mjpg流媒体和USB网络摄像头

  10. 10

    Raspberry Pi 1B,mjpg流媒体和USB网络摄像头

  11. 11

    在Raspberry PI上使用Python的433MHz发送器和接收器

  12. 12

    在编码之前从Raspberry Pi上的摄像头捕获视频并在OpenGL中过滤

  13. 13

    网络摄像头捕获API-如何使其在Raspberry Pi上运行?

  14. 14

    无法在Raspberry Pi(Arch Linux)上获取网络摄像头图像

  15. 15

    在Raspberry Pi上通过Node.js启动USB网络摄像头

  16. 16

    我正在尝试在 Raspberry Pi 中连接两个网络摄像头

  17. 17

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

  18. 18

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

  19. 19

    Raspberry PI 3、SerialPort 和奇怪的响应

  20. 20

    Raspberry Pi Omxplayer OpenCV

  21. 21

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

  22. 22

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

  23. 23

    具有GUI的Pi摄像机预览-Raspberry Pi

  24. 24

    用于ARM处理器(Raspberry Pi)和python3的psutil的替代

  25. 25

    Raspberry Pi 3 BLE扫描

  26. 26

    Raspberry Pi 3 BLE扫描

  27. 27

    Raspberry Pi和GitLab

  28. 28

    NodeJS和Raspberry Pi

  29. 29

    QEMU和Raspberry Pi

热门标签

归档