Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrubbing for mp4 files #292

Merged
merged 19 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions openadapt/scripts/scrub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""Module for scrubbing a media file.

Usage: python -m openadapt.scripts.scrub <media_file_path>

"""

import os
import sys

from loguru import logger
from PIL import Image
import cv2
import numpy as np

from openadapt import scrub


def scrub_mp4(mp4_file: str) -> str:
"""
Scrub a mp4 file.

Args:
mp4_file: Path to the mp4 file.

Returns:
Path to the scrubbed (redacted) mp4 file.
"""

scrubbed_file = mp4_file[:-4] + "_scrubbed.mp4"

cap = cv2.VideoCapture(mp4_file)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
fourcc = cv2.VideoWriter_fourcc("m", "p", "4", "v")
KrishPatel13 marked this conversation as resolved.
Show resolved Hide resolved
out = cv2.VideoWriter(
scrubbed_file, fourcc, fps, (frame_width, frame_height)
)

while cap.isOpened():
KrishPatel13 marked this conversation as resolved.
Show resolved Hide resolved
# frameId = cap.get(1) # current frame number
KrishPatel13 marked this conversation as resolved.
Show resolved Hide resolved
ret, frame = cap.read()
if not ret:
break
# Convert frame to PIL.Image
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
KrishPatel13 marked this conversation as resolved.
Show resolved Hide resolved

# Apply redaction to the frame
redacted_image = scrub.scrub_image(image)

# Convert redacted image back to OpenCV format
redacted_frame = cv2.cvtColor(
np.array(redacted_image), cv2.COLOR_RGB2BGR
)
out.write(redacted_frame)

cap.release()
out.release()

return scrubbed_file


def scrub_media_file(media_file_path: str) -> str:
"""
Scrub a media file based on its extension.

Args:
media_file_path: Path to the media file.

Returns:
Path to the scrubbed media file.
"""

file_extension = os.path.splitext(media_file_path)[1].lower()
scrub_functions = {
KrishPatel13 marked this conversation as resolved.
Show resolved Hide resolved
".mp4": scrub_mp4,
# Add more extensions and corresponding functions as needed
KrishPatel13 marked this conversation as resolved.
Show resolved Hide resolved
}

if file_extension in scrub_functions:
scrub_function = scrub_functions[file_extension]
return scrub_function(media_file_path)
logger.info(f"Unsupported media file format: {file_extension}")
return ""
KrishPatel13 marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
if (
KrishPatel13 marked this conversation as resolved.
Show resolved Hide resolved
len(sys.argv) < 2
or len(sys.argv) > 2
or sys.argv[1] == "-h"
or sys.argv[1] == "--help"
):
logger.info(
"Usage: python -m openadapt.scripts.scrub <video_file_path>"
)
sys.exit(1)

scrubbed_file_path = scrub_media_file(sys.argv[1])

if scrubbed_file_path:
logger.info(f"Scrubbed media file saved at: {scrubbed_file_path}")
else:
logger.info("Failed to scrub the media file.")
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ torch==2.0.0
tqdm==4.64.0
nicegui==1.2.16
transformers==4.29.2
python-dotenv==1.0.0
KrishPatel13 marked this conversation as resolved.
Show resolved Hide resolved
python-dotenv==1.0.0
Binary file added resources/sample1.mp4
Binary file not shown.
Binary file added resources/sample1_scrubbed.mp4
Binary file not shown.
Binary file added resources/sample2.mp4
Binary file not shown.
Binary file added resources/sample2_scrubbed.mp4
Binary file not shown.