Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,199 +1,199 @@
|
|
1 |
-
import tensorflow as tf
|
2 |
-
from keras import models
|
3 |
-
import numpy as np
|
4 |
-
import gradio as gr
|
5 |
-
import cv2
|
6 |
-
|
7 |
-
# Load the model
|
8 |
-
try:
|
9 |
-
generator = models.load_model("generator.keras")
|
10 |
-
print("Model loaded successfully!")
|
11 |
-
except Exception as e:
|
12 |
-
print("Error loading model:", e)
|
13 |
-
|
14 |
-
|
15 |
-
# Function to preprocess the image (resize, normalize)
|
16 |
-
def preprocess_image(img):
|
17 |
-
img = cv2.resize(img, (256, 256))
|
18 |
-
|
19 |
-
# Convert L to range [-1, 1]
|
20 |
-
img = img.astype("float32")
|
21 |
-
img = (img / 127.5) - 1
|
22 |
-
|
23 |
-
# Convert to tensor
|
24 |
-
img = tf.convert_to_tensor(img, dtype=tf.float32)
|
25 |
-
|
26 |
-
img = tf.expand_dims(img, axis=-1) # Add image dimension
|
27 |
-
img = tf.expand_dims(img, axis=0) # Add batch dimension
|
28 |
-
|
29 |
-
return img
|
30 |
-
|
31 |
-
|
32 |
-
# Function to postprocess the image (denormalize)
|
33 |
-
def postprocess_image(img):
|
34 |
-
img = cv2.cvtColor(((img + 1) * 127.5).numpy().astype(np.uint8), cv2.COLOR_LAB2RGB)
|
35 |
-
return np.uint8(np.clip(img, 0, 255))
|
36 |
-
|
37 |
-
|
38 |
-
# Function to adjust brightness
|
39 |
-
def adjust_brightness(img, brightness=0.0):
|
40 |
-
# Apply brightness adjustment
|
41 |
-
img = cv2.convertScaleAbs(img, beta=int(brightness * 127.0 / 4.0))
|
42 |
-
return np.uint8(np.clip(img, 0, 255))
|
43 |
-
|
44 |
-
|
45 |
-
# Function to adjust contrast
|
46 |
-
def adjust_contrast(img, contrast=0.0):
|
47 |
-
# Apply contrast adjustment
|
48 |
-
img = cv2.convertScaleAbs(img, alpha=(contrast * 0.75 + 1.0))
|
49 |
-
return np.uint8(np.clip(img, 0, 255))
|
50 |
-
|
51 |
-
|
52 |
-
# Function to adjust hue
|
53 |
-
def adjust_hue(img, hue_shift=0.0):
|
54 |
-
# Convert the image to HSV
|
55 |
-
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
56 |
-
|
57 |
-
# Adjust the hue channel (value is between 0 and 179 in OpenCV's HSV)
|
58 |
-
hsv_img[:, :, 0] = (
|
59 |
-
hsv_img[:, :, 0] + hue_shift * 90
|
60 |
-
) % 180 # Hue is wrapped in OpenCV HSV format
|
61 |
-
|
62 |
-
# Convert back to BGR
|
63 |
-
img = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
|
64 |
-
|
65 |
-
return np.uint8(np.clip(img, 0, 255))
|
66 |
-
|
67 |
-
|
68 |
-
def adjust_saturation(img, saturation_factor=0.0):
|
69 |
-
# Convert the image to HSV
|
70 |
-
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
71 |
-
|
72 |
-
# Adjust the saturation channel (index 1 in HSV)
|
73 |
-
hsv_img[:, :, 1] = np.clip(hsv_img[:, :, 1] * (saturation_factor + 1.0), 0, 255)
|
74 |
-
|
75 |
-
# Convert back to BGR
|
76 |
-
img = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
|
77 |
-
|
78 |
-
return np.uint8(np.clip(img, 0, 255))
|
79 |
-
|
80 |
-
|
81 |
-
# Define the inference function
|
82 |
-
def colorize_image(input_image):
|
83 |
-
# Preprocess the image for the model
|
84 |
-
preprocessed_image = preprocess_image(input_image)
|
85 |
-
|
86 |
-
# Predict using the model
|
87 |
-
output_ab = generator.predict(preprocessed_image)
|
88 |
-
output = tf.concat([preprocessed_image[0], output_ab[0]], axis=-1)
|
89 |
-
|
90 |
-
# Postprocess the output
|
91 |
-
output_image = postprocess_image(output)
|
92 |
-
|
93 |
-
return output_image
|
94 |
-
|
95 |
-
|
96 |
-
# Function to colorize and store the result for further manipulation
|
97 |
-
def colorize_and_store(img):
|
98 |
-
# Colorize the image
|
99 |
-
colorized_image = colorize_image(img)
|
100 |
-
|
101 |
-
# Return the colorized image for further manipulation (no model call)
|
102 |
-
return colorized_image, colorized_image
|
103 |
-
|
104 |
-
|
105 |
-
def make_grayscale_256(img):
|
106 |
-
img = cv2.resize(img, (256, 256))
|
107 |
-
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
108 |
-
|
109 |
-
return img
|
110 |
-
|
111 |
-
|
112 |
-
css = """
|
113 |
-
h1 {
|
114 |
-
text-align: center;
|
115 |
-
display:block;
|
116 |
-
font-size:
|
117 |
-
}
|
118 |
-
p {
|
119 |
-
text-align: center;
|
120 |
-
display:block;
|
121 |
-
font-size:2rem;
|
122 |
-
}
|
123 |
-
#input-image img {
|
124 |
-
filter: grayscale(1);
|
125 |
-
}
|
126 |
-
"""
|
127 |
-
|
128 |
-
# Gradio Interface
|
129 |
-
with gr.Blocks(css=css) as demo:
|
130 |
-
demo.title = "Portrait Colorizer"
|
131 |
-
# Add a title
|
132 |
-
gr.Markdown("# Portrait Colorizer")
|
133 |
-
gr.Markdown(
|
134 |
-
"Upload a grayscale image to colorize it and fine-tune the output using the sliders below."
|
135 |
-
)
|
136 |
-
|
137 |
-
with gr.Row():
|
138 |
-
input_image = gr.Image(
|
139 |
-
type="numpy",
|
140 |
-
label="Grayscale Image",
|
141 |
-
image_mode="L",
|
142 |
-
height=256,
|
143 |
-
width=256,
|
144 |
-
elem_id="input-image",
|
145 |
-
)
|
146 |
-
output_image = gr.Image(
|
147 |
-
type="numpy",
|
148 |
-
label="Colorized Image",
|
149 |
-
image_mode="RGB",
|
150 |
-
height=256,
|
151 |
-
width=256,
|
152 |
-
)
|
153 |
-
process_button = gr.Button("Colorize")
|
154 |
-
bright_slider = gr.Slider(-1.0, 1.0, value=0.0, label="Brightness")
|
155 |
-
cont_slider = gr.Slider(-1.0, 1.0, value=0.0, label="Contrast")
|
156 |
-
sat_slider = gr.Slider(-1.0, 1.0, value=0.0, label="Saturation")
|
157 |
-
hue_slider = gr.Slider(-1.0, 1.0, value=0.0, label="Hue")
|
158 |
-
|
159 |
-
# Initially colorize and display the image when it is uploaded
|
160 |
-
colorized_image = gr.State()
|
161 |
-
|
162 |
-
# Button click triggers processing
|
163 |
-
process_button.click(
|
164 |
-
fn=colorize_and_store,
|
165 |
-
inputs=input_image,
|
166 |
-
outputs=[colorized_image, output_image],
|
167 |
-
)
|
168 |
-
|
169 |
-
# Apply hue adjustment to the stored colorized image (no re-generation)
|
170 |
-
bright_slider.change(
|
171 |
-
fn=adjust_brightness,
|
172 |
-
inputs=[colorized_image, bright_slider],
|
173 |
-
outputs=output_image, # Update output image
|
174 |
-
)
|
175 |
-
|
176 |
-
# Apply hue adjustment to the stored colorized image (no re-generation)
|
177 |
-
cont_slider.change(
|
178 |
-
fn=adjust_contrast,
|
179 |
-
inputs=[colorized_image, cont_slider],
|
180 |
-
outputs=output_image, # Update output image
|
181 |
-
)
|
182 |
-
|
183 |
-
# Apply hue adjustment to the stored colorized image (no re-generation)
|
184 |
-
hue_slider.change(
|
185 |
-
fn=adjust_hue,
|
186 |
-
inputs=[colorized_image, hue_slider],
|
187 |
-
outputs=output_image, # Update output image
|
188 |
-
)
|
189 |
-
|
190 |
-
# Apply saturation adjustment to the stored colorized image (no re-generation)
|
191 |
-
sat_slider.change(
|
192 |
-
fn=adjust_saturation,
|
193 |
-
inputs=[colorized_image, sat_slider],
|
194 |
-
outputs=output_image, # Update output image
|
195 |
-
)
|
196 |
-
|
197 |
-
# Launch the app
|
198 |
-
if __name__ == "__main__":
|
199 |
-
demo.launch()
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from keras import models
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
# Load the model
|
8 |
+
try:
|
9 |
+
generator = models.load_model("generator.keras")
|
10 |
+
print("Model loaded successfully!")
|
11 |
+
except Exception as e:
|
12 |
+
print("Error loading model:", e)
|
13 |
+
|
14 |
+
|
15 |
+
# Function to preprocess the image (resize, normalize)
|
16 |
+
def preprocess_image(img):
|
17 |
+
img = cv2.resize(img, (256, 256))
|
18 |
+
|
19 |
+
# Convert L to range [-1, 1]
|
20 |
+
img = img.astype("float32")
|
21 |
+
img = (img / 127.5) - 1
|
22 |
+
|
23 |
+
# Convert to tensor
|
24 |
+
img = tf.convert_to_tensor(img, dtype=tf.float32)
|
25 |
+
|
26 |
+
img = tf.expand_dims(img, axis=-1) # Add image dimension
|
27 |
+
img = tf.expand_dims(img, axis=0) # Add batch dimension
|
28 |
+
|
29 |
+
return img
|
30 |
+
|
31 |
+
|
32 |
+
# Function to postprocess the image (denormalize)
|
33 |
+
def postprocess_image(img):
|
34 |
+
img = cv2.cvtColor(((img + 1) * 127.5).numpy().astype(np.uint8), cv2.COLOR_LAB2RGB)
|
35 |
+
return np.uint8(np.clip(img, 0, 255))
|
36 |
+
|
37 |
+
|
38 |
+
# Function to adjust brightness
|
39 |
+
def adjust_brightness(img, brightness=0.0):
|
40 |
+
# Apply brightness adjustment
|
41 |
+
img = cv2.convertScaleAbs(img, beta=int(brightness * 127.0 / 4.0))
|
42 |
+
return np.uint8(np.clip(img, 0, 255))
|
43 |
+
|
44 |
+
|
45 |
+
# Function to adjust contrast
|
46 |
+
def adjust_contrast(img, contrast=0.0):
|
47 |
+
# Apply contrast adjustment
|
48 |
+
img = cv2.convertScaleAbs(img, alpha=(contrast * 0.75 + 1.0))
|
49 |
+
return np.uint8(np.clip(img, 0, 255))
|
50 |
+
|
51 |
+
|
52 |
+
# Function to adjust hue
|
53 |
+
def adjust_hue(img, hue_shift=0.0):
|
54 |
+
# Convert the image to HSV
|
55 |
+
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
56 |
+
|
57 |
+
# Adjust the hue channel (value is between 0 and 179 in OpenCV's HSV)
|
58 |
+
hsv_img[:, :, 0] = (
|
59 |
+
hsv_img[:, :, 0] + hue_shift * 90
|
60 |
+
) % 180 # Hue is wrapped in OpenCV HSV format
|
61 |
+
|
62 |
+
# Convert back to BGR
|
63 |
+
img = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
|
64 |
+
|
65 |
+
return np.uint8(np.clip(img, 0, 255))
|
66 |
+
|
67 |
+
|
68 |
+
def adjust_saturation(img, saturation_factor=0.0):
|
69 |
+
# Convert the image to HSV
|
70 |
+
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
71 |
+
|
72 |
+
# Adjust the saturation channel (index 1 in HSV)
|
73 |
+
hsv_img[:, :, 1] = np.clip(hsv_img[:, :, 1] * (saturation_factor + 1.0), 0, 255)
|
74 |
+
|
75 |
+
# Convert back to BGR
|
76 |
+
img = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
|
77 |
+
|
78 |
+
return np.uint8(np.clip(img, 0, 255))
|
79 |
+
|
80 |
+
|
81 |
+
# Define the inference function
|
82 |
+
def colorize_image(input_image):
|
83 |
+
# Preprocess the image for the model
|
84 |
+
preprocessed_image = preprocess_image(input_image)
|
85 |
+
|
86 |
+
# Predict using the model
|
87 |
+
output_ab = generator.predict(preprocessed_image)
|
88 |
+
output = tf.concat([preprocessed_image[0], output_ab[0]], axis=-1)
|
89 |
+
|
90 |
+
# Postprocess the output
|
91 |
+
output_image = postprocess_image(output)
|
92 |
+
|
93 |
+
return output_image
|
94 |
+
|
95 |
+
|
96 |
+
# Function to colorize and store the result for further manipulation
|
97 |
+
def colorize_and_store(img):
|
98 |
+
# Colorize the image
|
99 |
+
colorized_image = colorize_image(img)
|
100 |
+
|
101 |
+
# Return the colorized image for further manipulation (no model call)
|
102 |
+
return colorized_image, colorized_image
|
103 |
+
|
104 |
+
|
105 |
+
def make_grayscale_256(img):
|
106 |
+
img = cv2.resize(img, (256, 256))
|
107 |
+
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
108 |
+
|
109 |
+
return img
|
110 |
+
|
111 |
+
|
112 |
+
css = """
|
113 |
+
h1 {
|
114 |
+
text-align: center;
|
115 |
+
display:block;
|
116 |
+
font-size:4rem;
|
117 |
+
}
|
118 |
+
p {
|
119 |
+
text-align: center;
|
120 |
+
display:block;
|
121 |
+
font-size:2rem;
|
122 |
+
}
|
123 |
+
#input-image img {
|
124 |
+
filter: grayscale(1);
|
125 |
+
}
|
126 |
+
"""
|
127 |
+
|
128 |
+
# Gradio Interface
|
129 |
+
with gr.Blocks(css=css) as demo:
|
130 |
+
demo.title = "Portrait Colorizer"
|
131 |
+
# Add a title
|
132 |
+
gr.Markdown("# Portrait Colorizer")
|
133 |
+
gr.Markdown(
|
134 |
+
"Upload a grayscale image to colorize it and fine-tune the output using the sliders below."
|
135 |
+
)
|
136 |
+
|
137 |
+
with gr.Row():
|
138 |
+
input_image = gr.Image(
|
139 |
+
type="numpy",
|
140 |
+
label="Grayscale Image",
|
141 |
+
image_mode="L",
|
142 |
+
height=256,
|
143 |
+
width=256,
|
144 |
+
elem_id="input-image",
|
145 |
+
)
|
146 |
+
output_image = gr.Image(
|
147 |
+
type="numpy",
|
148 |
+
label="Colorized Image",
|
149 |
+
image_mode="RGB",
|
150 |
+
height=256,
|
151 |
+
width=256,
|
152 |
+
)
|
153 |
+
process_button = gr.Button("Colorize")
|
154 |
+
bright_slider = gr.Slider(-1.0, 1.0, value=0.0, label="Brightness")
|
155 |
+
cont_slider = gr.Slider(-1.0, 1.0, value=0.0, label="Contrast")
|
156 |
+
sat_slider = gr.Slider(-1.0, 1.0, value=0.0, label="Saturation")
|
157 |
+
hue_slider = gr.Slider(-1.0, 1.0, value=0.0, label="Hue")
|
158 |
+
|
159 |
+
# Initially colorize and display the image when it is uploaded
|
160 |
+
colorized_image = gr.State()
|
161 |
+
|
162 |
+
# Button click triggers processing
|
163 |
+
process_button.click(
|
164 |
+
fn=colorize_and_store,
|
165 |
+
inputs=input_image,
|
166 |
+
outputs=[colorized_image, output_image],
|
167 |
+
)
|
168 |
+
|
169 |
+
# Apply hue adjustment to the stored colorized image (no re-generation)
|
170 |
+
bright_slider.change(
|
171 |
+
fn=adjust_brightness,
|
172 |
+
inputs=[colorized_image, bright_slider],
|
173 |
+
outputs=output_image, # Update output image
|
174 |
+
)
|
175 |
+
|
176 |
+
# Apply hue adjustment to the stored colorized image (no re-generation)
|
177 |
+
cont_slider.change(
|
178 |
+
fn=adjust_contrast,
|
179 |
+
inputs=[colorized_image, cont_slider],
|
180 |
+
outputs=output_image, # Update output image
|
181 |
+
)
|
182 |
+
|
183 |
+
# Apply hue adjustment to the stored colorized image (no re-generation)
|
184 |
+
hue_slider.change(
|
185 |
+
fn=adjust_hue,
|
186 |
+
inputs=[colorized_image, hue_slider],
|
187 |
+
outputs=output_image, # Update output image
|
188 |
+
)
|
189 |
+
|
190 |
+
# Apply saturation adjustment to the stored colorized image (no re-generation)
|
191 |
+
sat_slider.change(
|
192 |
+
fn=adjust_saturation,
|
193 |
+
inputs=[colorized_image, sat_slider],
|
194 |
+
outputs=output_image, # Update output image
|
195 |
+
)
|
196 |
+
|
197 |
+
# Launch the app
|
198 |
+
if __name__ == "__main__":
|
199 |
+
demo.launch()
|