├── Codeformer_onnxrun.py ├── GFPGAN_ov.py ├── GPEN_ov.py ├── README.md ├── Restoreformer_onnxrun.py └── test_images ├── 1.jpg ├── 2.jpg ├── 3.jpg └── 4.jpg /Codeformer_onnxrun.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import onnxruntime 3 | import numpy as np 4 | 5 | class CodeFormer: 6 | def __init__(self, model_path="codeformer.onnx", device='cpu'): 7 | session_options = onnxruntime.SessionOptions() 8 | session_options.log_severity_level = 3 9 | session_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL 10 | providers = ["CPUExecutionProvider"] 11 | if device == 'cuda': 12 | providers = [("CUDAExecutionProvider", {"cudnn_conv_algo_search": "DEFAULT"}),"CPUExecutionProvider"] 13 | self.session = onnxruntime.InferenceSession(model_path, sess_options=session_options, providers=providers) 14 | self.resolution = self.session.get_inputs()[0].shape[-2:] 15 | 16 | def preprocess(self, img, w): 17 | img = cv2.resize(img, self.resolution, interpolation=cv2.INTER_LINEAR) 18 | img = img.astype(np.float32)[:,:,::-1] / 255.0 19 | img = img.transpose((2, 0, 1)) 20 | img = (img - 0.5) / 0.5 21 | img = np.expand_dims(img, axis=0).astype(np.float32) 22 | w = np.array([w], dtype=np.double) 23 | return img, w 24 | 25 | def postprocess(self, img): 26 | img = (img.transpose(1,2,0).clip(-1,1) + 1) * 0.5 27 | img = (img * 255)[:,:,::-1] 28 | img = img.clip(0, 255).astype('uint8') 29 | return img 30 | 31 | def enhance(self, img, w=0.9): 32 | img, w = self.preprocess(img, w) 33 | output = self.session.run(None, {'x':img, 'w':w})[0][0] 34 | output = self.postprocess(output) 35 | return output 36 | 37 | if __name__=='__main__': 38 | mynet = CodeFormer(model_path="weights/codeformer.onnx", device="cpu") 39 | image_path = 'test_images/1.jpg' 40 | img = cv2.imread(image_path) 41 | 42 | dstimg = mynet.enhance(img) 43 | cv2.namedWindow('srcimg', cv2.WINDOW_NORMAL) 44 | cv2.imshow('srcimg', img) 45 | winName = 'CodeFormer in OnnxRuntime' 46 | cv2.namedWindow(winName, cv2.WINDOW_NORMAL) 47 | cv2.imshow(winName, dstimg) 48 | cv2.waitKey(0) 49 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /GFPGAN_ov.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | from openvino.runtime import Core ###pip install openvino-dev[ONNX]==2023.0.2 -i https://pypi.tuna.tsinghua.edu.cn/simple 3 | import numpy as np 4 | 5 | class GFPGAN: 6 | def __init__(self, model_path="GFPGANv1.4.onnx", device='cpu'): 7 | ie = Core() 8 | model = ie.read_model(model_path) 9 | self.net = ie.compile_model(model=model, device_name=device.upper()) 10 | _, _, self.input_height, self.input_width = tuple(self.net.inputs[0].shape) 11 | 12 | def preprocess(self, img): 13 | img = cv2.resize(img, (self.input_width, self.input_height), interpolation=cv2.INTER_LINEAR) 14 | img = img.astype(np.float32)[:,:,::-1] / 255.0 15 | img = img.transpose((2, 0, 1)) 16 | img = (img - 0.5) / 0.5 17 | img = np.expand_dims(img, axis=0).astype(np.float32) 18 | return img 19 | 20 | def postprocess(self, img): 21 | img = (img.transpose(1,2,0).clip(-1,1) + 1) * 0.5 22 | img = (img * 255)[:,:,::-1] 23 | img = img.clip(0, 255).astype('uint8') 24 | return img 25 | 26 | def enhance(self, img): 27 | img = self.preprocess(img) 28 | output = self.net(img)[0][0] 29 | output = self.postprocess(output) 30 | return output 31 | 32 | if __name__=='__main__': 33 | mynet = GFPGAN(model_path="weights/GFPGANv1.4.onnx", device="cpu") 34 | image_path = 'test_images/1.jpg' 35 | img = cv2.imread(image_path) 36 | 37 | dstimg = mynet.enhance(img) 38 | cv2.namedWindow('srcimg', cv2.WINDOW_NORMAL) 39 | cv2.imshow('srcimg', img) 40 | winName = 'GFPGAN in OpenVINO' 41 | cv2.namedWindow(winName, cv2.WINDOW_NORMAL) 42 | cv2.imshow(winName, dstimg) 43 | cv2.waitKey(0) 44 | cv2.destroyAllWindows() 45 | -------------------------------------------------------------------------------- /GPEN_ov.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | from openvino.runtime import Core ###pip install openvino-dev[ONNX]==2023.0.2 -i https://pypi.tuna.tsinghua.edu.cn/simple 3 | import numpy as np 4 | 5 | class GPEN: 6 | def __init__(self, model_path="GPEN-BFR-512.onnx", device='cpu'): 7 | ie = Core() 8 | model = ie.read_model(model_path) 9 | self.net = ie.compile_model(model=model, device_name=device.upper()) 10 | _, _, self.input_height, self.input_width = tuple(self.net.inputs[0].shape) 11 | 12 | def preprocess(self, img): 13 | img = cv2.resize(img, (self.input_width, self.input_height), interpolation=cv2.INTER_LINEAR) 14 | img = img.astype(np.float32)[:,:,::-1] / 255.0 15 | img = img.transpose((2, 0, 1)) 16 | img = (img - 0.5) / 0.5 17 | img = np.expand_dims(img, axis=0).astype(np.float32) 18 | return img 19 | 20 | def postprocess(self, img): 21 | img = (img.transpose(1,2,0).clip(-1,1) + 1) * 0.5 22 | img = (img * 255)[:,:,::-1] 23 | img = img.clip(0, 255).astype('uint8') 24 | return img 25 | 26 | def enhance(self, img): 27 | img = self.preprocess(img) 28 | output = self.net(img)[0][0] 29 | output = self.postprocess(output) 30 | return output 31 | 32 | if __name__=='__main__': 33 | mynet = GPEN(model_path="weights/GPEN-BFR-512.onnx", device="cpu") 34 | image_path = 'test_images/1.jpg' 35 | img = cv2.imread(image_path) 36 | 37 | dstimg = mynet.enhance(img) 38 | cv2.namedWindow('srcimg', cv2.WINDOW_NORMAL) 39 | cv2.imshow('srcimg', img) 40 | winName = 'GPEN in OpenVINO' 41 | cv2.namedWindow(winName, cv2.WINDOW_NORMAL) 42 | cv2.imshow(winName, dstimg) 43 | cv2.waitKey(0) 44 | cv2.destroyAllWindows() 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Face-restoration-models 2 | 4个热门的模糊人脸修复模型的部署。 3 | 由于模型的预处理和后处理比较简单,因此本仓库的程序只提供了python的, 4 | 有兴趣的可以自己编写C++程序。 5 | 6 | onnx模型文件在百度云盘,链接:https://pan.baidu.com/s/1gdh16gPF2luYXS6AbpK9Ag  7 | 提取码:c2js 8 | -------------------------------------------------------------------------------- /Restoreformer_onnxrun.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import onnxruntime 3 | import numpy as np 4 | 5 | class RestoreFormer: 6 | def __init__(self, model_path="restoreformer.onnx", device='cpu'): 7 | session_options = onnxruntime.SessionOptions() 8 | session_options.log_severity_level = 3 9 | session_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL 10 | providers = ["CPUExecutionProvider"] 11 | if device == 'cuda': 12 | providers = [("CUDAExecutionProvider", {"cudnn_conv_algo_search": "DEFAULT"}),"CPUExecutionProvider"] 13 | self.session = onnxruntime.InferenceSession(model_path, sess_options=session_options, providers=providers) 14 | self.resolution = self.session.get_inputs()[0].shape[-2:] 15 | 16 | def preprocess(self, img): 17 | img = cv2.resize(img, self.resolution, interpolation=cv2.INTER_LINEAR) 18 | img = img.astype(np.float32)[:,:,::-1] / 255.0 19 | img = img.transpose((2, 0, 1)) 20 | img = (img - 0.5) / 0.5 21 | img = np.expand_dims(img, axis=0).astype(np.float32) 22 | return img 23 | 24 | def postprocess(self, img): 25 | img = (img.transpose(1,2,0).clip(-1,1) + 1) * 0.5 26 | img = (img * 255)[:,:,::-1] 27 | img = img.clip(0, 255).astype('uint8') 28 | return img 29 | 30 | def enhance(self, img): 31 | img = self.preprocess(img) 32 | output = self.session.run(None, {'input':img})[0][0] 33 | output = self.postprocess(output) 34 | return output 35 | 36 | if __name__=='__main__': 37 | mynet = RestoreFormer(model_path="weights/restoreformer.onnx", device="cpu") 38 | image_path = 'test_images/1.jpg' 39 | img = cv2.imread(image_path) 40 | 41 | dstimg = mynet.enhance(img) 42 | cv2.namedWindow('srcimg', cv2.WINDOW_NORMAL) 43 | cv2.imshow('srcimg', img) 44 | winName = 'RestoreFormer in OnnxRuntime' 45 | cv2.namedWindow(winName, cv2.WINDOW_NORMAL) 46 | cv2.imshow(winName, dstimg) 47 | cv2.waitKey(0) 48 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /test_images/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpc203/Face-restoration-models/0869392b499eb10f4c6fa60fa276d02fa468d7b8/test_images/1.jpg -------------------------------------------------------------------------------- /test_images/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpc203/Face-restoration-models/0869392b499eb10f4c6fa60fa276d02fa468d7b8/test_images/2.jpg -------------------------------------------------------------------------------- /test_images/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpc203/Face-restoration-models/0869392b499eb10f4c6fa60fa276d02fa468d7b8/test_images/3.jpg -------------------------------------------------------------------------------- /test_images/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpc203/Face-restoration-models/0869392b499eb10f4c6fa60fa276d02fa468d7b8/test_images/4.jpg --------------------------------------------------------------------------------