-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mechanicus_CAMERA TO_MOUSE_TRACKER copy 5.py
65 lines (48 loc) · 2.18 KB
/
Mechanicus_CAMERA TO_MOUSE_TRACKER copy 5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import cv2
import numpy as np
import pyautogui
# Initialize the video capture
cap = cv2.VideoCapture(1) # Use the appropriate video source (0 for the default camera)
while True:
# Read a frame from the video
ret, frame = cap.read()
if not ret:
continue
# Flip the frame horizontally to un-mirror it
frame = cv2.flip(frame, 1)
# Convert the frame to HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Define the lower and upper bounds for the green color (you can adjust these)
lower_green = np.array([35, 70, 70])
upper_green = np.array([90, 255, 255])
# Create a mask to isolate the green color
mask = cv2.inRange(hsv, lower_green, upper_green)
# Find contours of the green object in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Initialize variables for the green dot position
green_x, green_y = None, None
# If green object is detected, track it
if contours:
# Find the largest contour (assumed to be the green object)
largest_contour = max(contours, key=cv2.contourArea)
# Get the coordinates of the center of the green object
moments = cv2.moments(largest_contour)
if moments["m00"] != 0:
green_x = int(moments["m10"] / moments["m00"])
green_y = int(moments["m01"] / moments["m00"])
# Calculate the screen resolution (you may need to adjust this)
screen_width, screen_height = pyautogui.size()
# Map the green dot coordinates to the screen resolution
if green_x is not None and green_y is not None:
x_screen = int((green_x / frame.shape[1]) * screen_width)
y_screen = int((green_y / frame.shape[0]) * screen_height)
# Move the mouse cursor to the calculated position
pyautogui.moveTo(x_screen, y_screen)
# Display the frame with the green dot tracking
cv2.imshow('Green Dot Tracking', frame)
# Break the loop when the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture and close all windows
cap.release()
cv2.destroyAllWindows()