Skip to content

Commit 3d3c2d9

Browse files
committed
Added Webcam Capture Feature
Will modify the webcam alignment in combined output
1 parent 0e3e1cb commit 3d3c2d9

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ files/*
1010
data/data.json
1111
data/screenshot.png
1212
data/mouse-ss.png
13+
data/combined_image.png
14+
data/webcam-capture.jpg
1315
test.py
1416
commands-to-be-added/*
1.23 KB
Binary file not shown.

commands/mouse_movement.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
import datetime
88
from PIL import ImageGrab
99
import time
10+
import cv2
11+
import os
1012

1113
DATA_JSON_PATH = "D:\Coding\Discord bots\python-windows-bot\data\data.json"
1214
SCREENSHOT_PATH = "D:\Coding\Discord bots\python-windows-bot\data\mouse-ss.png"
13-
15+
WEBCAM_CAPTURE_PATH = "D:\Coding\Discord bots\python-windows-bot\data\webcam-capture.jpg"
16+
COMBINED_IMAGE_PATH = "D:\Coding\Discord bots\python-windows-bot\data\combined_image.png"
1417

1518
async def monitor_mouse_movement(client, config, starting_mouse_position):
1619
log_channel_id = config["mouse_log_channel_id"]
@@ -33,9 +36,7 @@ async def monitor_mouse_movement(client, config, starting_mouse_position):
3336
try:
3437
image_message = await log_channel.fetch_message(image_message_id)
3538
except discord.NotFound:
36-
image_message = (
37-
None # Set image_message to None if the message is not found
38-
)
39+
image_message = None # Set image_message to None if the message is not found
3940
last_movement_time = data.get("last_movement_time")
4041
if last_movement_time:
4142
last_movement_time = datetime.datetime.strptime(
@@ -44,6 +45,8 @@ async def monitor_mouse_movement(client, config, starting_mouse_position):
4445
except (FileNotFoundError, json.JSONDecodeError, discord.NotFound, ValueError):
4546
pass
4647

48+
webcam = cv2.VideoCapture(0) # Initialize webcam capture
49+
4750
while True:
4851
current_mouse_position = pyautogui.position()
4952

@@ -54,32 +57,42 @@ async def monitor_mouse_movement(client, config, starting_mouse_position):
5457
last_movement_time = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S")
5558

5659
try:
57-
screenshot = ImageGrab.grab(
58-
# bbox=(
59-
# current_mouse_position[0] - 400,
60-
# current_mouse_position[1] - 300,
61-
# current_mouse_position[0] + 400,
62-
# current_mouse_position[1] + 300,
63-
# )
64-
)
65-
60+
screenshot = ImageGrab.grab()
6661
screenshot.save(SCREENSHOT_PATH)
6762
except OSError as e:
6863
print(f"[mouse_movement_LOG] - Error capturing screenshot: {e}")
6964
await asyncio.sleep(5)
7065
continue # Skip the rest of the loop iteration if there's an error
7166

67+
# Capture webcam image
68+
ret, frame = webcam.read()
69+
if ret:
70+
cv2.imwrite(WEBCAM_CAPTURE_PATH, frame)
71+
72+
# Load the screenshot and webcam capture images
73+
screenshot = cv2.imread(SCREENSHOT_PATH)
74+
webcam_capture = cv2.imread(WEBCAM_CAPTURE_PATH)
75+
76+
# Calculate the scaling ratio to fit the webcam image within a maximum width and height
77+
max_width = 200 # Maximum width for the webcam capture image
78+
max_height = 150 # Maximum height for the webcam capture image
79+
scale_ratio = min(max_width / webcam_capture.shape[1], max_height / webcam_capture.shape[0])
80+
81+
# Resize the webcam capture image while maintaining aspect ratio
82+
webcam_capture_resized = cv2.resize(webcam_capture, (0, 0), fx=scale_ratio, fy=scale_ratio)
83+
84+
# Define the coordinates where the webcam capture will be placed in the screenshot
85+
x_offset = 10 # Adjust as needed
86+
y_offset = 10 # Adjust as needed
7287

88+
# Overlay the webcam capture on the screenshot
89+
screenshot[y_offset:y_offset+webcam_capture_resized.shape[0], x_offset:x_offset+webcam_capture_resized.shape[1]] = webcam_capture_resized
7390

74-
# # Check if image_message is not None and attempt to delete it if it exists
75-
# if image_message:
76-
# try:
77-
# await image_message.delete()
78-
# except discord.NotFound:
79-
# pass # Message already deleted or not found
91+
# Save the resulting image
92+
cv2.imwrite(COMBINED_IMAGE_PATH, screenshot)
8093

8194
image_message = await log_channel.send(
82-
message, file=discord.File(SCREENSHOT_PATH)
95+
message, file=discord.File(COMBINED_IMAGE_PATH)
8396
)
8497

8598
print(f"[mouse_movement_LOG] - {last_movement_time} : Mouse SS sent.")
@@ -106,4 +119,4 @@ async def monitor_mouse_movement(client, config, starting_mouse_position):
106119
last_message = await log_channel.send(message)
107120

108121
previous_mouse_position = current_mouse_position
109-
await asyncio.sleep(5)
122+
await asyncio.sleep(5)

0 commit comments

Comments
 (0)