pictocartoon / app.py
GeminiAi's picture
Update app.py
0213ced
raw
history blame
1.62 kB
import streamlit as st
import cv2
import numpy as np
def cartoonize_image(image):
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply a bilateral filter to reduce noise while preserving edges
smooth = cv2.bilateralFilter(gray, 9, 300, 300)
# Apply an edge-preserving filter to get the edges
edges = cv2.adaptiveThreshold(smooth, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 3)
# Combine the edges with the original image to create a cartoon effect
cartoon = cv2.bitwise_and(image, image, mask=edges)
return cartoon
def main():
st.title("Video to Cartoon Converter")
# Upload a video file
video_file = st.file_uploader("Choose a video file", type=["mp4"])
if video_file is not None:
# Read the video file
video = cv2.VideoCapture(video_file)
# Get video details
fps = video.get(cv2.CAP_PROP_FPS)
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Create a placeholder to display the processed video
cartoon_output = st.empty()
# Process each frame of the video
while True:
ret, frame = video.read()
if not ret:
break
# Cartoonize the frame
cartoon_frame = cartoonize_image(frame)
# Display the cartoonized frame
cartoon_output.image(cartoon_frame, channels="BGR", use_column_width=True)
st.video(video_file, format="video/mp4", start_time=0)
if __name__ == "__main__":
main()