chflame163 / ComfyUI_LayerStyle

A set of nodes for ComfyUI that can composite layer and mask to achieve Photoshop like functionality.
MIT License
937 stars 46 forks source link

ImportError: cannot import name 'guidedFilter' from 'cv2.ximgproc' #5

Open chflame163 opened 6 months ago

chflame163 commented 6 months ago

This error is caused by incorrect version of the opencv-contrib-python package,or this package is overwriteen by other opencv packages.

Solution:

spacepxl commented 6 months ago

This can also be caused by having multiple opencv packages installed simultaneously, eg opencv-python and opencv-contrib-python. If that happens, the only solution I've found is to uninstall anything except for opencv-contrib-python, because it includes everything in all the other variants. Unfortunately some custom node packs explicitly list requirements for opencv-python or opencv-python-headless, and when they get updated by the manager, it reinstalls "missing" dependencies, and then you have to go back and uninstall them again after it's done. I wish there was a better solution but I haven't found one yet.

chflame163 commented 6 months ago

Thank you for your detailed explanation. and thank you for your great work at ComfyUI Image Filters.

wibur0620 commented 6 months ago

As mentioned by the user above, some custom node packages explicitly specify requirements for opencv-python or opencv-python-headless, including version numbers. For example, the comfyui_controlnet_aux node requires opencv-python>=4.7.0.72. If I forcefully pip install opencv-contrib-python at this point, ComfyUI_InstantID, comfyui_controlnet_aux, and ComfyUI_LayerStyle plugins will fail to load successfully when launching ComfyUI.

Later, I uninstalled opencv-contrib-python and reinstalled it, setting the version to 4.7.0.72. I did this because I couldn't uninstall opencv-python>=4.7.0.72, as it's a dependency for the comfyui_controlnet_aux node. Therefore, I had to install opencv-contrib-python==4.7.0.72.

Windows PowerShell.txt

Next, I launched ComfyUI again. Despite receiving error messages, please refer to the attachment I sent for details, this time all nodes were successfully loaded. Upon entering ComfyUI, all nodes operated normally. ComfUI.txt

I hope the author can fully resolve this issue instead of following the method you mentioned earlier, such as pip uninstall opencv-contrib-python followed by pip install opencv-contrib-python again. This approach simply doesn't solve the problem. While using a specific version of opencv-contrib-python to install, all plugins are now functioning normally, but seeing error messages every time I start up is anxiety-inducing. Moreover, when updating the ComfyUI_LayerStyle node in the manager, all dependencies revert to their previous state, causing ComfyUI_LayerStyle, ComfyUI_InstantID, and comfyui_controlnet_aux nodes to fail loading again. Therefore, I urge the author to thoroughly address this issue.

spacepxl commented 6 months ago

Wibur, the reason you're getting an error is because you have both opencv-python and opencv-contrib-python installed in the same environment, which is not something that is supported by opencv. Opencv-contrib-python will actually satisfy any requirements for opencv-python or headless variants, because it includes everything that is in all the other 3 variants. Pip doesn't understand this, it sees them as separate packages, but when you import cv2 into a python script, it does not matter that you have contrib instead.

As I understand it, if you have opencv-python and opencv-contrib-python installed at the same time, any import statements will default to importing from opencv-python, which is what causes the cv2.ximgproc error. The fix is to uninstall opencv-python, even though it's "required" by comfyui_controlnet_aux. Opencv-contrib-python will also meet that reqirement, but pip is just not smart enough to understand. As for versions, I'm using 4.9.0.80 with no issues.

laidingq commented 6 months ago

@spacepxl @wibur0620 @chflame163 The solution to this problem is as follows:

pip uninstall opencv_python pip install opencv_python==4.9.0.80 pip install opencv_contrib_python_headless==4.9.0.80 pip install opencv_contrib_python==4.9.0.80 pip install opencv_python_headless==4.9.0.80

You only need to install the above four dependency packages at the same time and keep the versions consistent to solve the problem perfectly (it does not have to be 4.9xxxx, other versions can also be used, the important thing is to keep them consistent)

