# SPDX-License-Identifier: MIT # Copyright (C) Intel Corporation # # Light-Level Anomaly Detection: OpenCV sample. # See README.md for prerequisites and inputs. Run: python3 opencv_sample.py import cv2 import numpy as np INPUT_VIDEO = "test_video.mp4" DARK_BOUND = 40.0 # mean luminance below this is underexposed BRIGHT_BOUND = 215.0 # mean luminance above this is overexposed JUMP_BOUND = 35.0 # frame-to-frame luminance jump that counts as sudden cap = cv2.VideoCapture(INPUT_VIDEO) fps = cap.get(cv2.CAP_PROP_FPS) or 30.0 width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) writer = cv2.VideoWriter( "output_opencv.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height)) prev_level = None frame_idx = 0 anomalies = 0 while True: ok, frame = cap.read() if not ok: break frame_idx += 1 v = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)[:, :, 2] level = float(np.mean(v)) status = "normal" if level < DARK_BOUND: status = "dark" elif level > BRIGHT_BOUND: status = "bright" elif prev_level is not None and abs(level - prev_level) >= JUMP_BOUND: status = "sudden-change" prev_level = level if status != "normal": anomalies += 1 print(f"Frame {frame_idx}: LIGHT ANOMALY ({status}) level={level:.1f}", flush=True) color = (0, 255, 0) if status == "normal" else (0, 0, 255) label = f"level={level:.1f} {status}" (_, text_height), _ = cv2.getTextSize( label, cv2.FONT_HERSHEY_SIMPLEX, 5.0, 2) cv2.putText(frame, label, (10, text_height + 10), cv2.FONT_HERSHEY_SIMPLEX, 5.0, color, 2) writer.write(frame) cap.release() writer.release() print(f"Light-level anomalies detected: {anomalies}", flush=True)