-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_Ping.py
executable file
·83 lines (68 loc) · 2.71 KB
/
Main_Ping.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import cv2, subprocess, asyncio, websockets
# Function to open Notepad
def open_notepad():
subprocess.Popen(["notepad.exe"])
# Function to detect motion
async def detect_motion():
cap = cv2.VideoCapture(0)
motion_detected = False
prev_frame = None
while not motion_detected:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
if prev_frame is None:
prev_frame = blurred
continue
frame_diff = cv2.absdiff(prev_frame, blurred)
_, thresh = cv2.threshold(frame_diff, 85, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
if cv2.contourArea(contour) > 1000:
(x, y, w, h) = cv2.boundingRect(contour)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
motion_detected = True
cv2.imshow('Motion Detection', frame)
if motion_detected:
open_notepad()
ip_groups = [
["192.168.x.xyz", "192.168.x.xyz", "192.168.x.xyz"],
["192.168.x.xyz", "192.168.x.xyz"],
["192.168.x.xyz"],
]
# Connect to each IP and send a message
for group in ip_groups:
for ip in group:
try:
async with websockets.connect(f"ws://{ip}:35369") as websocket:
await websocket.send("Motion detected")
print(f"Sent 'Motion detected' to {ip}")
except asyncio.TimeoutError:
print(f"Timeout connecting to {ip}. Moving to the next IP.")
continue
await asyncio.sleep(3)
prev_frame = blurred
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# Function to start the WebSocket server
async def start_ws_server():
async def ws_server(websocket, path):
print("WebSocket server started.")
try:
async for message in websocket:
print(f"Received message: {message}")
except websockets.ConnectionClosedError:
print("Connection closed.")
# Start the WebSocket server
start_server = websockets.serve(ws_server, "192.168.x.xyz", 35369)
await start_server
# Main function
async def main():
# Start the WebSocket server
await start_ws_server()
# Start motion detection
await detect_motion()
# Run the main function
asyncio.run(main())