-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityCamPC.py
More file actions
130 lines (107 loc) · 4.95 KB
/
Copy pathSecurityCamPC.py
File metadata and controls
130 lines (107 loc) · 4.95 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import cv2
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
# Email details
sender_email = "your_new_email@gmail.com" # Replace with the new email (for sending from Pi)
receiver_email = "your_main_email@gmail.com" # Replace with your email (for receving images)
password = "aaaabbbbccccdddd" # Replace with your app password
# Initialize the webcam
cap = cv2.VideoCapture(0) # Use 0 for default webcam
# Initialize background subtractor with custom PMOG parameters
fgbg = cv2.createBackgroundSubtractorMOG2(history=50, varThreshold=5, detectShadows=False)
# Movement sensitivity
min_movement_area = 500 # Minimum area to detect motion
# Time tracking for consistent motion and email rate-limiting
motion_start_time = None # Start time of motion
motion_duration_threshold = 0.3 # Seconds of consistent motion required
last_email_time = 0 # Last time an email was sent
email_cooldown = 20 # Minimum time (in seconds) between emails
def send_email(image):
"""Send an email with the attached image."""
try:
# Encode the frame as a JPEG for emailing
_, encoded_image = cv2.imencode('.jpg', image)
image_bytes = encoded_image.tobytes()
# Email content
subject = "Motion Detected"
body = "Motion has been detected. See the attached image for details."
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message.attach(MIMEText(body, "plain"))
# Create a MIMEBase object for the in-memory image attachment
mime_base = MIMEBase("application", "octet-stream")
mime_base.set_payload(image_bytes)
encoders.encode_base64(mime_base)
mime_base.add_header(
"Content-Disposition",
"attachment; filename=motion_detected.jpg"
)
message.attach(mime_base)
# Connect to Gmail SMTP server and send email
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls() # Secure the connection
server.login(sender_email, password) # Use the app password here
server.sendmail(sender_email, receiver_email, message.as_string())
server.quit()
print("Email sent successfully with motion-detected image!")
except Exception as e:
print(f"Failed to send email: {e}")
while True:
# Capture the frame from the webcam
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
# Apply background subtraction
fgmask = fgbg.apply(frame)
# Threshold the mask to remove shadows
_, thresh = cv2.threshold(fgmask, 245, 255, cv2.THRESH_BINARY)
# Find contours in the thresholded image
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Initialize variables to hold the bounds of the encompassing box
x_min, y_min, x_max, y_max = float('inf'), float('inf'), float('-inf'), float('-inf')
# Flag to determine if motion is detected
motion_detected = False
for contour in contours:
area = cv2.contourArea(contour)
if area > min_movement_area:
motion_detected = True
x, y, w, h = cv2.boundingRect(contour)
x_min = min(x_min, x)
y_min = min(y_min, y)
x_max = max(x_max, x + w)
y_max = max(y_max, y + h)
# Check for consistent motion over time
if motion_detected:
if motion_start_time is None:
motion_start_time = time.time() # Record the start time of motion
elif time.time() - motion_start_time >= motion_duration_threshold:
# If motion has been consistent for the threshold duration
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
cv2.putText(frame, "Motion Detected", (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
1, (0, 0, 255), 2, cv2.LINE_AA)
# Send an email if the cooldown period has elapsed
current_time = time.time()
if current_time - last_email_time >= email_cooldown:
send_email(frame)
last_email_time = current_time # Update the last email sent time
else:
motion_start_time = None # Reset the motion start time if no motion
# Gradually increase the minimum movement area requirement
min_movement_area += 1 # Increase sensitivity requirement over time
# Display the original frame with bounding boxes
cv2.imshow("Webcam Feed", frame)
# Display the PMOG result for visualization
cv2.imshow("PMOG Visualization", fgmask)
# Exit on pressing 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close all windows
cap.release()
cv2.destroyAllWindows()