From a534e94deadc4aebd00c7c80c6df00317ec2bc16 Mon Sep 17 00:00:00 2001 From: Tejas Athalye <142726372+LitZeus@users.noreply.github.com> Date: Sun, 13 Oct 2024 01:19:47 +0530 Subject: [PATCH] Adding Video Tampering Detection Tool --- .../Video-tampering-detection/README.md | 69 ++++++++++++++++++ .../Video-tampering-detection/app.py | 63 ++++++++++++++++ .../frame_analyzer.py | 18 +++++ .../hash_calculator.py | 8 ++ .../Video-tampering-detection/metadata.py | 11 +++ .../requirements.txt | Bin 0 -> 54 bytes 6 files changed, 169 insertions(+) create mode 100644 Detection Models/Video-tampering-detection/README.md create mode 100644 Detection Models/Video-tampering-detection/app.py create mode 100644 Detection Models/Video-tampering-detection/frame_analyzer.py create mode 100644 Detection Models/Video-tampering-detection/hash_calculator.py create mode 100644 Detection Models/Video-tampering-detection/metadata.py create mode 100644 Detection Models/Video-tampering-detection/requirements.txt diff --git a/Detection Models/Video-tampering-detection/README.md b/Detection Models/Video-tampering-detection/README.md new file mode 100644 index 000000000..0a34803c4 --- /dev/null +++ b/Detection Models/Video-tampering-detection/README.md @@ -0,0 +1,69 @@ +# Video Tampering and Forensics Analysis Tool + +## Abstract + +The **Video Tampering and Forensic Analysis Tool** is designed to ensure the authenticity and integrity of digital video content. In fields like law enforcement and journalism, reliable video evidence is crucial. This tool uses advanced techniques such as metadata examination, frame-by-frame analysis, deepfake detection, and watermark authentication to detect video tampering and manipulations. It generates detailed forensic reports to aid investigative professionals in ensuring the credibility of video evidence. + +## Features + +- **User-Friendly Interface**: Interactive web-based application using Streamlit. +- **Video Input**: Supports video file formats such as MP4, AVI, and MOV (file size limit: 200MB). +- **Metadata Extraction**: Provides key details such as resolution, codec, frame count, duration, and geolocation data (if available). +- **Hashing (MD5)**: Verifies the integrity of the video by generating a unique hash for comparison. +- **Frame Analysis**: Detects tampering through frame-by-frame inspection for splicing, insertion, and pixel pattern anomalies. +- **Comprehensive Report Generation**: Outputs a JSON report with all findings, including extracted metadata and hash values. + +## Technologies Used + +- **Programming Language**: Python +- **Libraries**: OpenCV, FFmpeg, hashlib, os, json +- **Web Framework**: Streamlit +- **Hashing Algorithm**: MD5 + +## Setup Instructions + +#### First Fork the repository and then follow the steps given below! + +### 1. Clone the Repository + +```sh +git clone https://github.com//machine-learning-repos.git +cd Detection Models/Video-tampering-detection +``` +### 2. Create a virtual environment: + ```sh + python -m venv venv + source venv/bin/activate # On Windows, use `venv\Scripts\activate` + ``` + +### 3. Install the required packages: + ```sh + pip install -r requirements.txt + ``` + +### 4. Run the application: + ```sh + streamlit run main.py + ``` +### 5. Usage Instructions + + ##### 1. Open your web browser and go to `http://localhost:8501` to access the app. + + ##### 2. Upload a video file (MP4, AVI, MOV) for analysis. + + ##### 3. View the results on the web interface, including: + - Extracted metadata + - MD5 hash for integrity verification + - Frame-by-frame analysis + + ##### 4. Download the comprehensive JSON report summarizing the findings. + +### 6. Stop the Application: + To stop the Streamlit app, press `CTRL + C` in the terminal. + + +## Contributing +Contributions are welcome! If you find any bugs or have suggestions for improvements, please open an issue or create a pull request. + +## License +This project is licensed under the MIT License. See the `LICENSE` file for more details. diff --git a/Detection Models/Video-tampering-detection/app.py b/Detection Models/Video-tampering-detection/app.py new file mode 100644 index 000000000..bfbe2d8a2 --- /dev/null +++ b/Detection Models/Video-tampering-detection/app.py @@ -0,0 +1,63 @@ +import os +import json +import streamlit as st +from metadata import extract_metadata +from hash_calculator import calculate_hash +from frame_analyzer import analyze_frames + +# Streamlit app +st.title('Video Forensic Analysis Tool') + +# File uploader +uploaded_file = st.file_uploader('Upload a video file', type=['mp4', 'avi', 'mov']) + +if uploaded_file is not None: + # Check if 'temp' folder exists, create if not + temp_folder = 'temp' + if not os.path.exists(temp_folder): + os.makedirs(temp_folder) + + # Save the uploaded file temporarily + temp_file_path = os.path.join(temp_folder, uploaded_file.name) + with open(temp_file_path, 'wb') as f: + f.write(uploaded_file.read()) + + st.write("**File uploaded successfully**") + + # Extract metadata + st.write("**Extracting metadata...**") + metadata = extract_metadata(temp_file_path) + st.json(metadata) + + # Calculate hash + st.write("**Calculating file hash (MD5)...**") + video_hash = calculate_hash(temp_file_path) + st.write(f"MD5 Hash: {video_hash}") + + # Analyze frames for alterations + st.write("**Analyzing frames for alterations...**") + altered_frames = analyze_frames(temp_file_path) + st.write(f"Altered frames: {altered_frames}") + + # Generate report name based on video file name + file_name = os.path.splitext(uploaded_file.name)[0] + report_name = f'report-{file_name}.json' + + # Report creation + report = { + 'metadata': metadata, + 'hash': video_hash, + 'altered_frames': altered_frames + } + + st.write(f"**Video forensic analysis complete! Report saved as: {report_name}**") + st.json(report) + + # Offer download of the JSON report + report_json = json.dumps(report, indent=4) + st.download_button( + label="Download report as JSON", + data=report_json, + file_name=report_name, + mime="application/json" + ) diff --git a/Detection Models/Video-tampering-detection/frame_analyzer.py b/Detection Models/Video-tampering-detection/frame_analyzer.py new file mode 100644 index 000000000..bb0cbf22a --- /dev/null +++ b/Detection Models/Video-tampering-detection/frame_analyzer.py @@ -0,0 +1,18 @@ +import cv2 + +def analyze_frames(video_path): + cap = cv2.VideoCapture(video_path) + frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) + altered_frames = [] + prev_frame = None + + for i in range(frame_count): + ret, frame = cap.read() + if ret: + if prev_frame is not None: + if not (frame == prev_frame).all(): + altered_frames.append(i) + prev_frame = frame.copy() + + cap.release() + return altered_frames diff --git a/Detection Models/Video-tampering-detection/hash_calculator.py b/Detection Models/Video-tampering-detection/hash_calculator.py new file mode 100644 index 000000000..8bbf6ccb4 --- /dev/null +++ b/Detection Models/Video-tampering-detection/hash_calculator.py @@ -0,0 +1,8 @@ +import hashlib + +def calculate_hash(video_path): + hash_md5 = hashlib.md5() + with open(video_path, 'rb') as f: + for chunk in iter(lambda: f.read(4096), b''): + hash_md5.update(chunk) + return hash_md5.hexdigest() diff --git a/Detection Models/Video-tampering-detection/metadata.py b/Detection Models/Video-tampering-detection/metadata.py new file mode 100644 index 000000000..89d533f1e --- /dev/null +++ b/Detection Models/Video-tampering-detection/metadata.py @@ -0,0 +1,11 @@ +import cv2 + +def extract_metadata(video_path): + metadata = {} + cap = cv2.VideoCapture(video_path) + metadata['frame_count'] = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) + metadata['frame_width'] = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + metadata['frame_height'] = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + metadata['fps'] = int(cap.get(cv2.CAP_PROP_FPS)) + cap.release() + return metadata diff --git a/Detection Models/Video-tampering-detection/requirements.txt b/Detection Models/Video-tampering-detection/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..c0842643b68cd4026e6f8db7e332cbbca49b4118 GIT binary patch literal 54 zcmezWFQ1`+A(bJIA(^3!K^MrbWGG?CV8{oG@G@{Q6a#rhK>0+5T!tKmOdt;=3IKSu B3tIpH literal 0 HcmV?d00001