Having a cozy little place, I always fancied a simple security setup that could keep an eye on my abode while I’m away. The market options were either too fancy or just didn’t hit the right chord with my tech-savvy self. So, I decided to roll up my sleeves and build my own tailored security system, TeleSec-Cam. With a dash of Python, a sprinkle of Telegram bot magic, and my trusty old webcam, I embarked on a coding adventure that not only satiated my tech cravings but also brought a sense of security to my living space. In this article, I’ll share the crests and troughs of my journey with TeleSec-Cam, unravel the code that breathes life into it, and guide you on how you too can set up your personalized security sentinel. Dive into the TeleSec-Cam’s humble abode on GitHub to get a sneak peek of the action!
Features
How It Works
Motion Detection
TeleSec-Cam employs OpenCV, a powerful library for computer vision tasks, to access the webcam and analyze the video feed for motion. When motion is detected, it captures a frame and sends an alert to the authorized Telegram users.
Telegram Bot Integration
The Telegram bot acts as the interface between the user and the security camera system. It sends alerts when motion is detected, provides live captures on demand, and allows for user management. All interactions with the bot are secured, ensuring only authorized users have access.
User Management
Through the bot, the admin can add or remove users who have access to the system. This is done through a simple command and username input, making user management straightforward and secure.
Diving Deep into the Code
1. Motion Detection with OpenCV
The heartbeat of TeleSec-Cam is its ability to detect motion. The security_cam.py script houses the logic for accessing the webcam and processing the video feed using the OpenCV library.
Recommended by LinkedIn
import cv2# ... other imports ...def main(): cap = cv2.VideoCapture(0) # Access the webcam ret, frame1 = cap.read() ret, frame2 = cap.read() while cap.isOpened(): diff = cv2.absdiff(frame1, frame2) # Calculate absolute difference between consecutive frames gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) # Convert to grayscale blur = cv2.GaussianBlur(gray, (5,5), 0) # Apply Gaussian blur _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY) # Thresholding dilated = cv2.dilate(thresh, None, iterations=3) # Dilate thresholded image to fill in holes contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Find contours for contour in contours: (x, y, w, h) = cv2.boundingRect(contour) if cv2.contourArea(contour) < 700: # Ignore small movements continue cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 2) # Draw rectangle around moving object # ... alert logic ... # ... other code ... frame1 = frame2 ret, frame2 = cap.read() if cv2.waitKey(10) == 27: break cap.release() cv2.destroyAllWindows()if __name__ == "__main__": main()
In this snippet, OpenCV’s cv2.VideoCapture function initializes access to the webcam. The script then enters a loop, where it reads frames from the webcam, processes them to detect motion, and triggers an alert if motion is detected.
2. Engaging Telegram Bot API
Telegram provides a powerful bot API, which TeleSec-Cam harnesses for real-time notifications and user interaction. The telegram_bot.py script leverages the pyTelegramBotAPI library to handle incoming commands and send alerts.
import telebotfrom telebot import typesimport config # Importing the config file for credentialsbot = telebot.TeleBot(config.BOT_TOKEN)# ... other code ...@bot.message_handler(commands=['capture'])def handle_capture_command(message): # ... capture logic ...# ... other code ...if __name__ == "__main__": bot.polling(none_stop=True)
In this segment, telebot.TeleBot initializes the bot with the token obtained from Telegram. The @bot.message_handler decorator is a straightforward way to define how the bot should respond to specific commands.
3. User Management Dynamics
TeleSec-Cam empowers the admin to manage user access directly via the Telegram bot. The user management logic embedded within the Telegram bot script facilitates the addition or removal of authorized users.
# ... other code ...@bot.message_handler(func=lambda message: message.text == 'Add User' and message.chat.id == config.TELEGRAM_USER_ID)def prompt_for_username(message): # ... add user logic ...@bot.message_handler(func=lambda message: message.text == 'Remove User' and message.chat.id == config.TELEGRAM_USER_ID)def prompt_for_username_to_remove(message): # ... remove user logic ...# ... other code ...
Here, @bot.message_handler once again comes into play, defining the behavior of the bot when the 'Add User' or 'Remove User' commands are received.
Setting Up TeleSec-Cam: Your Personal Watchguard
Getting TeleSec-Cam up and running involves a few simple steps: cloning the TeleSec-Cam repository, installing the required dependencies, providing your Telegram credentials, and initiating the main.py script. The README in the repository provides a detailed walkthrough to guide you through the setup process.
TeleSec-Cam stands as a testament to the power of open-source tools and a bit of ingenuity. Dive into the GitHub repository, explore the code, set up your own security camera system, and perhaps even contribute to the project to make TeleSec-Cam even better!
Please ⭐ the project if you liked it and leave a comment if you have any questions!