官术网_书友最值得收藏!

  • Mastering OpenCV 4 with Python
  • Alberto Fernández Villán
  • 310字
  • 2021-07-02 12:07:18

Calculating frames per second

In the Reading camera frame and video files section, we saw how we can get some properties from the cv2.VideoCapture object. fps is an important metric in computer vision projects. This metric indicates how many frames are processed per second. It is safe to say that a higher number of fps is better. However, the number of frames your algorithm should process every second will depend on the specific problem you have to solve. For example, if your algorithm should track and detect people walking down the street, 15 fps is probably enough. But if your goal is to detect and track cars going fast on a highway, 20-25 fps are probably necessary. 

Therefore, it is important to know how to calculate the fps metric in your computer vision projects. In the following example, read_camera_fps.py, we are going to modify read_camera.py to output the number of fps. The key points are shown in the following code:

# Read until the video is completed, or 'q' is pressed
while capture.isOpened():
# Capture frame-by-frame from the camera
ret, frame = capture.read()

if ret is True:
# Calculate time before processing the frame:
processing_start = time.time()

# All the processing should be included here
# ...
# ...
# End of processing


# Calculate time after processing the frame
processing_end = time.time()

# Calculate the difference
processing_time_frame = processing_end - processing_start

# FPS = 1 / time_per_frame
# Show the number of frames per second
print("fps: {}".format(1.0 / processing_time_frame))

# Break the loop
else:
break

First, we take the time before the processing is done:

processing_start = time.time()

Then, we take the time after all the processing is done:

processing_end = time.time()

Following that, we calculate the difference:

processing_time_frame = processing_end - processing_start

Finally, we calculate and print the number of fps:

print("fps: {}".format(1.0 / processing_time_frame))
主站蜘蛛池模板: 苏尼特右旗| 临高县| 呼玛县| 承德县| 腾冲县| 东辽县| 万安县| 铜山县| 富裕县| 鄂托克前旗| 温州市| 山阴县| 乌兰县| 古浪县| 贵港市| 临漳县| 五常市| 彰化市| 平江县| 正蓝旗| 龙海市| 定安县| 文化| 佛山市| 新平| 会东县| 始兴县| 武威市| 襄垣县| 宁阳县| 龙门县| 天祝| 博野县| 北安市| 安徽省| 宜城市| 河北区| 兴安盟| 尚志市| 沾化县| 衡山县|