converting to base64 instead of uint8
Browse files- DeFogify_Main.py +10 -7
DeFogify_Main.py
CHANGED
@@ -2,19 +2,19 @@ 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))
|
8 |
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (size, size))
|
9 |
dc_img = cv2.erode(min_img, kernel)
|
10 |
return dc_img
|
11 |
|
12 |
-
def get_atmo(img, percent=0.001):
|
13 |
-
mean_perpix = np.mean(img, axis=2).reshape(-1)
|
14 |
mean_topper = mean_perpix[:int(img.shape[0] * img.shape[1] * percent)]
|
15 |
return np.mean(mean_topper)
|
16 |
|
17 |
-
def get_trans(img, atom, w=0.95):
|
18 |
x = img / atom
|
19 |
t = 1 - w * dark_channel(x, 15)
|
20 |
return t
|
@@ -40,13 +40,16 @@ def dehaze(image):
|
|
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 =
|
44 |
|
45 |
result = np.empty_like(img)
|
46 |
for i in range(3):
|
47 |
result[:, :, i] = (img[:, :, i] - atom) / trans_guided + atom
|
48 |
|
49 |
-
|
|
|
|
|
50 |
|
51 |
-
|
|
|
52 |
PixelDehazer.launch()
|
|
|
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))
|
8 |
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (size, size))
|
9 |
dc_img = cv2.erode(min_img, kernel)
|
10 |
return dc_img
|
11 |
|
12 |
+
def get_atmo(img, percent = 0.001):
|
13 |
+
mean_perpix = np.mean(img, axis = 2).reshape(-1)
|
14 |
mean_topper = mean_perpix[:int(img.shape[0] * img.shape[1] * percent)]
|
15 |
return np.mean(mean_topper)
|
16 |
|
17 |
+
def get_trans(img, atom, w = 0.95):
|
18 |
x = img / atom
|
19 |
t = 1 - w * dark_channel(x, 15)
|
20 |
return t
|
|
|
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 |
+
# Create Gradio interface
|
54 |
+
PixelDehazer = gr.Interface(fn=dehaze, inputs=gr.Image(type="numpy"), outputs="image")
|
55 |
PixelDehazer.launch()
|