sczhou commited on
Commit
8a3d969
1 Parent(s): 96cb425

fix queue jam.

Browse files
CodeFormer/facelib/detection/yolov5face/face_detector.py CHANGED
@@ -17,7 +17,7 @@ from facelib.detection.yolov5face.utils.general import (
17
  scale_coords_landmarks,
18
  )
19
 
20
- IS_HIGH_VERSION = tuple(map(int, torch.__version__.split('+')[0].split('.'))) >= (1, 9, 0)
21
 
22
 
23
  def isListempty(inList):
 
17
  scale_coords_landmarks,
18
  )
19
 
20
+ IS_HIGH_VERSION = tuple(map(int, torch.__version__.split('+')[0].split('.')[:3])) >= (1, 9, 0)
21
 
22
 
23
  def isListempty(inList):
CodeFormer/facelib/utils/face_restoration_helper.py CHANGED
@@ -6,7 +6,7 @@ from torchvision.transforms.functional import normalize
6
 
7
  from facelib.detection import init_detection_model
8
  from facelib.parsing import init_parsing_model
9
- from facelib.utils.misc import img2tensor, imwrite
10
 
11
 
12
  def get_largest_face(det_faces, h, w):
@@ -125,6 +125,9 @@ class FaceRestoreHelper(object):
125
  img = img[:, :, 0:3]
126
 
127
  self.input_img = img
 
 
 
128
 
129
  if min(self.input_img.shape[:2])<512:
130
  f = 512.0/min(self.input_img.shape[:2])
@@ -296,6 +299,8 @@ class FaceRestoreHelper(object):
296
 
297
 
298
  def add_restored_face(self, face):
 
 
299
  self.restored_faces.append(face)
300
 
301
 
 
6
 
7
  from facelib.detection import init_detection_model
8
  from facelib.parsing import init_parsing_model
9
+ from facelib.utils.misc import img2tensor, imwrite, is_gray, bgr2gray
10
 
11
 
12
  def get_largest_face(det_faces, h, w):
 
125
  img = img[:, :, 0:3]
126
 
127
  self.input_img = img
128
+ self.is_gray = is_gray(img, threshold=5)
129
+ if self.is_gray:
130
+ print('Grayscale input: True')
131
 
132
  if min(self.input_img.shape[:2])<512:
133
  f = 512.0/min(self.input_img.shape[:2])
 
299
 
300
 
301
  def add_restored_face(self, face):
302
+ if self.is_gray:
303
+ face = bgr2gray(face) # convert img into grayscale
304
  self.restored_faces.append(face)
305
 
306
 
CodeFormer/facelib/utils/misc.py CHANGED
@@ -1,6 +1,8 @@
1
  import cv2
2
  import os
3
  import os.path as osp
 
 
4
  import torch
5
  from torch.hub import download_url_to_file, get_dir
6
  from urllib.parse import urlparse
@@ -139,3 +141,34 @@ def scandir(dir_path, suffix=None, recursive=False, full_path=False):
139
  continue
140
 
141
  return _scandir(dir_path, suffix=suffix, recursive=recursive)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import cv2
2
  import os
3
  import os.path as osp
4
+ import numpy as np
5
+ from PIL import Image
6
  import torch
7
  from torch.hub import download_url_to_file, get_dir
8
  from urllib.parse import urlparse
 
141
  continue
142
 
143
  return _scandir(dir_path, suffix=suffix, recursive=recursive)
