0

我想用相机阅读一些数字显示器。我在这里找到了检测和阅读显示的教程:https ://www.pyimagesearch.com/2017/02/13/recognizing-digits-with-opencv-and-python/

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

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'

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

4

1 回答 1

0

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

为避免此错误,请在four_point_transform函数之前检查displayCntby:的值if displayCnt is not None:,如果为 None,则不要运行该函数。

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

于 2020-09-25T07:36:33.517 回答