Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import cv2 | |
| import numpy as np | |
| import tempfile | |
| import os | |
| # Function to apply selected filter | |
| def apply_filter(frame, filter_name, filter_param): | |
| if filter_name == 'Grayscale': | |
| return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| elif filter_name == 'Blur': | |
| return cv2.GaussianBlur(frame, (filter_param, filter_param), 0) | |
| elif filter_name == 'Sharpness': | |
| return cv2.convertScaleAbs(frame, alpha=filter_param, beta=0) | |
| elif filter_name == 'Contrast': | |
| return cv2.convertScaleAbs(frame, alpha=filter_param, beta=0) | |
| elif filter_name == 'Saturation': | |
| hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | |
| hsv[..., 1] = hsv[..., 1] * filter_param # Adjust saturation | |
| return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) | |
| elif filter_name == 'Brightness': | |
| return cv2.convertScaleAbs(frame, alpha=1, beta=filter_param) | |
| elif filter_name == 'Tint': | |
| tint_color = np.array([filter_param, 0, 0]) # Red tint for example | |
| return cv2.addWeighted(frame, 1, tint_color, 0.2, 0) | |
| else: | |
| return frame | |
| # Streamlit UI | |
| st.title("Real-Time Video Editor") | |
| st.write("Upload a video, apply filters, and adjust in real-time!") | |
| # Video upload | |
| video_file = st.file_uploader("Upload your video", type=["mp4", "avi", "mov"]) | |
| if video_file: | |
| # Show the uploaded video | |
| st.video(video_file) | |
| # Select filter from dropdown menu | |
| filter_choice = st.selectbox("Select a filter", [ | |
| "None", "Grayscale", "Blur", "Sharpness", "Contrast", "Saturation", "Brightness", "Tint"]) | |
| # Adjustable parameters for each filter | |
| if filter_choice == "Blur": | |
| filter_param = st.slider("Blur Radius", min_value=3, max_value=21, step=2, value=5) | |
| elif filter_choice == "Sharpness": | |
| filter_param = st.slider("Sharpness Factor", min_value=1.0, max_value=5.0, step=0.1, value=1.5) | |
| elif filter_choice == "Contrast": | |
| filter_param = st.slider("Contrast Intensity", min_value=1.0, max_value=5.0, step=0.1, value=2.0) | |
| elif filter_choice == "Saturation": | |
| filter_param = st.slider("Saturation Multiplier", min_value=1.0, max_value=2.5, step=0.1, value=1.5) | |
| elif filter_choice == "Brightness": | |
| filter_param = st.slider("Brightness Adjustment", min_value=-100, max_value=100, step=5, value=0) | |
| elif filter_choice == "Tint": | |
| filter_param = st.slider("Tint Level", min_value=0, max_value=255, step=1, value=100) | |
| else: | |
| filter_param = 0 # No filter parameter required for 'None' | |
| # Process the video and show the result dynamically as filter is applied | |
| if st.button("Apply Filter and Download"): | |
| # Temporary file to store the video | |
| with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file: | |
| tmp_file.write(video_file.read()) | |
| video_path = tmp_file.name | |
| # Open the video file | |
| cap = cv2.VideoCapture(video_path) | |
| frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
| frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
| fps = cap.get(cv2.CAP_PROP_FPS) | |
| # Output path for the edited video | |
| output_path = os.path.join("output", "edited_video.mp4") | |
| os.makedirs(os.path.dirname(output_path), exist_ok=True) | |
| fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
| out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height)) | |
| # Read the video frame by frame and apply the selected filter | |
| while cap.isOpened(): | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| processed_frame = apply_filter(frame, filter_choice, filter_param) | |
| out.write(processed_frame) | |
| # Release video objects | |
| cap.release() | |
| out.release() | |
| # Provide download link | |
| with open(output_path, "rb") as f: | |
| st.download_button("Download edited video", f, file_name="edited_video.mp4") | |