我正在树莓派4中使用此python程序进行乘客计数。当相机打开并且人员计数完成时,该程序在我的笔记本电脑中运行得很好,但在树莓派4中不起作用。 我正在为此项目使用Raspberry pi 4和7“显示器。我认为在尝试打印框架时它没有接受任何框架输入,但没有打印。相机甚至无法启动。谢谢您的帮助。
它显示以下错误:
Unable to stop the stream: Invalid argument
False
None
OpenCV Error: Assertion failed (dims <= 2 && step[0] > 0) in locateROI, file /build/opencv-L65chJ/opencv-3.2.0+dfsg/modules/core/src/matrix.cpp, line 949
Traceback (most recent call last):
File "/home/pi/Desktop/tracking.py", line 31, in <module>
fgmask = cv2.blur(frame, (10,10))
cv2.error: /build/opencv-L65chJ/opencv-3.2.0+dfsg/modules/core/src/matrix.cpp:949: error: (-215) dims <= 2 && step[0] > 0 in function locateROI
这是我的代码:
import numpy as np
import math
import cv2
cap = cv2.VideoCapture(1)
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG(history=150, backgroundRatio=0.3)
def line1(x,y):
return y - (29*x)/96.0 - 300
def line2(x,y):
return y - (29*x)/96.0 - 500
crossedAbove = 0
crossedBelow = 0
points = set()
pointFromAbove = set()
pointFromBelow = set()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('pedestrianOutput.avi',fourcc, 25.0, (1920,1080))
font = cv2.FONT_HERSHEY_SIMPLEX
while(1):
pointInMiddle = set()
prev = points
points = set()
ret, frame = cap.read()
print(ret)
print(frame)
fgmask = frame
fgmask = cv2.blur(frame, (10,10))
fgmask = fgbg.apply(fgmask)
fgmask = cv2.medianBlur(fgmask, 7)
oldFgmask = fgmask.copy()
contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL,1)
for contour in contours:
x,y,w,h = cv2.boundingRect(contour)
if w>40 and h>90:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2, lineType=cv2.LINE_AA)
point = (int(x+w/2.0), int(y+h/2.0))
points.add(point)
for point in points:
(xnew, ynew) = point
if line1(xnew, ynew) > 0 and line2(xnew, ynew) < 0:
pointInMiddle.add(point)
for prevPoint in prev:
(xold, yold) = prevPoint
dist = cv2.sqrt((xnew-xold)*(xnew-xold)+(ynew-yold)*(ynew-yold))
if dist[0] <= 120:
if line1(xnew, ynew) >= 0 and line2(xnew, ynew) <= 0:
if line1(xold, yold) < 0: # Point entered from line above
pointFromAbove.add(point)
elif line2(xold, yold) > 0: # Point entered from line below
pointFromBelow.add(point)
else: # Point was inside the block
if prevPoint in pointFromBelow:
pointFromBelow.remove(prevPoint)
pointFromBelow.add(point)
elif prevPoint in pointFromAbove:
pointFromAbove.remove(prevPoint)
pointFromAbove.add(point)
if line1(xnew, ynew) < 0 and prevPoint in pointFromBelow: # Point is above the line
print('One Crossed Above')
print(point)
crossedAbove += 1
pointFromBelow.remove(prevPoint)
if line2(xnew, ynew) > 0 and prevPoint in pointFromAbove: # Point is below the line
print('One Crossed Below')
print(point)
crossedBelow += 1
pointFromAbove.remove(prevPoint)
for point in points:
if point in pointFromBelow:
cv2.circle(frame, point, 3, (255,0,255),6)
elif point in pointFromAbove:
cv2.circle(frame, point, 3, (0,255,255),6)
else:
cv2.circle(frame, point, 3, (0,0,255),6)
cv2.line(frame, (0,300), (1920,880), (255, 0, 0), 4)
cv2.line(frame, (0,500), (1920,1080), (255, 0, 0), 4)
cv2.putText(frame,'People Going Above = '+str(crossedAbove),(1200,50), font, 1,(255,255,255),2,cv2.LINE_AA)
cv2.putText(frame,'People Going Below = '+str(crossedBelow),(1200,100), font, 1,(255,255,255),2,cv2.LINE_AA)
cv2.imshow('a',oldFgmask)
cv2.imshow('frame',frame)
out.write(frame)
l = cv2.waitKey(1) & 0xff
if l == 27:
break
cap.release()
cv2.destroyAllWindows()
答案 0 :(得分:0)
cap.read()
返回False
和空的frame
。
然后,您尝试在该frame
上应用模糊处理,从而触发异常。
仅当不为空时,才应处理框架。
可能有几个原因会导致空框架:
我将从后面的内容开始,将cap.read()
之后的所有内容放在if ret:
语句下。
while(1):
pointInMiddle = set()
prev = points
points = set()
ret, frame = cap.read()
print(ret)
print(frame)
if ret:
fgmask = frame
fgmask = cv2.blur(frame, (10,10))
fgmask = fgbg.apply(fgmask)
fgmask = cv2.medianBlur(fgmask, 7)
oldFgmask = fgmask.copy()
contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL,1)
...