File size: 1,620 Bytes
2638282
0213ced
73ce7ab
2638282
0213ced
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()