如何提高cv2.dnn.readNetFromCaffe()的性能net.ward()需要更多时间(7到10秒/帧)才能给出结果 [英] How to improve performance net.forward() of cv2.dnn.readNetFromCaffe() , net.forward taking more time(7 to 10 seconds/frame) to give the result

查看:7
本文介绍了如何提高cv2.dnn.readNetFromCaffe()的性能net.ward()需要更多时间(7到10秒/帧)才能给出结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile),然后使用net.forward()遍历实时视频帧以获得每个帧的输出。

net.forward()每帧需要7到10秒才能得出结果。请帮助我提高性能(减少net.forward()中的处理时间)。

表示:从第1步到第2步每帧需要7到10秒。

(下面的代码中提到了步骤1和步骤2)。

import cv2
import time
import numpy as np

protoFile = "deploy.prototxt"
weightsFile = "iter_10.caffemodel"

inWidth = 300
inHeight = 300

# web camera
cap = cv2.VideoCapture(0)
hasFrame, frame = cap.read()

net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
k = 0
while 1:
    k+=1
    t = time.time()
    print("Start time = {}".format(t))
    hasFrame, frame = cap.read()

    if not hasFrame:
        cv2.waitKey()
        print("Wait====>")
        break

    inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
                              (0, 0, 0), swapRB=False, crop=False)


    net.setInput(inpBlob)

    # Step1
    print("before forward = {}".format(time.time() - t))

    output = net.forward()

    # Step2
    #taking close to 7 to 10 seconds for each frame
    print("forward = {}".format(time.time() - t))

推荐答案

我先减小图像大小,然后再将其通过网络。

# convert the input frame from BGR to RGB then resize it to have
# a width of 750px (to speed up processing)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
rgb = imutils.resize(frame, width=750)
imageBlob = cv2.dnn.blobFromImage(cv2.resize(rgb, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))

# apply OpenCV's deep learning-based face detector to localize
# faces in the input image
detector.setInput(imageBlob)
detections = detector.forward()

这篇关于如何提高cv2.dnn.readNetFromCaffe()的性能net.ward()需要更多时间(7到10秒/帧)才能给出结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