Created
June 27, 2024 10:44
-
-
Save Dbhardwaj99/ab6044d8fe557d818fb7ee5c7f5bdf6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
import sqlite3 | |
import pyttsx3 | |
# Database setup | |
conn = sqlite3.connect('object_tracking.db') | |
c = conn.cursor() | |
c.execute('''CREATE TABLE IF NOT EXISTS objects | |
(id INTEGER PRIMARY KEY AUTOINCREMENT, rx INTEGER, ry INTEGER, rw INTEGER, rh INTEGER, roi_hist BLOB)''') | |
conn.commit() | |
# Initialize pyttsx3 engine | |
engine = pyttsx3.init() | |
# Global variables for tracking and detection | |
drawing = False | |
ix, iy = -1, -1 # Initial coordinates for drawing ROI | |
rx, ry, rw, rh = -1, -1, -1, -1 # Variables to store ROI coordinates | |
bbox_selected = False # Flag indicating if ROI is selected | |
tracker = None # Object tracker instance | |
object_tracked = False # Flag indicating if an object is being tracked | |
object_removed = True # Flag indicating if object is removed from frame | |
min_contour_area = 100 # Minimum contour area threshold for object removal | |
def draw_rectangle(event, x, y, flags, param): | |
global ix, iy, drawing, rx, ry, rw, rh, bbox_selected, tracker, object_tracked | |
if event == cv2.EVENT_LBUTTONDOWN: | |
drawing = True | |
ix, iy = x, y | |
elif event == cv2.EVENT_MOUSEMOVE: | |
if drawing: | |
frame_copy = frame.copy() | |
cv2.rectangle(frame_copy, (ix, iy), (x, y), (0, 255, 0), 2) | |
cv2.imshow('Frame', frame_copy) | |
elif event == cv2.EVENT_LBUTTONUP: | |
drawing = False | |
rx, ry, rw, rh = ix, iy, x - ix, y - iy | |
if rw > 0 and rh > 0: | |
bbox_selected = True | |
object_tracked = True | |
cv2.rectangle(frame, (ix, iy), (x, y), (0, 255, 0), 2) | |
cv2.imshow('Frame', frame) | |
tracker = cv2.TrackerCSRT_create() # Use a more stable tracker (e.g., CSRT) | |
if tracker: | |
tracker.init(frame, (rx, ry, rw, rh)) | |
object_removed = False # Reset object removal flag | |
else: | |
print("Invalid ROI selected. Please select a valid region.") | |
# Speak function using pyttsx3 | |
def speak(text): | |
engine.say(text) | |
engine.runAndWait() | |
# Start video capture from video file or RTSP stream | |
rtsp_url = 'rtsp://admin:pass@[email protected]:554/cam/realmonitor?channel=4&subtype=0' | |
cap = cv2.VideoCapture(rtsp_url) | |
cv2.namedWindow('Frame', cv2.WINDOW_NORMAL) # Make window resizable | |
cv2.setMouseCallback('Frame', draw_rectangle) | |
while True: | |
ret, frame = cap.read() | |
if not ret: | |
break | |
if bbox_selected and tracker is not None: | |
success, bbox = tracker.update(frame) | |
if success: | |
# Update ROI coordinates | |
rx, ry, rw, rh = int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]) | |
cv2.rectangle(frame, (rx, ry), (rx + rw, ry + rh), (0, 255, 0), 2) | |
# Check if object is completely outside the frame | |
if rx + rw <= 0 or ry + rh <= 0 or rx >= frame.shape[1] or ry >= frame.shape[0]: | |
if not object_removed: | |
cv2.putText(frame, "Object removed!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) | |
object_removed = True # Set object removal flag | |
speak("Object removed!") # Speak message using pyttsx3 | |
bbox_selected = False | |
tracker = None | |
else: | |
if not object_removed: | |
cv2.putText(frame, "Object removed!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) | |
object_removed = True # Set object removal flag | |
speak("Object removed!") # Speak message using pyttsx3 | |
bbox_selected = False | |
tracker = None | |
else: | |
if object_tracked: | |
if rx + rw <= 0 or ry + rh <= 0 or rx >= frame.shape[1] or ry >= frame.shape[0]: | |
if object_removed: | |
cv2.putText(frame, "Object removed!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) | |
object_removed = True | |
speak("Object removed!") | |
bbox_selected = False | |
tracker = None | |
cv2.imshow('Frame', frame) | |
if cv2.waitKey(1) & 0xFF == 27: | |
break | |
# Clean up | |
cap.release() | |
cv2.destroyAllWindows() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment