Update app.py
Browse files
app.py
CHANGED
|
@@ -11,33 +11,29 @@ from gfpgan import GFPGANer
|
|
| 11 |
from basicsr.archs.srvgg_arch import SRVGGNetCompact
|
| 12 |
from realesrgan.utils import RealESRGANer
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
# Model Setup
|
| 16 |
-
# -------------------------------
|
| 17 |
MODEL_DIR = os.path.join(os.path.expanduser("~"), ".imgen_models")
|
| 18 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 19 |
|
|
|
|
| 20 |
GFPGAN_MODEL = os.path.join(MODEL_DIR, "GFPGANv1.4.pth")
|
| 21 |
GFPGAN_URL = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
|
| 22 |
-
|
| 23 |
if not os.path.exists(GFPGAN_MODEL):
|
| 24 |
print("Downloading GFPGAN model...")
|
| 25 |
r = requests.get(GFPGAN_URL, allow_redirects=True)
|
| 26 |
with open(GFPGAN_MODEL, 'wb') as f:
|
| 27 |
f.write(r.content)
|
| 28 |
|
|
|
|
| 29 |
ESRGAN_MODEL = os.path.join(MODEL_DIR, "realesr-general-x4v3.pth")
|
| 30 |
ESRGAN_URL = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth"
|
| 31 |
-
|
| 32 |
if not os.path.exists(ESRGAN_MODEL):
|
| 33 |
print("Downloading Real-ESRGAN model...")
|
| 34 |
r = requests.get(ESRGAN_URL, allow_redirects=True)
|
| 35 |
with open(ESRGAN_MODEL, 'wb') as f:
|
| 36 |
f.write(r.content)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
# Background Upsampler Setup
|
| 40 |
-
# -------------------------------
|
| 41 |
esr_model = SRVGGNetCompact(
|
| 42 |
num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32,
|
| 43 |
upscale=4, act_type='prelu'
|
|
@@ -53,9 +49,7 @@ bg_upsampler = RealESRGANer(
|
|
| 53 |
half=torch.cuda.is_available()
|
| 54 |
)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
# GFPGAN Restorer Setup
|
| 58 |
-
# -------------------------------
|
| 59 |
restorer = GFPGANer(
|
| 60 |
model_path=GFPGAN_MODEL,
|
| 61 |
upscale=2,
|
|
@@ -64,46 +58,37 @@ restorer = GFPGANer(
|
|
| 64 |
bg_upsampler=bg_upsampler
|
| 65 |
)
|
| 66 |
|
| 67 |
-
# -------------------------------
|
| 68 |
-
# Enhancement Function
|
| 69 |
-
# -------------------------------
|
| 70 |
def enhance(image):
|
| 71 |
-
#
|
| 72 |
img_np = np.array(image.convert("RGB"))
|
| 73 |
img = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
| 74 |
|
| 75 |
-
# Enhance
|
| 76 |
_, _, restored_img = restorer.enhance(
|
| 77 |
img,
|
| 78 |
has_aligned=False,
|
| 79 |
-
only_center_face=False,
|
| 80 |
-
paste_back=True
|
| 81 |
)
|
| 82 |
|
| 83 |
-
# Convert to PIL
|
| 84 |
restored_pil = Image.fromarray(cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB))
|
| 85 |
|
| 86 |
-
# Save
|
| 87 |
temp_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
| 88 |
restored_pil.save(temp_file.name, format="JPEG", quality=95)
|
| 89 |
|
| 90 |
-
return temp_file.name
|
| 91 |
|
| 92 |
-
# -------------------------------
|
| 93 |
-
# Gradio Interface
|
| 94 |
-
# -------------------------------
|
| 95 |
iface = gr.Interface(
|
| 96 |
fn=enhance,
|
| 97 |
inputs=gr.Image(type="pil", label="Upload Image (ID, CV, Profile)"),
|
| 98 |
outputs=gr.File(label="Download Enhanced JPG"),
|
| 99 |
title="📸 IMGEN - AI Photo Enhancer (Face + Outfit)",
|
| 100 |
-
description="Upload your photo and enhance both
|
| 101 |
allow_flagging="never"
|
| 102 |
)
|
| 103 |
|
| 104 |
-
# -------------------------------
|
| 105 |
-
# Launch App
|
| 106 |
-
# -------------------------------
|
| 107 |
if __name__ == "__main__":
|
| 108 |
print("Running on GPU" if torch.cuda.is_available() else "Running on CPU")
|
| 109 |
iface.launch()
|
|
|
|
| 11 |
from basicsr.archs.srvgg_arch import SRVGGNetCompact
|
| 12 |
from realesrgan.utils import RealESRGANer
|
| 13 |
|
| 14 |
+
# Create model directory
|
|
|
|
|
|
|
| 15 |
MODEL_DIR = os.path.join(os.path.expanduser("~"), ".imgen_models")
|
| 16 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 17 |
|
| 18 |
+
# Download GFPGAN model if missing
|
| 19 |
GFPGAN_MODEL = os.path.join(MODEL_DIR, "GFPGANv1.4.pth")
|
| 20 |
GFPGAN_URL = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
|
|
|
|
| 21 |
if not os.path.exists(GFPGAN_MODEL):
|
| 22 |
print("Downloading GFPGAN model...")
|
| 23 |
r = requests.get(GFPGAN_URL, allow_redirects=True)
|
| 24 |
with open(GFPGAN_MODEL, 'wb') as f:
|
| 25 |
f.write(r.content)
|
| 26 |
|
| 27 |
+
# Download RealESRGAN model if missing
|
| 28 |
ESRGAN_MODEL = os.path.join(MODEL_DIR, "realesr-general-x4v3.pth")
|
| 29 |
ESRGAN_URL = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth"
|
|
|
|
| 30 |
if not os.path.exists(ESRGAN_MODEL):
|
| 31 |
print("Downloading Real-ESRGAN model...")
|
| 32 |
r = requests.get(ESRGAN_URL, allow_redirects=True)
|
| 33 |
with open(ESRGAN_MODEL, 'wb') as f:
|
| 34 |
f.write(r.content)
|
| 35 |
|
| 36 |
+
# Setup RealESRGAN background upsampler
|
|
|
|
|
|
|
| 37 |
esr_model = SRVGGNetCompact(
|
| 38 |
num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32,
|
| 39 |
upscale=4, act_type='prelu'
|
|
|
|
| 49 |
half=torch.cuda.is_available()
|
| 50 |
)
|
| 51 |
|
| 52 |
+
# Setup GFPGAN restorer with bg upsampler
|
|
|
|
|
|
|
| 53 |
restorer = GFPGANer(
|
| 54 |
model_path=GFPGAN_MODEL,
|
| 55 |
upscale=2,
|
|
|
|
| 58 |
bg_upsampler=bg_upsampler
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
def enhance(image):
|
| 62 |
+
# Convert PIL image to OpenCV BGR
|
| 63 |
img_np = np.array(image.convert("RGB"))
|
| 64 |
img = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
| 65 |
|
| 66 |
+
# Enhance faces + upscale background/outfit
|
| 67 |
_, _, restored_img = restorer.enhance(
|
| 68 |
img,
|
| 69 |
has_aligned=False,
|
| 70 |
+
only_center_face=False, # Enhance all faces in image
|
| 71 |
+
paste_back=True # Paste restored faces back on bg
|
| 72 |
)
|
| 73 |
|
| 74 |
+
# Convert back to PIL RGB
|
| 75 |
restored_pil = Image.fromarray(cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB))
|
| 76 |
|
| 77 |
+
# Save to temporary JPG file for download
|
| 78 |
temp_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
| 79 |
restored_pil.save(temp_file.name, format="JPEG", quality=95)
|
| 80 |
|
| 81 |
+
return temp_file.name
|
| 82 |
|
|
|
|
|
|
|
|
|
|
| 83 |
iface = gr.Interface(
|
| 84 |
fn=enhance,
|
| 85 |
inputs=gr.Image(type="pil", label="Upload Image (ID, CV, Profile)"),
|
| 86 |
outputs=gr.File(label="Download Enhanced JPG"),
|
| 87 |
title="📸 IMGEN - AI Photo Enhancer (Face + Outfit)",
|
| 88 |
+
description="Upload your photo and enhance both face and outfit with AI (GFPGAN + RealESRGAN). Output is a downloadable JPG file.",
|
| 89 |
allow_flagging="never"
|
| 90 |
)
|
| 91 |
|
|
|
|
|
|
|
|
|
|
| 92 |
if __name__ == "__main__":
|
| 93 |
print("Running on GPU" if torch.cuda.is_available() else "Running on CPU")
|
| 94 |
iface.launch()
|