' NoneType'对象没有attritube' __ getitem __'

时间:2017-11-26 03:07:41

标签: python opencv raspberry-pi3

我注意到我的代码没有正常运行。但是我已经宣布了这条线。

它说: 第28行,在 对于行[0]中的x​​1,y1,x2,y2: TypeError:' NoneType'对象没有属性' 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

我该怎么做才能解决这个错误?

1 个答案:

答案 0 :(得分:-1)

似乎此行返回None lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=10,maxLineGap=100) 然后在下一行for x1,y1,x2,y2 in lines[0]失败,因为linesNone。您现在需要确保cv2.HoughLinesP()返回正确的数据或添加检查以进行检查: if lines: for x1,y1,x2,y2 in lines[0]: ...