1

I have used net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile) and then looping through the live video frames to get the outputs for each frames using net.forward().

But the net.forward() takes 7 to 10 seconds for each frames to give the result. Please help me how to improve the performance (reduce the time taking to process in net.forward()).

Means: From Step1 to Step2 takes 7 to 10 seconds for each frames.

(Step1 and Step2 are mentioned in the below code).

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))
4

2 回答 2

2

在通过网络传递图像之前,我会减小图像大小。

# 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()
于 2020-02-03T17:51:17.953 回答
2

OpenPose 模型本身就很庞大。有几种方法可以提高效率。

1)尝试主干版本(.caffemodel.prototxt)。

2) 降低输入 blob 分辨率。请注意,它可能会显着降低准确性。

3) 尝试不同的模型。您可以查看使用英特尔推理引擎 (OpenVINO) 构建 OpenCV 的选项并尝试此模型:https ://github.com/opencv/open_model_zoo/blob/2018/intel_models/human-pose-estimation-0001/description/人体姿态估计-0001.md。如果只有 Intel CPU 或 GPU 或 Movidius 神经计算棒,则第三个选项。

于 2019-02-08T04:28:05.897 回答