如何解决视频中YOLOV3车辆检测滞后
我使用 yolov3 模型使用 python3 和 opencv 进行车辆检测。
对于视频中的检测,它由于图像处理所花费的时间而滞后。在打击代码中,我打印了每一步所花费的时间,并发现行 layersOutputs = net.forward(output_layers_names)
花费很长时间(参见带有粗体橙色线的输出)比较对其他人来说,这就是为什么每个图像处理所花费的时间需要很长时间并且视频滞后的原因。谁能给我解决方案以减少处理每一帧所需的时间。
使用:
1.预训练的 yolov3 模型
2.蟒蛇 3.8
3.张量流-GPU(2.4.1)
4. Ubuntu 18.04
from itertools import chain
import cv2
import time
import numpy as np
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
def flatten(T):
res = tuple(chain.from_iterable(T))
return res
start = time.time()
print("LLLLLL")
net = cv2.dnn.readNet('/home/tsi/Desktop/ANPR/Time_Cal/yolov3.weights','/home/tsi/Desktop/ANPR/Time_Cal/yolov3.cfg')
print("Time elapsed to load model " + "{:.4f}".format(time.time() - start))
t1 = time.time()
t00 = time.time()
camera = cv2.VideoCapture("/home/tsi/Desktop/ANPR/Time_Cal/blr_traffic2_compressed.mp4")
print("Time elapsed to load video " + "{:.4f}".format(time.time() - t1))
print("---------")
print("Entering in Video")
while True:
t2 = time.time()
ret,img = camera.read(1)
print("Time elapsed to read frame from video : " + "{:.4f}".format(time.time() - t2))
t3 = time.time()
classes = []
boxes = []
confidences = []
class_ids = []
print("Time elapsed to initialization: " + "{:.4f}".format(time.time() - t3))
try:
img = cv2.resize(img,(700,400))
except:
break
t4 = time.time()
with open('/home/tsi/Desktop/ANPR/Time_Cal/coco_names.txt','r') as f:
classes = f.read().splitlines()
print("Time elapsed to open COCO and split: " + "{:.4f}".format(time.time() - t4))
t5 = time.time()
height,width,_ = img.shape
print("Time elapsed to determin height & width : " + "{:.4f}".format(time.time() - t5))
t6 = time.time()
blob = cv2.dnn.blobFromImage(img,1 / 255,(416,416),(0,0),swapRB=True,crop=False)
print("Time elapsed for blobbing : " + "{:.4f}".format(time.time() - t6))
t7 = time.time()
net.setInput(blob)
print("Time elapsed for setting input blob to net : " + "{:.4f}".format(time.time() - t7))
t8 = time.time()
output_layers_names = net.getUnconnectedOutLayersNames()
print("Time elapsed for assigning output layers : " + "{:.4f}".format(time.time() - t8))
t13 = time.time()
layersOutputs = net.forward(output_layers_names)
print("Time elapsed to get layer output : " + "{:.4f}".format(time.time() - t13))
t9 = time.time()
print("Entering layer loop:")
for output in layersOutputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x,y,w,h])
confidences.append((float(confidence)))
class_ids.append(class_id)
print("Time elapsed for getting clases : " + "{:.4f}".format(time.time() - t9))
t10 = time.time()
indexes = cv2.dnn.NMSBoxes(boxes,confidences,0.5,0.4)
print("Time elapsed for getting boxes : " + "{:.4f}".format(time.time() - t10))
t11 = time.time()
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0,255,size=(len(boxes),1))
print("Time elapsed for setting font and colors : " + "{:.4f}".format(time.time() - t11))
t12 = time.time()
for i in flatten(indexes):
x,h = boxes[i]
label = str(classes[class_ids[i]])
confidence = str(round(confidences[i],2))
color = colors[i]
output = img.copy()
alpha = 0.3
if label == "car" or label == "motorbike" or label == "truck" or label == "bus":
cv2.rectangle(img,(x,y),(x + 250,y + 25),-1)
cv2.rectangle(img,(x + w,y + h),color,2)
cx,cy = int(x + (w / 2)),int(y + (h / 2))
cv2.putText(img,label + " " + confidence,y + 20),font,2,(204,153),2)
cv2.addWeighted(img,alpha,output,1 - alpha,output)
cv2.imshow("Final Image",img)
print("Time elapsed for final loop : " + "{:.4f}".format(time.time() - t12))
print("Frame Process Ended..........."+str(time.time()-t1))
if cv2.waitKey(1) & 0xFF == ord('q'):
print("Total time elapsed :"+str(time.time() - t00))
break
camera.release()
cv2.destroyAllWindows()
输出:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。