-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathgfpgan.py
executable file
Β·129 lines (107 loc) Β· 3.97 KB
/
gfpgan.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# https://github.com/Woolverine94/biniou
# gfpgan.py
import gradio as gr
import os
import cv2
import PIL
import torch
import numpy as np
from gfpgan.utils import GFPGANer
from ressources.common import *
from huggingface_hub import snapshot_download, hf_hub_download
device_label_gfpgan, model_arch = detect_device()
device_gfpgan = torch.device(device_label_gfpgan)
model_path_gfpgan = "./models/gfpgan/"
os.makedirs(model_path_gfpgan, exist_ok=True)
model_list_gfpgan = [
"leonelhs/gfpgan",
]
variant_list_gfpgan = [
"GFPGANv1.pth",
"GFPGANCleanv1-NoCE-C2.pth",
"GFPGANv1.2.pth",
"GFPGANv1.3.pth",
"GFPGANv1.4.pth",
# "RestoreFormer.pth",
]
@metrics_decoration
def image_gfpgan(modelid_gfpgan, variantid_gfpgan, img_gfpgan, progress_gfpgan=gr.Progress(track_tqdm=True)):
print(">>>[GFPGAN π]: starting module")
path_gfpgan = os.path.join(model_path_gfpgan, variantid_gfpgan)
device = torch.device(device_gfpgan)
if os.path.exists(path_gfpgan) == False :
snapshot_path_gfpgan = snapshot_download(
repo_id=modelid_gfpgan,
local_dir=model_path_gfpgan,
local_dir_use_symlinks=False,
resume_download=True,
# local_files_only=True if offline_test() else None
)
path_gfpgan = os.path.join(snapshot_path_gfpgan, variantid_gfpgan)
model_gfpgan = GFPGANer(
model_path=path_gfpgan,
upscale=1,
arch='clean',
channel_multiplier=2
)
image_inter_gfpgan = np.array(Image.open(img_gfpgan).convert('RGB'))
image_input_gfpgan = cv2.cvtColor(image_inter_gfpgan, cv2.COLOR_RGB2BGR)
_, _, image_gfpgan = model_gfpgan.enhance(
image_input_gfpgan,
has_aligned=False,
only_center_face=False,
paste_back=True
)
final_image = []
savename = name_image()
image_gfpgan = cv2.cvtColor(image_gfpgan, cv2.COLOR_BGR2RGB)
image_gfpgan_save = Image.fromarray(image_gfpgan)
image_gfpgan_save.save(savename)
final_image.append(savename)
print(f">>>[GFPGAN π]: generated 1 batch(es) of 1")
reporting_gfpgan = f">>>[GFPGAN π]: "+\
f"Settings : Model={modelid_gfpgan} | "+\
f"Variant={variantid_gfpgan}"
print(reporting_gfpgan)
exif_writer_png(reporting_gfpgan, final_image)
del model_gfpgan, image_inter_gfpgan, image_input_gfpgan, image_gfpgan
clean_ram()
print(f">>>[GFPGAN π]: leaving module")
return final_image, final_image
def image_gfpgan_mini(img_gfpgan):
modelid_gfpgan = model_list_gfpgan[0]
variantid_gfpgan = variant_list_gfpgan[4]
path_gfpgan = os.path.join(model_path_gfpgan, variantid_gfpgan)
device = torch.device(device_gfpgan)
if os.path.exists(path_gfpgan) == False :
snapshot_path_gfpgan = snapshot_download(
repo_id=modelid_gfpgan,
local_dir=model_path_gfpgan,
local_dir_use_symlinks=False,
resume_download=True,
# local_files_only=True if offline_test() else None
)
path_gfpgan = os.path.join(snapshot_path_gfpgan, variantid_gfpgan)
model_gfpgan = GFPGANer(
model_path=path_gfpgan,
upscale=1,
arch='clean',
channel_multiplier=2
)
if isinstance(img_gfpgan, np.ndarray):
image_inter_gfpgan = img_gfpgan
else :
image_inter_gfpgan = np.array(img_gfpgan.convert('RGB'))
image_input_gfpgan = cv2.cvtColor(image_inter_gfpgan, cv2.COLOR_RGB2BGR)
_, _, image_gfpgan = model_gfpgan.enhance(
image_input_gfpgan,
has_aligned=False,
only_center_face=False,
paste_back=True
)
image_gfpgan = cv2.cvtColor(image_gfpgan, cv2.COLOR_BGR2RGB)
image_gfpgan_output = Image.fromarray(image_gfpgan)
del model_gfpgan, image_inter_gfpgan, image_input_gfpgan, image_gfpgan
clean_ram()
print(">>>[GFPGAN-mini π]: enhanced 1 image")
return image_gfpgan_output