|
import cv2 |
|
import numpy as np |
|
|
|
def detect_anomalies(frame1, frame2): |
|
|
|
gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) |
|
gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) |
|
|
|
|
|
diff = cv2.absdiff(gray1, gray2) |
|
|
|
|
|
_, thresh = cv2.threshold(diff, 50, 255, cv2.THRESH_BINARY) |
|
|
|
|
|
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
|
|
for contour in contours: |
|
if cv2.contourArea(contour) > 500: |
|
x, y, w, h = cv2.boundingRect(contour) |
|
cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 2) |
|
|
|
return frame1 |
|
|
|
|
|
cap = cv2.VideoCapture(0) |
|
|
|
|
|
ret, prev_frame = cap.read() |
|
|
|
while True: |
|
|
|
ret, curr_frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
|
|
result_frame = detect_anomalies(prev_frame, curr_frame) |
|
|
|
|
|
cv2.imshow('Anomalies Detected', result_frame) |
|
|
|
|
|
prev_frame = curr_frame |
|
|
|
|
|
if cv2.waitKey(1) & 0xFF == ord('q'): |
|
break |
|
|
|
|
|
cap.release() |
|
cv2.destroyAllWindows() |
|
|