(venv) C:\WINDOWS\system32>python visualizeStuff.py
Downloading: "https://github.com/ultralytics/yolov5/zipball/v5.0" to C:\Users\Ninja.000/.cache\torch\hub\v5.0.zip

                 from  n    params  module                                  arguments
  0                -1  1      3520  models.common.Focus                     [3, 32, 3]
  1                -1  1     18560  models.common.Conv                      [32, 64, 3, 2]
  2                -1  1     18816  models.common.C3                        [64, 64, 1]
  3                -1  1     73984  models.common.Conv                      [64, 128, 3, 2]
  4                -1  1    156928  models.common.C3                        [128, 128, 3]
  5                -1  1    295424  models.common.Conv                      [128, 256, 3, 2]
  6                -1  1    625152  models.common.C3                        [256, 256, 3]
  7                -1  1   1180672  models.common.Conv                      [256, 512, 3, 2]
  8                -1  1    656896  models.common.SPP                       [512, 512, [5, 9, 13]]
  9                -1  1   1182720  models.common.C3                        [512, 512, 1, False]
 10                -1  1    131584  models.common.Conv                      [512, 256, 1, 1]
 11                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']
 12           [-1, 6]  1         0  models.common.Concat                    [1]
 13                -1  1    361984  models.common.C3                        [512, 256, 1, False]
 14                -1  1     33024  models.common.Conv                      [256, 128, 1, 1]
 15                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']
 16           [-1, 4]  1         0  models.common.Concat                    [1]
 17                -1  1     90880  models.common.C3                        [256, 128, 1, False]
 18                -1  1    147712  models.common.Conv                      [128, 128, 3, 2]
 19          [-1, 14]  1         0  models.common.Concat                    [1]
 20                -1  1    296448  models.common.C3                        [256, 256, 1, False]
 21                -1  1    590336  models.common.Conv                      [256, 256, 3, 2]
 22          [-1, 10]  1         0  models.common.Concat                    [1]
 23                -1  1   1182720  models.common.C3                        [512, 512, 1, False]
 24      [17, 20, 23]  1    229245  models.yolo.Detect                      [80, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]
C:\WINDOWS\system32\venv\Lib\site-packages\torch\functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ..\aten\src\ATen\native\TensorShape.cpp:3527.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
Model Summary: 283 layers, 7276605 parameters, 7276605 gradients, 17.2 GFLOPS

