Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用OpenCV和网络摄像头进行7段检测_Python_Opencv_Imutils - Fatal编程技术网

Python 使用OpenCV和网络摄像头进行7段检测

Python 使用OpenCV和网络摄像头进行7段检测,python,opencv,imutils,Python,Opencv,Imutils,我想用相机看一些数码显示器。我在这里找到了检测和读取显示的教程: 因为我需要实时读取,所以我设置了网络摄像头的视频输入。这是我的密码: from imutils.perspective import four_point_transform from imutils import contours import imutils import cv2 # creating a dictionary for 7-segment detection DIGITS_LOOKUP = { (1,

我想用相机看一些数码显示器。我在这里找到了检测和读取显示的教程:

因为我需要实时读取,所以我设置了网络摄像头的视频输入。这是我的密码:

from imutils.perspective import four_point_transform
from imutils import contours
import imutils
import cv2

# creating a dictionary for 7-segment detection
DIGITS_LOOKUP = {
    (1, 1, 1, 0, 1, 1, 1): 0,
    (0, 0, 1, 0, 0, 1, 0): 1,
    (1, 0, 1, 1, 1, 1, 0): 2,
    (1, 0, 1, 1, 0, 1, 1): 3,
    (0, 1, 1, 1, 0, 1, 0): 4,
    (1, 1, 0, 1, 0, 1, 1): 5,
    (1, 1, 0, 1, 1, 1, 1): 6,
    (1, 0, 1, 0, 0, 1, 0): 7,
    (1, 1, 1, 1, 1, 1, 1): 8,
    (1, 1, 1, 1, 0, 1, 1): 9
}

# capturing from webcam
cap = cv2.VideoCapture(0)
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

# define codec and create VideoWriter object
out = cv2.VideoWriter('out_videos/cam_blur.avi', 
                      cv2.VideoWriter_fourcc('M','J','P','G'), 
                      30, 
                      (frame_width,frame_height))

# continuous capture from webcam
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        blurred = cv2.GaussianBlur(gray, (7, 7), 0)
        edged = cv2.Canny(blurred, 50, 200, 200)
        cv2.imshow('Video', edged)
                
        # find contours in the edge map, then sort them by their size in descending order
        cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
            cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
        displayCnt = None
        
        # loop over the contours
        for c in cnts:
            # approximate the contour
            peri = cv2.arcLength(c, True)
            approx = cv2.approxPolyDP(c, 0.02 * peri, True)
            
        # if the contour has four vertices, then we have found
        # the thermostat display
            if len(approx) == 4:
                 displayCnt = approx
                 break
        
        # extract the thermostat display, apply a perspective transform
        # to it
        warped = four_point_transform(gray, displayCnt.reshape(4, 2))
        output = four_point_transform(frame, displayCnt.reshape(4, 2))
        cv2.imshow('Birdeye', output)
        
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
       
cap.release()
cv2.destroyAllWindows()
在第一次尝试时,程序运行良好,尽管有时它会检测到随机矩形对象,并在崩溃时发送此错误:

File "7segmentcam.py", line 63, in <module>
    warped = four_point_transform(gray, displayCnt.reshape(4, 2))

AttributeError: 'NoneType' object has no attribute 'reshape'
文件“7segmentcam.py”,第63行,在
扭曲=四点变换(灰色,显示。重塑(4,2))
AttributeError:“非类型”对象没有属性“重塑”

在我调整了第37行和第38行中的一些参数后,程序没有响应,并以上面相同的错误崩溃。当相机没有检测到任何物体时,有没有更好的方法来运行程序而不出错?

这是因为有时,您的代码在
approxPolyDP
之后找不到任何具有4个顶点的轮廓,因此,变量
displayCnt
的值保持在for循环之前设置的
None
,因此
four_point_transform
函数显示错误,因为它没有将
None
作为输入

为避免此错误,在使用
four\u point\u transform
函数之前,请通过以下方式检查
displayCnt
的值:
如果displayCnt不是None:
,如果它是None,则不要运行该函数


另外,据我所知,for循环中的if条件是不正确的,因为它在极少数情况下满足,并且在图像处理中,当您确定它在至少1个条件下为真时,您可以使用这些特定条件。因此,在我看来,改变if条件。

您显式地设置了
displayCnt=None
,然后尝试稍后对其进行重塑。好吧,我犯了一个错误。我应该设置
displayCnt=approx
它适合我。当它捕获4个顶点时,我将if条件更改为process
four\u point\u transform
,当vertice不是4时,我中断了该过程