0% found this document useful (0 votes)
13 views3 pages

A Starting Guide On Python

Helped me learn python really fast

Uploaded by

amabdulrahman512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

A Starting Guide On Python

Helped me learn python really fast

Uploaded by

amabdulrahman512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import cv2

import numpy as np
import pyautogui
import time
import keyboard

def capture_screen():
screen = pyautogui.screenshot()
frame = np.array(screen)
frame = cv2.cvtColor(frame,
cv2.COLOR_BGR2RGB)
return frame

def detect_heads(frame):
hsv = cv2.cvtColor(frame,
cv2.COLOR_BGR2HSV)

# Define the color range for red


(in HSV)
lower_color = np.array([0, 100,
100]) # Lower bound for red
upper_color = np.array([10, 255,
255]) # Upper bound for red
lower_color2 = np.array([160,
100, 100]) # Lower bound for red
(for the other end of the spectrum)
upper_color2 = np.array([180,
255, 255]) # Upper bound for red
(for the other end of the spectrum)

mask1 = cv2.inRange(hsv,
lower_color, upper_color)
mask2 = cv2.inRange(hsv,
lower_color2, upper_color2)
mask = mask1 | mask2 #
Combine both masks for full red
coverage

contours, _ =
cv2.findContours(mask,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
heads = []

for contour in contours:


if cv2.contourArea(contour) >
100:
x, y, w, h =
cv2.boundingRect(contour)
center_x = x + w // 2
center_y = y + h // 2
heads.append((center_x,
center_y))

return heads

def aim_at(head):
target_x, target_y = head
pyautogui.moveTo(target_x,
target_y)

def main():
aiming = False

while True:
if keyboard.is_pressed('a'):
aiming = not aiming
time.sleep(0.5)

if aiming:
frame = capture_screen()
heads =
detect_heads(frame)

if heads:
aim_at(heads[0])

for head in heads:


cv2.circle(frame, head, 5,
(0, 255, 0), -1)

cv2.imshow("Detected
Heads", frame)

if cv2.waitKey(1) & 0xFF ==


ord('q'):
break

time.sleep(0.01)

cv2.destroyAllWindows()

if __name__ == "__main__":
main()

You might also like