0

I trained a yolo v8 according to the video posted on roboflow but now I want to create a new version using other objects, however, I am not able to "delete" the previous yolo. Every time I run my program it goes back to using the previous yolo. I would like a python program similar to the one I'm going to pass from which delete the previous yolo and use a new one, with new arguments and datasets.

import cv2
import argparse
from ultralytics import YOLO

import supervision as sv
import numpy as np

ZONE_POLYGON = np.array([
    [0, 0],
    [0.5, 0],
    [0.5, 1],
    [0, 1]
])

def parse_arguments() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="YOLOv8 live")
    parser.add_argument(
        "--webcam-resolution",
        default=[640, 480],
        nargs=2,
        type=int
    )
    args = parser.parse_args()
    return args

def main():
    # Change camera settings
    args = parse_arguments()
    frame_width, frame_height = args.webcam_resolution
    
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)
    
    # YOLO File
    
    model = YOLO("yolov8s.pt")
    
    box_annotator = sv.BoxAnnotator(
        thickness=2,
        text_thickness=2,
        text_scale=1
    )
    
    zone_polygon = (ZONE_POLYGON * np.array(args.webcam_resolution)).astype(int)
    zone = sv.PolygonZone(polygon=zone_polygon, frame_resolution_wh=tuple(args.webcam_resolution))
    zone_annotator = sv.PolygonZoneAnnotator(
        zone=zone, 
        color=sv.Color.red(),
        thickness=2,
        text_thickness=4,
        text_scale=2
    )
    
    while True:
        ret, frame = cap.read()
        if not ret:
            print("Error capturing frame")
            break
        
        result = model(frame, agnostic_nms=False)[0]
        detections = sv.Detections.from_yolov8(result)
        
        labels = [
            f"{model.model.names.get(class_id, 'Unknown')} {confidence:0.2f}"
            for _, confidence, class_id, _, _ in detections
            if class_id in model.model.names and model.model.names[class_id] == "cellphone"
        ]
        
        frame = box_annotator.annotate(
            scene=frame,
            detections=detections,
            labels=labels
        )
        
        zone.trigger(detections=detections)
        frame = zone_annotator.annotate(scene=frame)
        
        cv2.imshow("Yolov8", frame)
        
        if cv2.waitKey(1) == 27:
            break
    
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

I tried to resolve my problem deleting all files but didn't succeed

1 Answers1

0

The model file script will use is defined here:

model = YOLO("yolov8s.pt")

"yolov8s.pt" is one of the default pre-trained models YOLO has. If you don't have this model in your system (or deleted it) but have assigned this file as your model object, YOLO will automatically download this file and use it in the script.

To use your own trained model instead, define a model object by the path to the best.pt file generated by the training process (somewhere in runs/project name/experiment name/weights/best.pt)

To use another pre-trained default YOLO models choose their names from here: https://docs.ultralytics.com/models/yolov8/#supported-modes

hanna_liavoshka
  • 115
  • 1
  • 6