laidingq commented 6 months ago

pip install opencv_python>=4.7.0.72 pip install opencv_contrib_python_headless>=4.7.0.72 pip install opencv_contrib_python>=4.7.0.72 pip install opencv_python_headless>=4.7.0.72 最好这样做,因为 comfyui_controlnet_aux 插件和其他几个插件指定版本 >= 4.7 .0.72

In this way, version inconsistency may still occur. It is safe to use ==, and several other plug-ins are compatible with higher versions. There is no need to worry. I have practiced these.

wibur0620 commented 6 months ago

pip install opencv_python>=4.7.0.72 pip install opencv_contrib_python_headless>=4.7.0.72 pip install opencv_contrib_python>=4.7.0.72 pip install opencv_python_headless>=4.7.0.72 最好这样做,因为 comfyui_controlnet_aux 插件和其他几个插件指定版本 >= 4.7 .0.72

In this way, version inconsistency may still occur. It is safe to use ==, and several other plug-ins are compatible with higher versions. There is no need to worry. I have practiced these.

Unfortunate things happen. After I had them all == 4.9.0.80. A new problem appeared

ComfUI.txt requirementsrequirements.txt

laidingq commented 6 months ago

You can try other versions, such as 4.8xxxx, I have no problem using 4.9xxxx

spacepxl commented 6 months ago

pip uninstall opencv_python pip install opencv_python==4.9.0.80 pip install opencv_contrib_python_headless==4.9.0.80 pip install opencv_contrib_python==4.9.0.80 pip install opencv_python_headless==4.9.0.80

You only need to install the above four dependency packages at the same time and keep the versions consistent to solve the problem perfectly (it does not have to be 4.9xxxx, other versions can also be used, the important thing is to keep them consistent)

Just tested and this did work for me. Guess I'm adding all 4 to the requirements for my project, lol. No errors related to guidedfilter or gapi after updating comfy+extensions, including controlnet_aux.

requirements.txt for reference

wibur0620 commented 6 months ago

pip uninstall opencv_python pip install opencv_python==4.9.0.80 pip install opencv_contrib_python_headless==4.9.0.80 pip install opencv_contrib_python==4.9.0.80 pip install opencv_python_headless==4.9.0.80 You only need to install the above four dependency packages at the same time and keep the versions consistent to solve the problem perfectly (it does not have to be 4.9xxxx, other versions can also be used, the important thing is to keep them consistent)

Just tested and this did work for me. Guess I'm adding all 4 to the requirements for my project, lol. No errors related to guidedfilter or gapi after updating comfy+extensions, including controlnet_aux.

requirements.txt for reference

If you have installed both comfyui-reactor-node and comfyui_controlnet_aux, then the issue must still exist. comfyui_controlnet_aux will fail to load. The solution to this problem is to first execute python.exe -m pip uninstall -y opencv-python opencv-contrib-python opencv-python-headless opencv-contrib-python-headless and then execute python.exe -m pip install opencv-python==4.7.0.72. Now comfyui_controlnet_aux and comfyui-reactor-node can run normally together. However, ComfyUI_LayerStyle is now unable to run, so the errors occurring between these three plugins leave me puzzled. Unless I give up one of the plugins, I cannot resolve this.

wibur0620 commented 6 months ago

It's like having three musketeers. They contend with each other, incompatible like water and fire.

wibur0620 commented 6 months ago

After I removed comfyui-reactor-node. They are now in a good place. Import time for custom nodes: 0.4 seconds: D:\AI\ComfyUIbats\ComfyUI\custom_nodes\ComfyUI-Manager 0.9 seconds D:\AI\ComfyUIbats\ComfyUI\custom_nodes\comfyui_controlnet_aux 26.3 seconds D:\AI\ComfyUIbats\ComfyUI\custom_nodes\ComfyUI_LayerStyle

Start Server

wibur0620 commented 6 months ago

