help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
surveillance-video
RSK World
surveillance-video
Surveillance Video Dataset - Security Camera + Person Detection + Anomaly Detection + OpenCV + YOLO
surveillance-video
  • __pycache__
  • annotations
  • config
  • frames
  • models
  • output
  • videos
  • .gitignore713 B
  • GITHUB_DEPLOYMENT.md3.5 KB
  • LICENSE1.3 KB
  • README.md13 KB
  • RELEASE_NOTES.md5.6 KB
  • alert_system.py12 KB
  • analytics.js9.2 KB
  • analytics_dashboard.html9.3 KB
  • api_server.py10.7 KB
  • batch_processor.py8.3 KB
  • config.py1.2 KB
  • create_sample_video.py7.4 KB
  • create_zip.py2.6 KB
  • detect_anomalies.py7.2 KB
  • detect_persons.py6.3 KB
  • download_sample_videos.py2.5 KB
  • extract_frames.py4.5 KB
  • index.html62.2 KB
  • multi_camera_system.py12.5 KB
  • package.json1.2 KB
  • process_video.py4.4 KB
  • project_data.json1.3 KB
  • project_data.php1.6 KB
  • project_data.py1.7 KB
  • requirements.txt372 B
  • script.js6.6 KB
  • setup.py2 KB
  • styles.css13.4 KB
  • surveillance-video.zip1.2 MB
  • train_ml_model.py9 KB
  • verify_project.py5 KB
  • video_quality_analyzer.py9.5 KB
  • video_search.py10.7 KB
detect_anomalies.py
detect_anomalies.py
Raw Download
Find: Go to:
# Project: Surveillance Video Dataset
# Author: Molla Samser
# Website: https://rskworld.in/
# Contact: help@rskworld.in
# Phone: +91 93305 39277
# Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147

"""
Anomaly detection script for surveillance video dataset.
This script detects anomalies such as unusual movements, rapid changes, etc.
"""

import cv2
import json
import numpy as np
import os
from pathlib import Path


def detect_anomalies_motion(video_path, output_file='annotations/anomalies.json', threshold=5000):
    """
    Detect anomalies based on motion detection.
    
    Args:
        video_path: Path to the input video file
        output_file: Path to save anomaly results
        threshold: Motion threshold for anomaly detection
    """
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        print(f"Error: Could not open video file {video_path}")
        return
    
    fps = cap.get(cv2.CAP_PROP_FPS)
    
    # Initialize background subtractor
    back_sub = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=50, detectShadows=True)
    
    anomalies = []
    frame_count = 0
    motion_history = []
    window_size = int(fps * 2)  # 2 seconds window
    
    print("Processing video for anomaly detection...")
    
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        
        # Apply background subtraction
        fg_mask = back_sub.apply(frame)
        
        # Calculate motion amount
        motion_amount = np.sum(fg_mask > 0)
        motion_history.append(motion_amount)
        
        # Keep only recent history
        if len(motion_history) > window_size:
            motion_history.pop(0)
        
        # Calculate average motion
        if len(motion_history) >= window_size:
            avg_motion = np.mean(motion_history)
            std_motion = np.std(motion_history)
            
            # Detect anomaly: motion significantly above average
            if motion_amount > avg_motion + 2 * std_motion and motion_amount > threshold:
                current_time = frame_count / fps
                
                # Check if this is continuation of previous anomaly
                if anomalies and current_time - anomalies[-1]['end_time'] < 1.0:
                    anomalies[-1]['end_time'] = current_time
                else:
                    anomalies.append({
                        'id': len(anomalies) + 1,
                        'type': 'unusual_movement',
                        'start_time': round(current_time, 2),
                        'end_time': round(current_time, 2),
                        'description': f'Rapid movement detected (motion: {motion_amount:.0f})'
                    })
        
        # Visualize
        cv2.imshow('Anomaly Detection', fg_mask)
        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        
        frame_count += 1
    
    cap.release()
    cv2.destroyAllWindows()
    
    # Save anomalies
    os.makedirs(os.path.dirname(output_file) if os.path.dirname(output_file) else '.', exist_ok=True)
    
    output_data = {
        'video_id': os.path.basename(video_path),
        'total_frames': frame_count,
        'fps': fps,
        'anomalies': anomalies
    }
    
    with open(output_file, 'w') as f:
        json.dump(output_data, f, indent=2)
    
    print(f"Anomaly detection complete. Found {len(anomalies)} anomaly events.")
    print(f"Results saved to {output_file}")


def detect_anomalies_optical_flow(video_path, output_file='annotations/anomalies.json'):
    """
    Detect anomalies using optical flow.
    
    Args:
        video_path: Path to the input video file
        output_file: Path to save anomaly results
    """
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        print(f"Error: Could not open video file {video_path}")
        return
    
    fps = cap.get(cv2.CAP_PROP_FPS)
    
    # Read first frame
    ret, old_frame = cap.read()
    old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
    
    anomalies = []
    frame_count = 0
    flow_history = []
    window_size = int(fps * 2)
    
    print("Processing video for anomaly detection using optical flow...")
    
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        
        frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        
        # Calculate optical flow
        flow = cv2.calcOpticalFlowFarneback(old_gray, frame_gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
        
        # Calculate magnitude of flow
        magnitude = np.sqrt(flow[..., 0]**2 + flow[..., 1]**2)
        avg_magnitude = np.mean(magnitude)
        flow_history.append(avg_magnitude)
        
        if len(flow_history) > window_size:
            flow_history.pop(0)
        
        # Detect anomaly
        if len(flow_history) >= window_size:
            avg_flow = np.mean(flow_history)
            std_flow = np.std(flow_history)
            
            if avg_magnitude > avg_flow + 2 * std_flow:
                current_time = frame_count / fps
                
                if anomalies and current_time - anomalies[-1]['end_time'] < 1.0:
                    anomalies[-1]['end_time'] = current_time
                else:
                    anomalies.append({
                        'id': len(anomalies) + 1,
                        'type': 'unusual_movement',
                        'start_time': round(current_time, 2),
                        'end_time': round(current_time, 2),
                        'description': f'Unusual optical flow detected'
                    })
        
        # Visualize optical flow
        hsv = np.zeros_like(frame)
        hsv[..., 1] = 255
        mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
        hsv[..., 0] = ang * 180 / np.pi / 2
        hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
        bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
        
        cv2.imshow('Optical Flow', bgr)
        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        
        old_gray = frame_gray.copy()
        frame_count += 1
    
    cap.release()
    cv2.destroyAllWindows()
    
    # Save anomalies
    os.makedirs(os.path.dirname(output_file) if os.path.dirname(output_file) else '.', exist_ok=True)
    
    output_data = {
        'video_id': os.path.basename(video_path),
        'total_frames': frame_count,
        'fps': fps,
        'anomalies': anomalies
    }
    
    with open(output_file, 'w') as f:
        json.dump(output_data, f, indent=2)
    
    print(f"Anomaly detection complete. Found {len(anomalies)} anomaly events.")
    print(f"Results saved to {output_file}")


if __name__ == "__main__":
    video_path = "videos/sample.mp4"
    
    if os.path.exists(video_path):
        # Use motion-based detection
        detect_anomalies_motion(video_path)
        
        # Alternative: Use optical flow
        # detect_anomalies_optical_flow(video_path)
    else:
        print(f"Video file not found: {video_path}")
        print("Please place your video files in the 'videos' directory")

223 lines•7.2 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer