实时计算 OpenCV Python 预测的运行时间

发布于 2025-01-09 17:54:25 字数 1946 浏览 0 评论 0原文

我正在尝试获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏雨凉 2025-01-16 17:54:25

我认为你可以利用 python 装饰器,
这是一个例子:

# Timer decorator


def timer(fn):    
    from time import perf_counter
    def inner(*args, **kwargs):
        start_time = perf_counter()
        to_execute = fn(*args, **kwargs)
        end_time = perf_counter()
        execution_time = end_time - start_time
        print('{0} took {1:.8f}s to execute'.format(fn.__name__, execution_time))
        return to_execute

    return inner

@timer
def function_1():
    for i in range(100000):
        pass

@timer
def function_2():
    for i in range(10000000):
        pass


@timer
def function_3():
    for i in range(10000):
        pass

@timer
def function_4():
    for i in range(100000000):
        pass

function_1()
function_2()
function_3()
function_4()

I think you could make use of python decorators,
here is an example:

# Timer decorator


def timer(fn):    
    from time import perf_counter
    def inner(*args, **kwargs):
        start_time = perf_counter()
        to_execute = fn(*args, **kwargs)
        end_time = perf_counter()
        execution_time = end_time - start_time
        print('{0} took {1:.8f}s to execute'.format(fn.__name__, execution_time))
        return to_execute

    return inner

@timer
def function_1():
    for i in range(100000):
        pass

@timer
def function_2():
    for i in range(10000000):
        pass


@timer
def function_3():
    for i in range(10000):
        pass

@timer
def function_4():
    for i in range(100000000):
        pass

function_1()
function_2()
function_3()
function_4()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文