Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def cartoonize_image(image):
|
| 6 |
# Convert the image to grayscale
|
|
@@ -30,20 +32,21 @@ def main():
|
|
| 30 |
video_file = st.file_uploader("Choose a video file", type=["mp4"])
|
| 31 |
|
| 32 |
if video_file is not None:
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
f.write(video_bytes)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
def cartoonize_video(video_path):
|
| 49 |
cap = cv2.VideoCapture(video_path)
|
|
@@ -53,8 +56,8 @@ def cartoonize_video(video_path):
|
|
| 53 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 54 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 55 |
|
| 56 |
-
# Create
|
| 57 |
-
cartoon_video_path = "
|
| 58 |
cartoon_video = cv2.VideoWriter(cartoon_video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
|
| 59 |
|
| 60 |
while True:
|
|
@@ -66,20 +69,13 @@ def cartoonize_video(video_path):
|
|
| 66 |
# Cartoonize the frame
|
| 67 |
cartoon_frame = cartoonize_image(frame)
|
| 68 |
|
| 69 |
-
# Display the cartoonized frame
|
| 70 |
-
st.image(cartoon_frame, channels="BGR", use_column_width=True)
|
| 71 |
-
|
| 72 |
# Write the cartoonized frame to the VideoWriter
|
| 73 |
cartoon_video.write(cartoon_frame)
|
| 74 |
|
| 75 |
cap.release()
|
| 76 |
cartoon_video.release()
|
| 77 |
|
| 78 |
-
|
| 79 |
-
with open(cartoon_video_path, "rb") as f:
|
| 80 |
-
cartoon_video_bytes = f.read()
|
| 81 |
-
|
| 82 |
-
return cartoon_video_bytes
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
def cartoonize_image(image):
|
| 8 |
# Convert the image to grayscale
|
|
|
|
| 32 |
video_file = st.file_uploader("Choose a video file", type=["mp4"])
|
| 33 |
|
| 34 |
if video_file is not None:
|
| 35 |
+
# Save the uploaded video to a temporary file
|
| 36 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
|
| 37 |
+
temp_video.write(video_file.read())
|
| 38 |
+
temp_video_path = temp_video.name
|
| 39 |
|
| 40 |
+
# Process and display the cartoonized video
|
| 41 |
+
cartoon_video_path = cartoonize_video(temp_video_path)
|
| 42 |
|
| 43 |
+
# Display the cartoonized video
|
| 44 |
+
with open(cartoon_video_path, "rb") as f:
|
| 45 |
+
st.video(f.read(), format="video/mp4", start_time=0)
|
|
|
|
| 46 |
|
| 47 |
+
# Clean up the temporary files
|
| 48 |
+
os.remove(temp_video_path)
|
| 49 |
+
os.remove(cartoon_video_path)
|
| 50 |
|
| 51 |
def cartoonize_video(video_path):
|
| 52 |
cap = cv2.VideoCapture(video_path)
|
|
|
|
| 56 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 57 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 58 |
|
| 59 |
+
# Create a temporary file to save the cartoonized video
|
| 60 |
+
cartoon_video_path = tempfile.mktemp(suffix=".mp4")
|
| 61 |
cartoon_video = cv2.VideoWriter(cartoon_video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
|
| 62 |
|
| 63 |
while True:
|
|
|
|
| 69 |
# Cartoonize the frame
|
| 70 |
cartoon_frame = cartoonize_image(frame)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
# Write the cartoonized frame to the VideoWriter
|
| 73 |
cartoon_video.write(cartoon_frame)
|
| 74 |
|
| 75 |
cap.release()
|
| 76 |
cartoon_video.release()
|
| 77 |
|
| 78 |
+
return cartoon_video_path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
main()
|