Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,11 @@ import numpy as np
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# Function to apply
|
| 8 |
def apply_filter(frame, filter_name, filter_param):
|
| 9 |
if filter_name == 'Grayscale':
|
| 10 |
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 11 |
elif filter_name == 'Blur':
|
| 12 |
-
# Adjustable blur radius using the filter_param
|
| 13 |
return cv2.GaussianBlur(frame, (filter_param, filter_param), 0)
|
| 14 |
elif filter_name == 'Sharpness':
|
| 15 |
return cv2.convertScaleAbs(frame, alpha=filter_param, beta=0)
|
|
@@ -19,68 +18,65 @@ def apply_filter(frame, filter_name, filter_param):
|
|
| 19 |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
| 20 |
hsv[..., 1] = hsv[..., 1] * filter_param # Adjust saturation
|
| 21 |
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
| 22 |
-
elif filter_name == '
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
lower_white = np.array([200, 200, 200]) * filter_param
|
| 28 |
-
upper_white = np.array([255, 255, 255])
|
| 29 |
-
mask = cv2.inRange(frame, lower_white, upper_white)
|
| 30 |
-
frame[mask > 0] = [0, 0, 0] # Turn white areas into black
|
| 31 |
-
return frame
|
| 32 |
else:
|
| 33 |
return frame
|
| 34 |
|
| 35 |
# Streamlit UI
|
| 36 |
-
st.title("Video Editor")
|
| 37 |
-
st.write("Upload a video and
|
| 38 |
|
| 39 |
# Video upload
|
| 40 |
video_file = st.file_uploader("Upload your video", type=["mp4", "avi", "mov"])
|
| 41 |
|
| 42 |
if video_file:
|
| 43 |
-
#
|
| 44 |
st.video(video_file)
|
| 45 |
|
| 46 |
-
# Select filter
|
| 47 |
filter_choice = st.selectbox("Select a filter", [
|
| 48 |
-
"None", "Grayscale", "Blur", "Sharpness", "Contrast", "Saturation", "
|
| 49 |
|
| 50 |
-
#
|
| 51 |
if filter_choice == "Blur":
|
| 52 |
filter_param = st.slider("Blur Radius", min_value=3, max_value=21, step=2, value=5)
|
| 53 |
-
elif filter_choice
|
| 54 |
-
filter_param = st.slider("
|
|
|
|
|
|
|
| 55 |
elif filter_choice == "Saturation":
|
| 56 |
filter_param = st.slider("Saturation Multiplier", min_value=1.0, max_value=2.5, step=0.1, value=1.5)
|
| 57 |
-
elif filter_choice == "
|
| 58 |
-
filter_param = st.slider("
|
| 59 |
-
elif filter_choice == "
|
| 60 |
-
filter_param = st.slider("
|
| 61 |
else:
|
| 62 |
-
filter_param = 0 # No parameter
|
| 63 |
|
| 64 |
-
# Process the video and
|
| 65 |
if st.button("Apply Filter and Download"):
|
| 66 |
# Temporary file to store the video
|
| 67 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
|
| 68 |
tmp_file.write(video_file.read())
|
| 69 |
video_path = tmp_file.name
|
| 70 |
|
| 71 |
-
# Open video file
|
| 72 |
cap = cv2.VideoCapture(video_path)
|
| 73 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 74 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 75 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
output_path = os.path.join("output", "edited_video.mp4")
|
| 79 |
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 80 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 81 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 82 |
|
| 83 |
-
# Read and
|
| 84 |
while cap.isOpened():
|
| 85 |
ret, frame = cap.read()
|
| 86 |
if not ret:
|
|
@@ -88,7 +84,7 @@ if video_file:
|
|
| 88 |
processed_frame = apply_filter(frame, filter_choice, filter_param)
|
| 89 |
out.write(processed_frame)
|
| 90 |
|
| 91 |
-
# Release
|
| 92 |
cap.release()
|
| 93 |
out.release()
|
| 94 |
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Function to apply selected filter
|
| 8 |
def apply_filter(frame, filter_name, filter_param):
|
| 9 |
if filter_name == 'Grayscale':
|
| 10 |
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 11 |
elif filter_name == 'Blur':
|
|
|
|
| 12 |
return cv2.GaussianBlur(frame, (filter_param, filter_param), 0)
|
| 13 |
elif filter_name == 'Sharpness':
|
| 14 |
return cv2.convertScaleAbs(frame, alpha=filter_param, beta=0)
|
|
|
|
| 18 |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
| 19 |
hsv[..., 1] = hsv[..., 1] * filter_param # Adjust saturation
|
| 20 |
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
| 21 |
+
elif filter_name == 'Brightness':
|
| 22 |
+
return cv2.convertScaleAbs(frame, alpha=1, beta=filter_param)
|
| 23 |
+
elif filter_name == 'Tint':
|
| 24 |
+
tint_color = np.array([filter_param, 0, 0]) # Red tint for example
|
| 25 |
+
return cv2.addWeighted(frame, 1, tint_color, 0.2, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
else:
|
| 27 |
return frame
|
| 28 |
|
| 29 |
# Streamlit UI
|
| 30 |
+
st.title("Real-Time Video Editor")
|
| 31 |
+
st.write("Upload a video, apply filters, and adjust in real-time!")
|
| 32 |
|
| 33 |
# Video upload
|
| 34 |
video_file = st.file_uploader("Upload your video", type=["mp4", "avi", "mov"])
|
| 35 |
|
| 36 |
if video_file:
|
| 37 |
+
# Show the uploaded video
|
| 38 |
st.video(video_file)
|
| 39 |
|
| 40 |
+
# Select filter from dropdown menu
|
| 41 |
filter_choice = st.selectbox("Select a filter", [
|
| 42 |
+
"None", "Grayscale", "Blur", "Sharpness", "Contrast", "Saturation", "Brightness", "Tint"])
|
| 43 |
|
| 44 |
+
# Adjustable parameters for each filter
|
| 45 |
if filter_choice == "Blur":
|
| 46 |
filter_param = st.slider("Blur Radius", min_value=3, max_value=21, step=2, value=5)
|
| 47 |
+
elif filter_choice == "Sharpness":
|
| 48 |
+
filter_param = st.slider("Sharpness Factor", min_value=1.0, max_value=5.0, step=0.1, value=1.5)
|
| 49 |
+
elif filter_choice == "Contrast":
|
| 50 |
+
filter_param = st.slider("Contrast Intensity", min_value=1.0, max_value=5.0, step=0.1, value=2.0)
|
| 51 |
elif filter_choice == "Saturation":
|
| 52 |
filter_param = st.slider("Saturation Multiplier", min_value=1.0, max_value=2.5, step=0.1, value=1.5)
|
| 53 |
+
elif filter_choice == "Brightness":
|
| 54 |
+
filter_param = st.slider("Brightness Adjustment", min_value=-100, max_value=100, step=5, value=0)
|
| 55 |
+
elif filter_choice == "Tint":
|
| 56 |
+
filter_param = st.slider("Tint Level", min_value=0, max_value=255, step=1, value=100)
|
| 57 |
else:
|
| 58 |
+
filter_param = 0 # No filter parameter required for 'None'
|
| 59 |
|
| 60 |
+
# Process the video and show the result dynamically as filter is applied
|
| 61 |
if st.button("Apply Filter and Download"):
|
| 62 |
# Temporary file to store the video
|
| 63 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
|
| 64 |
tmp_file.write(video_file.read())
|
| 65 |
video_path = tmp_file.name
|
| 66 |
|
| 67 |
+
# Open the video file
|
| 68 |
cap = cv2.VideoCapture(video_path)
|
| 69 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 70 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 71 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 72 |
|
| 73 |
+
# Output path for the edited video
|
| 74 |
output_path = os.path.join("output", "edited_video.mp4")
|
| 75 |
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 76 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 77 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 78 |
|
| 79 |
+
# Read the video frame by frame and apply the selected filter
|
| 80 |
while cap.isOpened():
|
| 81 |
ret, frame = cap.read()
|
| 82 |
if not ret:
|
|
|
|
| 84 |
processed_frame = apply_filter(frame, filter_choice, filter_param)
|
| 85 |
out.write(processed_frame)
|
| 86 |
|
| 87 |
+
# Release video objects
|
| 88 |
cap.release()
|
| 89 |
out.release()
|
| 90 |
|