我正在使用 cv2.dnn 和 frozen_inference_graph.pb 在opencv中进行手势检测。代码工作正常,但问题是它真的很慢。
所以我想通过以下方式在gpu上运行代码:
cvNet = cv2.dnn.readNetFromTensorflow('model/frozen_inference_graph.pb', 'model/graph.pbtxt')
cvNet.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
cvNet.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL_FP16)
从https://support.apple.com/en-us/HT202823中,我可以看到笔记本电脑中已经内置了opencl。我只是一个学生,所以我买不起NVIDIA GPU。
所有代码
import tensorflow as tf
import numpy as np
import time
import cv2
cvNet = cv2.dnn.readNetFromTensorflow('model/frozen_inference_graph.pb', 'model/graph.pbtxt')
cvNet.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
cvNet.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL_FP16)
capture = cv2.VideoCapture(0)
while True:
# capture frame by frame
ret, frame = capture.read()
frame = cv2.flip(frame, 1)
rows = frame.shape[0]
cols = frame.shape[1]
cvNet.setInput(cv2.dnn.blobFromImage(frame, size=(300, 300), swapRB=True, crop=False))
start = time.time()
cvOut = cvNet.forward()
end = time.time()
print(end-start)
for detection in cvOut[0, 0, :, :]:
score = float(detection[2])
if score > 0.3:
left = detection[3] * cols
top = detection[4] * rows
right = detection[5] * cols
bottom = detection[6] * rows
cv2.rectangle(frame, (int(left), int(top)), (int(right),
int(bottom)), (23, 230, 210), thickness=2)
cv2.imshow('img', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.release()
cv2.destroyAllWindows()
运行代码时,它使用了70%的cpu,但使用了0%的gpu。无论如何,我可以加快视频速度以获得更多fps吗?
谢谢。