I have redeployed comfyui, currently only installing comfyui-reactor-node, ComfyUI-Manager, ComfyUI_LayerStyle, and comfyui_controlnet_aux plugins. I have aligned the version of the OpenCV library. Now with all four plugins installed and comfyui launched, comfyui_controlnet_aux and ComfyUI_LayerStyle fail to load, with an error message: "AttributeError: module 'cv2.gapi.wip.draw' has no attribute 'Text'". For details, please refer to the environment dependency snapshot and startup information I provided below. requirements.txt comfyui.bat.txt

wibur0620 commented 6 months ago

After shutting down comfyui_controlnet_aux and restarting comfyui, ComfyUI_LayerStyle fails to load again with the error message: "AttributeError: module 'cv2.gapi.wip.draw' has no attribute 'Text'". comfyui.txt

wibur0620 commented 6 months ago

After importing comfyui_controlnet_aux again and simultaneously closing ComfyUI_LayerStyle, then restarting comfyui, comfyui_controlnet_aux fails to load with the error message: "AttributeError: module 'cv2.gapi.wip.draw' has no attribute 'Text'". comfyui.txt

wibur0620 commented 6 months ago

After importing ComfyUI_LayerStyle, this time I closed comfyui-reactor-node and restarted comfyui. Finally, everything is working fine now, which is quite perplexing. I really need all three plugins, but now I have to give up starting one of them in order to run smoothly. comfyui.txt

fernicar commented 6 months ago

It fix for me in Python 3.10.11 after the recommended version uninstall/install method. python.exe -m pip uninstall -y opencv_python python.exe -m pip install opencv_python==4.7.0.72 opencv_contrib_python_headless==4.7.0.72 opencv_contrib_python==4.7.0.72 opencv_python_headless==4.7.0.72

pip list ... opencv-contrib-python 4.7.0.72 opencv-contrib-python-headless 4.7.0.72 opencv-python 4.7.0.72 opencv-python-headless 4.7.0.72 ... workflow

chflame163 commented 6 months ago

Hi everyone. I saw the discussion. I have an idea to make the 'opencv-contrib-python' as an internal package, which should completely solve the problem. But I encountered a problem, when I doing this, many errors have occurred, and I don't know how to solve it. I find this is not a good idea.

chflame163 commented 6 months ago

After importing ComfyUI_LayerStyle, this time I closed comfyui-reactor-node and restarted comfyui. Finally, everything is working fine now, which is quite perplexing. I really need all three plugins, but now I have to give up starting one of them in order to run smoothly. comfyui.txt

As you said, the comfyui-reactor-node has done some processing on sys.modules. I checked his code and found the issue at line 32 of comfyui-reactor-node/ init. py.

Modifying this line to

if module not in original_modules and not module.startswith("google.protobuf") and not module.startswith("onnx") and not module.startswith("cv2"):

can solve the problem.

20240223153613 20240223154454
laidingq commented 6 months ago

Haha, I haven't made any changes. These plugins have been running normally all along

wibur0620 commented 6 months ago

After importing ComfyUI_LayerStyle, this time I closed comfyui-reactor-node and restarted comfyui. Finally, everything is working fine now, which is quite perplexing. I really need all three plugins, but now I have to give up starting one of them in order to run smoothly. comfyui.txt导入ComfyUI_LayerStyle后,这次我关闭了 comfyui-reactor-node 并重新启动了 comfyui。最后,现在一切正常,这非常令人困惑。我真的需要所有三个插件,但现在我必须放弃启动其中一个才能顺利运行。comfyui.txt

As you said, the comfyui-reactor-node has done some processing on sys.modules. I checked his code and found the issue at line 32 of comfyui-reactor-node/ init. py.正如您所说,comfyui-reactor-node 对 sys.modules 进行了一些处理。我检查了他的代码,并在 comfyui-reactor-node/ init 的第 32 行发现了问题。py。

Modifying this line to将此行修改为

if module not in original_modules and not module.startswith("google.protobuf") and not module.startswith("onnx") and not module.startswith("cv2"):

can solve the problem.可以解决问题。

20240223153613 20240223154454

