Python ';非类型';对象没有属性'__获取项目';

Python ';非类型';对象没有属性'__获取项目';,python,opencv,raspberry-pi3,Python,Opencv,Raspberry Pi3,我注意到我的代码没有正常运行。但我已经为台词声明了 它说: 第28行,在 对于[0]行中的x1、y1、x2、y2: TypeError:“非类型”对象没有属性“getitem” # importthe necessary packages from picamera.array import PiRGBArray from picamera import PiCamera import numpy as np import time import cv2 #initialize the cam

我注意到我的代码没有正常运行。但我已经为台词声明了

它说: 第28行,在 对于[0]行中的x1、y1、x2、y2: TypeError:“非类型”对象没有属性“getitem

# importthe necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import numpy as np
import time
import cv2

#initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (720,240)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(720,240))

#allow the camera to warmup
time.sleep(0.1)

#capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    #grab the raw NumPy array representing the image, then initialize the timestamp
    #and occupied/unoccupied text
    image = frame.array
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    #image = cv2.medianBlur (gray, 0)
    edges = cv2.Canny(gray,100,250,apertureSize = 3)
    lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=10,maxLineGap=100)
    #ret, th1 =cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

    for x1,y1,x2,y2 in lines[0]:
        cv2.line(crop_img,(x1,y1),(x2,y2),(0,255,0),2)
        cv2.line(empty,(x1,y1),(x2,y2),(0,255,0),2)


    # Draw a diagonal blue line with thickness of 5 px
    cv2.line(image,(0,240),(50,180),(255,0,0),2)
    cv2.line(image,(320,240),(270,180),(255,0,0),2)

    #show the frame
    cv2.imshow("Original", image)
    cv2.imshow("Edges", edges)
    key = cv2.waitKey(1) & 0xFF

    #clear the stream in preparation for the next frame
    rawCapture.truncate(0)

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

我该怎么做才能修复此错误?

这行似乎返回了

线条=cv2.HoughLinesP(边,1,np.pi/180100,最小线条长度=10,最大线条间距=100)
然后在下一行中,
对于第[0]行中的x1、y1、x2、y2,由于
而失败。您现在需要确保
cv2.HoughLinesP()
返回正确的数据,或者添加一个检查来进行检查,如:

如果行:
对于[0]行中的x1、y1、x2、y2:
...

`TypeError:'NoneType'对象没有属性'getitem'`这意味着
。你应该检查它,因为使用
行[0]
打印(行)
…你看到了什么?我有很多数字[[4 153 136 42][5 157 139 44][17 146 139 44]]。当同样的错误出现时,它停止了。显然,当它没有检测到任何行时,它会出现一个错误,因为它确实得到了数据。如果我没有将相机指向直线,它将显示错误并自动停止操作。我希望它不会停止,即使没有线被检测到。好的,现在它有一个新的错误。值错误:包含多个元素的数组的真值不明确。使用a.any()或a.all()