Spaces:
Build error
Build error
Update colorization.py
Browse files- colorization.py +7 -75
colorization.py
CHANGED
|
@@ -6,9 +6,11 @@ from pathlib import Path
|
|
| 6 |
from datetime import datetime
|
| 7 |
from typing import Optional
|
| 8 |
|
|
|
|
| 9 |
model = hub.Module(name='deoldify')
|
| 10 |
# NOTE: Max is 45 with 11GB video cards. 35 is a good default
|
| 11 |
-
render_factor=
|
|
|
|
| 12 |
|
| 13 |
def colorize_image(image):
|
| 14 |
# now = datetime.now().strftime("%Y%m%d-%H%M%S-%f")
|
|
@@ -17,86 +19,17 @@ def colorize_image(image):
|
|
| 17 |
# if image is not None:
|
| 18 |
# image.save(f"./output/{now}-input.jpg")
|
| 19 |
model.predict(image.name)
|
| 20 |
-
return
|
| 21 |
-
|
| 22 |
-
# def inference(img, version, scale, weight):
|
| 23 |
-
def inferenceColorize(img, scale):
|
| 24 |
-
# weight /= 100
|
| 25 |
-
print(img, scale)
|
| 26 |
-
'''
|
| 27 |
-
try:
|
| 28 |
-
extension = os.path.splitext(os.path.basename(str(img)))[1]
|
| 29 |
-
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
| 30 |
-
if len(img.shape) == 3 and img.shape[2] == 4:
|
| 31 |
-
img_mode = 'RGBA'
|
| 32 |
-
elif len(img.shape) == 2: # for gray inputs
|
| 33 |
-
img_mode = None
|
| 34 |
-
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 35 |
-
else:
|
| 36 |
-
img_mode = None
|
| 37 |
-
h, w = img.shape[0:2]
|
| 38 |
-
if h < 300:
|
| 39 |
-
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
| 40 |
|
| 41 |
-
try:
|
| 42 |
-
# _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True, weight=weight)
|
| 43 |
-
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
| 44 |
-
except RuntimeError as error:
|
| 45 |
-
print('Error', error)
|
| 46 |
-
|
| 47 |
-
try:
|
| 48 |
-
if scale != 2:
|
| 49 |
-
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
| 50 |
-
h, w = img.shape[0:2]
|
| 51 |
-
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
| 52 |
-
except Exception as error:
|
| 53 |
-
print('wrong scale input.', error)
|
| 54 |
-
|
| 55 |
-
if img_mode == 'RGBA': # RGBA images should be saved in png format
|
| 56 |
-
extension = 'png'
|
| 57 |
-
else:
|
| 58 |
-
extension = 'jpg'
|
| 59 |
-
'''
|
| 60 |
-
os.makedirs('output', exist_ok=True)
|
| 61 |
-
model.predict(img.name)
|
| 62 |
-
save_path = f'output/DeOldify/'+Path(img.name).stem+'.{extension}'
|
| 63 |
-
|
| 64 |
-
'''
|
| 65 |
-
cv2.imwrite(save_path, output)
|
| 66 |
-
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
| 67 |
-
'''
|
| 68 |
-
return img, save_path
|
| 69 |
-
'''
|
| 70 |
-
except Exception as error:
|
| 71 |
-
print('global exception', error)
|
| 72 |
-
return None, None
|
| 73 |
-
'''
|
| 74 |
|
| 75 |
-
colorize = gr.Interface(
|
| 76 |
-
inferenceColorize, [
|
| 77 |
-
gr.Markdown("Colorize old black & white photos"),
|
| 78 |
-
gr.Image(type="filepath", label="Input"),
|
| 79 |
-
|
| 80 |
-
gr.inputs.Number(label="Rescaling factor", default=2),
|
| 81 |
-
gr.Button("Upscale & Colorize!")
|
| 82 |
-
|
| 83 |
-
# gr.Slider(0, 100, label='Weight, only for CodeFormer. 0 for better quality, 100 for better identity', default=50)
|
| 84 |
-
], [
|
| 85 |
-
gr.outputs.Image(type="numpy", label="Output (The colorized image)"),
|
| 86 |
-
gr.outputs.File(label="Download the colorized image")
|
| 87 |
-
])
|
| 88 |
-
|
| 89 |
-
colorize.queue(concurrency_count=4)
|
| 90 |
-
colorize.launch()
|
| 91 |
-
'''
|
| 92 |
def create_interface():
|
| 93 |
with gr.Blocks() as enhancer:
|
| 94 |
gr.Markdown("Colorize old black & white photos")
|
| 95 |
with gr.Column(scale=1, label = "Colorize photo", visible=True) as colorize_column:
|
| 96 |
colorize_input = gr.Image(type="file")
|
| 97 |
colorize_button = gr.Button("Colorize!")
|
| 98 |
-
colorize_output = gr.outputs.Image(type="numpy", label="Output
|
| 99 |
-
download_colorize_button = gr.outputs.File(label="Download
|
| 100 |
colorize_button.click(colorize_image, inputs=colorize_input, outputs=colorize_output)
|
| 101 |
enhancer.launch()
|
| 102 |
|
|
@@ -104,5 +37,4 @@ def run_code():
|
|
| 104 |
create_interface()
|
| 105 |
|
| 106 |
# The main function
|
| 107 |
-
run_code()
|
| 108 |
-
'''
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
from typing import Optional
|
| 8 |
|
| 9 |
+
|
| 10 |
model = hub.Module(name='deoldify')
|
| 11 |
# NOTE: Max is 45 with 11GB video cards. 35 is a good default
|
| 12 |
+
render_factor=35
|
| 13 |
+
|
| 14 |
|
| 15 |
def colorize_image(image):
|
| 16 |
# now = datetime.now().strftime("%Y%m%d-%H%M%S-%f")
|
|
|
|
| 19 |
# if image is not None:
|
| 20 |
# image.save(f"./output/{now}-input.jpg")
|
| 21 |
model.predict(image.name)
|
| 22 |
+
return './output/DeOldify/'+Path(image.name).stem+".png"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def create_interface():
|
| 26 |
with gr.Blocks() as enhancer:
|
| 27 |
gr.Markdown("Colorize old black & white photos")
|
| 28 |
with gr.Column(scale=1, label = "Colorize photo", visible=True) as colorize_column:
|
| 29 |
colorize_input = gr.Image(type="file")
|
| 30 |
colorize_button = gr.Button("Colorize!")
|
| 31 |
+
colorize_output = gr.outputs.Image(type="numpy", label="Output(The colorized image)")
|
| 32 |
+
download_colorize_button = gr.outputs.File(label="Download colorized image!")
|
| 33 |
colorize_button.click(colorize_image, inputs=colorize_input, outputs=colorize_output)
|
| 34 |
enhancer.launch()
|
| 35 |
|
|
|
|
| 37 |
create_interface()
|
| 38 |
|
| 39 |
# The main function
|
| 40 |
+
run_code()
|
|
|