Traceback (most recent call last):
  File "C:\Users\Ninja.000/.cache\torch\hub\ultralytics_yolov5_v5.0\hubconf.py", line 40, in create
    ckpt = torch.load(fname, map_location=torch.device('cpu'))  # load
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\WINDOWS\system32\venv\Lib\site-packages\torch\serialization.py", line 1014, in load
    return _load(opened_zipfile,
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\WINDOWS\system32\venv\Lib\site-packages\torch\serialization.py", line 1422, in _load
    result = unpickler.load()
             ^^^^^^^^^^^^^^^^
  File "C:\WINDOWS\system32\venv\Lib\site-packages\torch\serialization.py", line 1415, in find_class
    return super().find_class(mod_name, name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Can't get attribute 'SPPF' on <module 'models.common' from 'C:\\Users\\Ninja.000/.cache\\torch\\hub\\ultralytics_yolov5_v5.0\\models\\common.py'>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\WINDOWS\system32\visualizeStuff.py", line 97, in <module>
    visualize_and_process_frames()
  File "C:\WINDOWS\system32\visualizeStuff.py", line 70, in visualize_and_process_frames
    model = torch.hub.load('ultralytics/yolov5:v5.0', 'yolov5s', force_reload=True)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\WINDOWS\system32\venv\Lib\site-packages\torch\hub.py", line 566, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\WINDOWS\system32\venv\Lib\site-packages\torch\hub.py", line 595, in _load_local
    model = entry(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ninja.000/.cache\torch\hub\ultralytics_yolov5_v5.0\hubconf.py", line 84, in yolov5s
    return create('yolov5s', pretrained, channels, classes, autoshape)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ninja.000/.cache\torch\hub\ultralytics_yolov5_v5.0\hubconf.py", line 55, in create
    raise Exception(s) from e
Exception: Cache maybe be out of date, try force_reload=True. See https://github.com/ultralytics/yolov5/issues/36 for help.


import time
import os
import cv2
import pygetwindow as gw
from PIL import ImageGrab
import numpy as np
import torch

# Change the current working directory to the script's directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))

# Add this line before the torch.hub.load line
torch.hub._validate_not_a_forked_repo = lambda a, b, c: None

MAX_EPISODE_LENGTH = 1000

def is_game_open():
    game_window = gw.getWindowsWithTitle('Superfighters Deluxe')
    return bool(game_window)

def draw_boxes(window, local_player_box, enemy_box):
    frame = np.array(ImageGrab.grab(bbox=(window.left, window.top, window.right, window.bottom)))

    color = (0, 255, 0)  # Green color for bounding boxes (you can adjust this)

    if local_player_box is not None:
        x_lp, y_lp, w_lp, h_lp = local_player_box
        x_lp, y_lp, w_lp, h_lp = int(x_lp), int(y_lp), int(w_lp), int(h_lp)  # Convert to integers
        cv2.rectangle(frame, (x_lp, y_lp), (x_lp + w_lp, y_lp + h_lp), color, 2)
        cv2.putText(frame, "localPlayer", (x_lp, y_lp - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

    if enemy_box is not None:
        x_enemy, y_enemy, w_enemy, h_enemy = enemy_box
        x_enemy, y_enemy, w_enemy, h_enemy = int(x_enemy), int(y_enemy), int(w_enemy), int(h_enemy)  # Convert to integers
        cv2.rectangle(frame, (x_enemy, y_enemy), (x_enemy + w_enemy, y_enemy + h_enemy), color, 2)
        cv2.putText(frame, "enemy", (x_enemy, y_enemy - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

    cv2.imshow("Annotated Frame", frame)
    cv2.waitKey(1)

def process_frame(frame, model):
    # Convert frame to format expected by YOLOv5
    frame = frame[:, :, ::-1]  # BGR to RGB
    results = model(frame)

    local_player_detected = False
    enemy_detected = False

    for box in results.xyxy[0].cpu().numpy():
        class_id = int(box[5])
        box = box[:4]

        if class_id == 0:  # Assuming class 0 is localPlayer
            local_player_detected = True
            local_player_box = box
        elif class_id == 1:  # Assuming class 1 is enemy
            enemy_detected = True
            enemy_box = box

    return local_player_box if local_player_detected else None, enemy_box if enemy_detected else None

def visualize_and_process_frames():
    try:
        game_window = gw.getWindowsWithTitle('Superfighters Deluxe')[0]

        local_player_box = None
        enemy_box = None

        # Load the YOLOv5 model outside the loop
        model = torch.hub.load('ultralytics/yolov5:v5.0', 'yolov5s', force_reload=True)
        model = attempt_load('yolov5s.pt', map_location='cuda')

        for t in range(6, MAX_EPISODE_LENGTH, 6):
            start_time = time.time()

            frame = np.array(ImageGrab.grab(bbox=(game_window.left, game_window.top, game_window.right, game_window.bottom)))
            local_player_box, enemy_box = process_frame(frame, model)
            draw_boxes(game_window, local_player_box, enemy_box)

            if local_player_box is not None:
                print("Local player detected!")

            if enemy_box is not None:
                print("Enemy detected!")

            elapsed_time = time.time() - start_time
            # Introduce a delay to limit CPU usage
            time.sleep(max(0, 0.0333 - elapsed_time))

        print("Done watching all frames. Press Ctrl + C to exit.")

    except KeyboardInterrupt:
        print("Script interrupted by user. Exiting...")

if __name__ == "__main__":
    if is_game_open():
        visualize_and_process_frames()
    else:
        print("Superfighters Deluxe is not open. Please open the game and try again.")