Training failure + Next steps

Since the way I was training wasn’t working, I’ve opted to use the model I had going as a means to get coordinates, height and width to automatically crop each perk from each screenshot.

I’ll be able to take the upscaled 640×640 images and upload them to roboflow. It’ll be a manual process for me to go back through and label them one at a time as to what the perks are, but I should be left with a very good size dataset when I’m done. It’ll no doubt be ten hours of labor so it’ll be a few days before i can even download them to run through the model as a test. Unfortunate as that may be, I think that’s going to end up being the proper next steps.

Below is my python application which allows me to drag and drop my screenshots into the watched folder and it’ll spit out upscaled cropped images of each perk. There’s duplicates, but in this case I think having the duplicates works in my favor actually. In practice, I think I’ll go back through my old model and change every tag to just “Perk” – This will allow me to use that model just as a perk detector on its own.

import os
import cv2
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import subprocess
import os
import re
from datetime import datetime


def find_most_recent_exp_dir(base_dir='runs/detect'):
    """
    Finds the most recent experiment directory based on naming convention (exp, exp1, exp2, ...).

    Parameters:
    - base_dir (str): Base directory where the experiment folders are located.

    Returns:
    - str: Path to the most recent experiment directory.
    """
    exp_dirs = [d for d in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, d))]
    # Filter directories that match the 'exp' naming pattern
    exp_dirs = [d for d in exp_dirs if re.match(r'^exp\d*$', d)]

    # Sort directories by their numerical suffix (or 0 if no suffix)
    exp_dirs.sort(key=lambda d: int(re.search(r'\d+$', d).group()) if re.search(r'\d+$', d) else 0)

    if exp_dirs:
        return os.path.join(base_dir, exp_dirs[-1])
    else:
        return None


class DetectionHandler(FileSystemEventHandler):

    def on_created(self, event):
        if event.is_directory:
            return
        if event.src_path.endswith('.png') or event.src_path.endswith('.jpg'):
            print(f"Detected new image for processing: {event.src_path}")
            self.process_image(event.src_path)

    def process_image(self, image_path):
        # Run YOLOv7 detect.py on the image
        subprocess.run(
            ['python', 'detect.py', '--weights', './runs/train/exp17/weights/best.pt', '--conf', '0.03', '--img-size', '640',
             '--source', image_path, '--save-txt'])

        # Find the most recent 'exp' directory
        most_recent_exp_dir = find_most_recent_exp_dir()

        if most_recent_exp_dir:
            # Construct the path to the .txt file based on the most recent exp directory
            image_name = os.path.basename(image_path)
            txt_path = os.path.join(most_recent_exp_dir, 'labels', image_name.replace('.jpg', '.txt').replace('.png', '.txt'))

            if os.path.exists(txt_path):
                img = cv2.imread(image_path)
                with open(txt_path, 'r') as f:
                    lines = f.readlines()
                    crops_path = 'C:/Users/Administrator/PycharmProjects/pythonProject4/yolov7/images_to_process'
                    crops_dir = os.path.join(crops_path, 'crops')

                    os.makedirs(crops_dir, exist_ok=True)
                    for i, line in enumerate(lines):
                        parts = line.strip().split()
                        x_center, y_center, width, height = map(float, parts[1:5])
                        # Convert YOLO format to bounding box coordinates
                        img_h, img_w, _ = img.shape
                        x1, y1, x2, y2 = int((x_center - width / 2) * img_w), int((y_center - height / 2) * img_h), int((x_center + width / 2) * img_w), int((y_center + height / 2) * img_h)
                        crop_img = img[y1:y2, x1:x2]

                        # Resize the cropped image to 640x640
                        resized_crop_img = cv2.resize(crop_img, (640, 640), interpolation=cv2.INTER_CUBIC)

                        # Get the current date and time for filename
                        now = datetime.now()  # Current date and time
                        date_time = now.strftime("%Y%m%d_%H%M%S")  # Format as a string

                        # Save the cropped image in the crops directory
                        crop_img_path = os.path.join(crops_dir, f"{date_time}_detected_{i}_{image_name}")
                        cv2.imwrite(crop_img_path, resized_crop_img)
                        print(f"Saved cropped image: {crop_img_path}")


if __name__ == "__main__":
    path = 'C:/Users/Administrator/PycharmProjects/pythonProject4/yolov7/images_to_process'
    event_handler = DetectionHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=False)
    observer.start()
    try:
        while True:
            pass
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

Similar Posts