Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # aim_server.py (Version 4 - Final Fix)
- # This script runs on your more powerful Windows PC.
- import cv2
- import numpy as np
- import base64
- from flask import Flask
- from flask_socketio import SocketIO
- from ultralytics import YOLO
- import os
- # --- Configuration ---
- # Make sure to set your IP here.
- SERVER_HOST = 'YOUR_WINDOWS_LAPTOP_IP' # e.g., '192.168.1.15'
- SERVER_PORT = 5000
- CONFIDENCE_THRESHOLD = 0.4
- # --- AI Model Setup ---
- print("Loading AI model...")
- try:
- # This loads your custom fortnite model, which you've named 'yolov8n.pt'
- model = YOLO('yolov8n.pt')
- print("AI Model loaded successfully.")
- except Exception as e:
- print(f"Error loading model: {e}")
- exit()
- # --- Server Setup ---
- app = Flask(__name__)
- socketio = SocketIO(app, async_mode='threading', cors_allowed_origins="*")
- # --- Image Processing Function ---
- def process_frame(data_url):
- try:
- encoded_data = data_url.split(',')[1]
- nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
- frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
- if frame is None: return None
- results = model(frame, classes=[0], verbose=False) # Assumes your player class is 0
- best_target = None
- highest_conf = 0
- for result in results:
- for box in result.boxes:
- conf = box.conf[0].item()
- if conf > CONFIDENCE_THRESHOLD and conf > highest_conf:
- highest_conf = conf
- xyxy = box.xyxy[0].cpu().numpy()
- center_x = int((xyxy[0] + xyxy[2]) / 2)
- center_y = int((xyxy[1] + xyxy[3]) / 2)
- best_target = {'x': center_x, 'y': center_y}
- return best_target
- except Exception:
- return None
- # --- WebSocket Event Handlers ---
- @socketio.on('connect')
- def handle_connect():
- print(f"Client connected securely!")
- @socketio.on('disconnect')
- def handle_disconnect():
- print("Client disconnected.")
- @socketio.on('frame')
- def handle_frame(data_url):
- target = process_frame(data_url)
- if target:
- socketio.emit('aim_target', target)
- # --- Main Entry Point ---
- if __name__ == '__main__':
- if SERVER_HOST == 'YOUR_WINDOWS_LAPTOP_IP':
- print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
- print("!!! ERROR: You must edit the SERVER_HOST variable in !!!")
- print("!!! this script before running. !!!")
- print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
- elif not os.path.exists('cert.pem') or not os.path.exists('key.pem'):
- print("!!! ERROR: cert.pem or key.pem not found. Run generate_cert.py first! !!!")
- else:
- print(f"Starting secure server on https://{SERVER_HOST}:{SERVER_PORT}")
- # =======================================================================
- # === THIS IS THE FIX. We now use ssl_context instead of keyfile. ===
- # =======================================================================
- socketio.run(app, host=SERVER_HOST, port=SERVER_PORT, debug=False,
- ssl_context=('cert.pem', 'key.pem'))
Add Comment
Please, Sign In to add comment