Upload DeFogify_Main.py
Browse files- DeFogify_Main.py +135 -10
DeFogify_Main.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
| 5 |
def dark_channel(img, size=15):
|
| 6 |
r, g, b = cv2.split(img)
|
| 7 |
min_img = cv2.min(r, cv2.min(g, b))
|
|
@@ -36,26 +40,123 @@ def guided_filter(p, i, r, e):
|
|
| 36 |
def dehaze(image):
|
| 37 |
img = image.astype('float64') / 255
|
| 38 |
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).astype('float64') / 255
|
| 39 |
-
|
| 40 |
atom = get_atmo(img)
|
| 41 |
trans = get_trans(img, atom)
|
| 42 |
trans_guided = guided_filter(trans, img_gray, 20, 0.0001)
|
| 43 |
trans_guided = np.maximum(trans_guided, 0.25) # Ensure trans_guided is not below 0.25
|
| 44 |
-
|
| 45 |
result = np.empty_like(img)
|
| 46 |
for i in range(3):
|
| 47 |
result[:, :, i] = (img[:, :, i] - atom) / trans_guided + atom
|
| 48 |
-
|
| 49 |
-
# Ensure the result is in the range [0, 1]
|
| 50 |
result = np.clip(result, 0, 1)
|
| 51 |
return (result * 255).astype(np.uint8)
|
| 52 |
|
| 53 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
example_images = [
|
| 55 |
"Sample Images for Testing/ai-generated-9025430_1280.jpg",
|
| 56 |
"Sample Images for Testing/meadow-5648849_1280.jpg",
|
| 57 |
"Sample Images for Testing/mountains-7662717_1280.jpg",
|
| 58 |
-
"Sample Images for Testing/mountains-8292685_1280.jpg",
|
| 59 |
"Sample Images for Testing/nature-6722031_1280.jpg"
|
| 60 |
]
|
| 61 |
|
|
@@ -66,13 +167,37 @@ for i, img_path in enumerate(example_images):
|
|
| 66 |
cv2.imwrite(save_path, img)
|
| 67 |
example_paths.append([save_path])
|
| 68 |
|
| 69 |
-
#
|
| 70 |
PixelDehazer = gr.Interface(
|
| 71 |
-
fn=
|
| 72 |
inputs=gr.Image(type="numpy"),
|
| 73 |
outputs="image",
|
| 74 |
examples=example_paths,
|
| 75 |
-
cache_examples=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
|
| 8 |
+
# Original Functions
|
| 9 |
def dark_channel(img, size=15):
|
| 10 |
r, g, b = cv2.split(img)
|
| 11 |
min_img = cv2.min(r, cv2.min(g, b))
|
|
|
|
| 40 |
def dehaze(image):
|
| 41 |
img = image.astype('float64') / 255
|
| 42 |
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).astype('float64') / 255
|
|
|
|
| 43 |
atom = get_atmo(img)
|
| 44 |
trans = get_trans(img, atom)
|
| 45 |
trans_guided = guided_filter(trans, img_gray, 20, 0.0001)
|
| 46 |
trans_guided = np.maximum(trans_guided, 0.25) # Ensure trans_guided is not below 0.25
|
|
|
|
| 47 |
result = np.empty_like(img)
|
| 48 |
for i in range(3):
|
| 49 |
result[:, :, i] = (img[:, :, i] - atom) / trans_guided + atom
|
|
|
|
|
|
|
| 50 |
result = np.clip(result, 0, 1)
|
| 51 |
return (result * 255).astype(np.uint8)
|
| 52 |
|
| 53 |
+
# Single Image Processing
|
| 54 |
+
def process_single_image(image):
|
| 55 |
+
dehazed_img = dehaze(image)
|
| 56 |
+
return dehazed_img
|
| 57 |
+
|
| 58 |
+
# Batch Processing Function for Multiple Images with Progress Bar
|
| 59 |
+
def process_images(files):
|
| 60 |
+
temp_dir = tempfile.mkdtemp()
|
| 61 |
+
output_files = []
|
| 62 |
+
|
| 63 |
+
for file in tqdm(files, desc="Processing Images"):
|
| 64 |
+
img = cv2.imread(file.name)
|
| 65 |
+
if img is not None:
|
| 66 |
+
dehazed_img = dehaze(img)
|
| 67 |
+
output_path = os.path.join(temp_dir, os.path.basename(file.name))
|
| 68 |
+
cv2.imwrite(output_path, dehazed_img)
|
| 69 |
+
output_files.append(output_path)
|
| 70 |
+
|
| 71 |
+
return output_files
|
| 72 |
+
|
| 73 |
+
# Video Dehazing Function with Gradio Progress Bar and Error Handling
|
| 74 |
+
def dehaze_video(input_video_path, output_video_path, progress=None):
|
| 75 |
+
try:
|
| 76 |
+
cap = cv2.VideoCapture(input_video_path)
|
| 77 |
+
if not cap.isOpened():
|
| 78 |
+
raise ValueError("Error: Could not open video.")
|
| 79 |
+
|
| 80 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 81 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 82 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 83 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 84 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 85 |
+
|
| 86 |
+
if total_frames <= 0: # Assume a constant count for webcam scenarios
|
| 87 |
+
total_frames = 1000
|
| 88 |
+
|
| 89 |
+
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
|
| 90 |
+
frame_count = 0
|
| 91 |
+
|
| 92 |
+
if progress is not None:
|
| 93 |
+
progress(0, desc="Processing Video", unit="frame")
|
| 94 |
+
|
| 95 |
+
while cap.isOpened():
|
| 96 |
+
ret, frame = cap.read()
|
| 97 |
+
if not ret:
|
| 98 |
+
break
|
| 99 |
+
dehazed_frame = dehaze(frame)
|
| 100 |
+
out.write(dehazed_frame)
|
| 101 |
+
frame_count += 1
|
| 102 |
+
|
| 103 |
+
if progress is not None:
|
| 104 |
+
progress(frame_count / total_frames) # Ensure progress is within 0-1 range
|
| 105 |
+
|
| 106 |
+
cap.release()
|
| 107 |
+
out.release()
|
| 108 |
+
print(f"\nDehazed video saved to: {output_video_path}")
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"An error occurred during video processing: {e}")
|
| 111 |
+
|
| 112 |
+
# Gradio Video Processing Wrapper
|
| 113 |
+
def process_video(file):
|
| 114 |
+
input_video_path = file # File is a string representing the path
|
| 115 |
+
output_video_path = os.path.join(tempfile.mkdtemp(), "dehazed_video.mp4")
|
| 116 |
+
progress = gr.Progress()
|
| 117 |
+
dehaze_video(input_video_path, output_video_path, progress)
|
| 118 |
+
return output_video_path
|
| 119 |
+
|
| 120 |
+
# Real-Time Webcam Processing with Gradio Progress Bar
|
| 121 |
+
def dehaze_webcam(progress=gr.Progress()):
|
| 122 |
+
try:
|
| 123 |
+
cap = cv2.VideoCapture(0) # Capture from the first webcam
|
| 124 |
+
if not cap.isOpened():
|
| 125 |
+
raise ValueError("Unable to open webcam")
|
| 126 |
+
|
| 127 |
+
frame_count = 0
|
| 128 |
+
total_frames = 100 # Arbitrary number for progress bar
|
| 129 |
+
progress(0, desc="Processing Webcam Feed", unit="frame")
|
| 130 |
+
|
| 131 |
+
while frame_count < total_frames:
|
| 132 |
+
ret, frame = cap.read()
|
| 133 |
+
if not ret:
|
| 134 |
+
break
|
| 135 |
+
dehazed_frame = dehaze(frame)
|
| 136 |
+
frame_count += 1
|
| 137 |
+
progress(frame_count / total_frames) # Ensure progress is within 0-1 range
|
| 138 |
+
|
| 139 |
+
cv2.imshow('Dehazed Webcam Feed', dehazed_frame)
|
| 140 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 141 |
+
break
|
| 142 |
+
|
| 143 |
+
cap.release()
|
| 144 |
+
cv2.destroyAllWindows()
|
| 145 |
+
progress(1) # Ensure progress bar reaches 100%
|
| 146 |
+
except Exception as e:
|
| 147 |
+
print(f"An error occurred during webcam processing: {e}")
|
| 148 |
+
|
| 149 |
+
# Gradio Webcam Processing Wrapper
|
| 150 |
+
def process_webcam():
|
| 151 |
+
progress = gr.Progress()
|
| 152 |
+
dehaze_webcam(progress)
|
| 153 |
+
return "Webcam processing completed."
|
| 154 |
+
|
| 155 |
+
# Example Images for Testing
|
| 156 |
example_images = [
|
| 157 |
"Sample Images for Testing/ai-generated-9025430_1280.jpg",
|
| 158 |
"Sample Images for Testing/meadow-5648849_1280.jpg",
|
| 159 |
"Sample Images for Testing/mountains-7662717_1280.jpg",
|
|
|
|
| 160 |
"Sample Images for Testing/nature-6722031_1280.jpg"
|
| 161 |
]
|
| 162 |
|
|
|
|
| 167 |
cv2.imwrite(save_path, img)
|
| 168 |
example_paths.append([save_path])
|
| 169 |
|
| 170 |
+
# Gradio Interfaces
|
| 171 |
PixelDehazer = gr.Interface(
|
| 172 |
+
fn=process_single_image,
|
| 173 |
inputs=gr.Image(type="numpy"),
|
| 174 |
outputs="image",
|
| 175 |
examples=example_paths,
|
| 176 |
+
cache_examples=False,
|
| 177 |
+
description="Upload a single image to remove haze."
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
BatchDehazer = gr.Interface(
|
| 181 |
+
fn=process_images,
|
| 182 |
+
inputs=gr.Files(label="Upload Multiple Images", file_types=["image"]),
|
| 183 |
+
outputs=gr.Files(label="Download Dehazed Images"),
|
| 184 |
+
description="Upload multiple images to remove haze. Download the processed dehazed images."
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
VideoDehazer = gr.Interface(
|
| 188 |
+
fn=process_video,
|
| 189 |
+
inputs=gr.Video(label="Upload a Video"),
|
| 190 |
+
outputs=gr.File(label="Download Dehazed Video"),
|
| 191 |
+
description="Upload a video to remove haze. Download the processed dehazed video."
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
# Combined Gradio App
|
| 195 |
+
app = gr.TabbedInterface(
|
| 196 |
+
[PixelDehazer, BatchDehazer, VideoDehazer],
|
| 197 |
+
["Single Image Dehazing", "Batch Image Dehazing", "Video Dehazing"],
|
| 198 |
+
title="DeFogify App"
|
| 199 |
)
|
| 200 |
|
| 201 |
+
# Launch the Gradio App
|
| 202 |
+
if __name__ == "__main__":
|
| 203 |
+
app.launch()
|