Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NicholasShatokhin/e209c8dde9415d45e865c87456ecdf34 to your computer and use it in GitHub Desktop.
Save NicholasShatokhin/e209c8dde9415d45e865c87456ecdf34 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
import time
from ultralytics import YOLO
# creating the videocapture object
# and reading from the input file
# Change it to 0 if reading from webcam
cap = cv2.VideoCapture('/home/ubuntu/testvideo.mp4')
model = YOLO("/home/ubuntu/yolo8target_nano.pt")
# used to record the time when we processed last frame
prev_frame_time = 0
# used to record the time at which we processed current frame
new_frame_time = 0
# Reading the video file until finished
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
# if video finished or no Video Input
if not ret:
break
model.predict(source=frame,show=False)
# time when we finish processing for this frame
new_frame_time = time.time()
# Calculating the fps
# fps will be number of frame processed in given time frame
# since their will be most of time error of 0.001 second
# we will be subtracting it to get more accurate result
fps = 1/(new_frame_time-prev_frame_time)
prev_frame_time = new_frame_time
# converting the fps into integer
fps = int(fps)
# converting the fps to string so that we can display it on frame
# by using putText function
fps = str(fps)
print(fps)
# When everything done, release the capture
cap.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment