Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ctypes
- import cv2
- import mss
- import numpy
- import os
- import signal
- import sys
- import time
- from libs import Mouse
- class Point(ctypes.Structure):
- _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
- class Yolo:
- def __init__(self, configuration, weights):
- # Control handles.
- self.mouse = Mouse()
- # Detection configuration.
- self.activation = 512
- self.confidence = 0.36
- self.threshold = 0.22
- # Load YOLO model.
- self.neural_network = cv2.dnn.readNetFromDarknet(configuration, weights)
- self.neural_network.setPreferableBackend(cv2.dnn.DNN_BACKEND_DEFAULT)
- self.neural_network.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL)
- # Select the required output layer names.
- self.layer_names = self.neural_network.getLayerNames()
- self.layer_names = [self.layer_names[i[0] - 1] for i in self.neural_network.getUnconnectedOutLayers()]
- # Capture configuration.
- self.sct = mss.mss()
- self.width, self.height = None, None
- self.screen_width, self.screen_height = ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1)
- self.bounding_box = (int(self.screen_width / 2 - self.activation / 2),
- int(self.screen_height / 2 - self.activation / 2),
- int(self.screen_width / 2 + self.activation / 2),
- int(self.screen_height / 2 + self.activation / 2))
- # Enable OpenCL if supported.
- if cv2.ocl.haveOpenCL():
- cv2.ocl.setUseOpenCL(True)
- cv2.ocl.useOpenCL()
- print("Using OpenCL.")
- else:
- print("No GPU acceleration.")
- def start(self):
- start_time = time.time()
- # YOLO loop.
- while True:
- # Grab frame, resize it, and convert it from RGBA to BGR to work with OpenCV.
- frame = numpy.array(self.sct.grab(self.bounding_box))
- frame = cv2.resize(frame, (self.activation, self.activation))
- frame = cv2.cvtColor(frame, cv2.COLOR_RGBA2BGR)
- # Get frame shape if None.
- if self.width is None or self.height is None:
- (self.height, self.width) = frame.shape[:2]
- # Convert frame to matrix object for GPU use.
- frame = cv2.UMat(frame)
- # Create a blob for the neural network.
- # Perform a forward pass using YOLO.
- # Recieve bounding boxes and associated probabilities.
- blob = cv2.dnn.blobFromImage(frame, 1 / 260.0, (416, 416), swapRB=False, crop=False)
- self.neural_network.setInput(blob)
- outputs = self.neural_network.forward(self.layer_names)
- # Intialise lists for detected bounding boxes, confidences, and class IDs.
- boxes = []
- confidences = []
- class_ids = []
- # Loop through the outputs from the neural network and parse detections.
- for output in outputs:
- for detection in output:
- # Class ID of 0 is equal to person. Extract this from our current detection.
- scores = detection[5:]
- class_id = 0
- confidence = scores[0]
- if confidence > self.confidence:
- # Scale the bounding box coordinates to the size of the frame.
- # YOLO returns the center coordinates followed by width and height.
- box = detection[0:4] * numpy.array([self.width, self.height, self.width, self.height])
- (x_center, y_center, w, h) = box.astype("int")
- # Using the bounding box coordinates, derive the top and left corner as y, x respectively.
- x = int(x_center - (w / 2))
- y = int(y_center - (h / 2))
- boxes.append([x, y, int(w), int(h)])
- confidences.append(float(confidence))
- class_ids.append(class_id)
- # Surpress weak, and overlapping bounding boxes using non-maxima surpression.
- found = cv2.dnn.NMSBoxes(boxes, confidences, self.confidence, self.threshold)
- if len(found) > 0:
- best_match = confidences[numpy.argmax(confidences)]
- skip = False
- # Check is the mouse is already on the target.
- for i in found.flatten():
- (x, y) = (boxes[i][0], boxes[i][1])
- (w, h) = (boxes[i][2], boxes[i][3])
- mouse_x, mouse_y = (self.bounding_box[0] + x + w / 2, self.bounding_box[1] + y + h / 8)
- current_mouse_x, current_mouse_y = self.mouse.get_position()
- if abs(mouse_x - current_mouse_x) < w * 2 and abs(mouse_y - current_mouse_y) < h * 2:
- skip = True
- if abs(mouse_x - current_mouse_x) > w * 0.5 or abs(mouse_y - current_mouse_y) > h * 0.5:
- self.mouse.move_mouse((mouse_x, mouse_y))
- # Loop over the indexes we are keeping.
- if not skip:
- for i in found.flatten():
- (x, y) = (boxes[i][0], boxes[i][1])
- (w, h) = (boxes[i][2], boxes[i][3])
- if best_match == confidences[i]:
- mouse_x = self.bounding_box[0] + x + w / 2
- mouse_y = self.bounding_box[1] + y + h / 8
- self.mouse.move_mouse((mouse_x, mouse_y))
- print("FPS: ", 1.0 / (time.time() - start_time))
- self.sct.close()
- cv2.destroyAllWindows()
- sys.exit(0)
- def main():
- aimbot = Yolo(os.path.sep.join(["models", "yolo-v3-tiny.cfg"]), os.path.sep.join(['models', "yolo-v3-tiny.weights"]))
- aimbot.start()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement