aamsko commited on
Commit
e9ea41c
·
verified ·
1 Parent(s): 0b44944

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -19
app.py CHANGED
@@ -1,33 +1,78 @@
1
- import gradio as gr
2
  import os
3
  import cv2
4
  import torch
 
5
  import numpy as np
6
  from PIL import Image
 
 
 
7
  from gfpgan import GFPGANer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- # Download GFPGAN model if not already present
10
- model_url = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
11
- model_path = "GFPGANv1.4.pth"
12
 
13
- if not os.path.exists(model_path):
14
- import requests
15
- r = requests.get(model_url, allow_redirects=True)
16
- open(model_path, 'wb').write(r.content)
 
17
 
18
- # Initialize GFPGAN
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  restorer = GFPGANer(
20
- model_path=model_path,
21
  upscale=2,
22
  arch='clean',
23
  channel_multiplier=2,
24
- bg_upsampler=None
25
  )
26
 
 
 
 
27
  def enhance(image):
28
- # Convert PIL to numpy
29
- img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
 
30
 
 
31
  _, _, restored_img = restorer.enhance(
32
  img,
33
  has_aligned=False,
@@ -35,16 +80,30 @@ def enhance(image):
35
  paste_back=True
36
  )
37
 
 
38
  restored_pil = Image.fromarray(cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB))
39
- return restored_pil
40
 
 
 
 
 
 
 
 
 
 
41
  iface = gr.Interface(
42
  fn=enhance,
43
- inputs=gr.Image(type="pil"),
44
- outputs="image",
45
- title="IMGEN - AI Photo Enhancer",
46
- description="Upload your photo (ID, CV, profile) and enhance it with AI using GFPGAN."
 
47
  )
48
 
 
 
 
49
  if __name__ == "__main__":
50
- iface.launch()
 
 
 
1
  import os
2
  import cv2
3
  import torch
4
+ import gradio as gr
5
  import numpy as np
6
  from PIL import Image
7
+ import tempfile
8
+ import requests
9
+
10
  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'
44
+ )
45
+
46
+ bg_upsampler = RealESRGANer(
47
+ scale=4,
48
+ model_path=ESRGAN_MODEL,
49
+ model=esr_model,
50
+ tile=0,
51
+ tile_pad=10,
52
+ pre_pad=0,
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,
62
  arch='clean',
63
  channel_multiplier=2,
64
+ bg_upsampler=bg_upsampler
65
  )
66
 
67
+ # -------------------------------
68
+ # Enhancement Function
69
+ # -------------------------------
70
  def enhance(image):
71
+ # Ensure RGB format
72
+ img_np = np.array(image.convert("RGB"))
73
+ img = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
74
 
75
+ # Enhance with GFPGAN
76
  _, _, restored_img = restorer.enhance(
77
  img,
78
  has_aligned=False,
 
80
  paste_back=True
81
  )
82
 
83
+ # Convert to PIL for saving
84
  restored_pil = Image.fromarray(cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB))
 
85
 
86
+ # Save as .jpg to a temporary file
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 # Path to JPG file
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 the face and outfit using AI (GFPGAN + RealESRGAN). Output is a downloadable JPG file.",
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()