(venv) C:\WINDOWS\system32>python test.py
Training the model...
Traceback (most recent call last):
  File "C:\WINDOWS\system32\test.py", line 160, in <module>
    main()
  File "C:\WINDOWS\system32\test.py", line 144, in main
    train(model, train_loader, criterion, optimizer)
  File "C:\WINDOWS\system32\test.py", line 52, in train
    images = preprocess_image(image_path)  # Remove [0] since train_loader now provides the path directly
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\WINDOWS\system32\test.py", line 34, in preprocess_image
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Can't convert object to 'str' for 'filename'



import os
import cv2
import torch
import torch.nn as nn
import torch.optim as optim
import random
import numpy as np
import torchvision.transforms as transforms
import pygetwindow as gw
import pydirectinput
import time

# Define the CNN model
class SimpleCNN(nn.Module):
    def __init__(self, output_size):
        super(SimpleCNN, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=2)
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=2)
        self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=2)
        self.fc1 = nn.Linear(64 * 6 * 6, 128)
        self.fc2 = nn.Linear(128, output_size)

    def forward(self, x):
        x = torch.relu(self.conv1(x))
        x = torch.relu(self.conv2(x))
        x = torch.relu(self.conv3(x))
        x = x.view(x.size(0), -1)
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        return x

# Image preprocessing
def preprocess_image(image_path):
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    image = cv2.resize(image, (84, 84))  # Resize to a consistent size
    image = image / 255.0  # Normalize pixel values to [0, 1]
    image = torch.from_numpy(image).float().unsqueeze(0).unsqueeze(0)  # Add batch and channel dimensions
    return image

def load_dataset(data_folder):
    dataset = []
    for root, dirs, files in os.walk(data_folder):
        for file in files:
            image_path = os.path.join(root, file)
            dataset.append((image_path,))
    return dataset

# Training function
def train(model, train_loader, criterion, optimizer, epochs=5):
    for epoch in range(epochs):
        for image_path in train_loader:
            images = preprocess_image(image_path)  # Remove [0] since train_loader now provides the path directly
            labels = torch.randint(0, 4, (1,))
            optimizer.zero_grad()
            outputs = model(images)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

# Testing function
def test(model, test_loader):
    for image_path in test_loader:
        image = preprocess_image(image_path)
        output = model(image)
        predicted_action = torch.argmax(output).item()
        print(f"Predicted Action: {predicted_action} for Image: {image_path}")
        simulate_action(predicted_action)

# Function to check if the game is open
def is_game_open():
    game_window = gw.getWindowsWithTitle('Superfighters Deluxe')
    return bool(game_window)

# Function to simulate actions based on the agent's output
def simulate_action(action):
    if action == 0:
        # Randomly choose between Jump / ClimbLadder and Move to the Left or Move to the Right
        if np.random.rand() < 0.5:
            pydirectinput.keyDown('up')  
            pydirectinput.keyDown('left')
            print("Simulating action: Jump / ClimbLadder and Move to the Left")
        else:
            pydirectinput.keyDown('up')
            pydirectinput.keyDown('right')
            print("Simulating action: Jump / ClimbLadder and Move to the Right")
    elif action == 1:
        pydirectinput.keyDown('down')  # Crouch / SlideDownLadder
        print("Simulating action: Crouch / SlideDownLadder")
    elif action == 2:
        # Move to the left
        pydirectinput.keyDown('left')
        print("Simulating action: Move to the left")
    elif action == 3:
        # Move to the right
        pydirectinput.keyDown('right')
        print("Simulating action: Move to the right")

    time.sleep(0.1)

    keys_to_release = ['up', 'down', 'left', 'right', 'd', 'a', 's', 'shift']
    keys_to_release.extend(map(str, range(1, 6)))
    keys_to_release.extend(['left', 'right', 'down', 'f'])

    for key in keys_to_release:
        pydirectinput.keyUp(key)

# Main function
def main():
    data_folder = r"C:\Users\Ninja.000\Downloads\SFD_AI\gameplay\Tutorial"
    output_size = 4  # Assuming there are 4 possible actions

    # Create a simple CNN model
    model = SimpleCNN(output_size)

    # Specify the path to save the trained model
    model_path = r"C:\Users\Ninja.000\Downloads\SFD_AI\trained_model.pth"

    # Check if the trained model file exists
    if os.path.exists(model_path):
        # Load the trained model
        model.load_state_dict(torch.load(model_path))
        model.eval()
    else:
        # Train the model if the file doesn't exist
        print("Training the model...")
        # Load the dataset
        dataset = load_dataset(data_folder)

        # Define the loss function and optimizer
        criterion = nn.CrossEntropyLoss()
        optimizer = optim.Adam(model.parameters(), lr=0.001)

        # Split the dataset into training and testing sets
        random.shuffle(dataset)
        split_index = int(0.8 * len(dataset))
        train_dataset = dataset[:split_index]
        test_dataset = dataset[split_index:]

        # Create data loaders
        train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True)
        test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False)

        # Train the model
        train(model, train_loader, criterion, optimizer)

        # Test the model
        test(model, test_loader)

        # Save the trained model
        torch.save(model.state_dict(), model_path)
        print(f"Trained model saved at: {model_path}")

    # List all images in the dataset folder
    dataset = [os.path.join(data_folder, f) for f in os.listdir(data_folder) if f.endswith('.jpg')]

    # Test the model on the dataset
    test(model, dataset)

if __name__ == "__main__":
    main()