research-07-aug-2024 / untitled15.py
ysn-rfd's picture
Upload 20 files
014de59 verified
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 4 13:39:00 2024
@author: ysnrfd
"""
import cv2
import numpy as np
# Initialize the background subtractor
background_subtractor = cv2.createBackgroundSubtractorMOG2()
# Open a connection to the camera
cap = cv2.VideoCapture(0)
# Check if the camera opened successfully
if not cap.isOpened():
print("Error: Could not open camera.")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
print("Error: Failed to capture image")
break
# Apply background subtraction
fg_mask = background_subtractor.apply(frame)
# Find contours of the detected objects
contours, _ = cv2.findContours(fg_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Draw contours on the frame
for contour in contours:
if cv2.contourArea(contour) > 15: # Adjust the threshold for contour area
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 1)
cv2.putText(frame, "Anomaly Detected", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
# Display the resulting frame
cv2.imshow('Ghost Detector (Anomaly Detection)', frame)
# Break the loop on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and close all windows
cap.release()
cv2.destroyAllWindows()