AttributeError:“非类型”对象没有属性“副本”

2024-05-23 16:54:22 发布

您现在位置:Python中文网/ 问答频道 /正文

完全错误是:


    OpenCV: out device of bound (0-0): 1
    OpenCV: camera failed to properly initialize!
    Traceback (most recent call last):
      File "/Users/syedrishad/PycharmProjects/OpenCVPython/venv/project1.py", line 60, in <module>
        imgResult = img.copy()
    AttributeError: 'NoneType' object has no attribute 'copy'
     ```

完整代码为:

```import cv2
import numpy as np

frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(1)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, 150)

myColors = [[78, 119, 70, 255, 97, 255],
            [63, 108, 44, 255, 0, 118],
            [0, 179, 69, 255, 100, 255],
            [90, 48, 0, 118, 255, 255]]
myColorValues = [[51, 153, 255],  ## BGR
                 [255, 0, 255],
                 [0, 255, 0],
                 [255, 0, 0]]

myPoints = []  ## [x , y , colorId ]


def findColor(img, myColors, myColorValues):
    imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    count = 0
    newPoints = []
    for color in myColors:
        lower = np.array(color[0:3])
        upper = np.array(color[3:6])
        mask = cv2.inRange(imgHSV, lower, upper)
        x, y = getContours(mask)
        cv2.circle(imgResult, (x, y), 15, myColorValues[count], cv2.FILLED)
        if x != 0 and y != 0:
            newPoints.append([x, y, count])
        count += 1
        # cv2.imshow(str(color[0]),mask)
    return newPoints


def getContours(img):
    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    x, y, w, h = 0, 0, 0, 0
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > 500:
            # cv2.drawContours(imgResult, cnt, -1, (255, 0, 0), 3)
            peri = cv2.arcLength(cnt, True)
            approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
            x, y, w, h = cv2.boundingRect(approx)
    return x + w // 2, y


def drawOnCanvas(myPoints, myColorValues):
    for point in myPoints:
        cv2.circle(imgResult, (point[0], point[1]), 10, myColorValues[point[2]], cv2.FILLED)


while True:
    success, img = cap.read()
    imgResult = img.copy()
    newPoints = findColor(img, myColors, myColorValues)
    if len(newPoints) != 0:
        for newP in newPoints:
            myPoints.append(newP)
    if len(myPoints) != 0:
        drawOnCanvas(myPoints, myColorValues)

    cv2.imshow("Result", imgResult)
    if cv2.waitKey(1) and 0xFF == ord('q'):
        break ```

如何修复此错误?任何帮助都将不胜感激,我已经尝试了一个星期,但仍然没有找到答案。我不明白为什么它不起作用。我已经看过这个错误的其他例子,但没有一个解决方案有效。
再次感谢你的帮助


Tags: inimgforifcountcv2colorpoint
2条回答

我猜你是想用网络摄像头。请将该值更改为0,因为它用于使用网络摄像头。祝你今天愉快

cap = cv2.VideoCapture(0)

导致错误的行:

imgResult = img.copy()

利用前一行中定义的img

success, img = cap.read()

{a1}文档声明:

The methods/functions combine VideoCapture::grab() and VideoCapture::retrieve() in one call. This is the most convenient method for reading video files or capturing data from decode and return the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer.

显然{}是{}由于没有数据读取

改为:

while True:
    success, img = cap.read()
    if img is None:
        break
    imgResult = img.copy()

另外,请检查导致未抓取帧的原因,或者:

  1. 摄像头已断开(检查所有驱动程序,确保已安装并检测到摄像头)
  2. 视频文件中没有更多帧

相关问题 更多 >