HighCWu commited on
Commit
7ec2225
·
1 Parent(s): 95ed4d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -65
app.py CHANGED
@@ -1,12 +1,14 @@
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 ')
6
- os.system('wget "https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/realesrnet_x2.pth?OSSAccessKeyId=LTAI4G6bfnyW4TA4wFUXTYBe&Expires=1962694780&Signature=lI%2FolhA%2FyigiTRvoDIVbtMIyhjI%3D" -O weights/realesrnet_x2.pth ')
7
- os.system('wget "https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-Inpainting-1024.pth?OSSAccessKeyId=LTAI4G6bfnyW4TA4wFUXTYBe&Expires=1961116338&Signature=tvYhdLaLgW7UdcUrApXp2jsek8w%3D" -O weights/GPEN-Inpainting-1024.pth ')
8
- jksp = os.environ['GPEN-BFR-2048']
9
- os.system(f'wget "{jksp}" -O weights/GPEN-BFR-2048.pth > jksp.log')
 
 
10
 
11
  import gradio as gr
12
 
@@ -17,65 +19,10 @@ import gradio as gr
17
  import os
18
  import cv2
19
  import glob
20
- import time
21
- import math
22
- import argparse
23
- import numpy as np
24
- from PIL import Image, ImageDraw
25
- import __init_paths
26
  from face_enhancement import FaceEnhancement
27
  from face_colorization import FaceColorization
28
  from face_inpainting import FaceInpainting
29
-
30
- def brush_stroke_mask(img, color=(255,255,255)):
31
- min_num_vertex = 8
32
- max_num_vertex = 28
33
- mean_angle = 2*math.pi / 5
34
- angle_range = 2*math.pi / 15
35
- min_width = 12
36
- max_width = 80
37
- def generate_mask(H, W, img=None):
38
- average_radius = math.sqrt(H*H+W*W) / 8
39
- mask = Image.new('RGB', (W, H), 0)
40
- if img is not None: mask = img #Image.fromarray(img)
41
-
42
- for _ in range(np.random.randint(1, 4)):
43
- num_vertex = np.random.randint(min_num_vertex, max_num_vertex)
44
- angle_min = mean_angle - np.random.uniform(0, angle_range)
45
- angle_max = mean_angle + np.random.uniform(0, angle_range)
46
- angles = []
47
- vertex = []
48
- for i in range(num_vertex):
49
- if i % 2 == 0:
50
- angles.append(2*math.pi - np.random.uniform(angle_min, angle_max))
51
- else:
52
- angles.append(np.random.uniform(angle_min, angle_max))
53
-
54
- h, w = mask.size
55
- vertex.append((int(np.random.randint(0, w)), int(np.random.randint(0, h))))
56
- for i in range(num_vertex):
57
- r = np.clip(
58
- np.random.normal(loc=average_radius, scale=average_radius//2),
59
- 0, 2*average_radius)
60
- new_x = np.clip(vertex[-1][0] + r * math.cos(angles[i]), 0, w)
61
- new_y = np.clip(vertex[-1][1] + r * math.sin(angles[i]), 0, h)
62
- vertex.append((int(new_x), int(new_y)))
63
-
64
- draw = ImageDraw.Draw(mask)
65
- width = int(np.random.uniform(min_width, max_width))
66
- draw.line(vertex, fill=color, width=width)
67
- for v in vertex:
68
- draw.ellipse((v[0] - width//2,
69
- v[1] - width//2,
70
- v[0] + width//2,
71
- v[1] + width//2),
72
- fill=color)
73
-
74
- return mask
75
 
76
- width, height = img.size
77
- mask = generate_mask(height, width, img)
78
- return mask
79
 
80
  def inference(file, mode):
81
 
@@ -101,7 +48,6 @@ def inference(file, mode):
101
  elif mode == "inpainting":
102
  model = {'name':'GPEN-Inpainting-1024', 'size':1024}
103
  faceinpainter = FaceInpainting(size=model['size'], model=model['name'], channel_multiplier=2, device='cpu')
104
- im = np.asarray(brush_stroke_mask(Image.fromarray(im)))
105
  inpaint = faceinpainter.process(im)
106
 
107
  cv2.imwrite(os.path.join("output.png"), inpaint)
@@ -140,4 +86,4 @@ gr.Interface(
140
  ['selfie.png', 'selfie']
141
  ],
142
  enable_queue=True
143
- ).launch()
 
1
  import os
2
+ import glob
3
+
4
+ if len(glob.glob('weights/*.pth')) != 6:
5
+ os.system('wget -q "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')
6
+ os.system('wget -q "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')
7
+ os.system('wget -q "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 ')
8
+ os.system('wget -q "https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/realesrnet_x2.pth?OSSAccessKeyId=LTAI4G6bfnyW4TA4wFUXTYBe&Expires=1962694780&Signature=lI%2FolhA%2FyigiTRvoDIVbtMIyhjI%3D" -O weights/realesrnet_x2.pth ')
9
+ os.system('wget -q "https://public-vigen-video.oss-cn-shanghai.aliyuncs.com/robin/models/GPEN-Inpainting-1024.pth?OSSAccessKeyId=LTAI4G6bfnyW4TA4wFUXTYBe&Expires=1961116338&Signature=tvYhdLaLgW7UdcUrApXp2jsek8w%3D" -O weights/GPEN-Inpainting-1024.pth ')
10
+ jksp = os.environ['GPEN-BFR-2048']
11
+ os.system(f'wget -q "{jksp}" -O weights/GPEN-BFR-2048.pth')
12
 
13
  import gradio as gr
14
 
 
19
  import os
20
  import cv2
21
  import glob
 
 
 
 
 
 
22
  from face_enhancement import FaceEnhancement
23
  from face_colorization import FaceColorization
24
  from face_inpainting import FaceInpainting
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
 
 
 
26
 
27
  def inference(file, mode):
28
 
 
48
  elif mode == "inpainting":
49
  model = {'name':'GPEN-Inpainting-1024', 'size':1024}
50
  faceinpainter = FaceInpainting(size=model['size'], model=model['name'], channel_multiplier=2, device='cpu')
 
51
  inpaint = faceinpainter.process(im)
52
 
53
  cv2.imwrite(os.path.join("output.png"), inpaint)
 
86
  ['selfie.png', 'selfie']
87
  ],
88
  enable_queue=True
89
+ ).launch()