以下代码在Mac OS X中完美运行,但在Windows和Raspberry OS中会出现错误



我们使用以下代码来处理摄像机捕获的图像,并将其分类为前进,左还是右。它们都与交通有关。在前向优先检测中使用级联。级联尝试查找向右或向左箭头。然后,如果它不对其进行分类,那么它就是前进的方向。如果它返回 true,我们从 decider deciderfunc 调用。每次 deciderfunc 它运行缓慢(再次在窗口和 pi 中(。 它决定它是左还是右。现在,当我在mac os x下运行它时,它可以完美运行。但是在窗口中,它会给出以下错误。我认为错误与视频捕获和上限读取有关。如何解决此问题?提前致谢..:

[ WARN:1] videoio(MSMF): OnReadSample() is called with error status: -1072873821
[ WARN:1] videoio(MSMF): async ReadSample() call is failed with error status: -1072873821
0 31
F
[ WARN:0] videoio(MSMF): can't grab frame. Error: -1072873821
Traceback (most recent call last):
File "C:/Users/merts/Desktop/the3r4yslayer-master/combiner.py", line 15, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.0.0) C:projectsopencv-pythonopencvmodulesimgprocsrccolor.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
[ WARN:0] terminating async callback
Process finished with exit code 1 

主要:

import cv2
import numpy as np
from forward import firstdetection
from decider import deciderfunc
cameraCapture = cv2.VideoCapture(1)
cv2.namedWindow('camera')
success, frame = cameraCapture.read()
forwardcounter = leftcounter = rightcounter = total = 0
while success:
    success, frame = cameraCapture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    img = cv2.medianBlur(gray, 37)
    circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 50, param1=160, param2=40)
    cv2.imshow('camera', frame)

    try:
        if circles is not None:
            flag = firstdetection()
            if flag:
                if deciderfunc():  # it gives answer about left or right
                    print('L')
                else:
                    print('R')
            else:
                print('F')  # will be replaced according to pi
    except:
        continue

决策者:

import cv2
import numpy as np

def deciderfunc():
    cap = cv2.VideoCapture(1)
    success, camera = cap.read()
    left = right = total = 0
    while success and total < 15:
        value_0 = value_1 = 0
        gray = cv2.cvtColor(camera, cv2.COLOR_BGR2GRAY)
        img = cv2.medianBlur(gray, 37)
        circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 50, param1=160, param2=40)
        circles = np.uint16(np.around(circles))
        max_r, max_i = 0, 0
        for i in range(len(circles[:, :, 2][0])):
            if circles[:, :, 2][0][i] > 50 and circles[:, :, 2][0][i] > max_r:
                max_i = i
                max_r = circles[:, :, 2][0][i]
        x, y, r = circles[:, :, :][0][max_i]

        square = camera[y - r:y + r, x - r:x + r]
        zone_0 = square[square.shape[0] * 2 // 8:square.shape[0] * 6 // 8, square.shape[1] * 3 // 8:square.shape[1] * 4 // 8]
        gray_0 = cv2.cvtColor(zone_0, cv2.COLOR_BGR2GRAY)
        img_0 = cv2.medianBlur(gray_0, 37)
        zone_1 = square[square.shape[0] * 2 // 8:square.shape[0] * 6 // 8, square.shape[1] * 4 // 8:square.shape[1] * 5 // 8]
        gray_1 = cv2.cvtColor(zone_1, cv2.COLOR_BGR2GRAY)
        img_1 = cv2.medianBlur(gray_1, 37)
        image_data_0 = np.asarray(img_0)
        for i in range(len(image_data_0)):
            for j in range(len(image_data_0[0])):
                value_0 = value_0 + image_data_0[i][j]
        image_data_1 = np.asarray(img_1)
        for i in range(len(image_data_1)):
            for j in range(len(image_data_1[0])):
                value_1 = value_1 + image_data_1[i][j]
        if value_0 < value_1:
            left+=1
            total+=1
        else:
            right+=1
            total+=1
    if left>right:
        return 1
    else:
        return 0

向前:

import cv2
def firstdetection():

    cameraCapture = cv2.VideoCapture(1)

    success, cap = cameraCapture.read()
    ok_cascade = cv2.CascadeClassifier('new_kocum.xml')      # this is the cascade we just made. Call what you want
    flag=0
    true_counter = 0
    false_counter = 0
    while success and flag<31:
        flag+=1
        img = cap
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        oks = ok_cascade.detectMultiScale(gray, 3, 4)
        if len(oks)!=0:
            true_counter+=1
            continue
        else:
            false_counter+=1
            continue
    if(true_counter>false_counter):
        cameraCapture.release()
        print(true_counter,false_counter)
        return True
    else:
        cameraCapture.release()
        print(true_counter,false_counter)
        return False

您可能正在从不存在的设备读取。VideoCapture(1)调用指示 OoenCV 从设备 1 读取,如果没有,则会失败。首先尝试VideoCapture(0)因为网络摄像头最有可能首先显示在那里(除非有另一台设备在 0,在这种情况下可能是 1(

最新更新