-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsite.py
112 lines (91 loc) · 3.59 KB
/
website.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import cv2
import dehazing_algorithm as alg
import time
import numpy as np
from PIL import Image
import streamlit as st
import tempfile
from moviepy.editor import ImageSequenceClip
import os
def dehaze_image(image):
cap = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
start_time = time.time()
output = alg.dehaze_frame(cap, 4, 8) * 255
end_time = time.time()
total_time = end_time - start_time
output = output.astype(np.uint8)
return Image.fromarray(cv2.cvtColor(output, cv2.COLOR_BGR2RGB)), total_time
def dehaze_video(file_buffer):
skip = 1
tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(file_buffer.read())
cap = cv2.VideoCapture(tfile.name)
if not cap.isOpened():
st.error("Error: Could not open video")
return
frames = []
dehazed_frames = []
with st.spinner("Processing video..."):
i=0
while cap.isOpened():
success, frame = cap.read()
if i % skip == 0:
if not success:
break
frames.append(frame)
dehazed_frame = alg.dehaze_frame(frame, 5, n=8) * 255
dehazed_frames.append(dehazed_frame.astype(np.uint8))
i += 1
cap.release()
if frames:
st.subheader("Original Video")
st.video(tfile.name)
st.subheader("Dehazed Video")
dehazed_frames_rgb = [cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) for frame in dehazed_frames]
clip = ImageSequenceClip(dehazed_frames_rgb, fps=30)
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
output_filename = output_file.name
output_file.close()
clip.write_videofile(output_filename, codec='libx264')
with open(output_filename, "rb") as file:
video_bytes = file.read()
st.video(video_bytes)
else:
st.error("No frames were processed from the video.")
st.set_page_config(page_title="Dehazing App", page_icon="🌫️", layout="wide")
st.title("Dehazing System")
st.sidebar.title("Options")
app_mode = st.sidebar.selectbox("Choose the mode", ["Upload Image", "Upload Video", "Realtime Dehazing"])
if app_mode == "Upload Image":
st.subheader("Upload Image to be Dehazed")
uploaded_image = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg", "gif"])
if uploaded_image is not None:
image = Image.open(uploaded_image)
st.image(image, caption="Original Image", use_column_width=True)
if st.button("Dehaze Image"):
dehazed_image, processing_time = dehaze_image(image)
st.image(dehazed_image, caption="Dehazed Image", use_column_width=True)
st.write(f"Time taken to dehaze: {processing_time:.2f} seconds")
elif app_mode == "Upload Video":
st.subheader("Upload Video to be Dehazed")
uploaded_video = st.file_uploader("Choose a video...", type=["mp4", "avi", "mkv"])
if uploaded_video is not None:
if st.button("Dehaze Video"):
dehaze_video(uploaded_video)
elif app_mode == "Realtime Dehazing":
st.subheader("Realtime Dehazing")
# dehaze_realtime()
st.write("For realtime dehazing, please download our desktop application:")
st.markdown("[Download Desktop App](https://github.com/chhaviGupta986/Dehazing-Software/releases/tag/v1.0.0)")
st.markdown("""
<style>
.stApp {
background: #1c1c1c;
color: white;
}
.css-1offfwp, .css-1offfwp:visited {
color: white;
text-decoration: none;
}
</style>
""", unsafe_allow_html=True)