Spaces:
doevent
/
Running on Zero

ohayonguy commited on
Commit
8ea5b1f
1 Parent(s): c6b10d8

trying to fix daemonic processes error

Browse files
Files changed (1) hide show
  1. app.py +54 -55
app.py CHANGED
@@ -94,63 +94,62 @@ def enhance_face(img, face_helper, has_aligned, only_center_face=False, paste_ba
94
  def inference(img, aligned, scale, num_steps):
95
  if scale > 4:
96
  scale = 4 # avoid too large scale value
97
- try:
98
- img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
99
- if len(img.shape) == 3 and img.shape[2] == 4:
100
- img_mode = 'RGBA'
101
- elif len(img.shape) == 2: # for gray inputs
102
- img_mode = None
103
- img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
104
- else:
105
- img_mode = None
106
-
107
- h, w = img.shape[0:2]
108
- if h > 3500 or w > 3500:
109
- print('Image size too large.')
110
- return None, None
111
-
112
- if h < 300:
113
- img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
114
-
115
- face_helper = FaceRestoreHelper(
116
- scale,
117
- face_size=512,
118
- crop_ratio=(1, 1),
119
- det_model='retinaface_resnet50',
120
- save_ext='png',
121
- use_parse=True,
122
- device=device,
123
- model_rootpath=None)
124
-
125
- has_aligned = True if aligned == 'Yes' else False
126
- _, restored_aligned, restored_img = enhance_face(img, face_helper, has_aligned, only_center_face=False,
127
- paste_back=True)
128
- if has_aligned:
129
- output = restored_aligned[0]
130
- else:
131
- output = restored_img
132
-
133
-
134
- # try:
135
- # if scale != 2:
136
- # interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
137
- # h, w = img.shape[0:2]
138
- # output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
139
- # except Exception as error:
140
- # print('Wrong scale input.', error)
141
- if img_mode == 'RGBA': # RGBA images should be saved in png format
142
- extension = 'png'
143
- else:
144
- extension = 'jpg'
145
- save_path = f'output/out.{extension}'
146
- cv2.imwrite(save_path, output)
147
-
148
- output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
149
- return output, save_path
150
- except Exception as error:
151
- print('global exception', error)
152
  return None, None
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
  css = r"""
156
  """
 
94
  def inference(img, aligned, scale, num_steps):
95
  if scale > 4:
96
  scale = 4 # avoid too large scale value
97
+ img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
98
+ if len(img.shape) == 3 and img.shape[2] == 4:
99
+ img_mode = 'RGBA'
100
+ elif len(img.shape) == 2: # for gray inputs
101
+ img_mode = None
102
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
103
+ else:
104
+ img_mode = None
105
+
106
+ h, w = img.shape[0:2]
107
+ if h > 3500 or w > 3500:
108
+ print('Image size too large.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  return None, None
110
 
111
+ if h < 300:
112
+ img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
113
+
114
+ face_helper = FaceRestoreHelper(
115
+ scale,
116
+ face_size=512,
117
+ crop_ratio=(1, 1),
118
+ det_model='retinaface_resnet50',
119
+ save_ext='png',
120
+ use_parse=True,
121
+ device=device,
122
+ model_rootpath=None)
123
+
124
+ has_aligned = True if aligned == 'Yes' else False
125
+ _, restored_aligned, restored_img = enhance_face(img, face_helper, has_aligned, only_center_face=False,
126
+ paste_back=True)
127
+ if has_aligned:
128
+ output = restored_aligned[0]
129
+ else:
130
+ output = restored_img
131
+
132
+
133
+ # try:
134
+ # if scale != 2:
135
+ # interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
136
+ # h, w = img.shape[0:2]
137
+ # output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
138
+ # except Exception as error:
139
+ # print('Wrong scale input.', error)
140
+ if img_mode == 'RGBA': # RGBA images should be saved in png format
141
+ extension = 'png'
142
+ else:
143
+ extension = 'jpg'
144
+ save_path = f'output/out.{extension}'
145
+ cv2.imwrite(save_path, output)
146
+
147
+ output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
148
+ return output, save_path
149
+ # except Exception as error:
150
+ # print('global exception', error)
151
+ # return None, None
152
+
153
 
154
  css = r"""
155
  """