-2

I have a trained model and I have detected my required object using following code

import cv2
from PIL import Image
from ultralytics import YOLO

image = cv2.imread("screenshot.png")
model = YOLO('runs/detect/train4/weights/best.pt')
results = model.predict(image, show=True, stream=True, classes=0, imgsz=512)
for result in results:
    for box in result.boxes:
        class_id = result.names[box.cls[0].item()]
        if (class_id == "myclassname"): 
            cords = box.xyxy[0].tolist()
            cords = [round(x) for x in cords]
            conf = round(box.conf[0].item(), 2)
            print("Object type:", class_id)
            print("Coordinates:", cords)
            print("Probability:", conf)
            print("---")

From this detected portion of image I need to detect an other class how I can do that?

I have searched enough but I could not see any post for this.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
GNChishti
  • 39
  • 10

1 Answers1

0

Take the xyxy box coordinates ([x1 y1 x2 y2]), slice (crop) the box area from your original image: box_image = image[y1:y2, x1:x2], and make another prediction on this cropped image with the changed 'classes' parameter to the relevant class id: model.predict(box_image, classes=0).

hanna_liavoshka
  • 115
  • 1
  • 6