Thank you very much for your help, they are now working perfectly after I made the changes the way you did, but I'm still a bit worried. If the next update will the same problem occur again ComfUI.txt [Uploading Windows PowerShell.txt…]()

chflame163 commented 6 months ago

After importing ComfyUI_LayerStyle, this time I closed comfyui-reactor-node and restarted comfyui. Finally, everything is working fine now, which is quite perplexing. I really need all three plugins, but now I have to give up starting one of them in order to run smoothly. comfyui.txt导入ComfyUI_LayerStyle后,这次我关闭了 comfyui-reactor-node 并重新启动了 comfyui。最后,现在一切正常,这非常令人困惑。我真的需要所有三个插件,但现在我必须放弃启动其中一个才能顺利运行。comfyui.txt

As you said, the comfyui-reactor-node has done some processing on sys.modules. I checked his code and found the issue at line 32 of comfyui-reactor-node/ init. py.正如您所说,comfyui-reactor-node 对 sys.modules 进行了一些处理。我检查了他的代码,并在 comfyui-reactor-node/ init 的第 32 行发现了问题。py。 Modifying this line to将此行修改为 if module not in original_modules and not module.startswith("google.protobuf") and not module.startswith("onnx") and not module.startswith("cv2"): can solve the problem.可以解决问题。 20240223153613 20240223154454

Thank you very much for your help, they are now working perfectly after I made the changes the way you did, but I'm still a bit worried. If the next update will the same problem occur again ComfUI.txt Uploading Windows PowerShell.txt…

This require that plugin author to submit code modifications.

Gourieff commented 6 months ago

Fixed on the ReActor Node side https://github.com/Gourieff/comfyui-reactor-node/commit/b08d47588c0683b186dc9fd623e86bbeb5eb9dba All 3 nodes should work together correctly for now

chflame163 commented 5 months ago

Fixed on the ReActor Node side Gourieff/comfyui-reactor-node@b08d475 All 3 nodes should work together correctly for now

Thank you for your works.

ultimatech-cn commented 5 months ago

What is the final solution? reinstall related opencv package or update all the node to latest?

chflame163 commented 5 months ago
  • Solution:

Close ComfyUI and open the terminal window in the plugin directory and execute the following command :

 ../../../python_embeded/python.exe -s -m pip uninstall -y opencv-python opencv-python-headless opencv-contrib-python-headless opencv-contrib-python

then enter:

../../../python_embeded/python.exe -s -m pip install opencv-contrib-python

Restart ComfyUI.

This way.

ultimatech-cn commented 5 months ago
  • Solution:

Close ComfyUI and open the terminal window in the plugin directory and execute the following command :

 ../../../python_embeded/python.exe -s -m pip uninstall -y opencv-python opencv-python-headless opencv-contrib-python-headless opencv-contrib-python

then enter:

../../../python_embeded/python.exe -s -m pip install opencv-contrib-python

Restart ComfyUI.

This way.

I have tried, still got red node on the comfyui. Still checking. No error on starting, but still red node 1709542820661

chflame163 commented 5 months ago

I have tried, still got red node on the comfyui. Still checking. No error on starting, but still red node !

Please attach the log of ComfyUI starting, and run ../../../python_embeded/python.exe -s -m pip list terminal command from plugin folder,list all of the startwith "opencv...." package here.

ultimatech-cn commented 5 months ago

Log file: comfyui_8188.log Result of pip list

open-clip-torch              2.24.0
openai                       0.28.1
opencv-contrib-python        4.9.0.80
opendatalab                  0.0.9
openmim                      0.3.9

Error in my comfyui 1709556234554

Thanks for your help.

chflame163 commented 5 months ago

Log file: comfyui_8188.log Result of pip list

open-clip-torch              2.24.0
openai                       0.28.1
opencv-contrib-python        4.9.0.80
opendatalab                  0.0.9
openmim                      0.3.9

Error in my comfyui Thanks for your help.

There is no "ImportError:" in your log file, and the LayerStyle also shows that it has been loaded normally. You have installed opencv-contrib-python and only have this one opencv package, indicating that you are not THIS ISSUE. I saw you have installed 100 plugins. There may be a possibility of conflicts with so many plugins. I suggest you deploy a new ComfyUI environment for testing in another directory.

