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__)))

MAX_EPISODE_LENGTH = 1000
FRAMES_DIRECTORY = r'C:\Users\Ninja.000\Downloads\SFD_AI\gameplay\Tutorial'
ANNOTATIONS_DIRECTORY = r'C:\Users\Ninja.000\Downloads\txtanote'

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)))
    print("Captured frame:", frame.shape)  # Check if frames are being captured
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)  # Convert to BGR for OpenCV

    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, "Local Player", (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 load_annotations(frame_path):
    annotation_path = os.path.join(ANNOTATIONS_DIRECTORY, f'frame_{os.path.splitext(os.path.basename(frame_path))[0]}.txt')
    if os.path.exists(annotation_path):
        with open(annotation_path, 'r') as f:
            lines = f.readlines()
        annotations = [list(map(float, line.strip().split())) for line in lines]
        return annotations
    else:
        return []

def process_frame(frame, annotations, model, default_class='local_player', confidence_threshold=0.5):
    # Convert frame to format expected by YOLOv5
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = model(frame)

    local_player_detected = False
    enemy_detected = False

    local_player_box = None
    enemy_box = None

    for box in results.xyxy[0].cpu().numpy():
        confidence = box[4]
        if confidence >= confidence_threshold:
            label = model.names[int(box[5])]
            box = box[:4]

            print("Model Prediction:", label, confidence, box)  # Print model predictions

            # Assuming YOLO format annotations: [class, x_center, y_center, width, height]
            for annotation in annotations:
                if len(annotation) == 5:  # Check if class label is present in the annotation
                    annotation_label = int(annotation[0])
                else:
                    annotation_label = default_class

                if annotation_label == -1 and 'local_player' in label:
                    local_player_detected = True
                    local_player_box = annotation[1:]
                elif annotation_label == -1 and 'enemy' in label:  # You can use a different condition for the enemy class
                    enemy_detected = True
                    enemy_box = annotation[1:]

    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:v7.0', 'yolov5s', force_reload=True)

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

            frame_path = os.path.join(FRAMES_DIRECTORY, f'frame_{t}.jpg')

            if os.path.exists(frame_path):
                frame = cv2.imread(frame_path)
                annotations = load_annotations(frame_path)
                local_player_box, enemy_box = process_frame(frame, annotations, model)
                draw_boxes(game_window, local_player_box, enemy_box)

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

                elapsed_time = time.time() - start_time
                # Introduce a delay to limit CPU usage
                time.sleep(max(0, 0.0333 - elapsed_time))
            else:
                print(f"Frame {t} not found. Skipping to the next frame.")

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

    except KeyboardInterrupt:
        print("Script interrupted by user. Exiting...")
    finally:
        # Clean up OpenCV windows
        cv2.destroyAllWindows()

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.")



(venv) C:\WINDOWS\system32>python visualizeStuff.py
Downloading: "https://github.com/ultralytics/yolov5/zipball/v7.0" to C:\Users\Ninja.000/.cache\torch\hub\v7.0.zip
YOLOv5  2024-1-2 Python-3.11.7 torch-2.1.2+cpu CPU

Fusing layers...
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients, 16.4 GFLOPs
Adding AutoShape...
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Model Prediction: traffic light 0.5079408 [     525.88      196.32      596.27      318.48]
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Model Prediction: traffic light 0.51428753 [     489.03      139.27       542.3      235.41]
Captured frame: (663, 816, 3)
Model Prediction: traffic light 0.5082578 [      482.9      143.71      541.29      242.76]
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Model Prediction: traffic light 0.5196799 [     493.07      149.34      527.56       205.4]
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Captured frame: (663, 816, 3)
Script interrupted by user. Exiting...


It's not showing any drawn boxes why?