0

It is working great for yolov5 but i want to upgrade to yolov8

How can I do that?

import cv2
import torch
import os
from PIL import Image

# Load the pre-trained YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5x')

# Aspect ratios to consider
aspect_ratios = [(1024, 1280)]

def auto_zoom(input_dir, output_base_dir):
    # Loop over all files in the input directory
    for filename in os.listdir(input_dir):
        # Create the full input path and read the file
        input_path = os.path.join(input_dir, filename)
        img = cv2.imread(input_path)

        if img is None:
            continue

        # Run the image through the model
        results = model(img)

        # Find the first human detected in the image
        human = next((x for x in results.xyxy[0] if int(x[5]) == 0), None)

        if human is None:
            print(f"No human detected in the image {input_path}.")
            os.remove(input_path)
            continue

        # Crop the image to the bounding box of the human
        x1, y1, x2, y2 = map(int, human[:4])
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

1 Answers1

0

You can load yolov8 models using Ultralytics library:

!pip install ultralytics
from ultralytics import YOLO
model = YOLO('yolov8x.pt')

To reach detected boxes with conf and class_id:

human = next((x for x in results[0].boxes.data if int(x[5]) == 0), None)

Ultralytics quickstart here. More about working with results: https://docs.ultralytics.com/modes/predict/#working-with-results

hanna_liavoshka
  • 115
  • 1
  • 6