KenjieDec commited on
Commit
648d463
1 Parent(s): d2763b3
Files changed (1) hide show
  1. app.py +16 -18
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import os
2
 
3
- os.system('nvidia-smi')
4
  os.system('wget "https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/RetinaFace-R50.pth?OSSAccessKeyId=LTAI4G6bfnyW4TA4wFUXTYBe&Expires=1961116085&Signature=GlUNW6%2B8FxvxWmE9jKIZYOOciKQ%3D" -O weights/RetinaFace-R50.pth')
5
  os.system('wget "https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-BFR-512.pth?OSSAccessKeyId=LTAI4G6bfnyW4TA4wFUXTYBe&Expires=1961116208&Signature=hBgvVvKVSNGeXqT8glG%2Bd2t2OKc%3D" -O weights/GPEN-512.pth')
6
  os.system('wget "https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-Colorization-1024.pth?OSSAccessKeyId=LTAI4G6bfnyW4TA4wFUXTYBe&Expires=1961116315&Signature=9tPavW2h%2F1LhIKiXj73sTQoWqcc%3D" -O weights/GPEN-1024-Color.pth ')
@@ -20,24 +19,17 @@ from face_colorization import FaceColorization
20
 
21
  def inference(file, mode):
22
 
 
 
 
 
 
 
23
  if mode == "enhance":
24
- model = {'name':'GPEN-512', 'size':512}
25
- im = cv2.imread(file, cv2.IMREAD_COLOR)
26
- im = cv2.resize(im, (0,0), fx=2, fy=2)
27
- faceenhancer = FaceEnhancement(size=model['size'], model=model['name'], channel_multiplier=2, device='cpu')
28
- img, orig_faces, enhanced_faces = faceenhancer.process(im)
29
- cv2.imwrite(os.path.join("output.png"), img)
30
- return os.path.join("output.png")
31
- else:
32
- emodel = {'name':'GPEN-512', 'size':512}
33
- im = cv2.imread(file, cv2.IMREAD_COLOR)
34
- im = cv2.resize(im, (0,0), fx=2, fy=2)
35
- faceenhancer = FaceEnhancement(size=model['size'], model=emodel['name'], channel_multiplier=2, device='cpu')
36
- img, orig_faces, enhanced_faces = faceenhancer.process(im)
37
- cv2.imwrite(os.path.join("enhanced.png"), img)
38
-
39
  model = {'name':'GPEN-1024-Color', 'size':1024}
40
- grayf = cv2.imread("enhanced.png", cv2.IMREAD_GRAYSCALE)
41
  grayf = cv2.cvtColor(grayf, cv2.COLOR_GRAY2BGR) # channel: 1->3
42
  facecolorizer = FaceColorization(size=model['size'], model=model['name'], channel_multiplier=2, device='cpu')
43
  colorf = facecolorizer.process(grayf)
@@ -45,6 +37,12 @@ def inference(file, mode):
45
  colorf = cv2.resize(colorf, (grayf.shape[1], grayf.shape[0]))
46
  cv2.imwrite(os.path.join("output.png"), colorf)
47
  return os.path.join("output.png")
 
 
 
 
 
 
48
 
49
  title = "GPEN"
50
  description = "Gradio demo for GAN Prior Embedded Network for Blind Face Restoration in the Wild. This version of gradio demo includes face colorization from GPEN. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
@@ -54,7 +52,7 @@ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2105.060
54
 
55
  gr.Interface(
56
  inference,
57
- [gr.inputs.Image(type="filepath", label="Input"),gr.inputs.Radio(["enhance","colorize"], type="value", default="enhance", label="Type")],
58
  gr.outputs.Image(type="file", label="Output"),
59
  title=title,
60
  description=description,
 
1
  import os
2
 
 
3
  os.system('wget "https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/RetinaFace-R50.pth?OSSAccessKeyId=LTAI4G6bfnyW4TA4wFUXTYBe&Expires=1961116085&Signature=GlUNW6%2B8FxvxWmE9jKIZYOOciKQ%3D" -O weights/RetinaFace-R50.pth')
4
  os.system('wget "https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-BFR-512.pth?OSSAccessKeyId=LTAI4G6bfnyW4TA4wFUXTYBe&Expires=1961116208&Signature=hBgvVvKVSNGeXqT8glG%2Bd2t2OKc%3D" -O weights/GPEN-512.pth')
5
  os.system('wget "https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-Colorization-1024.pth?OSSAccessKeyId=LTAI4G6bfnyW4TA4wFUXTYBe&Expires=1961116315&Signature=9tPavW2h%2F1LhIKiXj73sTQoWqcc%3D" -O weights/GPEN-1024-Color.pth ')
 
19
 
20
  def inference(file, mode):
21
 
22
+ im = cv2.imread(file, cv2.IMREAD_COLOR)
23
+ im = cv2.resize(im, (0,0), fx=2, fy=2)
24
+ faceenhancer = FaceEnhancement(size=512, model='GPEN-512', channel_multiplier=2, device='cpu', u=False)
25
+ img, orig_faces, enhanced_faces = faceenhancer.process(im)
26
+ cv2.imwrite(os.path.join("e.png"), img)
27
+
28
  if mode == "enhance":
29
+ return os.path.join("e.png")
30
+ else if mode == "colorize":
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  model = {'name':'GPEN-1024-Color', 'size':1024}
32
+ grayf = cv2.imread("e.png", cv2.IMREAD_GRAYSCALE)
33
  grayf = cv2.cvtColor(grayf, cv2.COLOR_GRAY2BGR) # channel: 1->3
34
  facecolorizer = FaceColorization(size=model['size'], model=model['name'], channel_multiplier=2, device='cpu')
35
  colorf = facecolorizer.process(grayf)
 
37
  colorf = cv2.resize(colorf, (grayf.shape[1], grayf.shape[0]))
38
  cv2.imwrite(os.path.join("output.png"), colorf)
39
  return os.path.join("output.png")
40
+ else:
41
+ faceenhancer = FaceEnhancement(size=512, model='GPEN-512', channel_multiplier=2, device='cpu', u=False)
42
+ img, orig_faces, enhanced_faces = faceenhancer.process(im)
43
+ cv2.imwrite(os.path.join("output.png"), img)
44
+ return os.path.join("output.png")
45
+
46
 
47
  title = "GPEN"
48
  description = "Gradio demo for GAN Prior Embedded Network for Blind Face Restoration in the Wild. This version of gradio demo includes face colorization from GPEN. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
 
52
 
53
  gr.Interface(
54
  inference,
55
+ [gr.inputs.Image(type="filepath", label="Input"),gr.inputs.Radio(["enhance", "colorize", "enhanced+background"], type="value", default="enhance", label="Type")],
56
  gr.outputs.Image(type="file", label="Output"),
57
  title=title,
58
  description=description,