|
|
|
|
|
""" |
|
Created on Sun Aug 4 13:39:00 2024 |
|
|
|
@author: ysnrfd |
|
""" |
|
|
|
import cv2 |
|
import numpy as np |
|
|
|
|
|
background_subtractor = cv2.createBackgroundSubtractorMOG2() |
|
|
|
|
|
cap = cv2.VideoCapture(0) |
|
|
|
|
|
if not cap.isOpened(): |
|
print("Error: Could not open camera.") |
|
exit() |
|
|
|
while True: |
|
|
|
ret, frame = cap.read() |
|
if not ret: |
|
print("Error: Failed to capture image") |
|
break |
|
|
|
|
|
fg_mask = background_subtractor.apply(frame) |
|
|
|
|
|
contours, _ = cv2.findContours(fg_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
|
|
for contour in contours: |
|
if cv2.contourArea(contour) > 15: |
|
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) |
|
|
|
|
|
cv2.imshow('Ghost Detector (Anomaly Detection)', frame) |
|
|
|
|
|
if cv2.waitKey(1) & 0xFF == ord('q'): |
|
break |
|
|
|
|
|
cap.release() |
|
cv2.destroyAllWindows() |
|
|