Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pip install tensorflow tensorflow-hub numpy pyautogui opencv-python pywin32
- import tensorflow as tf
- import tensorflow_hub as hub
- import numpy as np
- import pyautogui
- import win32api, win32con, win32gui
- import cv2
- import math
- import time
- # --- Aimbot Configuration ---
- # Load Detection model from TensorFlow Hub. [1]
- # The specific model used is 'centernet/resnet50v1_fpn_512x512/1' which is an image object detection model. [1]
- detector = hub.load("https://tfhub.dev/tensorflow/centernet/resnet50v1_fpn_512x512/1")
- # This scale factor is used to resize the screenshot for faster processing on less powerful hardware. [1]
- # The video indicates a size_scale of 3 was used to resize to 1/3 of the original image. [1]
- size_scale = 3
- # Main loop for the aimbot to continuously run
- while True:
- # 1. Get rect of Window
- # Find the game window by its title. [1]
- # Changed to target "Google Chrome" as requested.
- hwnd = win32gui.FindWindow(None, 'Google Chrome')
- # If the game window is not found, print a message and continue to the next iteration.
- if hwnd == 0:
- print("Google Chrome window not found. Make sure Chrome is running and the window title is correct.")
- time.sleep(1) # Wait a bit before retrying
- continue
- # Get the bounding box coordinates (left, top, right, bottom) of the game window. [1]
- rect = win32gui.GetWindowRect(hwnd)
- # Convert the rectangle to (x, y, width, height) format for pyautogui.screenshot. [1]
- region = rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1]
- # 2. Get a screen and pre-process
- # Take a screenshot of the specified game window region. [1]
- # The output is a NumPy array representing the image. [1]
- ori_img = np.array(pyautogui.screenshot(region=region))
- # Resize the image to match the `size_scale` for faster processing. [1]
- # The model expects specific input dimensions. [1]
- ori_img = cv2.resize(ori_img, (ori_img.shape[1] // size_scale, ori_img.shape[0] // size_scale))
- # Add a batch dimension to the image, as the TensorFlow model expects input shape (1, H, W, 3). [1]
- image = np.expand_dims(ori_img, 0)
- # Store the width and height of the processed image for later calculations. [1]
- img_w, img_h = image.shape[2], image.shape[1]
- # 3. Detection and calculate distance
- # Pass the image to the loaded detection model. [1]
- result = detector(image)
- # Convert TensorFlow tensors in the result to NumPy arrays. [1]
- result = {key:value.numpy() for key,value in result.items()}
- # Extract detection boxes, scores, and classes. [1]
- boxes = result['detection_boxes'][0]
- scores = result['detection_scores'][0]
- classes = result['detection_classes'][0]
- detected_boxes = []
- # Iterate through each detected object. [1]
- for i, box in enumerate(boxes):
- # The COCO dataset, which this model is trained on, labels 'person' as class 1. [1]
- # Filter detections to only include 'persons' with a confidence score of 0.5 or higher. [1]
- if classes[i] == 1 and scores[i] >= 0.5:
- ymin, xmin, ymax, xmax = tuple(box)
- # Custom condition to ignore detections in the bottom part of the screen. [1]
- # This is to prevent detecting the player's own weapon as a human. [1]
- # These values (0.5 and 0.8) are specific to CS:GO in the video and may need adjustment. [1]
- if ymin > 0.5 and ymax > 0.8:
- continue
- # Convert normalized bounding box coordinates to pixel coordinates relative to the image. [1]
- # The result is the portion of the image. So you need to multiply width and height. [1]
- left = int(xmin * img_w)
- right = int(xmax * img_w)
- top = int(ymin * img_h)
- bottom = int(ymax * img_h)
- detected_boxes.append((left, right, top, bottom))
- # Optional: Draw rectangles on the image for debugging and visualization.
- # cv2.rectangle(ori_img, (left, top), (right, bottom), (255, 255, 0), 2)
- # Check if any objects were detected. [1]
- if len(detected_boxes) >= 1:
- min_dist = 99999 # Initialize minimum distance to a very high value. [1]
- closest_idx = 0 # Index of the closest detected object. [1]
- centers = [] # To store the center coordinates of detected boxes. [1]
- # Calculate the center of each detected bounding box and its distance from the screen's crosshair (center). [1]
- for i, box in enumerate(detected_boxes):
- x1, y1, x2, y2 = box
- c_x = ((x2 - x1) / 2) + x1 # Center X coordinate
- c_y = ((y2 - y1) / 2) + y1 # Center Y coordinate
- centers.append((c_x, c_y))
- # Calculate the Euclidean distance from the image center (crosshair) to the object's center. [1]
- dist = math.sqrt(math.pow(img_w/2 - c_x, 2) + math.pow(img_h/2 - c_y, 2))
- # Update the closest object if a closer one is found. [1]
- if dist < min_dist:
- min_dist = dist
- closest_idx = i
- # 4. Move mouse and shoot
- # Calculate the pixel difference in X and Y from the crosshair to the target's center. [1]
- x_diff = centers[closest_idx][0] - img_w/2
- # For Y, an offset (0.45 of box height) is added to aim for the head. This value is empirical. [1]
- y_diff = centers[closest_idx][1] - img_h/2 - (detected_boxes[closest_idx][3] - detected_boxes[closest_idx][2]) * 0.45
- # Scale the pixel difference to match in-game mouse sensitivity. [1]
- # This '1.7' value is derived from trial and error in the video for the specific game/setup. [1]
- # It's multiplied by 'size_scale' because the image was downscaled, and we need to compensate. [1]
- mouse_scale = 1.7 * size_scale
- scaled_x = int(x_diff * mouse_scale)
- scaled_y = int(y_diff * mouse_scale)
- # Use win32api to move the mouse relative to its current position. [1]
- # MOUSEEVENTF_MOVE is used for movement. [1]
- win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, scaled_x, scaled_y, 0, 0)
- time.sleep(0.05) # Short delay after moving the mouse. [1]
- # Simulate a left mouse button press (shoot). [1]
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, scaled_x, scaled_y, 0, 0)
- time.sleep(0.1) # Hold the mouse button down for a brief moment. [1]
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, scaled_x, scaled_y, 0, 0) # Release the mouse button. [1]
- # Optional: Display the processed image with detections (uncomment if you want to see the visual output)
- # This requires an OpenCV window to be set up, which might interfere with game performance.
- # cv2.imshow("Detection Output", cv2.cvtColor(ori_img, cv2.COLOR_BGR2RGB))
- # cv2.waitKey(1) # Wait for 1ms for a key event (keeps the window responsive)
- time.sleep(0.1) # General delay for the loop to prevent excessive CPU usage. [1]
Advertisement