我们使用以下代码来处理摄像机捕获的图像并将其分类为正向,左向或右向符号。它们全都与流量有关。在前向优先检测中,使用级联。级联尝试查找向右或向左箭头,然后如果未对箭头进行分类,则为正向。如果它返回true,我们会从Decisionr Decisionrfunc中调用。每次definerfunc运行缓慢(同样在Windows和pi中)。它决定是左还是右。现在,当我在Mac OS X下运行它时,它可以完美运行。但是在Windows中,它会出现以下错误。我认为错误与视频捕获和上限读取有关。如何解决此问题?预先感谢..:
[ 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:\projects\opencv-python\opencv\modules\imgproc\src\color.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
答案 0 :(得分:0)
您可能正在从不存在的设备读取数据。 VideoCapture(1)
调用指示OoenCV从设备编号1中读取,但是如果没有,则失败。首先尝试VideoCapture(0)
,因为最有可能首先出现在网络摄像头中(除非有另一台设备位于0,在这种情况下可能是1)