144
+
145
+
146
+ def is_gray(img, threshold=10):
147
+ img = Image.fromarray(img)
148
+ if len(img.getbands()) == 1:
149
+ return True
150
+ img1 = np.asarray(img.getchannel(channel=0), dtype=np.int16)
151
+ img2 = np.asarray(img.getchannel(channel=1), dtype=np.int16)
152
+ img3 = np.asarray(img.getchannel(channel=2), dtype=np.int16)
153
+ diff1 = (img1 - img2).var()
154
+ diff2 = (img2 - img3).var()
155
+ diff3 = (img3 - img1).var()
156
+ diff_sum = (diff1 + diff2 + diff3) / 3.0
157
+ if diff_sum <= threshold:
158
+ return True
159
+ else:
160
+ return False
161
+
162
+ def rgb2gray(img, out_channel=3):
163
+ r, g, b = img[:,:,0], img[:,:,1], img[:,:,2]
164
+ gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
165
+ if out_channel == 3:
166
+ gray = gray[:,:,np.newaxis].repeat(3, axis=2)
167
+ return gray
168
+
169
+ def bgr2gray(img, out_channel=3):
170
+ b, g, r = img[:,:,0], img[:,:,1], img[:,:,2]
171
+ gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
172
+ if out_channel == 3:
173
+ gray = gray[:,:,np.newaxis].repeat(3, axis=2)
174
+ return gray
app.py CHANGED
@@ -16,6 +16,7 @@ from torchvision.transforms.functional import normalize
16
  from basicsr.utils import imwrite, img2tensor, tensor2img
17
  from basicsr.utils.download_util import load_file_from_url
18
  from facelib.utils.face_restoration_helper import FaceRestoreHelper
 
19
  from basicsr.archs.rrdbnet_arch import RRDBNet
20
  from basicsr.utils.realesrgan_utils import RealESRGANer
21
 
@@ -108,6 +109,7 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
108
  draw_box = False
109
  detection_model = "retinaface_resnet50"
110
 
 
111
  face_helper = FaceRestoreHelper(
112
  upscale,
113
  face_size=512,
@@ -125,6 +127,9 @@ def inference(image, background_enhance, face_upsample, upscale, codeformer_fide
125
  if has_aligned:
126
  # the input faces are already cropped and aligned
127
  img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
 
 
 
128
  face_helper.cropped_faces = [img]
129
  else:
130
  face_helper.read_image(img)
@@ -228,7 +233,7 @@ If you have any questions, please feel free to reach me out at <b>shangchenzhou@
228
  ![visitors](https://visitor-badge.glitch.me/badge?page_id=sczhou/CodeFormer)
229
  """
230
 
231
- gr.Interface(
232
  inference, [
233
  gr.inputs.Image(type="filepath", label="Input"),
234
  gr.inputs.Checkbox(default=True, label="Background_Enhance"),
@@ -249,3 +254,6 @@ gr.Interface(
249
  ['05.jpg', True, True, 2, 0.1]
250
  ]
251
  ).launch()
 
 
 
 
16
  from basicsr.utils import imwrite, img2tensor, tensor2img
17
  from basicsr.utils.download_util import load_file_from_url
18
  from facelib.utils.face_restoration_helper import FaceRestoreHelper
19
+ from facelib.utils.misc import is_gray
20
  from basicsr.archs.rrdbnet_arch import RRDBNet
21
  from basicsr.utils.realesrgan_utils import RealESRGANer
22
 
 
109
  draw_box = False
110
  detection_model = "retinaface_resnet50"
111
 
112
+ upscale = int(upscale) # covert type to int
113
  face_helper = FaceRestoreHelper(
114
  upscale,
115
  face_size=512,
 
127
  if has_aligned:
128
  # the input faces are already cropped and aligned
129
  img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
130
+ face_helper.is_gray = is_gray(img, threshold=5)
131
+ if face_helper.is_gray:
132
+ print('Grayscale input: True')
133
  face_helper.cropped_faces = [img]
134
  else:
135
  face_helper.read_image(img)
 
233
  ![visitors](https://visitor-badge.glitch.me/badge?page_id=sczhou/CodeFormer)
234
  """
235
 
236
+ demo = gr.Interface(
237
  inference, [
238
  gr.inputs.Image(type="filepath", label="Input"),
239
  gr.inputs.Checkbox(default=True, label="Background_Enhance"),
 
254
  ['05.jpg', True, True, 2, 0.1]
255
  ]
256
  ).launch()
257
+
258
+ demo.queue(concurrency_count=4)
259
+ demo.launch()