ultimatech-cn commented 5 months ago

Error in my comfyui Thanks for your help.

There is no "ImportError:" in your log file, and the LayerStyle also shows that it has been loaded normally. You have installed opencv-contrib-python and only have this one opencv package, indicating that you are not THIS ISSUE. I saw you have installed 100 plugins. There may be a possibility of conflicts with so many plugins. I suggest you deploy a new ComfyUI environment for testing in another directory.

Thanks a lot. I will try it. Another question, is there any easy way to find the confilct nodes?

chflame163 commented 5 months ago

Thanks a lot. I will try it. Another question, is there any easy way to find the confilct nodes?

This is a tedious task that requires some time to verify.

Firstly, update your ComfyUI and all plugins to latest version. If this still persists, you can troubleshoot the issue by moving the plugin folders in custom_modes folder.

I saw that you installed ComfyUI on the E drive, so

ultimatech-cn commented 5 months ago

Your kind reply is most appreciated. I will try it.

escalate007 commented 4 months ago

@spacepxl @wibur0620 @chflame163 The solution to this problem is as follows:

pip uninstall opencv_python pip install opencv_python==4.9.0.80 pip install opencv_contrib_python_headless==4.9.0.80 pip install opencv_contrib_python==4.9.0.80 pip install opencv_python_headless==4.9.0.80

You only need to install the above four dependency packages at the same time and keep the versions consistent to solve the problem perfectly (it does not have to be 4.9xxxx, other versions can also be used, the important thing is to keep them consistent)

I fixed it according to your suggestion.Thanks.

Autantpourmoi commented 3 months ago

still facing that problem , tried different version, uninstall, reinstall, no change ,still are unable to import guidedfilter update: was able to solve it , uninstalling one by one: opencv-contrib-python opencv-contrib-python-headless opencv-python opencv-python-headless Then reinstalling only opencv-contrib-python

lior007 commented 3 months ago

This error is caused by incorrect version of the opencv-contrib-python package,or this package is overwriteen by other opencv packages.

Solution:

  • Close ComfyUI and open the terminal window in the plugin directory and execute the following command :
 ../../../python_embeded/python.exe -s -m pip uninstall -y opencv-python opencv-python-headless opencv-contrib-python-headless opencv-contrib-python
  • To avoid other plugins install opencv packages, you can install here first, also can skip this step:
../../../python_embeded/python.exe -s -m pip install opencv-python opencv-python-headless opencv-contrib-python-headless
  • Then install opencv-contrib-python, ensuring that it is the LAST ONE to be installed:
../../../python_embeded/python.exe -s -m pip install opencv-contrib-python
  • Restart ComfyUI.

where is the "plugin directory" . i have a lot plugin directory

chflame163 commented 3 months ago

where is the "plugin directory" . i have a lot plugin directory

ComfyUI/custom_nodes/ComfyUI_LayerStyle

lior007 commented 3 months ago

where is the "plugin directory" . i have a lot plugin directory

ComfyUI/custom_nodes/ComfyUI_LayerStyle

THANK YOU!!!

Autantpourmoi commented 3 months ago

Can someone drive me to a solution , as i'm facing that error message again on Fedora Linux ?

Humanoidme commented 2 months ago

still facing that problem , tried different version, uninstall, reinstall, no change ,still are unable to import guidedfilter update: was able to solve it , uninstalling one by one: opencv-contrib-python opencv-contrib-python-headless opencv-python opencv-python-headless Then reinstalling only opencv-contrib-python

Did you do this inside ComfyUI/custom_nodes/ComfyUI_LayerStyle ?

PrometheusDante commented 2 months ago

From within ComfyUI\python_embeded open a cmd window and enter

python -m pip uninstall opencv-python opencv-python-headless opencv-contrib-python-headless

and then to install/reinstall to make as sure as it gets

python -m pip install opencv-contrib-python

