File size: 2,278 Bytes
0dca4ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from PIL import Image
import sys
print(sys.executable)
from src.remover import BackgroundRemover
import cv2
from moviepy.editor import *
import os

def save_uploaded_file(uploaded_file,filename, save_dir="./static/video"):
    file_path = os.path.join(save_dir, filename)
    with open(file_path, "wb") as f:
        f.write(uploaded_file.getbuffer())
    return file_path


def process_video(input_path, output_path,background_path):

    # Open the video file
    video_capture = cv2.VideoCapture(input_path)
    # Get video properties
    images = []
    fps = int(video_capture.get(cv2.CAP_PROP_FPS))
    # Process each frame in the video
    while True:
        ret, frame = video_capture.read()
        if not ret:
            break
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        processed_frame = obj.process_video(frame,background_path)
        images.append(processed_frame)
        if cv2.waitKey(1) & 0xFF == ord('s'):
            break

    cv2.destroyAllWindows()
    video_capture.release()
    clip = ImageSequenceClip(images, fps =fps)
    clip.write_videofile(output_path)


st.set_page_config(
    page_title="Video Background Remover",
    page_icon="👋",
)

if __name__ == "__main__":

    obj = BackgroundRemover()

    st.title("DEMO")
    st.write("Upload an Video")

    uploaded_file = st.file_uploader("Choose a file",type=["mp4", "avi", "mov"])
    VIDEO_DIR = "./output"

    if uploaded_file is not None:
        # To read file as bytes:
        bytes_data = uploaded_file.getvalue()
        file_name = "upload.mp4"
        OUT_PATH =  os.path.join(VIDEO_DIR,"results.mp4")
        BACKGROUND_PATH = os.path.join(os.getcwd(),"static/background/2.png")

        col1, col2 = st.columns(2)
         
        with col1:
            st.video(data=uploaded_file)
            video_path = save_uploaded_file(uploaded_file,file_name)
            st.success(f"Video saved as {video_path}")
            flag1 = 1
            process_video(video_path,OUT_PATH,BACKGROUND_PATH)
            flag2 = 1

        with col2:
            if flag1==1 and flag2==1:
                st.video(data=OUT_PATH)
                st.success(f"Removing Background")
            
    else:

        st.write("Failed to upload video")