Skip to content

Instantly share code, notes, and snippets.

@Merwanski
Created April 2, 2024 16:37
Show Gist options
  • Save Merwanski/5440b151325dd25faf385b04487a16c0 to your computer and use it in GitHub Desktop.
Save Merwanski/5440b151325dd25faf385b04487a16c0 to your computer and use it in GitHub Desktop.
YOLOv8 time inference script
from ultralytics import YOLO
import time
import cv2
import math
image = cv2.imread('bus.jpg')
model = YOLO('yolov8n.pt') # pretrained YOLOv8n model
# object classes
classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
"baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
"fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
"carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
"diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
"microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
"teddy bear", "hair drier", "toothbrush"
]
for i in range(10):
print("******************** Iteration ********************")
print(i)
t0 = time.time()
results = model(image, stream=True)
t1 = time.time()
print(f"Time: {(t1-t0)} and frequency of inference: {1/(t1-t0)} Hz")
# coordinates
for r in results:
boxes = r.boxes
for box in boxes:
# bounding box
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
# confidence
confidence = math.ceil((box.conf[0]*100))/100
print("Confidence --->",confidence)
# class name
cls = int(box.cls[0])
print("Class name -->", classNames[cls])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment