实时计算 OpenCV Python 预测的运行时间
我正在尝试获取 TensorFlow 中神经网络预测的运行时间,并在 OpenCV 中使用 Python 运行实时推理。每当分类为真时,我都会在窗口中显示一个文本标签,并且我非常想测量它的真实时间为多少秒。
我的想法是获取流开始时的开始时间,然后获取预测的开始时间并减去这两个时间以获得经过的时间。
但这让我得到了与预期不同的价值观。解决这个问题的最佳方法是什么?
这是我的代码:
import time
import cv2
cap_device = args.device
cap_width = args.width
cap_height = args.height
cap = cv2.VideoCapture(cap_device, cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, cap_height)
start_time = time.time() #get start time
class_time = 0 #initialize class time
with mp_pose.Pose(model_complexity = 2) as pose_tracker: #load model
while cap.isOpened(): #while camera is running
ret, frame = cap.read() #read frames in
...
...
...
with open("labels.txt") as file: #gets labels to show pose class
lines = file.readlines()
lines = [line.rstrip() for line in lines]
#classification confidence
for i in range(0, len(prediction[0])):
if prediction[0][0] > 0.50 and prediction[0][0]<=1.00:
draw_info_text(image, lines[0],cap_width,cap_height)
class_time = time.time()
elif prediction[0][1] > 0.50 and prediction[0][1]<=1.00: =
draw_info_text(image,lines[1],cap_width,cap_height)
elif prediction[0][2] > 0.50 and prediction[0][2]<= 1.00:
draw_info_text(image,lines[2],cap_width,cap_height)
elif prediction[0][3] > 0.50 and prediction[0][3] <= 1.00:
draw_info_text(image,lines[3],cap_width,cap_height)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
#show video
cv2.imshow('webcam', image)
if cv2.waitKey(1) == ord('q'):
break
total_prediction_time = start_time - class_time # calculate the total time pred was true
print(total_prediction_time)
cap.release()
cv2.destroyAllWindows()
I am trying to get the elapsed time of a neural network prediction in TensorFlow running real-time inference in OpenCV w/ Python. Whenever a classification is true, I display a text label in the window and I pretty much want to measure how many seconds it is true for.
My thinking was to get the start time at the beginning of the stream and then getting the start time of the prediction and subtracting those two to get the elapsed time.
This is getting me different values than expected though. What is the best way to go about this?
Here is my code:
import time
import cv2
cap_device = args.device
cap_width = args.width
cap_height = args.height
cap = cv2.VideoCapture(cap_device, cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, cap_height)
start_time = time.time() #get start time
class_time = 0 #initialize class time
with mp_pose.Pose(model_complexity = 2) as pose_tracker: #load model
while cap.isOpened(): #while camera is running
ret, frame = cap.read() #read frames in
...
...
...
with open("labels.txt") as file: #gets labels to show pose class
lines = file.readlines()
lines = [line.rstrip() for line in lines]
#classification confidence
for i in range(0, len(prediction[0])):
if prediction[0][0] > 0.50 and prediction[0][0]<=1.00:
draw_info_text(image, lines[0],cap_width,cap_height)
class_time = time.time()
elif prediction[0][1] > 0.50 and prediction[0][1]<=1.00: =
draw_info_text(image,lines[1],cap_width,cap_height)
elif prediction[0][2] > 0.50 and prediction[0][2]<= 1.00:
draw_info_text(image,lines[2],cap_width,cap_height)
elif prediction[0][3] > 0.50 and prediction[0][3] <= 1.00:
draw_info_text(image,lines[3],cap_width,cap_height)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
#show video
cv2.imshow('webcam', image)
if cv2.waitKey(1) == ord('q'):
break
total_prediction_time = start_time - class_time # calculate the total time pred was true
print(total_prediction_time)
cap.release()
cv2.destroyAllWindows()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你可以利用 python 装饰器,
这是一个例子:
I think you could make use of python decorators,
here is an example: