Spaces:
Runtime error
Runtime error
File size: 3,957 Bytes
5158b65 19b03aa 5cbd151 ab9cba9 988f269 5cbd151 988f269 5cbd151 988f269 5cbd151 988f269 19b03aa 988f269 19b03aa ab9cba9 5cbd151 5158b65 ab9cba9 5cbd151 ab9cba9 5cbd151 ab9cba9 71b7254 ab9cba9 5cbd151 ab9cba9 988f269 ab9cba9 988f269 ab9cba9 988f269 ab9cba9 988f269 ab9cba9 5cbd151 19b03aa ab9cba9 3f07dda 19b03aa ab9cba9 19b03aa 3f07dda ab9cba9 3f07dda 988f269 3f07dda ab9cba9 3f07dda 19b03aa 5cbd151 19b03aa 5cbd151 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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")
|