That's at least how I did it. But it might just happen that it will reinstall undesired opencv versions (or at least try to) upon starting up comfy, which is annoying to say the least.

piovis2023 commented 2 months ago

After importing ComfyUI_LayerStyle, this time I closed comfyui-reactor-node and restarted comfyui. Finally, everything is working fine now, which is quite perplexing. I really need all three plugins, but now I have to give up starting one of them in order to run smoothly. comfyui.txt

As you said, the comfyui-reactor-node has done some processing on sys.modules. I checked his code and found the issue at line 32 of comfyui-reactor-node/ init. py.

Modifying this line to

if module not in original_modules and not module.startswith("google.protobuf") and not module.startswith("onnx") and not module.startswith("cv2"):

can solve the problem.

20240223153613 20240223154454

I tried this node set up but cant replicate the result (which looks amazing by the way). I dont have any errors.

rafstahelin commented 2 months ago

python.exe -s -m pip install opencv-python opencv-python-headless opencv-contrib-python-headless

when i try to install the headless 2nd command, i get permissions error:

E:\comfy_ui\ComfyUI_windows_portable\python_embeded>python.exe -s -m pip install opencv-python opencv-python-headless opencv-contrib-python-headless Requirement already satisfied: opencv-python in e:\comfy_ui\comfyui_windows_portable\python_embeded\lib\site-packages (4.10.0.84) Requirement already satisfied: opencv-python-headless in e:\comfy_ui\comfyui_windows_portable\python_embeded\lib\site-packages (4.10.0.84) Collecting opencv-contrib-python-headless Using cached opencv_contrib_python_headless-4.10.0.84-cp37-abi3-win_amd64.whl.metadata (20 kB) Requirement already satisfied: numpy>=1.21.2 in e:\comfy_ui\comfyui_windows_portable\python_embeded\lib\site-packages (from opencv-python) (1.26.4) Using cached opencv_contrib_python_headless-4.10.0.84-cp37-abi3-win_amd64.whl (45.4 MB) Installing collected packages: opencv-contrib-python-headless ERROR: Could not install packages due to an OSError: [WinError 5] Access is denied: 'E:\comfy_ui\ComfyUI_windows_portable\python_embeded\Lib\site-packages\cv2\cv2.pyd' Consider using the --user option or check the permissions.

chflame163 commented 2 months ago

when i try to install the headless 2nd command, i get permissions error:

Perhaps ComfyUi has not been closed?

rafstahelin commented 2 months ago

Thanks small oversightStill getting later style errors when I start comfyui though. Opened an issue Best, Raf On 19 Jun 2024, at 04:17, chflame163 @.***> wrote:

when i try to install the headless 2nd command, i get permissions error:

Perhaps ComfyUi has not been closed?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>

rafstahelin commented 2 months ago

none of the above fixed. Havent tried re-installing with opencv-python>=4.7.0.72 Is that still the best solution? i tried using the repair_dependency.bat also but no luck

rafstahelin commented 2 months ago

i have installed layer style on a fresh comfy install, following all the suggestions above and still get errors (repair_dependency.bat, reinstalling opencv, etc). Really looking forward to a real fix

daxcay commented 1 month ago

https://github.com/chflame163/ComfyUI_LayerStyle/issues/141

Hey guys! i have managed to solve this issue: Cannot import name 'guidedFilter' from 'cv2.ximgproc'

Install: python.exe -m pip install opencv-contrib-python-headless

And open cmd prompt and install this:

image

I am also on my discord channel to discuss bugs and issues as I am a starting python/comfyui engineer, feel free to drop by https://discord.gg/Z44Zjpurjp and say hi

More than happy to chat.

RGX650 commented 1 month ago

pip install --force-reinstall numpy==1.26.4 opencv-python-headless==4.10.0.84 opencv-contrib-python==4.10.0.84

Shingkyo commented 1 month ago

python.exe -m pip install opencv_python==4.7.0.72 opencv_contrib_python_headless==4.7.0.72 opencv_contrib_python==4.7.0.72 opencv_python_headless==4.7.0.72

This version works for me.. Thx