Go to https://github.com/mav-rik/facerestore_cf for a more up to date fork of this project.
These ComfyUI nodes can be used to restore faces in images similar to the face restore option in AUTOMATIC1111 webui.
Installation
Extract the zip and put the facerestore directory inside the ComfyUI custom_nodes directory.
If running the portable windows version of ComfyUI, run embedded_install.bat
Windows users not running the portable version can run install.bat. Linux and mac users can run install.sh
If the install fails for whatever reason, you'll need to work out how to install opencv-python yourself (you'll get a cv2 not found error otherwise)
Face Restoration Models
These models perform the face restoration. If you use automatic1111 you probably already have them but if not here are the download links.
Just put them in models/facerestore_models.
https://github.com/TencentARC/GFPGAN/releases/
https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.4.pth
https://github.com/sczhou/CodeFormer/releases/
https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth
Face detection models
The face restoration model only works with cropped face images. A face detection model is used to send a crop of each face found to the face restoration model.
These will automaticly be downloaded and placed in models/facedetection the first time each is used.
Here's the links if you'd rather download them yourself.
https://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_Resnet50_Final.pth
https://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_mobilenet0.25_Final.pth
https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/yolov5l-face.pth
https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/yolov5n-face.pth
This also gets downloaded and put in the facedetection folder. It's used to put the restored faces back into the original image.
https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/parsing_parsenet.pth
Description
FAQ
Comments (25)
I keep getting
"RuntimeError: The size of tensor a (36) must match the size of tensor b (256) at non-singleton dimension 0"
I ran this with your setup and a default set up and I still get this error, Im not sure if it's me or I need to contact someone else?
Also what and how do i do the pip install? what do i need to install and where do i run the pip command?
I am as well, i tried to find the right lib/dependency that needed to be installed but came up short getting it to work
That's the error you get if you use the ImageUpscaleWithModel node. You should be using the FaceRestoreWithModelNode (found under the Add Node/facerestore).
The error is because the face restore model expects the input image to be a 512x512 face crop but it's given a smaller image. The FaceRestoreWithModel node extracts a 512x512 face crop using the selected face detection model, processes the extracted face with the face restore model, and blends it back into the image.
Using ImageUpscaleWithModel node will fail even if your input image is 512x512 because it breaks the image up into tiles and sends each tile to the upscale model.
it is not showing up in the UI for me
Hey guys, the dependency that you need is cv2. To install it you only need to run pip3 install opencv-python. If you are using the portable version of ComfyUI for windows the dependecy will be installed on the python directory, the problem is that this version has an python_embeded python. So what you need to do is grab the dependencies that you have installed on the python directory and move them to "python_embeded\Lib\site-packages" inside the ComfyUI root folder. If you don't know where to find your root dependency directory if you run again the same command it will throw a messgae saying that you have alredy installed that dependency and the path where it is.
To make this running you need to install a dependency called cv2. To install it you only need to run: pip3 install opencv-python. If you are using the portable version of ComfyUI for windows the dependecy will be installed on the python directory, the problem is that this version has an python_embeded python. So what you need to do is grab the dependencies that you have installed on the python directory and move them to "python_embeded\Lib\site-packages" inside the ComfyUI root folder. If you don't know where to find your root dependency directory if you run again the same command it will throw a messgae saying that you have alredy installed that dependency and the path where it is.
If you have the standalone (portable) version with the embedded python, you can also:
E:\portables\ComfyUI_windows_portable> .\python_embeded\python -m pip install opencv-python
Works very well! :) I got it to work! Works really well when generating multiple images to all have it! Thank you!!
works good ... will it be possible to use the YOLO models ?
Traceback (most recent call last): File "D:\SD\ComfyUI\ComfyUI\execution.py", line 182, in execute executed += recursive_execute(self.server, prompt, self.outputs, x, extra_data) File "D:\SD\ComfyUI\ComfyUI\execution.py", line 58, in recursive_execute executed += recursive_execute(server, prompt, outputs, input_unique_id, extra_data) File "D:\SD\ComfyUI\ComfyUI\execution.py", line 58, in recursive_execute executed += recursive_execute(server, prompt, outputs, input_unique_id, extra_data) File "D:\SD\ComfyUI\ComfyUI\execution.py", line 67, in recursive_execute outputs[unique_id] = getattr(obj, obj.FUNCTION)(**input_data_all) File "D:\SD\ComfyUI\ComfyUI\custom_nodes\facerestore\__init__.py", line 120, in restore_face self.face_helper = FaceRestoreHelper(1, face_size=512, crop_ratio=(1, 1), det_model=facedetection, save_ext='png', use_parse=True, device=device) File "D:\SD\ComfyUI\ComfyUI\custom_nodes\facerestore\facelib\utils\face_restoration_helper.py", line 105, in __init__ self.face_det = init_detection_model(det_model, half=False, device=self.device) File "D:\SD\ComfyUI\ComfyUI\custom_nodes\facerestore\facelib\detection\__init__.py", line 18, in init_detection_model model = init_yolov5face_model(model_name, device) File "D:\SD\ComfyUI\ComfyUI\custom_nodes\facerestore\facelib\detection\__init__.py", line 51, in init_yolov5face_model model = YoloDetector(config_name='custom_nodes/facerestore/facelib/detection/yolov5face/models/yolov5l.yaml', device=device) File "D:\SD\ComfyUI\ComfyUI\custom_nodes\facerestore\facelib\detection\yolov5face\face_detector.py", line 45, in __init__ self.detector = Model(cfg=config_name) File "D:\SD\ComfyUI\ComfyUI\custom_nodes\facerestore\facelib\detection\yolov5face\models\yolo.py", line 99, in __init__ with Path(cfg).open(encoding="utf8") as f: File "pathlib.py", line 1119, in open FileNotFoundError: [Errno 2] No such file or directory: 'custom_nodes\\facerestore\\facelib\\detection\\yolov5face\\models\\yolov5l.yaml'
The yolo models should just work (works on my machine ¯\_(ツ)_/¯)
The error you're seeing is because yolov5l.yaml can't be found. Some troubleshooting steps
- Double check the file is there
- A relative path is being used. Make sure ComfyUI is the working directory when you launch comfy.
- If all else fails, modify facerestore/facelib/detection/__init__.py line 51 to use an absolute file path
I'm also getting the same issues. I run ComfyUI by clicking on the "run_nvidia_gpu" bat file, everything works except the yolov5l. :/ Will try changing the path, I'll update if it works
Edit : I simply didn't have the .pth models somehow, it didn't got downloaded automatically. Will try manually, it should work after that !
Edit : Didn't work, I now get the same issue as above. Double-checked every file exists.
@m99 this a path microsoft build tools problem, i fix here.
@theneobr how to fix
File "D:\ComfyUI_Portable\ComfyUI\custom_nodes\facerestore\facelib\detection\yolov5face\models\yolo.py", line 99, in init
with Path(cfg).open(encoding="utf8") as f:
File "pathlib.py", line 1119, in open
FileNotFoundError: [Errno 2] No such file or directory: 'custom_nodes\\facerestore\\facelib\\detection\\yolov5face\\models\\yolov5n.yaml'
@kuma660224 I released an update that should fix the FileNotFoundError
can share workflow demo?
This is great!
One useful addition would be a strength slider, to affect how much the input is being changed.
Very useful. Works like a charm ;o) Thx a lot !
Honestly I tried so hard to change the code to allow for a batch_size of 2 or more, but it really goes over my head.
Think you could help?
I think I've managed it, it's a bit hacky but basically it just does a loop and then overwrites the input array values after processing and returns them.
@SilverAndJade I also managed to get it to work with a batch size of one, but cannot get a batch size of greater than one to work. What hack did you use to loop through the input array? Could you show a work flow? Please?
Skip D:\AI\ComfyUI_windows_portable\ComfyUI\custom_nodes\facerestore module for custom nodes due to the lack of NODE_CLASS_MAPPINGS.
how to fix?
There should be a file called `__init__.py` in the facerestore folder. At the end of that file you should see NODE_CLASS_MAPPINGS = { If that file is missing or corrupt, extract the zip again.
It's possible an update to comfy broke something as well, I havn't updated my copy in a while.
@m99 3q, it is work
