我一直在做这个项目,我们从人行道上获取交通的视频,检测汽车并读取它们的车牌。检测部分工作得很好,我得到了满意的结果。现在我试着提取汽车的边界框,并将它们保存为jpg格式,并且我也只想从框架的特定区域提取边界框。所以我写了这段代码,但是每次在第50帧左右我都会收到一个断言错误。我认为视频没有任何问题,因为检测部分工作得很好。我在第54行收到错误cv2.imwrite("frames\"+str(number)+'.jpg', frametosave)
我尝试了frames\
和frames/
,但我得到了两个路径名的错误。请在我被解雇之前帮帮我。
import cv2
import numpy as np
video = cv2.VideoCapture("Resources/highway.mp4")
wh = 320
classpathname = 'Resources/coco.names'
classNames = []
videoWidth,videoHeight = int(video.get(3)), int(video.get(4))
size = (videoWidth, videoHeight)
result = cv2.VideoWriter('result.mp4', cv2.VideoWriter_fourcc(*'MJPG'),10, size)
with open(classpathname,'rt') as f:
classNames = f.read().rstrip('n').split('n')
configFile = "Resources/yolov3.cfg"
weights = "Resources/yolov3.weights"
net = cv2.dnn.readNetFromDarknet(configFile, weights)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
def findObjects(output, img):
h, w, c = img.shape
boundingbox = []
classIDs = []
confidences = []
for out in output:
for det in out:
scores = det[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence> 0.6:
width,height= int(det[2]*w), int(det[3]*h)
x,y = int((det[0]*w )-width/2), int((det[1]*h) -height/2)
boundingbox.append([x,y, width, height])
classIDs.append(classID)
confidences.append(confidence)
indices = cv2.dnn.NMSBoxes(boundingbox, confidences, 0.6, 0.3)
number = 1
for i in indices:
box = boundingbox[i]
x,y,w,h = box[0],box[1],box[2],box[3]
if(y+h>1000 and x+(w/2) <1000 ):
frametosave = img[y:y+h, x:x+w]
cv2.imwrite("frames/"+str(number)+'.jpg', frametosave)
number+=1
cv2.rectangle(img,(x,y), (x+w, y+h), (255,0,255),2)
cv2.putText(img, f'{classNames[classIDs[i]].upper()} {int(confidences[i]*100)}%', (x,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255,0,255), 2)
framenumber = 1
while True:
success, img = video.read()
if success:
blob = cv2.dnn.blobFromImage(img, 1 / 255, (wh, wh), [0, 0, 0], 1, crop=False)
net.setInput(blob)
print(framenumber)
framenumber +=1
layerNames = net.getLayerNames()
outputLayerNames = [layerNames[i - 1] for i in net.getUnconnectedOutLayers()]
outputs = net.forward(outputLayerNames)
findObjects(outputs, img)
if success:
result.write(img)
cv2.imshow("img", img)
if cv2.waitKey(1) & 0xFF == ord('s'):
break
video.release()
result.release()
这是我得到的错误
Traceback (most recent call last):
File "C:UserschhetPycharmProjectsopencvobjectDetection.py", line 72, in <module>
findObjects(outputs, img)
File "C:UserschhetPycharmProjectsopencvobjectDetection.py", line 54, in findObjects
cv2.imwrite("frames\"+str(number)+'.jpg', frametosave)
cv2.error: OpenCV(4.5.4-dev) D:aopencv-pythonopencv-pythonopencvmodulesimgcodecssrcloadsave.cpp:799: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
OpenCV: FFMPEG: tag 0x47504a4d/'MJPG' is not supported with codec id 7 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x7634706d/'mp4v'
Process finished with exit code 1
所以我所做的是添加一些条件来检查框架是否有效并且它是否有效。以下是我检查的条件:
if(x<0):
x=0
if(y<0):
y=0
frametosave = img[y:y+h, x:x+w]
if (x + w > videoWidth):
if (y + h > videoHeight):
frametosave = img[y:videoHeight, x:videoWidth]
frametosave = img[y:y + h, x:videoWidth]
if(y+h> videoHeight):
frametosave = img[y:videoHeight, x:x+w]
if frametosave.data:
print(frametosave.data)
cv2.imwrite("frames/" + str(number) + '.jpg', frametosave)