diff --git a/.gitattributes b/.gitattributes index 28df5f900b358436f0267334b3e3e9af33f917ba..c13db462a1734eda47fcda43ccaf117590427c2a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -53,3 +53,13 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text *.jpg filter=lfs diff=lfs merge=lfs -text *.jpeg filter=lfs diff=lfs merge=lfs -text *.webp filter=lfs diff=lfs merge=lfs -text +custom_nodes/comfyui_controlnet_aux/NotoSans-Regular.ttf filter=lfs diff=lfs merge=lfs -text +custom_nodes/comfyui_controlnet_aux/src/custom_controlnet_aux/mesh_graphormer/hand_landmarker.task filter=lfs diff=lfs merge=lfs -text +custom_nodes/comfyui-elegant-resource-monitor/web/assets/css/material.woff2 filter=lfs diff=lfs merge=lfs -text +custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/core.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text +custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/impact_pack.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text +custom_nodes/comfyui-kjnodes/fonts/FreeMono.ttf filter=lfs diff=lfs merge=lfs -text +custom_nodes/comfyui-kjnodes/fonts/FreeMonoBoldOblique.otf filter=lfs diff=lfs merge=lfs -text +custom_nodes/comfyui-kjnodes/fonts/TTNorms-Black.otf filter=lfs diff=lfs merge=lfs -text +custom_nodes/comfyui-kjnodes/nodes/__pycache__/image_nodes.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text +custom_nodes/comfyui-kjnodes/nodes/__pycache__/nodes.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text diff --git a/custom_nodes/ComfyUI-Crystools/.editorconfig b/custom_nodes/ComfyUI-Crystools/.editorconfig new file mode 100644 index 0000000000000000000000000000000000000000..0fcf04f21ded11b05867a2612a67c79fdeada051 --- /dev/null +++ b/custom_nodes/ComfyUI-Crystools/.editorconfig @@ -0,0 +1,20 @@ +# EditorConfig helps developers define and maintain consistent +# coding styles between different editors and IDEs +# editorconfig.org + +root = true + +[*] +# Change these settings to your own preference +indent_style = space +indent_size = 2 +max_line_length = 120 + +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +max_line_length = off +trim_trailing_whitespace = false diff --git a/custom_nodes/ComfyUI-Crystools/.eslintrc.cjs b/custom_nodes/ComfyUI-Crystools/.eslintrc.cjs new file mode 100644 index 0000000000000000000000000000000000000000..f9cb51578788e3d2bc3bf061479860afe0b79cb1 --- /dev/null +++ b/custom_nodes/ComfyUI-Crystools/.eslintrc.cjs @@ -0,0 +1,164 @@ +module.exports = { + root: true, + env: { browser: true, es2020: true }, + extends: [ + 'eslint:recommended', + 'plugin:@typescript-eslint/recommended', + 'plugin:@typescript-eslint/recommended-type-checked', + 'plugin:@typescript-eslint/stylistic-type-checked' + ], + parser: '@typescript-eslint/parser', + plugins: ['@typescript-eslint', 'import'], + parserOptions: { + // tsconfigRootDir: __dirname, + sourceType: 'module', + ecmaVersion: 'latest', + ecmaFeatures: { + // experimentalObjectRestSpread: true, + modules: true, + legacyDecorators: true + }, + project: ['./tsconfig.json'], + }, + rules: { + // 'import/extensions': [ + // 'error', + // "ignorePackages", + // { + // "js": "always", + // "ts": "never" + // } + // ], + 'import/no-unresolved': ['off'], + 'class-methods-use-this': ['off'], + 'radix': ['off'], + 'import/prefer-default-export': ['off'], + 'implicit-arrow-linebreak': ['off'], + 'no-trailing-spaces': [ + 'error', { + 'skipBlankLines': true, + 'ignoreComments': true, + }, + ], + 'max-len': [ + 'error', { + 'code': 120, + }, + ], + 'prefer-rest-params': ['off'], + + 'import/no-extraneous-dependencies': [ + 'error', { + 'devDependencies': ['**/__tests__/*.ts', '**/__mocks__/*.ts'], + }, + ], + + 'one-var': ['off'], + + // false positive https://github.com/typescript-eslint/typescript-eslint/issues/2483 + 'no-shadow': 'off', + 'object-curly-newline': ["error", { + "ObjectExpression": { "multiline": true, "minProperties": 8 }, + "ObjectPattern": { "multiline": true, "minProperties": 8 }, + "ImportDeclaration": {"multiline": true, "minProperties": 8 }, + "ExportDeclaration": { "multiline": true, "minProperties": 8 } + }], + "@typescript-eslint/explicit-function-return-type": 'error', + '@typescript-eslint/triple-slash-reference': 'off', + '@typescript-eslint/no-shadow': 'error', + '@typescript-eslint/no-unsafe-argument': 'off', + '@typescript-eslint/no-unsafe-assignment': 'off', + '@typescript-eslint/no-unsafe-member-access': 'off', + '@typescript-eslint/no-unsafe-return': 'off', + '@typescript-eslint/no-unsafe-call': 'off', + '@typescript-eslint/prefer-nullish-coalescing': 'off', + '@typescript-eslint/consistent-type-definitions': 'off', + '@typescript-eslint/no-unused-expressions': [ + 'error', + { + 'allowShortCircuit': true, + 'allowTernary': true, + }, + ], + '@typescript-eslint/no-use-before-define': ['off'], + '@typescript-eslint/no-unused-vars': [ + 'error', + {'argsIgnorePattern': '^_'}, + ], + '@typescript-eslint/explicit-module-boundary-types': [ + 'off', { + allowArgumentsExplicitlyTypedAsAny: true, + }, + ], + '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/ban-ts-comment': [ + 'off', + {'ts-expect-error': 'allow-with-description'}, + ], + '@typescript-eslint/no-empty-function': [ + 'error', + { + 'allow': [ + 'methods', + 'asyncMethods', + ], + }, + ], + + 'lines-between-class-members': 'off', + '@typescript-eslint/lines-between-class-members': 'off', + + 'curly': ['error', 'multi-line', 'consistent'], + 'no-unused-vars': 'off', + 'jest/no-test-prefixes': 'off', + 'jest/no-focused-tests': 'off', + 'comma-dangle': ['error', 'only-multiline'], + 'linebreak-style': ['error', 'unix'], + 'quotes': ['error', 'single'], + 'semi': ['error', 'always'], + 'eqeqeq': ['error', 'always'], + 'complexity': [ + 'error', { + max: 8, + }, + ], + 'block-scoped-var': 'error', + 'no-else-return': [ + 'error', { + allowElseIf: true, + }, + ], + 'no-debugger': 'off', + 'no-eval': 'error', + 'no-lone-blocks': 'error', + 'no-multi-spaces': 'error', + 'no-useless-return': 'error', + 'no-var': 'error', + 'no-console': [ + // 'error', { + // allow: ['warn', 'error'], + // }, + 'off', + ], + 'no-throw-literal': 'error', + 'newline-per-chained-call': [ + 'error', { + ignoreChainWithDepth: 4, + }, + ], + 'no-extra-boolean-cast': [ + 'error', { + enforceForLogicalOperands: true, + }, + ], + 'no-fallthrough': 'error', + 'no-use-before-define': 'off', + 'no-case-declarations': 'off', + }, + ignorePatterns: [ + 'node_modules', + 'web/**/*.d.ts', + 'web/**/*.js' + ], + globals: {}, +} diff --git a/custom_nodes/ComfyUI-Crystools/.github/ISSUE_TEMPLATE/bug_report.md b/custom_nodes/ComfyUI-Crystools/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000000000000000000000000000000000000..68027caf8e00fbe74a37c9f243a2496634b7aa08 --- /dev/null +++ b/custom_nodes/ComfyUI-Crystools/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,47 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +__Please attach a workflow file to make it easier for others to reproduce the error!__ + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Error in console:** +If applicable, add the error message from the console. + +**Versions:** +Copy the output of the console (4 parts), like this: +``` +** Python version: 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)] +``` +``` +Total VRAM 12287 MB, total RAM 130996 MB +Set vram state to: NORMAL_VRAM +``` +``` +[Crystools INFO] Crystools version: 1.9.2 +[Crystools INFO] CPU: AMD Ryzen 9 5950X 16-Core Processor - Arch: AMD64 - OS: Windows 10 +[Crystools INFO] GPU/s: +[Crystools INFO] 0) NVIDIA GeForce RTX 3080 Ti +[Crystools INFO] NVIDIA Driver: 546.33 +``` +``` +### Loading: ComfyUI-Manager (V1.25.3) +### ComfyUI Revision: 1886 [6a10640f] | Released on '2024-01-08' +``` + +**Additional context** +Add any other context about the problem here. diff --git a/custom_nodes/ComfyUI-Crystools/.github/ISSUE_TEMPLATE/feature_request.md b/custom_nodes/ComfyUI-Crystools/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000000000000000000000000000000000000..72718d5aa63a292159351ae852c305fec1880a93 --- /dev/null +++ b/custom_nodes/ComfyUI-Crystools/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/custom_nodes/ComfyUI-Crystools/.github/workflows/publish.yml b/custom_nodes/ComfyUI-Crystools/.github/workflows/publish.yml new file mode 100644 index 0000000000000000000000000000000000000000..3f8fa2459b9ddcad50dd3115491c015ffb1400cb --- /dev/null +++ b/custom_nodes/ComfyUI-Crystools/.github/workflows/publish.yml @@ -0,0 +1,24 @@ +name: Publish to Comfy registry +on: + workflow_dispatch: + push: + branches: + - main + paths: + - "pyproject.toml" + +permissions: + issues: write + +jobs: + publish-node: + name: Publish Custom Node to registry + runs-on: ubuntu-latest + if: ${{ github.repository_owner == 'crystian' }} + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Publish Custom Node + uses: Comfy-Org/publish-node-action@v1 + with: + personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }} diff --git a/custom_nodes/ComfyUI-Crystools/.gitignore b/custom_nodes/ComfyUI-Crystools/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..c5ba16f06e812f93872b4fb863cf2bdecfa5c84a --- /dev/null +++ b/custom_nodes/ComfyUI-Crystools/.gitignore @@ -0,0 +1,156 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ +*.pyc + +.idea +/node_modules +/node.zip diff --git a/custom_nodes/ComfyUI-Crystools/.others/ComfyUI.run.xml b/custom_nodes/ComfyUI-Crystools/.others/ComfyUI.run.xml new file mode 100644 index 0000000000000000000000000000000000000000..cf00e2750e6fc7b723b204a043fc7754600f9e30 --- /dev/null +++ b/custom_nodes/ComfyUI-Crystools/.others/ComfyUI.run.xml @@ -0,0 +1,25 @@ + + + + + \ No newline at end of file diff --git a/custom_nodes/ComfyUI-Crystools/.others/lint.run.xml b/custom_nodes/ComfyUI-Crystools/.others/lint.run.xml new file mode 100644 index 0000000000000000000000000000000000000000..ab127c9f140d7d18882692ea33d7f9d5c60d3b80 --- /dev/null +++ b/custom_nodes/ComfyUI-Crystools/.others/lint.run.xml @@ -0,0 +1,12 @@ + + + + + + + + + +

Real-Time System Monitor

+
+
CPU: Loading...
+
RAM: Loading...
+
GPU: Loading...
+
VRAM: Loading...
+
HDD: Loading...
+
Temperature: Loading...
+
+ + + + diff --git a/custom_nodes/comfyui-elegant-resource-monitor/web/templates/perf-monitor/perf-monitor.html b/custom_nodes/comfyui-elegant-resource-monitor/web/templates/perf-monitor/perf-monitor.html new file mode 100644 index 0000000000000000000000000000000000000000..066ee94d6b853d97c4c0e3406f379ae31b6d4285 --- /dev/null +++ b/custom_nodes/comfyui-elegant-resource-monitor/web/templates/perf-monitor/perf-monitor.html @@ -0,0 +1,14 @@ + + + + + + + diff --git a/custom_nodes/comfyui-impact-pack/.github/workflows/publish.yml b/custom_nodes/comfyui-impact-pack/.github/workflows/publish.yml new file mode 100644 index 0000000000000000000000000000000000000000..1aa6978a6d4db8d7a1f6e71eaccb8970aa03e5d9 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/.github/workflows/publish.yml @@ -0,0 +1,25 @@ +name: Publish to Comfy registry +on: + workflow_dispatch: + push: + branches: + - main + paths: + - "pyproject.toml" + +permissions: + issues: write + +jobs: + publish-node: + name: Publish Custom Node to registry + runs-on: ubuntu-latest + if: ${{ github.repository_owner == 'ltdrdata' }} + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Publish Custom Node + uses: Comfy-Org/publish-node-action@v1 + with: + ## Add your own personal access token to your Github Repository secrets and reference it here. + personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }} diff --git a/custom_nodes/comfyui-impact-pack/.gitignore b/custom_nodes/comfyui-impact-pack/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..703fae20045f56b6a403648f924d2232d39a8c53 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/.gitignore @@ -0,0 +1,11 @@ +__pycache__ +*.ini +wildcards/** +.vscode/ +.idea/ +subpack +impact_subpack +*.txt +*.yaml +!requirements.txt +!LICENSE.txt \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/.gitmodules b/custom_nodes/comfyui-impact-pack/.gitmodules new file mode 100644 index 0000000000000000000000000000000000000000..9180e6465120d9a6e7de990c99b7517e29cda2e3 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/.gitmodules @@ -0,0 +1,3 @@ +[submodule "subpack"] + path = subpack + url = https://github.com/ltdrdata/ComfyUI-Impact-Subpack diff --git a/custom_nodes/comfyui-impact-pack/.tracking b/custom_nodes/comfyui-impact-pack/.tracking new file mode 100644 index 0000000000000000000000000000000000000000..5fdadd34ffa2c6a104f1c79baa3003e92e2cd6f8 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/.tracking @@ -0,0 +1,73 @@ +.github/workflows/publish.yml +.gitignore +.gitmodules +LICENSE.txt +README.md +__init__.py +custom_wildcards/put_wildcards_here +example_workflows/1-FaceDetailer.jpg +example_workflows/1-FaceDetailer.json +example_workflows/2-MaskDetailer.jpg +example_workflows/2-MaskDetailer.json +example_workflows/3-SEGSDetailer.jpg +example_workflows/3-SEGSDetailer.json +example_workflows/4-MakeTileSEGS-Upscale.jpg +example_workflows/4-MakeTileSEGS-Upscale.json +example_workflows/5-PreviewDetailerHookProvider.jpg +example_workflows/5-PreviewDetailerHookProvider.json +example_workflows/5-prompt-per-tile.jpg +example_workflows/5-prompt-per-tile.json +example_workflows/6-DetailerWildcard.jpg +example_workflows/6-DetailerWildcard.json +install.py +js/common.js +js/impact-image-util.js +js/impact-pack.js +js/impact-sam-editor.js +js/impact-segs-picker.js +js/mask-rect-area-advanced.js +js/mask-rect-area.js +latent.png +locales/ko/nodeDefs.json +modules/impact/additional_dependencies.py +modules/impact/animatediff_nodes.py +modules/impact/bridge_nodes.py +modules/impact/config.py +modules/impact/core.py +modules/impact/defs.py +modules/impact/detectors.py +modules/impact/hf_nodes.py +modules/impact/hook_nodes.py +modules/impact/hooks.py +modules/impact/impact_onnx.py +modules/impact/impact_pack.py +modules/impact/impact_sampling.py +modules/impact/impact_server.py +modules/impact/logics.py +modules/impact/pipe.py +modules/impact/segs_nodes.py +modules/impact/segs_upscaler.py +modules/impact/special_samplers.py +modules/impact/util_nodes.py +modules/impact/utils.py +modules/impact/wildcards.py +modules/thirdparty/noise_nodes.py +node_list.json +notebook/comfyui_colab_impact_pack.ipynb +pyproject.toml +requirements.txt +ruff.toml +test/advanced-sampler.json +test/detailer-pipe-test-sdxl.json +test/detailer-pipe-test.json +test/impactwildcardprocessor_separate_tests.json +test/impactwildcardprocessor_yaml_tests.json +test/loop-test.json +test/masks.json +test/regional_prompt.json +troubleshooting/TROUBLESHOOTING.md +troubleshooting/black1.png +troubleshooting/black2.png +wildcards/put_wildcards_here +wildcards/samples/flower.txt +wildcards/samples/jewel.txt \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/LICENSE.txt b/custom_nodes/comfyui-impact-pack/LICENSE.txt new file mode 100644 index 0000000000000000000000000000000000000000..f288702d2fa16d3cdf0035b15a9fcbc552cd88e7 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/LICENSE.txt @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/custom_nodes/comfyui-impact-pack/README.md b/custom_nodes/comfyui-impact-pack/README.md new file mode 100644 index 0000000000000000000000000000000000000000..5782c678c0f4740024bb3afc149ea7712ab0236e --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/README.md @@ -0,0 +1,514 @@ +[![Youtube Badge](https://img.shields.io/badge/Youtube-FF0000?style=for-the-badge&logo=Youtube&logoColor=white&link=https://www.youtube.com/watch?v=AccoxDZIg3Y&list=PL_Ej2RDzjQLGfEeizq4GISeY3FtVyFmGP)](https://www.youtube.com/watch?v=AccoxDZIg3Y&list=PL_Ej2RDzjQLGfEeizq4GISeY3FtVyFmGP) + +# ComfyUI-Impact-Pack + +**Custom node pack for ComfyUI** +This node pack helps to conveniently enhance images through Detector, Detailer, Upscaler, Pipe, and more. + +NOTE: The UltralyticsDetectorProvider node is not part of the ComfyUI-Impact-Pack. To use the UltralyticsDetectorProvider node, please install the ComfyUI-Impact-Subpack separately. + +## NOTICE +* V8.19: legacy nodes (mmdet and etc.) are removed +* V8.18: Support [facebookresearch/sam2](https://github.com/facebookresearch/sam2) models +* V8.0: The `Impact Subpack` is no longer installed automatically. To use `UltralyticsDetectorProvider` nodes, please install the `Impact Subpack` separately. +* V7.6: Automatic installation is no longer supported. Please install using ComfyUI-Manager, or manually install requirements.txt and run install.py to complete the installation. +* V7.0: Supports Switch based on Execution Model Inversion. +* V6.0: Supports FLUX.1 model in Impact KSampler, Detailers, PreviewBridgeLatent +* V5.0: It is no longer compatible with versions of ComfyUI before 2024.04.08. +* V4.87.4: Update to a version of ComfyUI after 2024.04.08 for proper functionality. +* V4.85: Incompatible with the outdated **ComfyUI IPAdapter Plus**. (A version dated March 24th or later is required.) +* V4.77: Compatibility patch applied. Requires ComfyUI version (Oct. 8th) or later. +* V4.73.3: ControlNetApply (SEGS) supports AnimateDiff +* V4.20.1: Due to the feature update in `RegionalSampler`, the parameter order has changed, causing malfunctions in previously created `RegionalSamplers`. Please adjust the parameters accordingly. +* V4.12: `MASKS` is changed to `MASK`. +* V4.7.2 isn't compatible with old version of `ControlNet Auxiliary Preprocessor`. If you will use `MediaPipe FaceMesh to SEGS` update to latest version(Sep. 17th). +* Selection weight syntax is changed(: -> ::) since V3.16. ([tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/ImpactWildcardProcessor.md)) +* Starting from V3.6, requires latest version(Aug 8, 9ccc965) of ComfyUI. +* **In versions below V3.3.1, there was an issue with the image quality generated after using the UltralyticsDetectorProvider. Please make sure to upgrade to a newer version.** +* Starting from V3.0, nodes related to `mmdet` are optional nodes that are activated only based on the configuration settings. + - Through ComfyUI-Impact-Subpack, you can utilize UltralyticsDetectorProvider to access various detection models. +* Between versions 2.22 and 2.21, there is partial compatibility loss regarding the Detailer workflow. If you continue to use the existing workflow, errors may occur during execution. An additional output called "enhanced_alpha_list" has been added to Detailer-related nodes. +* The permission error related to cv2 that occurred during the installation of Impact Pack has been patched in version 2.21.4. However, please note that the latest versions of ComfyUI and ComfyUI-Manager are required. +* The "PreviewBridge" feature may not function correctly on ComfyUI versions released before July 1, 2023. +* Attempting to load the "ComfyUI-Impact-Pack" on ComfyUI versions released before June 27, 2023, will result in a failure. +* With the addition of wildcard support in FaceDetailer, the structure of DETAILER_PIPE-related nodes and Detailer nodes has changed. There may be malfunctions when using the existing workflow. + + +## How To Install + +### **Recommended** +* Install via [ComfyUI-Manager](https://github.com/ltdrdata/ComfyUI-Manager). + +### **Manual** +* Navigate to `ComfyUI/custom_nodes` in your terminal (cmd). +* Clone the repository under the `custom_nodes` directory using the following command: + ``` + git clone https://github.com/ltdrdata/ComfyUI-Impact-Pack comfyui-impact-pack + cd comfyui-impact-pack + ``` +* Install dependencies in your Python environment. + * For Windows Portable, run the following command inside `ComfyUI\custom_nodes\comfyui-impact-pack`: + ``` + ..\..\..\python_embeded\python.exe -m pip install -r requirements.txt + ``` + * If using venv or conda, activate your Python environment first, then run: + ``` + pip install -r requirements.txt + ``` + +### Companion Pack +* If you need the `Ultralytics Detector Provider` to use various YOLO detection models, you should also install [ComfyUI-Impact-Subpack](https://github.com/ltdrdata/ComfyUI-Impact-Subpack). + + +## Custom Nodes +### [Detector nodes](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/detectors.md) + * `SAMLoader (Impact)` - Loads the SAM model. + * `ONNXDetectorProvider` - Loads the ONNX model to provide BBOX_DETECTOR. + * `CLIPSegDetectorProvider` - Wrapper for CLIPSeg to provide BBOX_DETECTOR. + * You need to install the ComfyUI-CLIPSeg node extension. + * `SEGM Detector (combined)` - Detects segmentation and returns a mask from the input image. + * `BBOX Detector (combined)` - Detects bounding boxes and returns a mask from the input image. + * `SAMDetector (combined)` - Utilizes the SAM technology to extract the segment at the location indicated by the input SEGS on the input image and outputs it as a unified mask. + * `SAMDetector (Segmented)` - It is similar to `SAMDetector (combined)`, but it separates and outputs the detected segments. Multiple segments can be found for the same detected area, and currently, a policy is in place to group them arbitrarily in sets of three. This aspect is expected to be improved in the future. + * As a result, it outputs the `combined_mask`, which is a unified mask, and `batch_masks`, which are multiple masks grouped together in batch form. + * While `batch_masks` may not be completely separated, it provides functionality to perform some level of segmentation. + * `Simple Detector (SEGS)` - Operating primarily with `BBOX_DETECTOR`, and with the additional provision of `SAM_MODEL` or `SEGM_DETECTOR`, this node internally generates improved SEGS through mask operations on both *bbox* and *silhouette*. It serves as a convenient tool to simplify a somewhat intricate workflow. + * `Simple Detector for Video (SEGS)` – Performs detection on videos composed of image frames. Instead of using a single mask, it performs detection individually on each image frame and generates a SEGS object with a batch of masks. + * `SAM2 Video Detector (SEGS)` – Similar to `Simple Detector for Video (SEGS)`, but utilizes SAM2’s video tracking technology to generate a SEGS object with a batch of masks. + * To use this node, you must select a SAM2 model in the SAMLoader. + + +### ControlNet, IPAdapter + * `ControlNetApply (SEGS)` - To apply ControlNet in SEGS, you need to use the Preprocessor Provider node from the Inspire Pack to utilize this node. + * `segs_preprocessor` and `control_image` can be selectively applied. If a `control_image` is given, `segs_preprocessor` will be ignored. + * If set to `control_image`, you can preview the cropped cnet image through `SEGSPreview (CNET Image)`. Images generated by `segs_preprocessor` should be verified through the `cnet_images` output of each Detailer. + * The `segs_preprocessor` operates by applying preprocessing on-the-fly based on the cropped image during the detailing process, while `control_image` will be cropped and used as input to `ControlNetApply (SEGS)`. + * `ControlNetClear (SEGS)` - Clear applied ControlNet in SEGS + * `IPAdapterApply (SEGS)` - To apply IPAdapter in SEGS, you need to use the Preprocessor Provider node from the Inspire Pack to utilize this node. + + +### Mask operation + * `Pixelwise(SEGS & SEGS)` - Performs a 'pixelwise and' operation between two SEGS. + * `Pixelwise(SEGS - SEGS)` - Subtracts one SEGS from another. + * `Pixelwise(SEGS & MASK)` - Performs a pixelwise AND operation between SEGS and MASK. + * `Pixelwise(SEGS & MASKS ForEach)` - Performs a pixelwise AND operation between SEGS and MASKS. + * Please note that this operation is performed with batches of MASKS, not just a single MASK. + * `Pixelwise(MASK & MASK)` - Performs a 'pixelwise and' operation between two masks. + * `Pixelwise(MASK - MASK)` - Subtracts one mask from another. + * `Pixelwise(MASK + MASK)` - Combine two masks. + * `SEGM Detector (SEGS)` - Detects segmentation and returns SEGS from the input image. + * `BBOX Detector (SEGS)` - Detects bounding boxes and returns SEGS from the input image. + * `Dilate Mask` - Dilate Mask. + * Support erosion for negative value. + * `Gaussian Blur Mask` - Apply Gaussian Blur to Mask. You can utilize this for mask feathering. + * `Mask Rect Area` - Create a rectangular mask defined by percentages with preview canvas. + * `Mask Rect Area (Advanced)` - Create a rectangular mask defined by pixels and image size. + + +### [Detailer nodes](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/detailers.md) + * `Detailer (SEGS)` - Refines the image based on SEGS. + * `Detailer (SEGS) with auto retry` - Refines the image based on SEGS and will automatically retry if the patch is all black. + * `DetailerDebug (SEGS)` - Refines the image based on SEGS. Additionally, it provides the ability to monitor the cropped image and the refined image of the cropped image. + * To prevent regeneration caused by the seed that does not change every time when using 'external_seed', please disable the 'seed random generate' option in the 'Detailer...' node. + * `MASK to SEGS` - Generates SEGS based on the mask. + * `MASK to SEGS For Video` - Generates SEGS based on the mask for Video. (Renamed from `MASK to SEGS For AnimateDiff`) + * When using a single mask, convert it to SEGS to apply it to the entire frame. + * When using a batch mask, the contour fill feature is disabled. + * `MediaPipe FaceMesh to SEGS` - Separate each landmark from the mediapipe facemesh image to create labeled SEGS. + * Usually, the size of images created through the MediaPipe facemesh preprocessor is downscaled. It resizes the MediaPipe facemesh image to the original size given as reference_image_opt for matching sizes during processing. + * `ToBinaryMask` - Separates the mask generated with alpha values between 0 and 255 into 0 and 255. The non-zero parts are always set to 255. + * `Masks to Mask List` - This node converts the MASKS in batch form to a list of individual masks. + * `Mask List to Masks` - This node converts the MASK list to MASK batch form. + * `EmptySEGS` - Provides an empty SEGS. + * `MaskPainter` - Provides a feature to draw masks. + * `FaceDetailer` - Easily detects faces and improves them. + * `FaceDetailer (pipe)` - Easily detects faces and improves them (for multipass). + * `MaskDetailer (pipe)` - This is a simple inpaint node that applies the Detailer to the mask area. + + * `FromDetailer (SDXL/pipe)`, `BasicPipe -> DetailerPipe (SDXL)`, `Edit DetailerPipe (SDXL)` - These are pipe functions used in Detailer for utilizing the refiner model of SDXL. + * `Any PIPE -> BasicPipe` - Convert the PIPE Value of other custom nodes that are not BASIC_PIPE but internally have the same structure as BASIC_PIPE to BASIC_PIPE. If an incompatible type is applied, it may cause runtime errors. + + +### SEGS Manipulation nodes + * `SEGSDetailer` - Performs detailed work on SEGS without pasting it back onto the original image. + * `SEGSPaste` - Pastes the results of SEGS onto the original image. + * If `ref_image_opt` is present, the images contained within SEGS are ignored. Instead, the image within `ref_image_opt` corresponding to the crop area of SEGS is taken and pasted. The size of the image in `ref_image_opt` should be the same as the original image size. + * This node can be used in conjunction with the processing results of AnimateDiff. + * `SEGSPreview` - Provides a preview of SEGS. + * This option is used to preview the improved image through `SEGSDetailer` before merging it into the original. Prior to going through ```SEGSDetailer```, SEGS only contains mask information without image information. If fallback_image_opt is connected to the original image, SEGS without image information will generate a preview using the original image. However, if SEGS already contains image information, fallback_image_opt will be ignored. + * This node can be used in conjunction with the processing results of AnimateDiff. + * `SEGSPreview (CNET Image)` - Show images configured with `ControlNetApply (SEGS)` for debugging purposes. + * `SEGSToImageList` - Convert SEGS To Image List + * `SEGSToMaskList` - Convert SEGS To Mask List + * `SEGS Filter (label)` - This node filters SEGS based on the label of the detected areas. + * `SEGS Filter (ordered)` - This node sorts SEGS based on size and position and retrieves SEGs within a certain range. + * `SEGS Filter (range)` - This node retrieves only SEGs from SEGS that have a size and position within a certain range. + * `SEGS Filter (non max suppression)` - This node filters SEGS by removing those with high overlap based on the Intersection over Union (IoU) threshold, keeping only the most confident detections. + * `SEGS Filter (intersection)` - This node filters segs1, keeping only the SEGS that do not significantly overlap with any SEGS in segs2, based on the Intersection over Area (IoA) threshold. + * `SEGS Assign (label)` - Assign labels sequentially to SEGS. This node is useful when used with `[LAB]` of FaceDetailer. + * `SEGSConcat` - Concatenate segs1 and segs2. If source shape of segs1 and segs2 are different from segs2 will be ignored. + * `SEGS Merge` - SEGS contains multiple SEGs. SEGS Merge integrates several SEGs into a single merged SEG. The label is changed to `merged` and the confidence becomes the minimum confidence. The applied controlnet and cropped_image are removed. + * `Picker (SEGS)` - Among the input SEGS, you can select a specific SEG through a dialog. If no SEG is selected, it outputs an empty SEGS. Increasing the batch_size of SEGSDetailer can be used for the purpose of selecting from the candidates. + * `Set Default Image For SEGS` - Set a default image for SEGS. SEGS with images set this way do not need to have a fallback image set. When override is set to false, the original image is preserved. + * `Remove Image from SEGS` - Remove the image set for the SEGS that has been configured by "Set Default Image for SEGS" or SEGSDetailer. When the image for the SEGS is removed, the Detailer node will operate based on the currently processed image instead of the SEGS. + * `Make Tile SEGS` - [experimental] Create SEGS in the form of tiles from an image to facilitate experiments for Tiled Upscale using the Detailer. + * The `filter_in_segs_opt` and `filter_out_segs_opt` are optional inputs. If these inputs are provided, when creating the tiles, the mask for each tile is generated by overlapping with the mask of `filter_in_segs_opt` and excluding the overlap with the mask of `filter_out_segs_opt`. Tiles with an empty mask will not be created as SEGS. + * `Dilate Mask (SEGS)` - Dilate/Erosion Mask in SEGS + * `Gaussian Blur Mask (SEGS)` - Apply Gaussian Blur to Mask in SEGS + * `SEGS_ELT Manipulation` - experimental nodes + * `DecomposeSEGS` - Decompose SEGS to allow for detailed manipulation. + * `AssembleSEGS` - Reassemble the decomposed SEGS. + * `From SEG_ELT` - Extract detailed information from SEG_ELT. + * `Edit SEG_ELT` - Modify some of the information in SEG_ELT. + * `Dilate SEG_ELT` - Dilate the mask of SEG_ELT. + * `From SEG_ELT` bbox - Extract coordinate from bbox in SEG_ELT + * `From SEG_ELT` crop_region - Extract coordinate from crop_region in SEG_ELT + * `Count Elt in SEGS` - Number of Elts ins SEGS + + +### Pipe nodes + * `ToDetailerPipe`, `FromDetailerPipe` - These nodes are used to bundle multiple inputs used in the detailer, such as models and vae, ..., into a single DETAILER_PIPE or extract the elements that are bundled in the DETAILER_PIPE. + * `ToBasicPipe`, `FromBasicPipe` - These nodes are used to bundle model, clip, vae, positive conditioning, and negative conditioning into a single BASIC_PIPE, or extract each element from the BASIC_PIPE. + * `EditBasicPipe`, `EditDetailerPipe` - These nodes are used to replace some elements in BASIC_PIPE or DETAILER_PIPE. + * `FromDetailerPipe_v2`, `FromBasicPipe_v2` - It has the same functionality as `FromDetailerPipe` and `FromBasicPipe`, but it has an additional output that directly exports the input pipe. It is useful when editing EditBasicPipe and EditDetailerPipe. +* `Latent Scale (on Pixel Space)` - This node converts latent to pixel space, upscales it, and then converts it back to latent. + * If upscale_model_opt is provided, it uses the model to upscale the pixel and then downscales it using the interpolation method provided in scale_method to the target resolution. +* `PixelKSampleUpscalerProvider` - An upscaler is provided that converts latent to pixels using VAEDecode, performs upscaling, converts back to latent using VAEEncode, and then performs k-sampling. This upscaler can be attached to nodes such as `Iterative Upscale` for use. + * Similar to `Latent Scale (on Pixel Space)`, if upscale_model_opt is provided, it performs pixel upscaling using the model. +* `PixelTiledKSampleUpscalerProvider` - It is similar to `PixelKSampleUpscalerProvider`, but it uses `ComfyUI_TiledKSampler` and Tiled VAE Decoder/Encoder to avoid GPU VRAM issues at high resolutions. + * You need to install the [BlenderNeko/ComfyUI_TiledKSampler](https://github.com/BlenderNeko/ComfyUI_TiledKSampler) node extension. + + +### PK_HOOK + * `DenoiseScheduleHookProvider` - IterativeUpscale provides a hook that gradually changes the denoise to target_denoise as the iterative-step progresses. + * `CfgScheduleHookProvider` - IterativeUpscale provides a hook that gradually changes the cfg to target_cfg as the iterative-step progresses. + * `StepsScheduleHookProvider` - IterativeUpscale provides a hook that gradually changes the sampling-steps to target_steps as the iterative-step progresses. + * `NoiseInjectionHookProvider` - During each iteration of IterativeUpscale, noise is injected into the latent space while varying the strength according to a schedule. + * You need to install the [BlenderNeko/ComfyUI_Noise](https://github.com/BlenderNeko/ComfyUI_Noise) node extension. + * The seed serves as the initial value required for generating noise, and it increments by 1 with each iteration as the process unfolds. + * The source determines the types of CPU noise and GPU noise to be configured. + * Currently, there is only a simple schedule available, where the strength of the noise varies from start_strength to end_strength during the progression of each iteration. + * `UnsamplerHookProvider` - Apply Unsampler during each iteration. To use this node, ComfyUI_Noise must be installed. + * `PixelKSampleHookCombine` - This is used to connect two PK_HOOKs. hook1 is executed first and then hook2 is executed. + * If you want to simultaneously change cfg and denoise, you can combine the PK_HOOKs of CfgScheduleHookProvider and PixelKSampleHookCombine. + + +### DETAILER_HOOK + * `NoiseInjectionDetailerHookProvider` - The `detailer_hook` is a hook in the `Detailer` that injects noise during the processing of each SEGS. + * `UnsamplerDetailerHookProvider` - Apply Unsampler during each cycle. To use this node, ComfyUI_Noise must be installed. + * `DenoiseSchedulerDetailerHookProvider` - During the progress of the cycle, the detailer's denoise is altered up to the `target_denoise`. + * `CoreMLDetailerHookProvider` - CoreML supports only 512x512, 512x768, 768x512, 768x768 size sampling. CoreMLDetailerHookProvider precisely fixes the upscale of the crop_region to this size. When using this hook, it will always be selected size, regardless of the guide_size. However, if the guide_size is too small, skipping will occur. + * `DetailerHookCombine` - This is used to connect two DETAILER_HOOKs. Similar to PixelKSampleHookCombine. + * `SEGSOrderedFilterDetailerHook`, SEGSRangeFilterDetailerHook, SEGSLabelFilterDetailerHook - There are a wrapper node that provides SEGSFilter nodes to be applied in FaceDetailer or Detector by creating DETAILER_HOOK. + * `PreviewDetailerHook` - Connecting this hook node helps provide assistance for viewing previews whenever SEGS Detailing tasks are completed. When working with a large number of SEGS, such as Make Tile SEGS, it allows for monitoring the situation as improvements progress incrementally. + * Since this is the hook applied when pasting onto the original image, it has no effect on nodes like `SEGSDetailer`. + * `VariationNoiseDetailerHookProvider` - Apply variation seed to the detailer. It can be applied in multiple stages through combine. + * `CustomSamplerDetailerHookProvider` - Apply a hook that allows you to use a custom sampler in the Detailer nodes. When using `DetailerHookCombine`, the sampler from the first hook is applied. + * `LamaRemoverDetailerHookProvider` – Applies Lama Remover to the upscaled image during the detailing stage. If `skip_sampling` is set to True, Lama Remover can be used alone without the detailing stage, allowing it to simply remove detected regions. + * Not applicable for **AnimateDiff** detailers. When using `DetailerHookCombine`, `skip_sampling` is only applied if it is set to `True` for all hooks. + * To use this node, the node pack at [Layer-norm/comfyui-lama-remover](https://github.com/Layer-norm/comfyui-lama-remover) must be installed. + + +### Iterative Upscale nodes + * `Iterative Upscale (Latent/on Pixel Space)` - The upscaler takes the input upscaler and splits the scale_factor into steps, then iteratively performs upscaling. + This takes latent as input and outputs latent as the result. + * `Iterative Upscale (Image)` - The upscaler takes the input upscaler and splits the scale_factor into steps, then iteratively performs upscaling. This takes image as input and outputs image as the result. + * Internally, this node uses 'Iterative Upscale (Latent)'. + + +### TwoSamplers nodes +* `TwoSamplersForMask` - This node can apply two samplers depending on the mask area. The base_sampler is applied to the area where the mask is 0, while the mask_sampler is applied to the area where the mask is 1. + * Note: The latent encoded through VAEEncodeForInpaint cannot be used. +* `KSamplerProvider` - This is a wrapper that enables KSampler to be used in TwoSamplersForMask TwoSamplersForMaskUpscalerProvider. +* `TiledKSamplerProvider` - ComfyUI_TiledKSampler is a wrapper that provides KSAMPLER. + * You need to install the [BlenderNeko/ComfyUI_TiledKSampler](https://github.com/BlenderNeko/ComfyUI_TiledKSampler) node extension. + +* `TwoAdvancedSamplersForMask` - TwoSamplersForMask is similar to TwoAdvancedSamplersForMask, but they differ in their operation. TwoSamplersForMask performs sampling in the mask area only after all the samples in the base area are finished. On the other hand, TwoAdvancedSamplersForMask performs sampling in both the base area and the mask area sequentially at each step. +* `KSamplerAdvancedProvider` - This is a wrapper that enables KSampler to be used in TwoAdvancedSamplersForMask, RegionalSampler. + * sigma_factor: By multiplying the denoise schedule by the sigma_factor, you can adjust the amount of denoising based on the configured denoise. + +* `TwoSamplersForMaskUpscalerProvider` - This is an Upscaler that extends TwoSamplersForMask to be used in Iterative Upscale. + * TwoSamplersForMaskUpscalerProviderPipe - pipe version of TwoSamplersForMaskUpscalerProvider. + + +### Image Utils + * `PreviewBridge (image)` - This custom node can be used with a bridge for image when using the MaskEditor feature of Clipspace. + * `PreviewBridge (latent)` - This custom node can be used with a bridge for latent image when using the MaskEditor feature of Clipspace. + * If a latent with a mask is provided as input, it displays the mask. Additionally, the mask output provides the mask set in the latent. + * If a latent without a mask is provided as input, it outputs the original latent as is, but the mask output provides an output with the entire region set as a mask. + * When set mask through MaskEditor, a mask is applied to the latent, and the output includes the stored mask. The same mask is also output as the mask output. + * When connected to `vae_opt`, it takes higher priority than the `preview_method`. + * `ImageSender`, `ImageReceiver` - The images generated in ImageSender are automatically sent to the ImageReceiver with the same link_id. + * `LatentSender`, `LatentReceiver` - The latent generated in LatentSender are automatically sent to the LatentReceiver with the same link_id. + * Furthermore, LatentSender is implemented with PreviewLatent, which stores the latent in payload form within the image thumbnail. + * Due to the current structure of ComfyUI, it is unable to distinguish between SDXL latent and SD1.5/SD2.1 latent. Therefore, it generates thumbnails by decoding them using the SD1.5 method. + + +### Switch nodes + * `Switch (image,mask)`, `Switch (latent)`, `Switch (SEGS)` - Among multiple inputs, it selects the input designated by the selector and outputs it. The first input must be provided, while the others are optional. However, if the input specified by the selector is not connected, an error may occur. + * `Switch (Any)` - This is a Switch node that takes an arbitrary number of inputs and produces a single output. Its type is determined when connected to any node, and connecting inputs increases the available slots for connections. + * `Inversed Switch (Any)` - In contrast to `Switch (Any)`, it takes a single input and outputs one of many. + * NOTE: See this [tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/switch.md) + + +### [Wildcards](http://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/ImpactWildcard.md) nodes + * These are nodes that supports syntax in the form of `__wildcard-name__` and dynamic prompt syntax like `{a|b|c}`. + * Wildcard files can be used by placing `.txt` or `.yaml` files under either `ComfyUI-Impact-Pack/wildcards` or `ComfyUI-Impact-Pack/custom_wildcards` paths. + * You can download and use [Wildcard YAML](https://civitai.com/models/138970/billions-of-wildcards-all-in-one) files in this format. + * After the first execution, you can change the custom wildcards path in the `custom_wildcards` entry within the `ComfyUI-Impact-Pack/impact-pack.ini` file created. + * `ImpactWildcardProcessor` - The text is generated by processing the wildcard in the Text. If the mode is set to "populate", a dynamic prompt is generated with each execution and the input is filled in the second textbox. If the mode is set to "fixed", the content of the second textbox remains unchanged. + * When an image is generated with the "fixed" mode, the prompt used for that particular generation is stored in the metadata. + * `ImpactWildcardEncode` - Similar to ImpactWildcardProcessor, this provides the loading functionality of LoRAs (e.g. ``). Populated prompts are encoded using the clip after all the lora loading is done. + * If the `Inspire Pack` is installed, you can use **Lora Block Weight** in the form of `LBW=lbw spec;` + * ``, ``, `` + + +### Regional Sampling + * These nodes offer the capability to divide regions and perform partial sampling using a mask. Unlike TwoSamplersForMask, sampling for each region is applied during each step. + * `RegionalPrompt` - This node combines a **mask** for specifying regions and the **sampler** to apply to each region to create `REGIONAL_PROMPTS`. + * `CombineRegionalPrompts` - Combine multiple `REGIONAL_PROMPTS` to create a single `REGIONAL_PROMPTS`. + * `RegionalSampler` - This node performs sampling using a base sampler and regional prompts. Sampling by the base sampler is executed at each step, while sampling for each region is performed through the sampler bound to each region. + * overlap_factor - Specifies the amount of overlap for each region to blend well with the area outside the mask. + * restore_latent - When sampling each region, restore the areas outside the mask to the base latent, preventing additional noise from being introduced outside the mask during region sampling. + * `RegionalSamplerAdvanced` - This is the Advanced version of the RegionalSampler. You can control it using `step` instead of `denoise`. + > NOTE: The `sde` sampler and `uni_pc` sampler introduce additional noise during each step of the sampling process. To mitigate this, when sampling each region, the `uni_pc` sampler applies additional `dpmpp_fast`, and the sde sampler applies the `dpmpp_2m` sampler as an additional measure. + + +### Impact KSampler + * These samplers support basic_pipe and AYS/OSS/GITS scheduler + * `KSampler (pipe)` - pipe version of KSampler + * `KSampler (advanced/pipe)` - pipe version of KSamplerAdvacned + * When converting the scheduler widget to input, refer to the `Impact Scheduler Adapter` node to resolve compatibility issues. + * `GITSScheduler Func Provider` - provider scheduler function for GITSScheduler + + +### Batch/List Util + * `Image Batch to Image List` - Convert Image batch to Image List + - You can use images generated in a multi batch to handle them + * `Image List to Image Batch` - Convert Image List to Image Batch + * `Make Image List` - Convert multiple images into a single image list + * `Make Image Batch` - Convert multiple images into a single image batch + - The input of images can be scaled up as needed + * `Masks to Mask List`, `Mask List to Masks`, `Make Mask List`, `Make Mask Batch` - It has the same functionality as the nodes above, but uses mask as input instead of image. + * `Flatten Mask Batch` - Flattens a Mask Batch into a single Mask. Normal operation is not guaranteed for non-binary masks. + * `Make List (Any)` - Create a list with arbitrary values. + * `Select Nth Item (Any list)` - Selects the Nth item from a list. If the index is out of range, it returns the last item in the list. + + +### Logics (experimental) + * These nodes are experimental nodes designed to implement the logic for loops and dynamic switching. + * `ImpactCompare`, `ImpactConditionalBranch`, `ImpactConditionalBranchSelMode`, `ImpactInt`, `ImpactBoolean`, `ImpactValueSender`, `ImpactValueReceiver`, `ImpactImageInfo`, `ImpactMinMax`, `ImpactNeg`, `ImpactConditionalStopIteration` + * `ImpactIsNotEmptySEGS` - This node returns `true` only if the input SEGS is not empty. + * `ImpactIfNone` - Returns `true` if any_input is None, and returns `false` if it is not None. + * `Queue Trigger` - When this node is executed, it adds a new queue to assist with repetitive tasks. It will only execute if the signal's status changes. + * `Queue Trigger (Countdown)` - Like the Queue Trigger, it adds a queue, but only adds it if it's greater than 1, and decrements the count by one each time it runs. + * `Sleep` - Waits for the specified time (in seconds). + * `Set Widget Value` - This node sets one of the optional inputs to the specified node's widget. An error may occur if the types do not match. + * `Set Mute State` - This node changes the mute state of a specific node. + * `Control Bridge` - This node modifies the state of the connected control nodes based on the `mode` and `behavior` . If there are nodes that require a change, the current execution is paused, the mute status is updated, and a new prompt queue is inserted. + * When the `mode` is `active`, it makes the connected control nodes active regardless of the behavior. + * When the `mode` is `Bypass/Mute`, it changes the state of the connected nodes based on whether the behavior is `Bypass` or `Mute`. + * **Limitation**: Due to these characteristics, it does not function correctly when the batch count exceeds 1. Additionally, it does not guarantee proper operation when the seed is randomized or when the state of nodes is altered by actions such as `Queue Trigger`, `Set Widget Value`, `Set Mute`, before the Control Bridge. + * When utilizing this node, please structure the workflow in such a way that `Queue Trigger`, `Set Widget Value`, `Set Mute State`, and similar actions are executed at the end of the workflow. + * If you want to change the value of the seed at each iteration, please ensure that Set Widget Value is executed at the end of the workflow instead of using randomization. + * It is not a problem if the seed changes due to randomization as long as it occurs after the Control Bridge section. + * `Remote Boolean (on prompt)`, `Remote Int (on prompt)` - At the start of the prompt, this node forcibly sets the `widget_value` of `node_id`. It is disregarded if the target widget type is different. + * You can find the `node_id` by checking through [ComfyUI-Manager](https://github.com/ltdrdata/ComfyUI-Manager) using the format `Badge: #ID Nickname`. + * Experimental set of nodes for implementing loop functionality (tutorial to be prepared later / [example workflow](test/loop-test.json)). + + +### HuggingFace nodes + * These nodes provide functionalities based on HuggingFace repository models. + * The path where the HuggingFace model cache is stored can be changed through the `HF_HOME` environment variable. + * `HF Transformers Classifier Provider` - This is a node that provides a classifier based on HuggingFace's transformers models. + * The 'repo id' parameter should contain HuggingFace's repo id. When `preset_repo_id` is set to `Manual repo id`, use the manually entered repo id in `manual_repo_id`. + * e.g. 'rizvandwiki/gender-classification-2' is a repository that provides a model for gender classification. + * `SEGS Classify` - This node utilizes the `TRANSFORMERS_CLASSIFIER` loaded with 'HF Transformers Classifier Provider' to classify `SEGS`. + * The 'expr' allows for forms like `label > number`, and in the case of `preset_expr` being `Manual expr`, it uses the expression entered in `manual_expr`. + * For example, in the case of `male <= 0.4`, if the score of the `male` label in the classification result is less than or equal to 0.4, it is categorized as `filtered_SEGS`, otherwise, it is categorized as `remained_SEGS`. + * For supported labels, please refer to the `config.json` of the respective HuggingFace repository. + * `#Female` and `#Male` are symbols that group multiple labels such as `Female, women, woman, ...`, for convenience, rather than being single labels. + + +### Etc nodes + * `Impact Scheduler Adapter` - With the addition of AYS to the scheduler of the Impact Pack and Inspire Pack, there is an issue of incompatibility when the existing scheduler widget is converted to input. The Impact Scheduler Adapter allows for an indirect connection to be possible. + * `StringListToString` - Convert String List to String + * `WildcardPromptFromString` - Create labeled wildcard for detailer from string. + * This node works well when used with MakeTileSEGS. [[Link](https://github.com/ltdrdata/ComfyUI-Impact-Pack/pull/536#discussion_r1586060779)] + + * `String Selector` - It selects and returns a portion of the string. When `multiline` mode is disabled, it simply returns the string of the line pointed to by the selector. When `multiline` mode is enabled, it divides the string based on lines that start with `#` and returns them. If the `select` value is larger than the number of items, it will start counting from the first line again and return accordingly. + * `Combine Conditionings` - It takes multiple conditionings as input and combines them into a single conditioning. + * `Concat Conditionings` - It takes multiple conditionings as input and concat them into a single conditioning. + * `Negative Cond Placeholder` - Models like FLUX.1 do not use Negative Conditioning. This is a placeholder node for them. You can use FLUX.1 by replacing the Negative Conditioning used in Impact KSampler, KSampler (Inspire), and Detailer with this node. + * `Execution Order Controller` - A helper node that can forcibly control the execution order of nodes. + * Connect the output of the node that should be executed first to the signal, and make the input of the node that should be executed later pass through this node. + * `List Bridge` - When passing the list output through this node, it collects and organizes the data before forwarding it, which ensures that the previous stage's sub-workflow has been completed. + + +## Feature +* `Interactive SAM Detector (Clipspace)` - When you right-click on a node that has 'MASK' and 'IMAGE' outputs, a context menu will open. From this menu, you can either open a dialog to create a SAM Mask using 'Open in SAM Detector', or copy the content (likely mask data) using 'Copy (Clipspace)' and generate a mask using 'Impact SAM Detector' from the clipspace menu, and then paste it using 'Paste (Clipspace)'. +* Providing a feature to detect errors that occur when mixing models and clips from checkpoints such as `SDXL Base`, `SDXL Refiner`, `SD1.x`, `SD2.x` during sample execution, and reporting appropriate errors. + + +## How To Install? + +### Install via ComfyUI-Manager (Recommended) +* Search `ComfyUI Impact Pack` in ComfyUI-Manager and click `Install` button. + +### Manual Install (Not Recommended) +1. `cd custom_nodes` +2. `git clone https://github.com/ltdrdata/ComfyUI-Impact-Pack` +3. `cd ComfyUI-Impact-Pack` +4. `pip install -r requirements.txt` + * **IMPORTANT**: + * You must install it within the Python environment where ComfyUI is running. + * For the portable version, use `\python_embeded\python.exe -m pip` instead of `pip`. For a `venv`, activate the `venv` first and then use `pip`. +5. Restart ComfyUI + +* NOTE1: If an error occurs during the installation process, please refer to [Troubleshooting Page](troubleshooting/TROUBLESHOOTING.md) for assistance. +* NOTE2: You can use this colab notebook [colab notebook](https://colab.research.google.com/github/ltdrdata/ComfyUI-Impact-Pack/blob/Main/notebook/comfyui_colab_impact_pack.ipynb) to launch it. This notebook automatically downloads the impact pack to the custom_nodes directory, installs the tested dependencies, and runs it. +* NOTE3: If you create an empty file named `skip_download_model` in the `ComfyUI/custom_nodes/` directory, it will skip the model download step during the installation of the impact pack. + + +## Package Dependencies (If you need to manual setup.) + +* pip install + * segment-anything + * scikit-image + * piexif + * opencv-python + * scipy + * numpy<2 + * dill + * matplotlib + * (optional) onnxruntime + * (deprecated) openmim # for mim + * (deprecated) pycocotools # for mim + +* linux packages (ubuntu) + * libgl1-mesa-glx + * libglib2.0-0 + + +## Config example +* Once you run the Impact Pack for the first time, an `impact-pack.ini` file will be automatically generated in the Impact Pack directory. You can modify this configuration file to customize the default behavior. + * `dependency_version` - don't touch this + * `sam_editor_cpu` - use cpu for `SAM editor` instead of gpu + * sam_editor_model: Specify the SAM model for the SAM editor. + * You can download various SAM models using ComfyUI-Manager. + * Path to SAM model: `ComfyUI/models/sams` +``` +[default] +sam_editor_cpu = False +sam_editor_model = sam_vit_b_01ec64.pth +``` + + +## Other Materials (auto-download when installing) + +* ComfyUI/models/sams <= https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth + + +## Troubleshooting page +* [Troubleshooting Page](troubleshooting/TROUBLESHOOTING.md) + + +## How To Use (DDetailer feature) + +#### 1. Basic auto face detection and refine exapmle. +![simple](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/simple.png) +* The face that has been damaged due to low resolution is restored with high resolution by generating and synthesizing it, in order to restore the details. +* The FaceDetailer node is a combination of a Detector node for face detection and a Detailer node for image enhancement. See the [Advanced Tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/tutorial/advanced.md) for a more detailed explanation. +* The MASK output of FaceDetailer provides a visualization of where the detected and enhanced areas are. + +![simple-orig](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/simple-original.png) ![simple-refined](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/simple-refined.png) +* You can see that the face in the image on the left has increased detail as in the image on the right. + +#### 2. 2Pass refine (restore a severely damaged face) +![2pass-workflow-example](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/2pass-simple.png) +* Although two FaceDetailers can be attached together for a 2-pass configuration, various common inputs used in KSampler can be passed through DETAILER_PIPE, so FaceDetailerPipe can be used to configure easily. +* In 1pass, only rough outline recovery is required, so restore with a reasonable resolution and low options. However, if you increase the dilation at this time, not only the face but also the surrounding parts are included in the recovery range, so it is useful when you need to reshape the face other than the facial part. + +![2pass-example-original](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/2pass-original.png) ![2pass-example-middle](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/2pass-1pass.png) ![2pass-example-result](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/2pass-2pass.png) +* In the first stage, the severely damaged face is restored to some extent, and in the second stage, the details are restored + +#### 3. Face Bbox(bounding box) + Person silhouette segmentation (prevent distortion of the background.) +![combination-workflow-example](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/combination.jpg) +![combination-example-original](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/combination-original.png) ![combination-example-refined](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/combination-refined.png) + +* Facial synthesis that emphasizes details is delicately aligned with the contours of the face, and it can be observed that it does not affect the image outside of the face. + +* The BBoxDetectorForEach node is used to detect faces, and the SAMDetectorCombined node is used to find the segment related to the detected face. By using the Segs & Mask node with the two masks obtained in this way, an accurate mask that intersects based on segs can be generated. If this generated mask is input to the DetailerForEach node, only the target area can be created in high resolution from the image and then composited. + +#### 4. Iterative Upscale +![upscale-workflow-example](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/upscale-workflow.png) + +* The IterativeUpscale node is a node that enlarges an image/latent by a scale_factor. In this process, the upscale is carried out progressively by dividing it into steps. +* IterativeUpscale takes an Upscaler as an input, similar to a plugin, and uses it during each iteration. PixelKSampleUpscalerProvider is an Upscaler that converts the latent representation to pixel space and applies ksampling. + * The upscale_model_opt is an optional parameter that determines whether to use the upscale function of the model base if available. Using the upscale function of the model base can significantly reduce the number of iterative steps required. If an x2 upscaler is used, the image/latent is first upscaled by a factor of 2 and then downscaled to the target scale at each step before further processing is done. + +* The following image is an image of 304x512 pixels and the same image scaled up to three times its original size using IterativeUpscale. + +![combination-example-original](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/upscale-original.png) ![combination-example-refined](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/upscale-3x.png) + + +#### 5. Interactive SAM Detector (Clipspace) + +* When you right-click on the node that outputs 'MASK' and 'IMAGE', a menu called "Open in SAM Detector" appears, as shown in the following picture. Clicking on the menu opens a dialog in SAM's functionality, allowing you to generate a segment mask. +![samdetector-menu](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/SAMDetector-menu.png) + +* By clicking the left mouse button on a coordinate, a positive prompt in blue color is entered, indicating the area that should be included. Clicking the right mouse button on a coordinate enters a negative prompt in red color, indicating the area that should be excluded. Positive prompts represent the areas that should be included, while negative prompts represent the areas that should be excluded. +* You can remove the points that were added by using the "undo" button. After selecting the points, pressing the "detect" button generates the mask. Additionally, you can adjust the fidelity slider to determine the extent to which the mask belongs to the confidence region. + +![samdetector-dialog](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/SAMDetector-dialog.jpg) + +* If you opened the dialog through "Open in SAM Detector" from the node, you can directly apply the changes by clicking the "Save to node" button. However, if you opened the dialog through the "clipspace" menu, you can save it to clipspace by clicking the "Save" button. + +![samdetector-result](https://github.com/ltdrdata/ComfyUI-extension-tutorials/raw/Main/ComfyUI-Impact-Pack/images/SAMDetector-result.jpg) + +* When you execute using the reflected mask in the node, you can observe that the image and mask are displayed separately. + + +## Others Tutorials +* [ComfyUI-extension-tutorials/ComfyUI-Impact-Pack](https://github.com/ltdrdata/ComfyUI-extension-tutorials/tree/Main/ComfyUI-Impact-Pack) - You can find various tutorials and workflows on this page. +* [Advanced Tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/advanced.md) +* [SAM Application](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/sam.md) +* [PreviewBridge](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/previewbridge.md) +* [Mask Pointer](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/maskpointer.md) +* [ONNX Tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/ONNX.md) +* [CLIPSeg Tutorial](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/clipseg.md) +* [Extreme Highresolution Upscale](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/extreme-upscale.md) +* [TwoSamplersForMask](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/TwoSamplers.md) +* [TwoAdvancedSamplersForMask](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/TwoAdvancedSamplers.md) +* [Advanced Iterative Upscale: PK_HOOK](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/pk_hook.md) +* [Advanced Iterative Upscale: TwoSamplersForMask Upscale Provider](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/TwoSamplersUpscale.md) +* [Interactive SAM + PreviewBridge](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/sam_with_preview_bridge.md) +* [ImageSender/ImageReceiver/LatentSender/LatentReceiver](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/sender_receiver.md) +* [ImpactWildcardProcessor](https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/ImpactWildcardProcessor.md) + + +## Credits + +ComfyUI/[ComfyUI](https://github.com/comfyanonymous/ComfyUI) - A powerful and modular stable diffusion GUI. + +dustysys/[ddetailer](https://github.com/dustysys/ddetailer) - DDetailer for Stable-diffusion-webUI extension. + +Bing-su/[dddetailer](https://github.com/Bing-su/dddetailer) - The anime-face-detector used in ddetailer has been updated to be compatible with mmdet 3.0.0, and we have also applied a patch to the pycocotools dependency for Windows environment in ddetailer. + +facebook/[segment-anything](https://github.com/facebookresearch/segment-anything) - Segmentation Anything! + +hysts/[anime-face-detector](https://github.com/hysts/anime-face-detector) - Creator of `anime-face_yolov3`, which has impressive performance on a variety of art styles. + +open-mmlab/[mmdetection](https://github.com/open-mmlab/mmdetection) - Object detection toolset. `dd-person_mask2former` was trained via transfer learning using their [R-50 Mask2Former instance segmentation model](https://github.com/open-mmlab/mmdetection/tree/master/configs/mask2former#instance-segmentation) as a base. + +biegert/[ComfyUI-CLIPSeg](https://github.com/biegert/ComfyUI-CLIPSeg) - This is a custom node that enables the use of CLIPSeg technology, which can find segments through prompts, in ComfyUI. + +BlenderNeok/[ComfyUI-TiledKSampler](https://github.com/BlenderNeko/ComfyUI_TiledKSampler) - The tile sampler allows high-resolution sampling even in places with low GPU VRAM. + +BlenderNeok/[ComfyUI_Noise](https://github.com/BlenderNeko/ComfyUI_Noise) - The noise injection feature relies on this function and slerp code for noise variation + +WASasquatch/[was-node-suite-comfyui](https://github.com/WASasquatch/was-node-suite-comfyui) - A powerful custom node extensions of ComfyUI. + +Trung0246/[ComfyUI-0246](https://github.com/Trung0246/ComfyUI-0246) - Nice bypass hack! + +Layer-norm/[comfyui-lama-remover](https://github.com/Layer-norm/comfyui-lama-remover) - Required for using `LamaRemoverDetailerHook`. diff --git a/custom_nodes/comfyui-impact-pack/__init__.py b/custom_nodes/comfyui-impact-pack/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..ee98acabb015300edfe657a7221065c8ed84c5e6 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/__init__.py @@ -0,0 +1,456 @@ +""" +@author: Dr.Lt.Data +@title: Impact Pack +@nickname: Impact Pack +@description: This extension offers various detector nodes and detailer nodes that allow you to configure a workflow that automatically enhances facial details. And provide iterative upscaler. +""" + +import folder_paths +import os +import sys +import logging + +comfy_path = os.path.dirname(folder_paths.__file__) +impact_path = os.path.join(os.path.dirname(__file__)) +modules_path = os.path.join(os.path.dirname(__file__), "modules") + +sys.path.append(modules_path) + +import impact.config +logging.info(f"### Loading: ComfyUI-Impact-Pack ({impact.config.version})") + +# Core +# recheck dependencies for colab +try: + import folder_paths + import torch # noqa: F401 + import cv2 # noqa: F401 + from cv2 import setNumThreads # noqa: F401 + import numpy as np # noqa: F401 + import comfy.samplers + import comfy.sd # noqa: F401 + from PIL import Image, ImageFilter # noqa: F401 + from skimage.measure import label, regionprops # noqa: F401 + from collections import namedtuple # noqa: F401 + import piexif # noqa: F401 + import nodes +except Exception as e: + import logging + logging.error("[Impact Pack] Failed to import due to several dependencies are missing!!!!") + raise e + + +import impact.impact_server # to load server api + +from .modules.impact.impact_pack import * # noqa: F403 +from .modules.impact.detectors import * # noqa: F403 +from .modules.impact.pipe import * # noqa: F403 +from .modules.impact.logics import * # noqa: F403 +from .modules.impact.util_nodes import * # noqa: F403 +from .modules.impact.segs_nodes import * # noqa: F403 +from .modules.impact.special_samplers import * # noqa: F403 +from .modules.impact.hf_nodes import * # noqa: F403 +from .modules.impact.bridge_nodes import * # noqa: F403 +from .modules.impact.hook_nodes import * # noqa: F403 +from .modules.impact.animatediff_nodes import * # noqa: F403 +from .modules.impact.segs_upscaler import * # noqa: F403 + +import threading + + +threading.Thread(target=impact.wildcards.wildcard_load).start() + + +NODE_CLASS_MAPPINGS = { + "SAMLoader": SAMLoader, # noqa: F405 + "CLIPSegDetectorProvider": CLIPSegDetectorProvider, # noqa: F405 + "ONNXDetectorProvider": ONNXDetectorProvider, # noqa: F405 + + "BitwiseAndMaskForEach": BitwiseAndMaskForEach, # noqa: F405 + "SubtractMaskForEach": SubtractMaskForEach, # noqa: F405 + + "DetailerForEach": DetailerForEach, # noqa: F405 + "DetailerForEachAutoRetry": DetailerForEachAutoRetry, # noqa: F405 + "DetailerForEachDebug": DetailerForEachTest, # noqa: F405 + "DetailerForEachPipe": DetailerForEachPipe, # noqa: F405 + "DetailerForEachDebugPipe": DetailerForEachTestPipe, # noqa: F405 + "DetailerForEachPipeForAnimateDiff": DetailerForEachPipeForAnimateDiff, # noqa: F405 + + "SAMDetectorCombined": SAMDetectorCombined, # noqa: F405 + "SAMDetectorSegmented": SAMDetectorSegmented, # noqa: F405 + + "FaceDetailer": FaceDetailer, # noqa: F405 + "FaceDetailerPipe": FaceDetailerPipe, # noqa: F405 + "MaskDetailerPipe": MaskDetailerPipe, # noqa: F405 + + "ToDetailerPipe": ToDetailerPipe, # noqa: F405 + "ToDetailerPipeSDXL": ToDetailerPipeSDXL, # noqa: F405 + "FromDetailerPipe": FromDetailerPipe, # noqa: F405 + "FromDetailerPipe_v2": FromDetailerPipe_v2, # noqa: F405 + "FromDetailerPipeSDXL": FromDetailerPipe_SDXL, # noqa: F405 + "AnyPipeToBasic": AnyPipeToBasic, # noqa: F405 + "ToBasicPipe": ToBasicPipe, # noqa: F405 + "FromBasicPipe": FromBasicPipe, # noqa: F405 + "FromBasicPipe_v2": FromBasicPipe_v2, # noqa: F405 + "BasicPipeToDetailerPipe": BasicPipeToDetailerPipe, # noqa: F405 + "BasicPipeToDetailerPipeSDXL": BasicPipeToDetailerPipeSDXL, # noqa: F405 + "DetailerPipeToBasicPipe": DetailerPipeToBasicPipe, # noqa: F405 + "EditBasicPipe": EditBasicPipe, # noqa: F405 + "EditDetailerPipe": EditDetailerPipe, # noqa: F405 + "EditDetailerPipeSDXL": EditDetailerPipeSDXL, # noqa: F405 + + "LatentPixelScale": LatentPixelScale, # noqa: F405 + "PixelKSampleUpscalerProvider": PixelKSampleUpscalerProvider, # noqa: F405 + "PixelKSampleUpscalerProviderPipe": PixelKSampleUpscalerProviderPipe, # noqa: F405 + "IterativeLatentUpscale": IterativeLatentUpscale, # noqa: F405 + "IterativeImageUpscale": IterativeImageUpscale, # noqa: F405 + "PixelTiledKSampleUpscalerProvider": PixelTiledKSampleUpscalerProvider, # noqa: F405 + "PixelTiledKSampleUpscalerProviderPipe": PixelTiledKSampleUpscalerProviderPipe, # noqa: F405 + "TwoSamplersForMaskUpscalerProvider": TwoSamplersForMaskUpscalerProvider, # noqa: F405 + "TwoSamplersForMaskUpscalerProviderPipe": TwoSamplersForMaskUpscalerProviderPipe, # noqa: F405 + + "PixelKSampleHookCombine": PixelKSampleHookCombine, # noqa: F405 + "DenoiseScheduleHookProvider": DenoiseScheduleHookProvider, # noqa: F405 + "StepsScheduleHookProvider": StepsScheduleHookProvider, # noqa: F405 + "CfgScheduleHookProvider": CfgScheduleHookProvider, # noqa: F405 + "NoiseInjectionHookProvider": NoiseInjectionHookProvider, # noqa: F405 + "UnsamplerHookProvider": UnsamplerHookProvider, # noqa: F405 + "CoreMLDetailerHookProvider": CoreMLDetailerHookProvider, # noqa: F405 + "PreviewDetailerHookProvider": PreviewDetailerHookProvider, # noqa: F405 + "BlackPatchRetryHookProvider": BlackPatchRetryHookProvider, # noqa: F405 + "CustomSamplerDetailerHookProvider": CustomSamplerDetailerHookProvider, # noqa: F405 + "LamaRemoverDetailerHookProvider": LamaRemoverDetailerHookProvider, # noqa: F405 + + "DetailerHookCombine": DetailerHookCombine, # noqa: F405 + "NoiseInjectionDetailerHookProvider": NoiseInjectionDetailerHookProvider, # noqa: F405 + "UnsamplerDetailerHookProvider": UnsamplerDetailerHookProvider, # noqa: F405 + "DenoiseSchedulerDetailerHookProvider": DenoiseSchedulerDetailerHookProvider, # noqa: F405 + "SEGSOrderedFilterDetailerHookProvider": SEGSOrderedFilterDetailerHookProvider, # noqa: F405 + "SEGSRangeFilterDetailerHookProvider": SEGSRangeFilterDetailerHookProvider, # noqa: F405 + "SEGSLabelFilterDetailerHookProvider": SEGSLabelFilterDetailerHookProvider, # noqa: F405 + "VariationNoiseDetailerHookProvider": VariationNoiseDetailerHookProvider, # noqa: F405 + # "CustomNoiseDetailerHookProvider": CustomNoiseDetailerHookProvider, + + "BitwiseAndMask": BitwiseAndMask, # noqa: F405 + "SubtractMask": SubtractMask, # noqa: F405 + "AddMask": AddMask, # noqa: F405 + "MaskRectArea": MaskRectArea, # noqa: F405 + "MaskRectAreaAdvanced": MaskRectAreaAdvanced, # noqa: F405 + "ImpactSegsAndMask": SegsBitwiseAndMask, # noqa: F405 + "ImpactSegsAndMaskForEach": SegsBitwiseAndMaskForEach, # noqa: F405 + "EmptySegs": EmptySEGS, # noqa: F405 + "ImpactFlattenMask": FlattenMask, # noqa: F405 + + "MediaPipeFaceMeshToSEGS": MediaPipeFaceMeshToSEGS, # noqa: F405 + "MaskToSEGS": MaskToSEGS, # noqa: F405 + "MaskToSEGS_for_AnimateDiff": MaskToSEGS_for_AnimateDiff, # noqa: F405 + "ToBinaryMask": ToBinaryMask, # noqa: F405 + "MasksToMaskList": MasksToMaskList, # noqa: F405 + "MaskListToMaskBatch": MaskListToMaskBatch, # noqa: F405 + "ImageListToImageBatch": ImageListToImageBatch, # noqa: F405 + "SetDefaultImageForSEGS": DefaultImageForSEGS, # noqa: F405 + "RemoveImageFromSEGS": RemoveImageFromSEGS, # noqa: F405 + + "BboxDetectorSEGS": BboxDetectorForEach, # noqa: F405 + "SegmDetectorSEGS": SegmDetectorForEach, # noqa: F405 + "ONNXDetectorSEGS": BboxDetectorForEach, # noqa: F405 + "ImpactSimpleDetectorSEGS_for_AD": SimpleDetectorForAnimateDiff, # noqa: F405 + "ImpactSAM2VideoDetectorSEGS": SAM2VideoDetectorSEGS, # noqa: F405 + "ImpactSimpleDetectorSEGS": SimpleDetectorForEach, # noqa: F405 + "ImpactSimpleDetectorSEGSPipe": SimpleDetectorForEachPipe, # noqa: F405 + "ImpactControlNetApplySEGS": ControlNetApplySEGS, # noqa: F405 + "ImpactControlNetApplyAdvancedSEGS": ControlNetApplyAdvancedSEGS, # noqa: F405 + "ImpactControlNetClearSEGS": ControlNetClearSEGS, # noqa: F405 + "ImpactIPAdapterApplySEGS": IPAdapterApplySEGS, # noqa: F405 + + "ImpactDecomposeSEGS": DecomposeSEGS, # noqa: F405 + "ImpactAssembleSEGS": AssembleSEGS, # noqa: F405 + "ImpactFrom_SEG_ELT": From_SEG_ELT, # noqa: F405 + "ImpactEdit_SEG_ELT": Edit_SEG_ELT, # noqa: F405 + "ImpactDilate_Mask_SEG_ELT": Dilate_SEG_ELT, # noqa: F405 + "ImpactDilateMask": DilateMask, # noqa: F405 + "ImpactGaussianBlurMask": GaussianBlurMask, # noqa: F405 + "ImpactDilateMaskInSEGS": DilateMaskInSEGS, # noqa: F405 + "ImpactGaussianBlurMaskInSEGS": GaussianBlurMaskInSEGS, # noqa: F405 + "ImpactScaleBy_BBOX_SEG_ELT": SEG_ELT_BBOX_ScaleBy, # noqa: F405 + "ImpactFrom_SEG_ELT_bbox": From_SEG_ELT_bbox, # noqa: F405 + "ImpactFrom_SEG_ELT_crop_region": From_SEG_ELT_crop_region, # noqa: F405 + "ImpactCount_Elts_in_SEGS": Count_Elts_in_SEGS, # noqa: F405 + + "BboxDetectorCombined_v2": BboxDetectorCombined, # noqa: F405 + "SegmDetectorCombined_v2": SegmDetectorCombined, # noqa: F405 + "SegsToCombinedMask": SegsToCombinedMask, # noqa: F405 + + "KSamplerProvider": KSamplerProvider, # noqa: F405 + "TwoSamplersForMask": TwoSamplersForMask, # noqa: F405 + "TiledKSamplerProvider": TiledKSamplerProvider, # noqa: F405 + + "KSamplerAdvancedProvider": KSamplerAdvancedProvider, # noqa: F405 + "TwoAdvancedSamplersForMask": TwoAdvancedSamplersForMask, # noqa: F405 + + "ImpactNegativeConditioningPlaceholder": NegativeConditioningPlaceholder, # noqa: F405 + + "PreviewBridge": PreviewBridge, # noqa: F405 + "PreviewBridgeLatent": PreviewBridgeLatent, # noqa: F405 + "ImageSender": ImageSender, # noqa: F405 + "ImageReceiver": ImageReceiver, # noqa: F405 + "LatentSender": LatentSender, # noqa: F405 + "LatentReceiver": LatentReceiver, # noqa: F405 + "ImageMaskSwitch": ImageMaskSwitch, # noqa: F405 + "LatentSwitch": GeneralSwitch, # noqa: F405 + "SEGSSwitch": GeneralSwitch, # noqa: F405 + "ImpactSwitch": GeneralSwitch, # noqa: F405 + "ImpactInversedSwitch": GeneralInversedSwitch, # noqa: F405 + + "ImpactWildcardProcessor": ImpactWildcardProcessor, # noqa: F405 + "ImpactWildcardEncode": ImpactWildcardEncode, # noqa: F405 + + "SEGSUpscaler": SEGSUpscaler, # noqa: F405 + "SEGSUpscalerPipe": SEGSUpscalerPipe, # noqa: F405 + "SEGSDetailer": SEGSDetailer, # noqa: F405 + "SEGSPaste": SEGSPaste, # noqa: F405 + "SEGSPreview": SEGSPreview, # noqa: F405 + "SEGSPreviewCNet": SEGSPreviewCNet, # noqa: F405 + "SEGSToImageList": SEGSToImageList, # noqa: F405 + "ImpactSEGSToMaskList": SEGSToMaskList, # noqa: F405 + "ImpactSEGSToMaskBatch": SEGSToMaskBatch, # noqa: F405 + "ImpactSEGSConcat": SEGSConcat, # noqa: F405 + "ImpactSEGSPicker": SEGSPicker, # noqa: F405 + "ImpactMakeTileSEGS": MakeTileSEGS, # noqa: F405 + "ImpactSEGSMerge": SEGSMerge, # noqa: F405 + + "SEGSDetailerForAnimateDiff": SEGSDetailerForAnimateDiff, # noqa: F405 + + "ImpactKSamplerBasicPipe": KSamplerBasicPipe, # noqa: F405 + "ImpactKSamplerAdvancedBasicPipe": KSamplerAdvancedBasicPipe, # noqa: F405 + + "ReencodeLatent": ReencodeLatent, # noqa: F405 + "ReencodeLatentPipe": ReencodeLatentPipe, # noqa: F405 + + "ImpactImageBatchToImageList": ImageBatchToImageList, # noqa: F405 + "ImpactMakeImageList": MakeImageList, # noqa: F405 + "ImpactMakeImageBatch": MakeImageBatch, # noqa: F405 + "ImpactMakeAnyList": MakeAnyList, # noqa: F405 + "ImpactMakeMaskList": MakeMaskList, # noqa: F405 + "ImpactMakeMaskBatch": MakeMaskBatch, # noqa: F405 + "ImpactSelectNthItemOfAnyList": NthItemOfAnyList, # noqa: F405 + + "RegionalSampler": RegionalSampler, # noqa: F405 + "RegionalSamplerAdvanced": RegionalSamplerAdvanced, # noqa: F405 + "CombineRegionalPrompts": CombineRegionalPrompts, # noqa: F405 + "RegionalPrompt": RegionalPrompt, # noqa: F405 + + "ImpactCombineConditionings": CombineConditionings, # noqa: F405 + "ImpactConcatConditionings": ConcatConditionings, # noqa: F405 + + "ImpactSEGSLabelAssign": SEGSLabelAssign, # noqa: F405 + "ImpactSEGSLabelFilter": SEGSLabelFilter, # noqa: F405 + "ImpactSEGSRangeFilter": SEGSRangeFilter, # noqa: F405 + "ImpactSEGSOrderedFilter": SEGSOrderedFilter, # noqa: F405 + "ImpactSEGSIntersectionFilter": SEGSIntersectionFilter, # noqa: F405 + "ImpactSEGSNMSFilter": SEGSNMSFilter, # noqa: F405 + + "ImpactCompare": ImpactCompare, # noqa: F405 + "ImpactConditionalBranch": ImpactConditionalBranch, # noqa: F405 + "ImpactConditionalBranchSelMode": ImpactConditionalBranchSelMode, # noqa: F405 + "ImpactIfNone": ImpactIfNone, # noqa: F405 + "ImpactConvertDataType": ImpactConvertDataType, # noqa: F405 + "ImpactLogicalOperators": ImpactLogicalOperators, # noqa: F405 + "ImpactInt": ImpactInt, # noqa: F405 + "ImpactFloat": ImpactFloat, # noqa: F405 + "ImpactBoolean": ImpactBoolean, # noqa: F405 + "ImpactValueSender": ImpactValueSender, # noqa: F405 + "ImpactValueReceiver": ImpactValueReceiver, # noqa: F405 + "ImpactImageInfo": ImpactImageInfo, # noqa: F405 + "ImpactLatentInfo": ImpactLatentInfo, # noqa: F405 + "ImpactMinMax": ImpactMinMax, # noqa: F405 + "ImpactNeg": ImpactNeg, # noqa: F405 + "ImpactConditionalStopIteration": ImpactConditionalStopIteration, # noqa: F405 + "ImpactStringSelector": StringSelector, # noqa: F405 + "StringListToString": StringListToString, # noqa: F405 + "WildcardPromptFromString": WildcardPromptFromString, # noqa: F405 + "ImpactExecutionOrderController": ImpactExecutionOrderController, # noqa: F405 + "ImpactListBridge": ImpactListBridge, # noqa: F405 + + "RemoveNoiseMask": RemoveNoiseMask, # noqa: F405 + + "ImpactLogger": ImpactLogger, # noqa: F405 + "ImpactDummyInput": ImpactDummyInput, # noqa: F405 + + "ImpactQueueTrigger": ImpactQueueTrigger, # noqa: F405 + "ImpactQueueTriggerCountdown": ImpactQueueTriggerCountdown, # noqa: F405 + "ImpactSetWidgetValue": ImpactSetWidgetValue, # noqa: F405 + "ImpactNodeSetMuteState": ImpactNodeSetMuteState, # noqa: F405 + "ImpactControlBridge": ImpactControlBridge, # noqa: F405 + "ImpactIsNotEmptySEGS": ImpactNotEmptySEGS, # noqa: F405 + "ImpactSleep": ImpactSleep, # noqa: F405 + "ImpactRemoteBoolean": ImpactRemoteBoolean, # noqa: F405 + "ImpactRemoteInt": ImpactRemoteInt, # noqa: F405 + + "ImpactHFTransformersClassifierProvider": HF_TransformersClassifierProvider, # noqa: F405 + "ImpactSEGSClassify": SEGS_Classify, # noqa: F405 + + "ImpactSchedulerAdapter": ImpactSchedulerAdapter, # noqa: F405 + "GITSSchedulerFuncProvider": GITSSchedulerFuncProvider # noqa: F405 +} + + +NODE_DISPLAY_NAME_MAPPINGS = { + "SAMLoader": "SAMLoader (Impact)", + + "BboxDetectorSEGS": "BBOX Detector (SEGS)", + "SegmDetectorSEGS": "SEGM Detector (SEGS)", + "ONNXDetectorSEGS": "ONNX Detector (SEGS/legacy) - use BBOXDetector", + "ImpactSimpleDetectorSEGS_for_AD": "Simple Detector for Video (SEGS)", + "ImpactSAM2VideoDetectorSEGS": "SAM2 Video Detector (SEGS)", + "ImpactSimpleDetectorSEGS": "Simple Detector (SEGS)", + "ImpactSimpleDetectorSEGSPipe": "Simple Detector (SEGS/pipe)", + "ImpactControlNetApplySEGS": "ControlNetApply (SEGS) - DEPRECATED", + "ImpactControlNetApplyAdvancedSEGS": "ControlNetApply (SEGS)", + "ImpactIPAdapterApplySEGS": "IPAdapterApply (SEGS)", + + "BboxDetectorCombined_v2": "BBOX Detector (combined)", + "SegmDetectorCombined_v2": "SEGM Detector (combined)", + "SegsToCombinedMask": "SEGS to MASK (combined)", + "MediaPipeFaceMeshToSEGS": "MediaPipe FaceMesh to SEGS", + "MaskToSEGS": "MASK to SEGS", + "MaskToSEGS_for_AnimateDiff": "MASK to SEGS for Video", + "BitwiseAndMaskForEach": "Pixelwise(SEGS & SEGS)", + "SubtractMaskForEach": "Pixelwise(SEGS - SEGS)", + "ImpactSegsAndMask": "Pixelwise(SEGS & MASK)", + "ImpactSegsAndMaskForEach": "Pixelwise(SEGS & MASKS ForEach)", + "BitwiseAndMask": "Pixelwise(MASK & MASK)", + "SubtractMask": "Pixelwise(MASK - MASK)", + "AddMask": "Pixelwise(MASK + MASK)", + "MaskRectArea": "Mask Rect Area", + "MaskRectAreaAdvanced": "Mask Rect Area (Advanced)", + "ImpactFlattenMask": "Flatten Mask Batch", + "DetailerForEach": "Detailer (SEGS)", + "DetailerForEachAutoRetry": "Detailer (SEGS) with auto retry", + "DetailerForEachPipe": "Detailer (SEGS/pipe)", + "DetailerForEachDebug": "DetailerDebug (SEGS)", + "DetailerForEachDebugPipe": "DetailerDebug (SEGS/pipe)", + "SEGSDetailerForAnimateDiff": "SEGSDetailer For Video (SEGS/pipe)", + "DetailerForEachPipeForAnimateDiff": "Detailer For Video (SEGS/pipe)", + "SEGSUpscaler": "Upscaler (SEGS)", + "SEGSUpscalerPipe": "Upscaler (SEGS/pipe)", + + "SAMDetectorCombined": "SAMDetector (combined)", + "SAMDetectorSegmented": "SAMDetector (segmented)", + "FaceDetailerPipe": "FaceDetailer (pipe)", + "MaskDetailerPipe": "MaskDetailer (pipe)", + + "FromDetailerPipeSDXL": "FromDetailer (SDXL/pipe)", + "BasicPipeToDetailerPipeSDXL": "BasicPipe -> DetailerPipe (SDXL)", + "EditDetailerPipeSDXL": "Edit DetailerPipe (SDXL)", + + "BasicPipeToDetailerPipe": "BasicPipe -> DetailerPipe", + "DetailerPipeToBasicPipe": "DetailerPipe -> BasicPipe", + "EditBasicPipe": "Edit BasicPipe", + "EditDetailerPipe": "Edit DetailerPipe", + "AnyPipeToBasic": "Any PIPE -> BasicPipe", + + "LatentPixelScale": "Latent Scale (on Pixel Space)", + "IterativeLatentUpscale": "Iterative Upscale (Latent/on Pixel Space)", + "IterativeImageUpscale": "Iterative Upscale (Image)", + + "TwoSamplersForMaskUpscalerProvider": "TwoSamplersForMask Upscaler Provider", + "TwoSamplersForMaskUpscalerProviderPipe": "TwoSamplersForMask Upscaler Provider (pipe)", + + "ReencodeLatent": "Reencode Latent", + "ReencodeLatentPipe": "Reencode Latent (pipe)", + + "ImpactKSamplerBasicPipe": "KSampler (pipe)", + "ImpactKSamplerAdvancedBasicPipe": "KSampler (Advanced/pipe)", + "ImpactSEGSLabelAssign": "SEGS Assign (label)", + "ImpactSEGSLabelFilter": "SEGS Filter (label)", + "ImpactSEGSRangeFilter": "SEGS Filter (range)", + "ImpactSEGSOrderedFilter": "SEGS Filter (ordered)", + "ImpactSEGSIntersectionFilter": "SEGS Filter (intersection)", + "ImpactSEGSNMSFilter": "SEGS Filter (non max suppression)", + "ImpactSEGSConcat": "SEGS Concat", + "ImpactSEGSToMaskList": "SEGS to Mask List", + "ImpactSEGSToMaskBatch": "SEGS to Mask Batch", + "ImpactSEGSPicker": "Picker (SEGS)", + "ImpactMakeTileSEGS": "Make Tile SEGS", + "ImpactSEGSMerge": "SEGS Merge", + + "ImpactDecomposeSEGS": "Decompose (SEGS)", + "ImpactAssembleSEGS": "Assemble (SEGS)", + "ImpactFrom_SEG_ELT": "From SEG_ELT", + "ImpactEdit_SEG_ELT": "Edit SEG_ELT", + "ImpactFrom_SEG_ELT_bbox": "From SEG_ELT bbox", + "ImpactFrom_SEG_ELT_crop_region": "From SEG_ELT crop_region", + "ImpactDilate_Mask_SEG_ELT": "Dilate Mask (SEG_ELT)", + "ImpactScaleBy_BBOX_SEG_ELT": "ScaleBy BBOX (SEG_ELT)", + "ImpactCount_Elts_in_SEGS": "Count Elts in SEGS", + "ImpactDilateMask": "Dilate Mask", + "ImpactGaussianBlurMask": "Gaussian Blur Mask", + "ImpactDilateMaskInSEGS": "Dilate Mask (SEGS)", + "ImpactGaussianBlurMaskInSEGS": "Gaussian Blur Mask (SEGS)", + + "PreviewBridge": "Preview Bridge (Image)", + "PreviewBridgeLatent": "Preview Bridge (Latent)", + "ImageSender": "Image Sender", + "ImageReceiver": "Image Receiver", + "ImageMaskSwitch": "Switch (images, mask)", + "ImpactSwitch": "Switch (Any)", + "ImpactInversedSwitch": "Inversed Switch (Any)", + "ImpactExecutionOrderController": "Execution Order Controller", + "ImpactListBridge": "List Bridge", + + "MasksToMaskList": "Mask Batch to Mask List", + "MaskListToMaskBatch": "Mask List to Mask Batch", + "ImpactImageBatchToImageList": "Image Batch to Image List", + "ImageListToImageBatch": "Image List to Image Batch", + + "ImpactMakeImageList": "Make Image List", + "ImpactMakeImageBatch": "Make Image Batch", + "ImpactMakeMaskList": "Make Mask List", + "ImpactMakeMaskBatch": "Make Mask Batch", + "ImpactMakeAnyList": "Make List (Any)", + "ImpactSelectNthItemOfAnyList": "Select Nth Item (Any list)", + + "ImpactStringSelector": "String Selector", + "StringListToString": "String List to String", + "WildcardPromptFromString": "Wildcard Prompt from String", + "ImpactIsNotEmptySEGS": "SEGS isn't Empty", + "SetDefaultImageForSEGS": "Set Default Image for SEGS", + "RemoveImageFromSEGS": "Remove Image from SEGS", + + "RemoveNoiseMask": "Remove Noise Mask", + + "ImpactCombineConditionings": "Combine Conditionings", + "ImpactConcatConditionings": "Concat Conditionings", + + "ImpactQueueTrigger": "Queue Trigger", + "ImpactQueueTriggerCountdown": "Queue Trigger (Countdown)", + "ImpactSetWidgetValue": "Set Widget Value", + "ImpactNodeSetMuteState": "Set Mute State", + "ImpactControlBridge": "Control Bridge", + "ImpactSleep": "Sleep", + "ImpactRemoteBoolean": "Remote Boolean (on prompt)", + "ImpactRemoteInt": "Remote Int (on prompt)", + + "ImpactHFTransformersClassifierProvider": "HF Transformers Classifier Provider", + "ImpactSEGSClassify": "SEGS Classify", + + "LatentSwitch": "Switch (latent/legacy)", + "SEGSSwitch": "Switch (SEGS/legacy)", + + "SEGSPreviewCNet": "SEGSPreview (CNET Image)", + + "ImpactSchedulerAdapter": "Impact Scheduler Adapter", + "GITSSchedulerFuncProvider": "GITSScheduler Func Provider", + "ImpactNegativeConditioningPlaceholder": "Negative Cond Placeholder" +} + + +# NOTE: Inject directly into EXTENSION_WEB_DIRS instead of WEB_DIRECTORY +# Provide the js path fixed as ComfyUI-Impact-Pack instead of the path name, making it available for external use + +# WEB_DIRECTORY = "js" -- deprecated method +nodes.EXTENSION_WEB_DIRS["ComfyUI-Impact-Pack"] = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'js') + + +__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] diff --git a/custom_nodes/comfyui-impact-pack/__pycache__/__init__.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f750e5875bb81f50301ffbe6cc53078f6171111a Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/__pycache__/__init__.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/custom_wildcards/put_wildcards_here b/custom_nodes/comfyui-impact-pack/custom_wildcards/put_wildcards_here new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/1-FaceDetailer.jpg b/custom_nodes/comfyui-impact-pack/example_workflows/1-FaceDetailer.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4711d5bdb53f174d58983f332cfc64708fe67be2 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/1-FaceDetailer.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c1a4ed7a9079d45a01a52043d9672d2646fdd28b88eac958e51dc2b38aa438c0 +size 64932 diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/1-FaceDetailer.json b/custom_nodes/comfyui-impact-pack/example_workflows/1-FaceDetailer.json new file mode 100644 index 0000000000000000000000000000000000000000..0801a0bb5dfc6e0e6bcc4de41d0005bb220578f8 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/1-FaceDetailer.json @@ -0,0 +1,1269 @@ +{ + "last_node_id": 61, + "last_link_id": 170, + "nodes": [ + { + "id": 28, + "type": "KSampler", + "pos": [ + 530, + 840 + ], + "size": [ + 320, + 600 + ], + "flags": {}, + "order": 14, + "mode": 0, + "inputs": [ + { + "name": "model", + "type": "MODEL", + "link": 65 + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": 57 + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": 170 + }, + { + "name": "latent_image", + "type": "LATENT", + "link": 59, + "slot_index": 3 + } + ], + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [ + 60 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "KSampler" + }, + "widgets_values": [ + 431433362471142, + "fixed", + 20, + 8, + "euler", + "normal", + 1 + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 43, + "type": "PreviewImage", + "pos": [ + 2390, + -140 + ], + "size": [ + 230, + 300 + ], + "flags": {}, + "order": 19, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 142 + } + ], + "outputs": [], + "title": "Cropped (refined)", + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 52, + "type": "PreviewImage", + "pos": [ + 2390, + 210 + ], + "size": [ + 230, + 310 + ], + "flags": {}, + "order": 20, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 146 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 53, + "type": "UltralyticsDetectorProvider", + "pos": [ + 1290, + 200 + ], + "size": [ + 315, + 78 + ], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "BBOX_DETECTOR", + "type": "BBOX_DETECTOR", + "shape": 3, + "links": [ + 150 + ], + "slot_index": 0 + }, + { + "name": "SEGM_DETECTOR", + "type": "SEGM_DETECTOR", + "shape": 3, + "links": null + } + ], + "properties": { + "Node name for S&R": "UltralyticsDetectorProvider" + }, + "widgets_values": [ + "bbox/face_yolov8m.pt" + ], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 16, + "type": "SAMLoader", + "pos": [ + 1290, + 340 + ], + "size": [ + 320, + 82 + ], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "SAM_MODEL", + "type": "SAM_MODEL", + "links": [ + 151 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "SAMLoader" + }, + "widgets_values": [ + "sam_vit_b_01ec64.pth", + "AUTO" + ], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 30, + "type": "VAEDecode", + "pos": [ + 1010, + 840 + ], + "size": [ + 140, + 50 + ], + "flags": {}, + "order": 15, + "mode": 0, + "inputs": [ + { + "name": "samples", + "type": "LATENT", + "link": 60 + }, + { + "name": "vae", + "type": "VAE", + "link": 164 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [ + 78, + 152 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "VAEDecode" + }, + "widgets_values": [], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 4, + "type": "CheckpointLoaderSimple", + "pos": [ + -640, + 190 + ], + "size": [ + 312.0885314941406, + 98 + ], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "MODEL", + "type": "MODEL", + "links": [ + 64, + 157 + ], + "slot_index": 0 + }, + { + "name": "CLIP", + "type": "CLIP", + "links": [ + 148, + 149, + 159 + ], + "slot_index": 1 + }, + { + "name": "VAE", + "type": "VAE", + "links": [ + 161, + 163 + ], + "slot_index": 2 + } + ], + "properties": { + "Node name for S&R": "CheckpointLoaderSimple" + }, + "widgets_values": [ + "SD1.5/fantexiRealistic_v10.safetensors" + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 58, + "type": "Reroute", + "pos": [ + 850, + 220 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 10, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 163 + } + ], + "outputs": [ + { + "name": "", + "type": "VAE", + "links": [ + 164 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 5, + "type": "CLIPTextEncode", + "pos": [ + -120, + 300 + ], + "size": [ + 310, + 180 + ], + "flags": {}, + "order": 6, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": 148 + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [ + 57, + 165 + ], + "slot_index": 0 + } + ], + "title": "Positive", + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "(photorealistic:1.4), best quality, masterpiece, 1girl, (detailed eyes), perfect anatomy, smile, details, perfect eyes, perfect face, (SpringGreen+letter_printed_sleeveless_turtleneck), ((white_low_waist_jeans)), (thigh_gap:1.2), at_the_top_of_mountain, snow, daytime, windy, path, mountain_villa, sky_view, slender, looking_away, (small breast:1.2)" + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 6, + "type": "CLIPTextEncode", + "pos": [ + -120, + 540 + ], + "size": [ + 310, + 120 + ], + "flags": {}, + "order": 7, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": 149 + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [ + 167 + ], + "slot_index": 0 + } + ], + "title": "Negative", + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "embedding:easynegative, embedding:badhandv4, paintings, sketches, (worst quality:1.4, low quality, normal quality), lowres, normal quality, (monochrome), (grayscale), skin spots, acnes, skin blemishes, age spot, glans, nsfw, watermark, signature, text, bikini, bad anatomy, (six_fingers), (nail_art), nail polish, blush, fruit," + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 60, + "type": "Reroute", + "pos": [ + 340, + 540 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 12, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 167 + } + ], + "outputs": [ + { + "name": "", + "type": "CONDITIONING", + "links": [ + 168, + 170 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 31, + "type": "Reroute", + "pos": [ + 130, + 190 + ], + "size": [ + 82, + 26 + ], + "flags": {}, + "order": 4, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 64 + } + ], + "outputs": [ + { + "name": "MODEL", + "type": "MODEL", + "links": [ + 65 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": true, + "horizontal": false + } + }, + { + "id": 29, + "type": "EmptyLatentImage", + "pos": [ + -120, + 900 + ], + "size": [ + 310, + 130 + ], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [ + 59 + ] + } + ], + "properties": { + "Node name for S&R": "EmptyLatentImage" + }, + "widgets_values": [ + 296, + 512, + 1 + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 51, + "type": "FaceDetailer", + "pos": [ + 1720, + -330 + ], + "size": [ + 350, + 1180 + ], + "flags": {}, + "order": 17, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 152 + }, + { + "name": "model", + "type": "MODEL", + "link": 158 + }, + { + "name": "clip", + "type": "CLIP", + "link": 160 + }, + { + "name": "vae", + "type": "VAE", + "link": 162 + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": 166 + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": 169 + }, + { + "name": "bbox_detector", + "type": "BBOX_DETECTOR", + "link": 150 + }, + { + "name": "sam_model_opt", + "type": "SAM_MODEL", + "shape": 7, + "link": 151 + }, + { + "name": "segm_detector_opt", + "type": "SEGM_DETECTOR", + "shape": 7, + "link": null + }, + { + "name": "detailer_hook", + "type": "DETAILER_HOOK", + "shape": 7, + "link": null + }, + { + "name": "scheduler_func_opt", + "type": "SCHEDULER_FUNC", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "image", + "type": "IMAGE", + "shape": 3, + "links": [ + 141 + ], + "slot_index": 0 + }, + { + "name": "cropped_refined", + "type": "IMAGE", + "shape": 6, + "links": [ + 142 + ], + "slot_index": 1 + }, + { + "name": "cropped_enhanced_alpha", + "type": "IMAGE", + "shape": 6, + "links": [ + 146 + ], + "slot_index": 2 + }, + { + "name": "mask", + "type": "MASK", + "shape": 3, + "links": [ + 153 + ], + "slot_index": 3 + }, + { + "name": "detailer_pipe", + "type": "DETAILER_PIPE", + "shape": 3, + "links": null + }, + { + "name": "cnet_images", + "type": "IMAGE", + "shape": 6, + "links": null + } + ], + "properties": { + "Node name for S&R": "FaceDetailer" + }, + "widgets_values": [ + 360, + true, + 768, + 0, + "fixed", + 20, + 8, + "euler", + "normal", + 0.5, + 5, + true, + false, + 0.5, + 15, + 3, + "center-1", + 0, + 0.93, + 0, + 0.7, + "False", + 10, + "", + 1, + false, + 20, + false, + false + ], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 17, + "type": "MaskToImage", + "pos": [ + 2150, + 590 + ], + "size": [ + 176.39999389648438, + 26 + ], + "flags": {}, + "order": 21, + "mode": 0, + "inputs": [ + { + "name": "mask", + "type": "MASK", + "link": 153 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [ + 107 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "MaskToImage" + }, + "widgets_values": [], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 18, + "type": "PreviewImage", + "pos": [ + 2390, + 590 + ], + "size": [ + 230, + 290 + ], + "flags": {}, + "order": 22, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 107 + } + ], + "outputs": [], + "title": "Mask", + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 7, + "type": "PreviewImage", + "pos": [ + 2660, + -320 + ], + "size": [ + 430, + 650 + ], + "flags": {}, + "order": 18, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 141 + } + ], + "outputs": [], + "title": "Enhanced", + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 33, + "type": "PreviewImage", + "pos": [ + 1250, + 840 + ], + "size": [ + 360, + 630 + ], + "flags": {}, + "order": 16, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 78 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 55, + "type": "Reroute", + "pos": [ + -190, + -310 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 157 + } + ], + "outputs": [ + { + "name": "", + "type": "MODEL", + "links": [ + 158 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 56, + "type": "Reroute", + "pos": [ + -190, + -290 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 8, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 159 + } + ], + "outputs": [ + { + "name": "", + "type": "CLIP", + "links": [ + 160 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 57, + "type": "Reroute", + "pos": [ + -190, + -270 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 9, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 161 + } + ], + "outputs": [ + { + "name": "", + "type": "VAE", + "links": [ + 162 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 59, + "type": "Reroute", + "pos": [ + 290, + -250 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 11, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 165 + } + ], + "outputs": [ + { + "name": "", + "type": "CONDITIONING", + "links": [ + 166 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 61, + "type": "Reroute", + "pos": [ + 520, + -230 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 13, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 168 + } + ], + "outputs": [ + { + "name": "", + "type": "CONDITIONING", + "links": [ + 169 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + } + ], + "links": [ + [ + 57, + 5, + 0, + 28, + 1, + "CONDITIONING" + ], + [ + 59, + 29, + 0, + 28, + 3, + "LATENT" + ], + [ + 60, + 28, + 0, + 30, + 0, + "LATENT" + ], + [ + 64, + 4, + 0, + 31, + 0, + "*" + ], + [ + 65, + 31, + 0, + 28, + 0, + "MODEL" + ], + [ + 78, + 30, + 0, + 33, + 0, + "IMAGE" + ], + [ + 107, + 17, + 0, + 18, + 0, + "IMAGE" + ], + [ + 141, + 51, + 0, + 7, + 0, + "IMAGE" + ], + [ + 142, + 51, + 1, + 43, + 0, + "IMAGE" + ], + [ + 146, + 51, + 2, + 52, + 0, + "IMAGE" + ], + [ + 148, + 4, + 1, + 5, + 0, + "CLIP" + ], + [ + 149, + 4, + 1, + 6, + 0, + "CLIP" + ], + [ + 150, + 53, + 0, + 51, + 6, + "BBOX_DETECTOR" + ], + [ + 151, + 16, + 0, + 51, + 7, + "SAM_MODEL" + ], + [ + 152, + 30, + 0, + 51, + 0, + "IMAGE" + ], + [ + 153, + 51, + 3, + 17, + 0, + "MASK" + ], + [ + 157, + 4, + 0, + 55, + 0, + "*" + ], + [ + 158, + 55, + 0, + 51, + 1, + "MODEL" + ], + [ + 159, + 4, + 1, + 56, + 0, + "*" + ], + [ + 160, + 56, + 0, + 51, + 2, + "CLIP" + ], + [ + 161, + 4, + 2, + 57, + 0, + "*" + ], + [ + 162, + 57, + 0, + 51, + 3, + "VAE" + ], + [ + 163, + 4, + 2, + 58, + 0, + "*" + ], + [ + 164, + 58, + 0, + 30, + 1, + "VAE" + ], + [ + 165, + 5, + 0, + 59, + 0, + "*" + ], + [ + 166, + 59, + 0, + 51, + 4, + "CONDITIONING" + ], + [ + 167, + 6, + 0, + 60, + 0, + "*" + ], + [ + 168, + 60, + 0, + 61, + 0, + "*" + ], + [ + 169, + 61, + 0, + 51, + 5, + "CONDITIONING" + ], + [ + 170, + 60, + 0, + 28, + 2, + "CONDITIONING" + ] + ], + "groups": [], + "config": {}, + "extra": { + "ds": { + "scale": 1, + "offset": [ + 740, + 430 + ] + }, + "groupNodes": {}, + "controller_panel": { + "controllers": {}, + "hidden": true, + "highlight": true, + "version": 2, + "default_order": [] + }, + "node_versions": { + "comfy-core": "0.3.14", + "comfyui-impact-subpack": "74db20c95eca152a6d686c914edc0ef4e4762cb8", + "comfyui-impact-pack": "1ae7cae2df8cca06027edfa3a24512671239d6c4" + }, + "ue_links": [], + "VHS_latentpreview": false, + "VHS_latentpreviewrate": 0, + "VHS_MetadataImage": true, + "VHS_KeepIntermediate": true + }, + "version": 0.4 +} \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/2-MaskDetailer.jpg b/custom_nodes/comfyui-impact-pack/example_workflows/2-MaskDetailer.jpg new file mode 100644 index 0000000000000000000000000000000000000000..198296605d5d51654e9997a87b1536a8fa632ff3 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/2-MaskDetailer.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1abd1823bed26d5e8c92707c0369d41b4f7c6a0629e70c3fa92fcc914585dc8a +size 114261 diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/2-MaskDetailer.json b/custom_nodes/comfyui-impact-pack/example_workflows/2-MaskDetailer.json new file mode 100644 index 0000000000000000000000000000000000000000..52ba3f05f7ce0ca6c0279754922b40f2166400d8 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/2-MaskDetailer.json @@ -0,0 +1,596 @@ +{ + "last_node_id": 5, + "last_link_id": 5, + "nodes": [ + { + "id": 1, + "type": "LoadImage", + "pos": [ + 30, + 210 + ], + "size": [ + 390, + 320 + ], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 3, + "links": [ + 1 + ] + }, + { + "name": "MASK", + "type": "MASK", + "shape": 3, + "links": [ + 2 + ] + } + ], + "properties": { + "Node name for S&R": "LoadImage" + }, + "widgets_values": [ + "clipspace/clipspace-mask-609196.2000000011.png [input]", + "image" + ] + }, + { + "id": 5, + "type": "PreviewImage", + "pos": [ + 1230, + 210 + ], + "size": [ + 210, + 246 + ], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 5 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [] + }, + { + "id": 3, + "type": "workflow>Impact::MAKE_BASIC_PIPE", + "pos": [ + 20, + 620 + ], + "size": [ + 400, + 200 + ], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "shape": 3, + "links": [ + 3 + ] + } + ], + "properties": { + "Node name for S&R": "workflow/Impact::MAKE_BASIC_PIPE" + }, + "widgets_values": [ + "SD1.5/realcartoon3d_v13.safetensors", + "(best quality:1.4), fox girl", + "(worst quality:1.4), nsfw" + ] + }, + { + "id": 2, + "type": "MaskDetailerPipe", + "pos": [ + 530, + 210 + ], + "size": [ + 569.4000244140625, + 850 + ], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 1 + }, + { + "name": "mask", + "type": "MASK", + "link": 2 + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "link": 3, + "slot_index": 2 + }, + { + "name": "refiner_basic_pipe_opt", + "type": "BASIC_PIPE", + "shape": 7, + "link": null + }, + { + "name": "detailer_hook", + "type": "DETAILER_HOOK", + "shape": 7, + "link": null + }, + { + "name": "scheduler_func_opt", + "type": "SCHEDULER_FUNC", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "image", + "type": "IMAGE", + "shape": 3, + "links": [ + 5 + ], + "slot_index": 0 + }, + { + "name": "cropped_refined", + "type": "IMAGE", + "shape": 6, + "links": null + }, + { + "name": "cropped_enhanced_alpha", + "type": "IMAGE", + "shape": 6, + "links": [ + 4 + ], + "slot_index": 2 + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "shape": 3, + "links": null + }, + { + "name": "refiner_basic_pipe_opt", + "type": "BASIC_PIPE", + "shape": 3, + "links": null + } + ], + "properties": { + "Node name for S&R": "MaskDetailerPipe" + }, + "widgets_values": [ + 512, + true, + 1024, + true, + 1003, + "fixed", + 20, + 8, + "euler", + "normal", + 0.75, + 5, + 3, + 10, + 0.2, + 1, + 1, + false, + 20, + false, + false + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 4, + "type": "PreviewImage", + "pos": [ + 1230, + 560 + ], + "size": [ + 210, + 246 + ], + "flags": {}, + "order": 4, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 4 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [] + } + ], + "links": [ + [ + 1, + 1, + 0, + 2, + 0, + "IMAGE" + ], + [ + 2, + 1, + 1, + 2, + 1, + "MASK" + ], + [ + 3, + 3, + 0, + 2, + 2, + "BASIC_PIPE" + ], + [ + 4, + 2, + 2, + 4, + 0, + "IMAGE" + ], + [ + 5, + 2, + 0, + 5, + 0, + "IMAGE" + ] + ], + "groups": [], + "config": {}, + "extra": { + "ds": { + "scale": 1, + "offset": [ + 80, + -110 + ] + }, + "groupNodes": { + "Impact::MAKE_BASIC_PIPE": { + "author": "Dr.Lt.Data", + "category": "", + "config": { + "1": { + "input": { + "text": { + "name": "Positive prompt" + } + } + }, + "2": { + "input": { + "text": { + "name": "Negative prompt" + } + } + } + }, + "datetime": 1708272471445, + "external": [], + "links": [ + [ + 0, + 1, + 1, + 0, + 1, + "CLIP" + ], + [ + 0, + 1, + 2, + 0, + 1, + "CLIP" + ], + [ + 0, + 0, + 3, + 0, + 1, + "MODEL" + ], + [ + 0, + 1, + 3, + 1, + 1, + "CLIP" + ], + [ + 0, + 2, + 3, + 2, + 1, + "VAE" + ], + [ + 1, + 0, + 3, + 3, + 3, + "CONDITIONING" + ], + [ + 2, + 0, + 3, + 4, + 4, + "CONDITIONING" + ] + ], + "nodes": [ + { + "flags": {}, + "index": 0, + "mode": 0, + "order": 0, + "outputs": [ + { + "links": [], + "name": "MODEL", + "shape": 3, + "slot_index": 0, + "type": "MODEL", + "localized_name": "MODEL" + }, + { + "links": [], + "name": "CLIP", + "shape": 3, + "slot_index": 1, + "type": "CLIP", + "localized_name": "CLIP" + }, + { + "links": [], + "name": "VAE", + "shape": 3, + "slot_index": 2, + "type": "VAE", + "localized_name": "VAE" + } + ], + "pos": [ + 550, + 360 + ], + "properties": { + "Node name for S&R": "CheckpointLoaderSimple" + }, + "size": { + "0": 315, + "1": 98 + }, + "type": "CheckpointLoaderSimple", + "widgets_values": [ + "SDXL/sd_xl_base_1.0_0.9vae.safetensors" + ], + "inputs": [] + }, + { + "flags": {}, + "index": 1, + "inputs": [ + { + "link": null, + "name": "clip", + "type": "CLIP", + "localized_name": "clip" + } + ], + "mode": 0, + "order": 1, + "outputs": [ + { + "links": [], + "name": "CONDITIONING", + "shape": 3, + "slot_index": 0, + "type": "CONDITIONING", + "localized_name": "CONDITIONING" + } + ], + "pos": [ + 940, + 480 + ], + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "size": { + "0": 263, + "1": 99 + }, + "title": "Positive", + "type": "CLIPTextEncode", + "widgets_values": [ + "" + ] + }, + { + "flags": {}, + "index": 2, + "inputs": [ + { + "link": null, + "name": "clip", + "type": "CLIP", + "localized_name": "clip" + } + ], + "mode": 0, + "order": 2, + "outputs": [ + { + "links": [], + "name": "CONDITIONING", + "shape": 3, + "slot_index": 0, + "type": "CONDITIONING", + "localized_name": "CONDITIONING" + } + ], + "pos": [ + 940, + 640 + ], + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "size": { + "0": 263, + "1": 99 + }, + "title": "Negative", + "type": "CLIPTextEncode", + "widgets_values": [ + "" + ] + }, + { + "flags": {}, + "index": 3, + "inputs": [ + { + "link": null, + "name": "model", + "type": "MODEL", + "localized_name": "model" + }, + { + "link": null, + "name": "clip", + "type": "CLIP", + "localized_name": "clip" + }, + { + "link": null, + "name": "vae", + "type": "VAE", + "localized_name": "vae" + }, + { + "link": null, + "name": "positive", + "type": "CONDITIONING", + "localized_name": "positive" + }, + { + "link": null, + "name": "negative", + "type": "CONDITIONING", + "localized_name": "negative" + } + ], + "mode": 0, + "order": 3, + "outputs": [ + { + "links": null, + "name": "basic_pipe", + "shape": 3, + "slot_index": 0, + "type": "BASIC_PIPE", + "localized_name": "basic_pipe" + } + ], + "pos": [ + 1320, + 360 + ], + "properties": { + "Node name for S&R": "ToBasicPipe" + }, + "size": { + "0": 241.79998779296875, + "1": 106 + }, + "type": "ToBasicPipe" + } + ], + "packname": "Impact", + "version": "1.0" + } + }, + "controller_panel": { + "controllers": {}, + "hidden": true, + "highlight": true, + "version": 2, + "default_order": [] + }, + "node_versions": { + "comfy-core": "0.3.14", + "comfyui-impact-pack": "1ae7cae2df8cca06027edfa3a24512671239d6c4" + }, + "ue_links": [], + "VHS_latentpreview": false, + "VHS_latentpreviewrate": 0, + "VHS_MetadataImage": true, + "VHS_KeepIntermediate": true + }, + "version": 0.4 +} \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/3-SEGSDetailer.jpg b/custom_nodes/comfyui-impact-pack/example_workflows/3-SEGSDetailer.jpg new file mode 100644 index 0000000000000000000000000000000000000000..8c45308643b46f8cf836032b7522fe70c6eff38b --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/3-SEGSDetailer.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9f4aed9e96b46408fba0ed644ce1a86ffa7ca3cfd373c2849a2cc1fc5f243175 +size 42824 diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/3-SEGSDetailer.json b/custom_nodes/comfyui-impact-pack/example_workflows/3-SEGSDetailer.json new file mode 100644 index 0000000000000000000000000000000000000000..08bd26dedabcb57a545d197a9a48635966debc2f --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/3-SEGSDetailer.json @@ -0,0 +1,1056 @@ +{ + "last_node_id": 19, + "last_link_id": 30, + "nodes": [ + { + "id": 8, + "type": "SAMLoader", + "pos": [ + 60, + 530 + ], + "size": [ + 315, + 82 + ], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "SAM_MODEL", + "type": "SAM_MODEL", + "shape": 3, + "links": [ + 7 + ] + } + ], + "properties": { + "Node name for S&R": "SAMLoader" + }, + "widgets_values": [ + "sam_vit_b_01ec64.pth", + "AUTO" + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 7, + "type": "UltralyticsDetectorProvider", + "pos": [ + 60, + 390 + ], + "size": [ + 315, + 78 + ], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "BBOX_DETECTOR", + "type": "BBOX_DETECTOR", + "shape": 3, + "links": [ + 6 + ] + }, + { + "name": "SEGM_DETECTOR", + "type": "SEGM_DETECTOR", + "shape": 3, + "links": null + } + ], + "properties": { + "Node name for S&R": "UltralyticsDetectorProvider" + }, + "widgets_values": [ + "bbox/face_yolov8m.pt" + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 14, + "type": "Reroute", + "pos": [ + 570, + 330 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 18 + } + ], + "outputs": [ + { + "name": "", + "type": "IMAGE", + "links": [ + 19 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + }, + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 15, + "type": "Reroute", + "pos": [ + 1240, + 330 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 7, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 19 + } + ], + "outputs": [ + { + "name": "", + "type": "IMAGE", + "links": [ + 24, + 26 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + }, + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 16, + "type": "Reroute", + "pos": [ + 1740, + 330 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 8, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 24 + } + ], + "outputs": [ + { + "name": "", + "type": "IMAGE", + "links": [ + 25 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + }, + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 17, + "type": "Reroute", + "pos": [ + 1390, + 390 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 9, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "pos": [ + 37.5, + 0 + ], + "link": 26 + } + ], + "outputs": [ + { + "name": "", + "type": "IMAGE", + "links": [ + 27, + 28 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": true + }, + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 13, + "type": "SEGSPaste", + "pos": [ + 1860, + 510 + ], + "size": [ + 570, + 122 + ], + "flags": {}, + "order": 10, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 25 + }, + { + "name": "segs", + "type": "SEGS", + "link": 22 + }, + { + "name": "ref_image_opt", + "type": "IMAGE", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 3, + "links": [ + 29 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "SEGSPaste" + }, + "widgets_values": [ + 5, + 255 + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 1, + "type": "LoadImage", + "pos": [ + 60, + 680 + ], + "size": [ + 315, + 314 + ], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 3, + "links": [ + 1, + 8, + 18 + ], + "slot_index": 0 + }, + { + "name": "MASK", + "type": "MASK", + "shape": 3, + "links": null + } + ], + "properties": { + "Node name for S&R": "LoadImage" + }, + "widgets_values": [ + "ComfyUI_temp_xltgv_00001_.png", + "image" + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 19, + "type": "workflow>MAKE_BASIC_PIPE", + "pos": [ + 60, + 70 + ], + "size": [ + 400, + 200 + ], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "shape": 3, + "links": [ + 30 + ] + } + ], + "properties": { + "Node name for S&R": "workflow/MAKE_BASIC_PIPE" + }, + "widgets_values": [ + "SD1.5/V07_v07.safetensors", + "best quality:1.4, detailed, (goth:0.8)", + "low quality:1.4, worst quality:1.4" + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 6, + "type": "SEGSPreview", + "pos": [ + 1460, + 600 + ], + "size": [ + 320, + 314 + ], + "flags": {}, + "order": 11, + "mode": 0, + "inputs": [ + { + "name": "segs", + "type": "SEGS", + "link": 5 + }, + { + "name": "fallback_image_opt", + "type": "IMAGE", + "shape": 7, + "link": 27 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 6, + "links": null + } + ], + "properties": { + "Node name for S&R": "SEGSPreview" + }, + "widgets_values": [ + true, + 0.2 + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 4, + "type": "SEGSDetailer", + "pos": [ + 960, + 530 + ], + "size": [ + 440, + 734 + ], + "flags": {}, + "order": 6, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 8 + }, + { + "name": "segs", + "type": "SEGS", + "link": 3 + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "link": 30, + "slot_index": 2 + }, + { + "name": "refiner_basic_pipe_opt", + "type": "BASIC_PIPE", + "shape": 7, + "link": null + }, + { + "name": "scheduler_func_opt", + "type": "SCHEDULER_FUNC", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "segs", + "type": "SEGS", + "shape": 3, + "links": [ + 5, + 22 + ], + "slot_index": 0 + }, + { + "name": "cnet_images", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 1 + } + ], + "properties": { + "Node name for S&R": "SEGSDetailer" + }, + "widgets_values": [ + 256, + true, + 768, + 1021210429641780, + "fixed", + 20, + 8, + "euler", + "normal", + 0.3, + true, + false, + 0.2, + 1, + 1, + false, + 20 + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 5, + "type": "PreviewImage", + "pos": [ + 1460, + 940 + ], + "size": [ + 320, + 310 + ], + "flags": {}, + "order": 12, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 28 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 18, + "type": "PreviewImage", + "pos": [ + 1860, + 690 + ], + "size": [ + 570, + 560 + ], + "flags": {}, + "order": 13, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 29 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 2, + "type": "ImpactSimpleDetectorSEGS", + "pos": [ + 570, + 530 + ], + "size": [ + 315, + 310 + ], + "flags": {}, + "order": 4, + "mode": 0, + "inputs": [ + { + "name": "bbox_detector", + "type": "BBOX_DETECTOR", + "link": 6, + "slot_index": 0 + }, + { + "name": "image", + "type": "IMAGE", + "link": 1 + }, + { + "name": "sam_model_opt", + "type": "SAM_MODEL", + "shape": 7, + "link": 7, + "slot_index": 2 + }, + { + "name": "segm_detector_opt", + "type": "SEGM_DETECTOR", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "SEGS", + "type": "SEGS", + "shape": 3, + "links": [ + 3 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "ImpactSimpleDetectorSEGS" + }, + "widgets_values": [ + 0.5, + 0, + 3, + 10, + 0.5, + 0, + 0, + 0.7, + 0 + ], + "color": "#322", + "bgcolor": "#533" + } + ], + "links": [ + [ + 1, + 1, + 0, + 2, + 1, + "IMAGE" + ], + [ + 3, + 2, + 0, + 4, + 1, + "SEGS" + ], + [ + 5, + 4, + 0, + 6, + 0, + "SEGS" + ], + [ + 6, + 7, + 0, + 2, + 0, + "BBOX_DETECTOR" + ], + [ + 7, + 8, + 0, + 2, + 2, + "SAM_MODEL" + ], + [ + 8, + 1, + 0, + 4, + 0, + "IMAGE" + ], + [ + 18, + 1, + 0, + 14, + 0, + "*" + ], + [ + 19, + 14, + 0, + 15, + 0, + "*" + ], + [ + 22, + 4, + 0, + 13, + 1, + "SEGS" + ], + [ + 24, + 15, + 0, + 16, + 0, + "*" + ], + [ + 25, + 16, + 0, + 13, + 0, + "IMAGE" + ], + [ + 26, + 15, + 0, + 17, + 0, + "*" + ], + [ + 27, + 17, + 0, + 6, + 1, + "IMAGE" + ], + [ + 28, + 17, + 0, + 5, + 0, + "IMAGE" + ], + [ + 29, + 13, + 0, + 18, + 0, + "IMAGE" + ], + [ + 30, + 19, + 0, + 4, + 2, + "BASIC_PIPE" + ] + ], + "groups": [], + "config": {}, + "extra": { + "groupNodes": { + "MAKE_BASIC_PIPE": { + "nodes": [ + { + "type": "CheckpointLoaderSimple", + "pos": [ + 140, + 150 + ], + "size": { + "0": 421.5882568359375, + "1": 98 + }, + "flags": {}, + "order": 3, + "mode": 0, + "outputs": [ + { + "name": "MODEL", + "type": "MODEL", + "links": [], + "shape": 3, + "slot_index": 0, + "localized_name": "MODEL" + }, + { + "name": "CLIP", + "type": "CLIP", + "links": [], + "shape": 3, + "slot_index": 1, + "localized_name": "CLIP" + }, + { + "name": "VAE", + "type": "VAE", + "links": [], + "shape": 3, + "localized_name": "VAE" + } + ], + "properties": { + "Node name for S&R": "CheckpointLoaderSimple" + }, + "widgets_values": [ + "SD1.5/V07_v07.safetensors" + ], + "color": "#222", + "bgcolor": "#000", + "index": 0, + "inputs": [] + }, + { + "type": "CLIPTextEncode", + "pos": [ + 740, + 60 + ], + "size": { + "0": 256.9515686035156, + "1": 76.1346435546875 + }, + "flags": {}, + "order": 6, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": null, + "localized_name": "clip" + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [], + "shape": 3, + "slot_index": 0, + "localized_name": "CONDITIONING" + } + ], + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "best quality:1.4, detailed, (goth:0.8)" + ], + "color": "#222", + "bgcolor": "#000", + "index": 1 + }, + { + "type": "CLIPTextEncode", + "pos": [ + 740, + 270 + ], + "size": { + "0": 258.04248046875, + "1": 79.95282745361328 + }, + "flags": {}, + "order": 7, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": null, + "slot_index": 0, + "localized_name": "clip" + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [], + "shape": 3, + "slot_index": 0, + "localized_name": "CONDITIONING" + } + ], + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "low quality:1.4, worst quality:1.4" + ], + "color": "#222", + "bgcolor": "#000", + "index": 2 + }, + { + "type": "ToBasicPipe", + "pos": [ + 1240, + 150 + ], + "size": { + "0": 241.79998779296875, + "1": 106 + }, + "flags": {}, + "order": 9, + "mode": 0, + "inputs": [ + { + "name": "model", + "type": "MODEL", + "link": null, + "localized_name": "model" + }, + { + "name": "clip", + "type": "CLIP", + "link": null, + "localized_name": "clip" + }, + { + "name": "vae", + "type": "VAE", + "link": null, + "slot_index": 2, + "localized_name": "vae" + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": null, + "localized_name": "positive" + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": null, + "localized_name": "negative" + } + ], + "outputs": [ + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "links": [], + "shape": 3, + "slot_index": 0, + "localized_name": "basic_pipe" + } + ], + "properties": { + "Node name for S&R": "ToBasicPipe" + }, + "color": "#222", + "bgcolor": "#000", + "index": 3 + } + ], + "links": [ + [ + 0, + 1, + 1, + 0, + 9, + "CLIP" + ], + [ + 0, + 1, + 2, + 0, + 9, + "CLIP" + ], + [ + 0, + 0, + 3, + 0, + 9, + "MODEL" + ], + [ + 0, + 1, + 3, + 1, + 9, + "CLIP" + ], + [ + 0, + 2, + 3, + 2, + 9, + "VAE" + ], + [ + 1, + 0, + 3, + 3, + 10, + "CONDITIONING" + ], + [ + 2, + 0, + 3, + 4, + 11, + "CONDITIONING" + ] + ], + "external": [ + [ + 3, + 0, + "BASIC_PIPE" + ] + ] + } + }, + "controller_panel": { + "controllers": {}, + "hidden": true, + "highlight": true, + "version": 2, + "default_order": [] + }, + "ds": { + "scale": 0.7513148009015777, + "offset": [ + 158.41700000000017, + 158.82600000000025 + ] + }, + "node_versions": { + "comfyui-impact-pack": "1ae7cae2df8cca06027edfa3a24512671239d6c4", + "comfyui-impact-subpack": "74db20c95eca152a6d686c914edc0ef4e4762cb8", + "comfy-core": "0.3.14" + }, + "ue_links": [], + "VHS_latentpreview": false, + "VHS_latentpreviewrate": 0, + "VHS_MetadataImage": true, + "VHS_KeepIntermediate": true + }, + "version": 0.4 +} \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/4-MakeTileSEGS-Upscale.jpg b/custom_nodes/comfyui-impact-pack/example_workflows/4-MakeTileSEGS-Upscale.jpg new file mode 100644 index 0000000000000000000000000000000000000000..307f1959556bbbfe8e117b3f31e9b9867eaff30d --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/4-MakeTileSEGS-Upscale.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b1d3d9a3563821ba82a6277610e83ec2fe20473679ebb25c70c811a252e06ecd +size 108834 diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/4-MakeTileSEGS-Upscale.json b/custom_nodes/comfyui-impact-pack/example_workflows/4-MakeTileSEGS-Upscale.json new file mode 100644 index 0000000000000000000000000000000000000000..902e5a7b10adfa5e7ab53439b3ae84616443dd28 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/4-MakeTileSEGS-Upscale.json @@ -0,0 +1,1627 @@ +{ + "last_node_id": 67, + "last_link_id": 115, + "nodes": [ + { + "id": 31, + "type": "Reroute", + "pos": [ + 1170, + 730 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 15, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 61 + } + ], + "outputs": [ + { + "name": "", + "type": "IMAGE", + "links": [ + 59, + 60 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 32, + "type": "SAMLoader", + "pos": [ + -160, + 840 + ], + "size": [ + 315, + 82 + ], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "SAM_MODEL", + "type": "SAM_MODEL", + "shape": 3, + "links": [ + 62 + ] + } + ], + "properties": { + "Node name for S&R": "SAMLoader" + }, + "widgets_values": [ + "sam_vit_b_01ec64.pth", + "AUTO" + ] + }, + { + "id": 24, + "type": "UltralyticsDetectorProvider", + "pos": [ + -160, + 700 + ], + "size": [ + 315, + 78 + ], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "BBOX_DETECTOR", + "type": "BBOX_DETECTOR", + "shape": 3, + "links": [ + 35 + ] + }, + { + "name": "SEGM_DETECTOR", + "type": "SEGM_DETECTOR", + "shape": 3, + "links": [], + "slot_index": 1 + } + ], + "properties": { + "Node name for S&R": "UltralyticsDetectorProvider" + }, + "widgets_values": [ + "segm/person_yolov8m-seg.pt" + ] + }, + { + "id": 9, + "type": "ImageScaleBy", + "pos": [ + 280, + 290 + ], + "size": [ + 315, + 82 + ], + "flags": {}, + "order": 12, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 8, + "slot_index": 0 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 3, + "links": [ + 10, + 28 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "ImageScaleBy" + }, + "widgets_values": [ + "lanczos", + 2 + ] + }, + { + "id": 52, + "type": "Reroute", + "pos": [ + 1816.5716552734375, + 473.7144470214844 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 21, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 106 + } + ], + "outputs": [ + { + "name": "", + "type": "SEGS", + "links": [ + 98 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 53, + "type": "Reroute", + "pos": [ + 1180, + 1540 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 19, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 110 + } + ], + "outputs": [ + { + "name": "", + "type": "SEGS", + "links": [ + 100 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 19, + "type": "workflow>MAKE_BASIC_PIPE", + "pos": [ + 1440, + 850 + ], + "size": [ + 420, + 170 + ], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "shape": 3, + "links": [ + 76 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "workflow/MAKE_BASIC_PIPE" + }, + "widgets_values": [ + "SDXL/MOHAWK_v20BackedVAE.safetensors", + "photograph of a girl, metalic robotic body, sun rising, snow field, hdr, cropped,", + "deformed, blurry, leather, fabric\n" + ] + }, + { + "id": 16, + "type": "PreviewImage", + "pos": [ + 2990, + 730 + ], + "size": [ + 610.069580078125, + 774.6857299804688 + ], + "flags": {}, + "order": 25, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 96 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [] + }, + { + "id": 54, + "type": "Reroute", + "pos": [ + 2390, + 1540 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 22, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 100 + } + ], + "outputs": [ + { + "name": "", + "type": "SEGS", + "links": [ + 101 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 51, + "type": "DetailerForEachDebugPipe", + "pos": [ + 2510, + 730 + ], + "size": [ + 410, + 996 + ], + "flags": {}, + "order": 24, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 95, + "slot_index": 0 + }, + { + "name": "segs", + "type": "SEGS", + "link": 101 + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "link": 94 + }, + { + "name": "detailer_hook", + "type": "DETAILER_HOOK", + "shape": 7, + "link": null + }, + { + "name": "refiner_basic_pipe_opt", + "type": "BASIC_PIPE", + "shape": 7, + "link": null + }, + { + "name": "scheduler_func_opt", + "type": "SCHEDULER_FUNC", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "image", + "type": "IMAGE", + "shape": 3, + "links": [ + 96 + ], + "slot_index": 0 + }, + { + "name": "segs", + "type": "SEGS", + "shape": 3, + "links": null + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "shape": 3, + "links": null + }, + { + "name": "cropped", + "type": "IMAGE", + "shape": 6, + "links": null + }, + { + "name": "cropped_refined", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 4 + }, + { + "name": "cropped_refined_alpha", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 5 + }, + { + "name": "cnet_images", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 6 + } + ], + "properties": { + "Node name for S&R": "DetailerForEachDebugPipe" + }, + "widgets_values": [ + 64, + true, + 1024, + 522790177337692, + "fixed", + 20, + 8, + "dpmpp_3m_sde_gpu", + "karras", + 0.4, + 10, + true, + true, + "[CONCAT] red double bun, metalic arm, zoey", + 0.2, + 1, + false, + 50, + false, + false + ] + }, + { + "id": 20, + "type": "Reroute", + "pos": [ + 660, + 730 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 14, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 28 + } + ], + "outputs": [ + { + "name": "", + "type": "IMAGE", + "links": [ + 61, + 107, + 111 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 28, + "type": "SEGSPreview", + "pos": [ + 1279, + 1610 + ], + "size": [ + 315, + 314 + ], + "flags": {}, + "order": 18, + "mode": 0, + "inputs": [ + { + "name": "segs", + "type": "SEGS", + "link": 109, + "slot_index": 0 + }, + { + "name": "fallback_image_opt", + "type": "IMAGE", + "shape": 7, + "link": 59, + "slot_index": 1 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "SEGSPreview" + }, + "widgets_values": [ + true, + 0.1 + ] + }, + { + "id": 56, + "type": "ImpactMakeTileSEGS", + "pos": [ + 780, + 470 + ], + "size": [ + 315, + 218 + ], + "flags": {}, + "order": 17, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 111 + }, + { + "name": "filter_in_segs_opt", + "type": "SEGS", + "shape": 7, + "link": null + }, + { + "name": "filter_out_segs_opt", + "type": "SEGS", + "shape": 7, + "link": 114 + } + ], + "outputs": [ + { + "name": "SEGS", + "type": "SEGS", + "shape": 3, + "links": [ + 105, + 106 + ] + } + ], + "properties": { + "Node name for S&R": "ImpactMakeTileSEGS" + }, + "widgets_values": [ + 768, + 1.5, + 200, + 30, + 0.7000000000000001, + "Reuse fast" + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 6, + "type": "SEGSPreview", + "pos": [ + 1292, + 268 + ], + "size": [ + 430.35296630859375, + 388.4536437988281 + ], + "flags": {}, + "order": 20, + "mode": 0, + "inputs": [ + { + "name": "segs", + "type": "SEGS", + "link": 105, + "slot_index": 0 + }, + { + "name": "fallback_image_opt", + "type": "IMAGE", + "shape": 7, + "link": 10, + "slot_index": 1 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "SEGSPreview" + }, + "widgets_values": [ + true, + 0.1 + ] + }, + { + "id": 60, + "type": "Note", + "pos": [ + -1033, + 292 + ], + "size": [ + 638.3837890625, + 178.84756469726562 + ], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [], + "outputs": [], + "title": "1.Intro", + "properties": { + "text": "" + }, + "widgets_values": [ + "This video demonstrates how to apply the newly added \"Make Tile SEGS\" in the Impact Pack to upscale using the upscale method.\n\n\"Make Tile SEGS\" node splits the image into tiles and creates SEGS.\n\nBy using this, you can mimic the tile-based upscale function and, if the detected SEGS is too large, you can also split it for detailing." + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 2, + "type": "LoadImage", + "pos": [ + -160, + 290 + ], + "size": [ + 315, + 314 + ], + "flags": {}, + "order": 4, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 3, + "links": [ + 8, + 34 + ], + "slot_index": 0 + }, + { + "name": "MASK", + "type": "MASK", + "shape": 3, + "links": null + } + ], + "properties": { + "Node name for S&R": "LoadImage" + }, + "widgets_values": [ + "20240107_013.webp", + "image" + ] + }, + { + "id": 62, + "type": "Note", + "pos": [ + 190, + 60 + ], + "size": [ + 396.33758544921875, + 127.46672821044922 + ], + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [], + "outputs": [], + "title": "2. Simple Upscale", + "properties": { + "text": "" + }, + "widgets_values": [ + "First, let's upscale the original 1024x1536 image to double its size.\n\nSimply upscale by 2x using the \"Upscale Image Scale By\". \nThe result will, of course, be blurry.\n" + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 61, + "type": "Note", + "pos": [ + 780, + 35 + ], + "size": [ + 677.756591796875, + 157.3253173828125 + ], + "flags": {}, + "order": 6, + "mode": 0, + "inputs": [], + "outputs": [], + "title": "3. Make Tile SEGS", + "properties": { + "text": "" + }, + "widgets_values": [ + "Let's process this image into SEGS using \"Make Tile SEGS\".\n\nYou can see that SEGS is structured so that every part of the image can be included in the mask area.\n\nUnlike the traditional tile upscaler, this method uses Detailer, so you can improve tile heterogeneity using the 'crop_factor'.\n\nAlso, setting 'mask_irregularity' to 0.7 will make the mask border irregular, improving the heterogeneity of the junctions." + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 63, + "type": "Note", + "pos": [ + -108, + 1056 + ], + "size": [ + 709.2979736328125, + 143.4364013671875 + ], + "flags": {}, + "order": 7, + "mode": 0, + "inputs": [], + "outputs": [], + "title": "4.Human SEGS", + "properties": { + "text": "" + }, + "widgets_values": [ + "Next, let's separate the background and the person to alleviate the noticeable artifacts, especially in the case of humans.\n\nApply the person ultralytics model to the \"Simple Detector\" to create SEGS containing the entire person.\n\nConnect the SEGS to the 'filter_out_segs_opt' in one \"Make Tile SEGS\" node, \nand in the other \"Make Tile SEGS\" node, connect it to the 'filter_in_segs_opt'." + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 65, + "type": "Note", + "pos": [ + 776, + 803 + ], + "size": [ + 620.825927734375, + 163.94039916992188 + ], + "flags": {}, + "order": 8, + "mode": 0, + "inputs": [], + "outputs": [], + "title": "5. filter_out_segs_opt", + "properties": { + "text": "" + }, + "widgets_values": [ + "The node connected to 'filter_out_segs_opt' creates SEGS excluding the mask of the input SEGS, allowing you to detail the background tiles.\n\n'min_overlap' determines how much the masks of each SEGS should overlap, and 'filter_segs_dilation' dilates the mask of the input SEGS.\n\nIncreasing 'filter_segs_dilation' in 'filter_out_segs_opt' generates masks further away from the person." + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 66, + "type": "Note", + "pos": [ + 814, + 2007 + ], + "size": [ + 620.825927734375, + 163.94039916992188 + ], + "flags": {}, + "order": 9, + "mode": 0, + "inputs": [], + "outputs": [], + "title": "6. filter_in_segs_opt", + "properties": { + "text": "" + }, + "widgets_values": [ + "On the other hand, the node connected to 'filter_in_segs_opt' creates SEGS with masks overlapping the input SEGS, allowing you to detail the person.\n\nSince detailing the person requires more attention than the background, increase 'bbox_size' to avoid creating small pieces, and increase 'min_overlap' to reduce junction artifacts and allow overlapping detailing.\n" + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 67, + "type": "Note", + "pos": [ + 1955, + 1805 + ], + "size": [ + 620.825927734375, + 163.94039916992188 + ], + "flags": {}, + "order": 10, + "mode": 0, + "inputs": [], + "outputs": [], + "title": "7. Detailing", + "properties": { + "text": "" + }, + "widgets_values": [ + "Now, using the SEGS created in this way, let's improve the upscaled image using two Detailer nodes.\n\nAlthough you can handle this with \"SEGS Concat\", separating into two Detailer nodes allows for separate options for background and person detailing.\n\nThis way, when modifying the detailing options for a person, you can prevent the recalculation of background detailing.\n" + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 64, + "type": "Note", + "pos": [ + 2994, + 500 + ], + "size": [ + 620.825927734375, + 163.94039916992188 + ], + "flags": {}, + "order": 11, + "mode": 0, + "inputs": [], + "outputs": [], + "title": "8. Result", + "properties": { + "text": "" + }, + "widgets_values": [ + "It seems that the image has upscaled well without significant artifacts in the 2048x3072 size." + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 10, + "type": "DetailerForEachDebugPipe", + "pos": [ + 1960, + 730 + ], + "size": [ + 410, + 996 + ], + "flags": {}, + "order": 23, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 60, + "slot_index": 0 + }, + { + "name": "segs", + "type": "SEGS", + "link": 98 + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "link": 76 + }, + { + "name": "detailer_hook", + "type": "DETAILER_HOOK", + "shape": 7, + "link": null + }, + { + "name": "refiner_basic_pipe_opt", + "type": "BASIC_PIPE", + "shape": 7, + "link": null + }, + { + "name": "scheduler_func_opt", + "type": "SCHEDULER_FUNC", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "image", + "type": "IMAGE", + "shape": 3, + "links": [ + 95 + ], + "slot_index": 0 + }, + { + "name": "segs", + "type": "SEGS", + "shape": 3, + "links": null + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "shape": 3, + "links": [ + 94 + ], + "slot_index": 2 + }, + { + "name": "cropped", + "type": "IMAGE", + "shape": 6, + "links": null + }, + { + "name": "cropped_refined", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 4 + }, + { + "name": "cropped_refined_alpha", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 5 + }, + { + "name": "cnet_images", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 6 + } + ], + "properties": { + "Node name for S&R": "DetailerForEachDebugPipe" + }, + "widgets_values": [ + 64, + true, + 1024, + 522790177337686, + "fixed", + 20, + 8, + "dpmpp_2m_sde_gpu", + "karras", + 0.46, + 10, + true, + true, + "", + 0.2, + 1, + false, + 10, + false, + false + ] + }, + { + "id": 57, + "type": "ImpactMakeTileSEGS", + "pos": [ + 820, + 1610 + ], + "size": [ + 315, + 218 + ], + "flags": {}, + "order": 16, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 107 + }, + { + "name": "filter_in_segs_opt", + "type": "SEGS", + "shape": 7, + "link": 115 + }, + { + "name": "filter_out_segs_opt", + "type": "SEGS", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "SEGS", + "type": "SEGS", + "shape": 3, + "links": [ + 109, + 110 + ] + } + ], + "properties": { + "Node name for S&R": "ImpactMakeTileSEGS" + }, + "widgets_values": [ + 1200, + 1.4000000000000001, + 200, + 100, + 0.7000000000000001, + "Reuse fast" + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 22, + "type": "ImpactSimpleDetectorSEGS", + "pos": [ + 282, + 699 + ], + "size": [ + 315, + 310 + ], + "flags": {}, + "order": 13, + "mode": 0, + "inputs": [ + { + "name": "bbox_detector", + "type": "BBOX_DETECTOR", + "link": 35, + "slot_index": 0 + }, + { + "name": "image", + "type": "IMAGE", + "link": 34, + "slot_index": 1 + }, + { + "name": "sam_model_opt", + "type": "SAM_MODEL", + "shape": 7, + "link": 62, + "slot_index": 2 + }, + { + "name": "segm_detector_opt", + "type": "SEGM_DETECTOR", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "SEGS", + "type": "SEGS", + "shape": 3, + "links": [ + 114, + 115 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "ImpactSimpleDetectorSEGS" + }, + "widgets_values": [ + 0.5, + 0, + 3, + 10, + 0.5, + 0, + 0, + 0.7000000000000001, + 0 + ] + } + ], + "links": [ + [ + 8, + 2, + 0, + 9, + 0, + "IMAGE" + ], + [ + 10, + 9, + 0, + 6, + 1, + "IMAGE" + ], + [ + 28, + 9, + 0, + 20, + 0, + "*" + ], + [ + 34, + 2, + 0, + 22, + 1, + "IMAGE" + ], + [ + 35, + 24, + 0, + 22, + 0, + "BBOX_DETECTOR" + ], + [ + 59, + 31, + 0, + 28, + 1, + "IMAGE" + ], + [ + 60, + 31, + 0, + 10, + 0, + "IMAGE" + ], + [ + 61, + 20, + 0, + 31, + 0, + "*" + ], + [ + 62, + 32, + 0, + 22, + 2, + "SAM_MODEL" + ], + [ + 76, + 19, + 0, + 10, + 2, + "BASIC_PIPE" + ], + [ + 94, + 10, + 2, + 51, + 2, + "BASIC_PIPE" + ], + [ + 95, + 10, + 0, + 51, + 0, + "IMAGE" + ], + [ + 96, + 51, + 0, + 16, + 0, + "IMAGE" + ], + [ + 98, + 52, + 0, + 10, + 1, + "SEGS" + ], + [ + 100, + 53, + 0, + 54, + 0, + "*" + ], + [ + 101, + 54, + 0, + 51, + 1, + "SEGS" + ], + [ + 105, + 56, + 0, + 6, + 0, + "SEGS" + ], + [ + 106, + 56, + 0, + 52, + 0, + "*" + ], + [ + 107, + 20, + 0, + 57, + 0, + "IMAGE" + ], + [ + 109, + 57, + 0, + 28, + 0, + "SEGS" + ], + [ + 110, + 57, + 0, + 53, + 0, + "*" + ], + [ + 111, + 20, + 0, + 56, + 0, + "IMAGE" + ], + [ + 114, + 22, + 0, + 56, + 2, + "SEGS" + ], + [ + 115, + 22, + 0, + 57, + 1, + "SEGS" + ] + ], + "groups": [], + "config": {}, + "extra": { + "groupNodes": { + "MAKE_BASIC_PIPE": { + "nodes": [ + { + "type": "CheckpointLoaderSimple", + "pos": [ + -80, + 1100 + ], + "size": { + "0": 315, + "1": 98 + }, + "flags": {}, + "order": 0, + "mode": 0, + "outputs": [ + { + "name": "MODEL", + "type": "MODEL", + "links": [], + "shape": 3, + "slot_index": 0, + "localized_name": "MODEL" + }, + { + "name": "CLIP", + "type": "CLIP", + "links": [], + "shape": 3, + "slot_index": 1, + "localized_name": "CLIP" + }, + { + "name": "VAE", + "type": "VAE", + "links": [], + "shape": 3, + "slot_index": 2, + "localized_name": "VAE" + } + ], + "properties": { + "Node name for S&R": "CheckpointLoaderSimple" + }, + "widgets_values": [ + "SD1.5/majicmixRealistic_v7.safetensors" + ], + "index": 0, + "inputs": [] + }, + { + "type": "CLIPTextEncode", + "pos": [ + 455, + 1026 + ], + "size": { + "0": 210, + "1": 104.50106048583984 + }, + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": null, + "localized_name": "clip" + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [], + "shape": 3, + "slot_index": 0, + "localized_name": "CONDITIONING" + } + ], + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "photograph, 4k, hdr, cropped, 1girl sit, blur hair, pink bag" + ], + "index": 1 + }, + { + "type": "CLIPTextEncode", + "pos": [ + 456, + 1239 + ], + "size": { + "0": 210, + "1": 104.50106048583984 + }, + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": null, + "slot_index": 0, + "localized_name": "clip" + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [], + "shape": 3, + "localized_name": "CONDITIONING" + } + ], + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "deformed, blurry\n" + ], + "index": 2 + }, + { + "type": "ToBasicPipe", + "pos": [ + 800, + 1100 + ], + "size": { + "0": 241.79998779296875, + "1": 106 + }, + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [ + { + "name": "model", + "type": "MODEL", + "link": null, + "localized_name": "model" + }, + { + "name": "clip", + "type": "CLIP", + "link": null, + "slot_index": 1, + "localized_name": "clip" + }, + { + "name": "vae", + "type": "VAE", + "link": null, + "localized_name": "vae" + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": null, + "localized_name": "positive" + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": null, + "slot_index": 4, + "localized_name": "negative" + } + ], + "outputs": [ + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "links": [], + "shape": 3, + "slot_index": 0, + "localized_name": "basic_pipe" + } + ], + "properties": { + "Node name for S&R": "ToBasicPipe" + }, + "index": 3 + } + ], + "links": [ + [ + 0, + 1, + 1, + 0, + 11, + "CLIP" + ], + [ + 0, + 1, + 2, + 0, + 11, + "CLIP" + ], + [ + 0, + 0, + 3, + 0, + 11, + "MODEL" + ], + [ + 0, + 1, + 3, + 1, + 11, + "CLIP" + ], + [ + 0, + 2, + 3, + 2, + 11, + "VAE" + ], + [ + 1, + 0, + 3, + 3, + 13, + "CONDITIONING" + ], + [ + 2, + 0, + 3, + 4, + 14, + "CONDITIONING" + ] + ], + "external": [ + [ + 3, + 0, + "BASIC_PIPE" + ] + ] + } + }, + "controller_panel": { + "controllers": {}, + "hidden": true, + "highlight": true, + "version": 2, + "default_order": [] + }, + "ds": { + "scale": 1.4641000000000006, + "offset": { + "0": -481.44390869140625, + "1": -92.16561126708984 + } + }, + "node_versions": { + "comfyui-impact-pack": "1ae7cae2df8cca06027edfa3a24512671239d6c4", + "comfyui-impact-subpack": "74db20c95eca152a6d686c914edc0ef4e4762cb8", + "comfy-core": "0.3.14" + }, + "ue_links": [], + "VHS_latentpreview": false, + "VHS_latentpreviewrate": 0, + "VHS_MetadataImage": true, + "VHS_KeepIntermediate": true + }, + "version": 0.4 +} \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/5-PreviewDetailerHookProvider.jpg b/custom_nodes/comfyui-impact-pack/example_workflows/5-PreviewDetailerHookProvider.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c5fcb464503b3fee737e5eb22d6f5cf161feb34f --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/5-PreviewDetailerHookProvider.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f37a67d0b42e4d94950a4d27de403b022d684824571021c702196a2989bf8349 +size 68539 diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/5-PreviewDetailerHookProvider.json b/custom_nodes/comfyui-impact-pack/example_workflows/5-PreviewDetailerHookProvider.json new file mode 100644 index 0000000000000000000000000000000000000000..d5c88dbd46c0576070bfb55d6386589d7317143d --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/5-PreviewDetailerHookProvider.json @@ -0,0 +1,1629 @@ +{ + "last_node_id": 70, + "last_link_id": 125, + "nodes": [ + { + "id": 31, + "type": "Reroute", + "pos": [ + 1170, + 730 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 12, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 61 + } + ], + "outputs": [ + { + "name": "", + "type": "IMAGE", + "links": [ + 59, + 60 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 32, + "type": "SAMLoader", + "pos": [ + -160, + 840 + ], + "size": [ + 315, + 82 + ], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "SAM_MODEL", + "type": "SAM_MODEL", + "shape": 3, + "links": [ + 62 + ] + } + ], + "properties": { + "Node name for S&R": "SAMLoader" + }, + "widgets_values": [ + "sam_vit_b_01ec64.pth", + "AUTO" + ] + }, + { + "id": 24, + "type": "UltralyticsDetectorProvider", + "pos": [ + -160, + 700 + ], + "size": [ + 315, + 78 + ], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "BBOX_DETECTOR", + "type": "BBOX_DETECTOR", + "shape": 3, + "links": [ + 35 + ] + }, + { + "name": "SEGM_DETECTOR", + "type": "SEGM_DETECTOR", + "shape": 3, + "links": [], + "slot_index": 1 + } + ], + "properties": { + "Node name for S&R": "UltralyticsDetectorProvider" + }, + "widgets_values": [ + "segm/person_yolov8m-seg.pt" + ] + }, + { + "id": 53, + "type": "Reroute", + "pos": [ + 1180, + 1540 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 16, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 110 + } + ], + "outputs": [ + { + "name": "", + "type": "SEGS", + "links": [ + 100 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 20, + "type": "Reroute", + "pos": [ + 660, + 730 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 10, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 28 + } + ], + "outputs": [ + { + "name": "", + "type": "IMAGE", + "links": [ + 61, + 107, + 111 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 28, + "type": "SEGSPreview", + "pos": [ + 1279, + 1610 + ], + "size": [ + 315, + 314 + ], + "flags": {}, + "order": 15, + "mode": 0, + "inputs": [ + { + "name": "segs", + "type": "SEGS", + "link": 109, + "slot_index": 0 + }, + { + "name": "fallback_image_opt", + "type": "IMAGE", + "shape": 7, + "link": 59, + "slot_index": 1 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "SEGSPreview" + }, + "widgets_values": [ + true, + 0.1 + ] + }, + { + "id": 6, + "type": "SEGSPreview", + "pos": [ + 1292, + 268 + ], + "size": [ + 430.35296630859375, + 388.4536437988281 + ], + "flags": {}, + "order": 17, + "mode": 0, + "inputs": [ + { + "name": "segs", + "type": "SEGS", + "link": 105, + "slot_index": 0 + }, + { + "name": "fallback_image_opt", + "type": "IMAGE", + "shape": 7, + "link": 10, + "slot_index": 1 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "SEGSPreview" + }, + "widgets_values": [ + true, + 0.1 + ] + }, + { + "id": 57, + "type": "ImpactMakeTileSEGS", + "pos": [ + 820, + 1610 + ], + "size": [ + 315, + 218 + ], + "flags": {}, + "order": 13, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 107 + }, + { + "name": "filter_in_segs_opt", + "type": "SEGS", + "shape": 7, + "link": 115 + }, + { + "name": "filter_out_segs_opt", + "type": "SEGS", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "SEGS", + "type": "SEGS", + "shape": 3, + "links": [ + 109, + 110 + ] + } + ], + "properties": { + "Node name for S&R": "ImpactMakeTileSEGS" + }, + "widgets_values": [ + 1200, + 1.4000000000000001, + 200, + 100, + 0.7000000000000001, + "Reuse fast" + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 22, + "type": "ImpactSimpleDetectorSEGS", + "pos": [ + 282, + 699 + ], + "size": [ + 315, + 310 + ], + "flags": {}, + "order": 8, + "mode": 0, + "inputs": [ + { + "name": "bbox_detector", + "type": "BBOX_DETECTOR", + "link": 35, + "slot_index": 0 + }, + { + "name": "image", + "type": "IMAGE", + "link": 34, + "slot_index": 1 + }, + { + "name": "sam_model_opt", + "type": "SAM_MODEL", + "shape": 7, + "link": 62, + "slot_index": 2 + }, + { + "name": "segm_detector_opt", + "type": "SEGM_DETECTOR", + "shape": 7, + "link": null, + "slot_index": 3 + } + ], + "outputs": [ + { + "name": "SEGS", + "type": "SEGS", + "shape": 3, + "links": [ + 114, + 115 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "ImpactSimpleDetectorSEGS" + }, + "widgets_values": [ + 0.5, + 0, + 3, + 10, + 0.5, + 0, + 0, + 0.7000000000000001, + 0 + ] + }, + { + "id": 2, + "type": "LoadImage", + "pos": [ + -160, + 290 + ], + "size": [ + 315, + 314 + ], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 3, + "links": [ + 8, + 34 + ], + "slot_index": 0 + }, + { + "name": "MASK", + "type": "MASK", + "shape": 3, + "links": null + } + ], + "properties": { + "Node name for S&R": "LoadImage" + }, + "widgets_values": [ + "combination-2pass-original.png", + "image" + ] + }, + { + "id": 9, + "type": "ImageScaleBy", + "pos": [ + 280, + 290 + ], + "size": [ + 315, + 82 + ], + "flags": {}, + "order": 7, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 8, + "slot_index": 0 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 3, + "links": [ + 10, + 28 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "ImageScaleBy" + }, + "widgets_values": [ + "lanczos", + 3 + ] + }, + { + "id": 16, + "type": "PreviewImage", + "pos": [ + 2990, + 730 + ], + "size": [ + 610.069580078125, + 774.6857299804688 + ], + "flags": {}, + "order": 22, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 96 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [] + }, + { + "id": 68, + "type": "PreviewDetailerHookProvider", + "pos": [ + 943, + -1972 + ], + "size": [ + 1360.0478515625, + 1943.85986328125 + ], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "DETAILER_HOOK", + "type": "DETAILER_HOOK", + "shape": 3, + "links": [ + 120 + ], + "slot_index": 0 + }, + { + "name": "UPSCALER_HOOK", + "type": "UPSCALER_HOOK", + "links": null + } + ], + "title": "PreviewDetailerHookProvider - Live Preview", + "properties": { + "Node name for S&R": "PreviewDetailerHookProvider" + }, + "widgets_values": [ + 95 + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 69, + "type": "Reroute", + "pos": [ + 2360, + -1920 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 9, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "pos": [ + 37.5, + 0 + ], + "link": 120 + } + ], + "outputs": [ + { + "name": "", + "type": "DETAILER_HOOK", + "links": [ + 121 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": true + } + }, + { + "id": 19, + "type": "workflow>MAKE_BASIC_PIPE", + "pos": [ + 1440, + 850 + ], + "size": [ + 451.0836486816406, + 279.9571533203125 + ], + "flags": {}, + "order": 4, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "shape": 3, + "links": [ + 76 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "workflow/MAKE_BASIC_PIPE" + }, + "widgets_values": [ + "SDXL/MOHAWK_v20BackedVAE.safetensors", + "cinematic photograph of a girl is walking, cinematic lighting, white inddor", + "deformed, blurry, \n" + ] + }, + { + "id": 52, + "type": "Reroute", + "pos": [ + 2330, + 470 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 18, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 106 + } + ], + "outputs": [ + { + "name": "", + "type": "SEGS", + "links": [ + 119 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 54, + "type": "Reroute", + "pos": [ + 1780, + 1540 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 19, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 100 + } + ], + "outputs": [ + { + "name": "", + "type": "SEGS", + "links": [ + 118 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 56, + "type": "ImpactMakeTileSEGS", + "pos": [ + 780, + 470 + ], + "size": [ + 315, + 218 + ], + "flags": {}, + "order": 14, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 111 + }, + { + "name": "filter_in_segs_opt", + "type": "SEGS", + "shape": 7, + "link": null + }, + { + "name": "filter_out_segs_opt", + "type": "SEGS", + "shape": 7, + "link": 114 + } + ], + "outputs": [ + { + "name": "SEGS", + "type": "SEGS", + "shape": 3, + "links": [ + 105, + 106 + ] + } + ], + "properties": { + "Node name for S&R": "ImpactMakeTileSEGS" + }, + "widgets_values": [ + 768, + 1.5, + 200, + 0, + 0.7000000000000001, + "Reuse fast" + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 10, + "type": "DetailerForEachDebugPipe", + "pos": [ + 1960, + 730 + ], + "size": [ + 410, + 996 + ], + "flags": {}, + "order": 20, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 60, + "slot_index": 0 + }, + { + "name": "segs", + "type": "SEGS", + "link": 118 + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "link": 76 + }, + { + "name": "detailer_hook", + "type": "DETAILER_HOOK", + "shape": 7, + "link": 124, + "slot_index": 3 + }, + { + "name": "refiner_basic_pipe_opt", + "type": "BASIC_PIPE", + "shape": 7, + "link": null + }, + { + "name": "scheduler_func_opt", + "type": "SCHEDULER_FUNC", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "image", + "type": "IMAGE", + "shape": 3, + "links": [ + 95 + ], + "slot_index": 0 + }, + { + "name": "segs", + "type": "SEGS", + "shape": 3, + "links": null + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "shape": 3, + "links": [ + 94 + ], + "slot_index": 2 + }, + { + "name": "cropped", + "type": "IMAGE", + "shape": 6, + "links": null + }, + { + "name": "cropped_refined", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 4 + }, + { + "name": "cropped_refined_alpha", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 5 + }, + { + "name": "cnet_images", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 6 + } + ], + "title": "DetailerDebug (SEGS/pipe) - person", + "properties": { + "Node name for S&R": "DetailerForEachDebugPipe" + }, + "widgets_values": [ + 64, + true, + 1024, + 522790177337686, + "fixed", + 20, + 8, + "dpmpp_3m_sde_gpu", + "karras", + 0.45, + 10, + true, + true, + "", + 0.2, + 1, + false, + 10, + false, + false + ] + }, + { + "id": 51, + "type": "DetailerForEachDebugPipe", + "pos": [ + 2510, + 730 + ], + "size": [ + 410, + 996 + ], + "flags": {}, + "order": 21, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 95, + "slot_index": 0 + }, + { + "name": "segs", + "type": "SEGS", + "link": 119 + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "link": 94 + }, + { + "name": "detailer_hook", + "type": "DETAILER_HOOK", + "shape": 7, + "link": 125 + }, + { + "name": "refiner_basic_pipe_opt", + "type": "BASIC_PIPE", + "shape": 7, + "link": null + }, + { + "name": "scheduler_func_opt", + "type": "SCHEDULER_FUNC", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "image", + "type": "IMAGE", + "shape": 3, + "links": [ + 96 + ], + "slot_index": 0 + }, + { + "name": "segs", + "type": "SEGS", + "shape": 3, + "links": null + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "shape": 3, + "links": null + }, + { + "name": "cropped", + "type": "IMAGE", + "shape": 6, + "links": null + }, + { + "name": "cropped_refined", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 4 + }, + { + "name": "cropped_refined_alpha", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 5 + }, + { + "name": "cnet_images", + "type": "IMAGE", + "shape": 6, + "links": [], + "slot_index": 6 + } + ], + "title": "DetailerDebug (SEGS/pipe) - background", + "properties": { + "Node name for S&R": "DetailerForEachDebugPipe" + }, + "widgets_values": [ + 64, + true, + 1024, + 522790177337693, + "fixed", + 20, + 8, + "dpmpp_2m_sde_gpu", + "karras", + 0.4, + 10, + true, + true, + "[CONCAT] red double bun, metalic arm, zoey", + 0.2, + 1, + false, + 50, + false, + false + ] + }, + { + "id": 60, + "type": "Note", + "pos": [ + -1033, + 292 + ], + "size": [ + 638.3837890625, + 178.84756469726562 + ], + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [], + "outputs": [], + "title": "1.Intro", + "properties": { + "text": "" + }, + "widgets_values": [ + "Using nodes like Make Tile SEGS for Detailer work will result in processing SEGS within a large number of Detailer nodes.\n\nPreviewDetailerHookProvider is connected to Detailers to monitor intermediate processes.\n" + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 62, + "type": "Note", + "pos": [ + 364, + -1967 + ], + "size": [ + 552.4130859375, + 204.45199584960938 + ], + "flags": {}, + "order": 6, + "mode": 0, + "inputs": [], + "outputs": [], + "title": "2. PreviewDetailerHookProvider", + "properties": { + "text": "" + }, + "widgets_values": [ + "To add PreviewDetailerHookProvider, simply connect it to the detailer_hook input of the Detailer node you want to monitor.\n\nThis node can also be used in the Detailer For AnimateDiff node.\n\nHowever, since this node provides a preview hook for pasting onto the original image, it cannot be used in SEGSDetailer where there is no pasting step.\n\n\n\nNow let's give it a try." + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 70, + "type": "Reroute", + "pos": [ + 2360, + 310 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 11, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "pos": [ + 37.5, + 0 + ], + "link": 121 + } + ], + "outputs": [ + { + "name": "", + "type": "DETAILER_HOOK", + "links": [ + 124, + 125 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": true + } + } + ], + "links": [ + [ + 8, + 2, + 0, + 9, + 0, + "IMAGE" + ], + [ + 10, + 9, + 0, + 6, + 1, + "IMAGE" + ], + [ + 28, + 9, + 0, + 20, + 0, + "*" + ], + [ + 34, + 2, + 0, + 22, + 1, + "IMAGE" + ], + [ + 35, + 24, + 0, + 22, + 0, + "BBOX_DETECTOR" + ], + [ + 59, + 31, + 0, + 28, + 1, + "IMAGE" + ], + [ + 60, + 31, + 0, + 10, + 0, + "IMAGE" + ], + [ + 61, + 20, + 0, + 31, + 0, + "*" + ], + [ + 62, + 32, + 0, + 22, + 2, + "SAM_MODEL" + ], + [ + 76, + 19, + 0, + 10, + 2, + "BASIC_PIPE" + ], + [ + 94, + 10, + 2, + 51, + 2, + "BASIC_PIPE" + ], + [ + 95, + 10, + 0, + 51, + 0, + "IMAGE" + ], + [ + 96, + 51, + 0, + 16, + 0, + "IMAGE" + ], + [ + 100, + 53, + 0, + 54, + 0, + "*" + ], + [ + 105, + 56, + 0, + 6, + 0, + "SEGS" + ], + [ + 106, + 56, + 0, + 52, + 0, + "*" + ], + [ + 107, + 20, + 0, + 57, + 0, + "IMAGE" + ], + [ + 109, + 57, + 0, + 28, + 0, + "SEGS" + ], + [ + 110, + 57, + 0, + 53, + 0, + "*" + ], + [ + 111, + 20, + 0, + 56, + 0, + "IMAGE" + ], + [ + 114, + 22, + 0, + 56, + 2, + "SEGS" + ], + [ + 115, + 22, + 0, + 57, + 1, + "SEGS" + ], + [ + 118, + 54, + 0, + 10, + 1, + "SEGS" + ], + [ + 119, + 52, + 0, + 51, + 1, + "SEGS" + ], + [ + 120, + 68, + 0, + 69, + 0, + "*" + ], + [ + 121, + 69, + 0, + 70, + 0, + "*" + ], + [ + 124, + 70, + 0, + 10, + 3, + "DETAILER_HOOK" + ], + [ + 125, + 70, + 0, + 51, + 3, + "DETAILER_HOOK" + ] + ], + "groups": [], + "config": {}, + "extra": { + "groupNodes": { + "MAKE_BASIC_PIPE": { + "nodes": [ + { + "type": "CheckpointLoaderSimple", + "pos": [ + -80, + 1100 + ], + "size": { + "0": 315, + "1": 98 + }, + "flags": {}, + "order": 0, + "mode": 0, + "outputs": [ + { + "name": "MODEL", + "type": "MODEL", + "links": [], + "shape": 3, + "slot_index": 0, + "localized_name": "MODEL" + }, + { + "name": "CLIP", + "type": "CLIP", + "links": [], + "shape": 3, + "slot_index": 1, + "localized_name": "CLIP" + }, + { + "name": "VAE", + "type": "VAE", + "links": [], + "shape": 3, + "slot_index": 2, + "localized_name": "VAE" + } + ], + "properties": { + "Node name for S&R": "CheckpointLoaderSimple" + }, + "widgets_values": [ + "SD1.5/majicmixRealistic_v7.safetensors" + ], + "index": 0, + "inputs": [] + }, + { + "type": "CLIPTextEncode", + "pos": [ + 455, + 1026 + ], + "size": { + "0": 210, + "1": 104.50106048583984 + }, + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": null, + "localized_name": "clip" + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [], + "shape": 3, + "slot_index": 0, + "localized_name": "CONDITIONING" + } + ], + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "photograph, 4k, hdr, cropped, 1girl sit, blur hair, pink bag" + ], + "index": 1 + }, + { + "type": "CLIPTextEncode", + "pos": [ + 456, + 1239 + ], + "size": { + "0": 210, + "1": 104.50106048583984 + }, + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": null, + "slot_index": 0, + "localized_name": "clip" + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [], + "shape": 3, + "localized_name": "CONDITIONING" + } + ], + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "deformed, blurry\n" + ], + "index": 2 + }, + { + "type": "ToBasicPipe", + "pos": [ + 800, + 1100 + ], + "size": { + "0": 241.79998779296875, + "1": 106 + }, + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [ + { + "name": "model", + "type": "MODEL", + "link": null, + "localized_name": "model" + }, + { + "name": "clip", + "type": "CLIP", + "link": null, + "slot_index": 1, + "localized_name": "clip" + }, + { + "name": "vae", + "type": "VAE", + "link": null, + "localized_name": "vae" + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": null, + "localized_name": "positive" + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": null, + "slot_index": 4, + "localized_name": "negative" + } + ], + "outputs": [ + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "links": [], + "shape": 3, + "slot_index": 0, + "localized_name": "basic_pipe" + } + ], + "properties": { + "Node name for S&R": "ToBasicPipe" + }, + "index": 3 + } + ], + "links": [ + [ + 0, + 1, + 1, + 0, + 11, + "CLIP" + ], + [ + 0, + 1, + 2, + 0, + 11, + "CLIP" + ], + [ + 0, + 0, + 3, + 0, + 11, + "MODEL" + ], + [ + 0, + 1, + 3, + 1, + 11, + "CLIP" + ], + [ + 0, + 2, + 3, + 2, + 11, + "VAE" + ], + [ + 1, + 0, + 3, + 3, + 13, + "CONDITIONING" + ], + [ + 2, + 0, + 3, + 4, + 14, + "CONDITIONING" + ] + ], + "external": [ + [ + 3, + 0, + "BASIC_PIPE" + ] + ] + } + }, + "controller_panel": { + "controllers": {}, + "hidden": true, + "highlight": true, + "version": 2, + "default_order": [] + }, + "ds": { + "scale": 0.620921323059155, + "offset": [ + 432.38467086326943, + 608.3387630215522 + ] + }, + "node_versions": { + "comfyui-impact-pack": "1ae7cae2df8cca06027edfa3a24512671239d6c4", + "comfyui-impact-subpack": "74db20c95eca152a6d686c914edc0ef4e4762cb8", + "comfy-core": "0.3.14" + }, + "ue_links": [], + "VHS_latentpreview": false, + "VHS_latentpreviewrate": 0, + "VHS_MetadataImage": true, + "VHS_KeepIntermediate": true + }, + "version": 0.4 +} \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/5-prompt-per-tile.jpg b/custom_nodes/comfyui-impact-pack/example_workflows/5-prompt-per-tile.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5b5447c266791208d4e2246cbac098480d3e1179 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/5-prompt-per-tile.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:232bde22503e087e47c391bb91bbcc830c9882889ac715c1ec139667ea3d2c03 +size 130918 diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/5-prompt-per-tile.json b/custom_nodes/comfyui-impact-pack/example_workflows/5-prompt-per-tile.json new file mode 100644 index 0000000000000000000000000000000000000000..15a6b8d003579ea1b815ac835f9cddefd5b39ee3 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/5-prompt-per-tile.json @@ -0,0 +1,1290 @@ +{ + "last_node_id": 30, + "last_link_id": 50, + "nodes": [ + { + "id": 3, + "type": "KSampler", + "pos": [ + -160, + -150 + ], + "size": [ + 315, + 474 + ], + "flags": {}, + "order": 4, + "mode": 0, + "inputs": [ + { + "name": "model", + "type": "MODEL", + "link": 1 + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": 4 + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": 6 + }, + { + "name": "latent_image", + "type": "LATENT", + "link": 2 + } + ], + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [ + 7 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "KSampler" + }, + "widgets_values": [ + 2, + "fixed", + 20, + 7, + "dpmpp_2m", + "karras", + 1 + ], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 4, + "type": "CheckpointLoaderSimple", + "pos": [ + -580, + -300 + ], + "size": [ + 315, + 98 + ], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "MODEL", + "type": "MODEL", + "links": [ + 1, + 25 + ], + "slot_index": 0 + }, + { + "name": "CLIP", + "type": "CLIP", + "links": [ + 3, + 5, + 26 + ], + "slot_index": 1 + }, + { + "name": "VAE", + "type": "VAE", + "links": [ + 8, + 27 + ], + "slot_index": 2 + } + ], + "properties": { + "Node name for S&R": "CheckpointLoaderSimple" + }, + "widgets_values": [ + "SD1.5/noosphere_v42.safetensors" + ], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 5, + "type": "EmptyLatentImage", + "pos": [ + -567, + 312 + ], + "size": [ + 315, + 106 + ], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [ + 2 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "EmptyLatentImage" + }, + "widgets_values": [ + 512, + 768, + 1 + ], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 6, + "type": "CLIPTextEncode", + "pos": [ + -610, + -150 + ], + "size": [ + 422.84503173828125, + 164.31304931640625 + ], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": 3 + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [ + 4, + 28 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "photo of a blonde girl and a dark haired man with beard, front view, detailed faces, high details, realistic, nature background, high saturation" + ], + "color": "#232", + "bgcolor": "#353" + }, + { + "id": 7, + "type": "CLIPTextEncode", + "pos": [ + -611, + 66 + ], + "size": [ + 425.27801513671875, + 180.6060791015625 + ], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": 5 + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [ + 6, + 29 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "text, watermark, nsfw" + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 8, + "type": "VAEDecode", + "pos": [ + -115, + -271 + ], + "size": [ + 210, + 46 + ], + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [ + { + "name": "samples", + "type": "LATENT", + "link": 7 + }, + { + "name": "vae", + "type": "VAE", + "link": 8 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [ + 21, + 22, + 30 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "VAEDecode" + }, + "widgets_values": [], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 10, + "type": "ImpactMakeTileSEGS", + "pos": [ + 840, + -90 + ], + "size": [ + 282.6341552734375, + 218 + ], + "flags": {}, + "order": 10, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 31 + }, + { + "name": "filter_in_segs_opt", + "type": "SEGS", + "shape": 7, + "link": null + }, + { + "name": "filter_out_segs_opt", + "type": "SEGS", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "SEGS", + "type": "SEGS", + "shape": 3, + "links": [ + 14, + 15 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "ImpactMakeTileSEGS" + }, + "widgets_values": [ + 704, + 1.1, + 4, + 0, + 0, + "Reuse fast" + ], + "color": "#2a363b", + "bgcolor": "#3f5159" + }, + { + "id": 11, + "type": "WD14Tagger|pysssss", + "pos": [ + 1901, + -282 + ], + "size": [ + 276.18115234375, + 470 + ], + "flags": { + "collapsed": false + }, + "order": 12, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 10 + } + ], + "outputs": [ + { + "name": "STRING", + "type": "STRING", + "shape": 6, + "links": [ + 39 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "WD14Tagger|pysssss" + }, + "widgets_values": [ + "wd-v1-4-moat-tagger-v2", + 0.35000000000000003, + 0.85, + true, + false, + "" + ], + "color": "#332922", + "bgcolor": "#593930" + }, + { + "id": 12, + "type": "DetailerForEach", + "pos": [ + 2881.51708984375, + -286.8627624511719 + ], + "size": [ + 310.9673767089844, + 790 + ], + "flags": {}, + "order": 17, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 32 + }, + { + "name": "segs", + "type": "SEGS", + "link": 12 + }, + { + "name": "model", + "type": "MODEL", + "link": 25 + }, + { + "name": "clip", + "type": "CLIP", + "link": 26 + }, + { + "name": "vae", + "type": "VAE", + "link": 27 + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": 28 + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": 29 + }, + { + "name": "detailer_hook", + "type": "DETAILER_HOOK", + "shape": 7, + "link": null + }, + { + "name": "wildcard", + "type": "STRING", + "widget": { + "name": "wildcard" + }, + "link": 49 + }, + { + "name": "scheduler_func_opt", + "type": "SCHEDULER_FUNC", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 3, + "links": [ + 17 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "DetailerForEach" + }, + "widgets_values": [ + 768, + true, + 1024, + 20, + "fixed", + 20, + 3.5, + "dpmpp_2m_sde_gpu", + "karras", + 0.5, + 16, + true, + true, + "", + 1, + false, + 16, + false, + false + ], + "color": "#2a363b", + "bgcolor": "#3f5159" + }, + { + "id": 13, + "type": "WD14Tagger|pysssss", + "pos": [ + 1388, + -240 + ], + "size": [ + 290, + 240 + ], + "flags": { + "collapsed": false + }, + "order": 7, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 22 + } + ], + "outputs": [ + { + "name": "STRING", + "type": "STRING", + "shape": 6, + "links": [ + 34 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "WD14Tagger|pysssss" + }, + "widgets_values": [ + "wd-v1-4-moat-tagger-v2", + 0.35000000000000003, + 0.85, + true, + false, + "" + ], + "color": "#332922", + "bgcolor": "#593930" + }, + { + "id": 14, + "type": "SEGSToImageList", + "pos": [ + 830, + 230 + ], + "size": [ + 276.6341552734375, + 46 + ], + "flags": {}, + "order": 11, + "mode": 0, + "inputs": [ + { + "name": "segs", + "type": "SEGS", + "link": 14 + }, + { + "name": "fallback_image_opt", + "type": "IMAGE", + "shape": 7, + "link": 33 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 6, + "links": [ + 10, + 18 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "SEGSToImageList" + }, + "widgets_values": [], + "color": "#2a363b", + "bgcolor": "#3f5159" + }, + { + "id": 15, + "type": "ImpactSEGSLabelAssign", + "pos": [ + 2409.8916015625, + 36.29731369018555 + ], + "size": [ + 283.6341552734375, + 103.9290771484375 + ], + "flags": {}, + "order": 16, + "mode": 0, + "inputs": [ + { + "name": "segs", + "type": "SEGS", + "link": 15 + }, + { + "name": "labels", + "type": "STRING", + "widget": { + "name": "labels" + }, + "link": 50 + } + ], + "outputs": [ + { + "name": "SEGS", + "type": "SEGS", + "shape": 3, + "links": [ + 12 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "ImpactSEGSLabelAssign" + }, + "widgets_values": [ + "" + ], + "color": "#2a363b", + "bgcolor": "#3f5159" + }, + { + "id": 16, + "type": "PreviewImage", + "pos": [ + 3270, + -287 + ], + "size": [ + 842.0664672851562, + 1217.6240234375 + ], + "flags": {}, + "order": 18, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 17 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#2a363b", + "bgcolor": "#3f5159" + }, + { + "id": 17, + "type": "PreviewImage", + "pos": [ + 788, + 339 + ], + "size": [ + 421.1688537597656, + 448.1822509765625 + ], + "flags": {}, + "order": 13, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 18 + } + ], + "outputs": [], + "title": "Preview Tiles", + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#2a363b", + "bgcolor": "#3f5159" + }, + { + "id": 19, + "type": "PreviewImage", + "pos": [ + 173, + -283 + ], + "size": [ + 475.25579833984375, + 668.4122924804688 + ], + "flags": {}, + "order": 6, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 21 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 21, + "type": "ImageScaleBy", + "pos": [ + 840, + -270 + ], + "size": [ + 315, + 82 + ], + "flags": {}, + "order": 8, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 30 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "shape": 3, + "links": [ + 31, + 32, + 33 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "ImageScaleBy" + }, + "widgets_values": [ + "bicubic", + 2.5 + ], + "color": "#233", + "bgcolor": "#355" + }, + { + "id": 25, + "type": "StringListToString", + "pos": [ + 1375, + 131 + ], + "size": [ + 315, + 82 + ], + "flags": {}, + "order": 9, + "mode": 0, + "inputs": [ + { + "name": "string_list", + "type": "STRING", + "widget": { + "name": "string_list" + }, + "link": 34 + } + ], + "outputs": [ + { + "name": "STRING", + "type": "STRING", + "shape": 3, + "links": [ + 47 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "StringListToString" + }, + "widgets_values": [ + "", + "" + ], + "color": "#332922", + "bgcolor": "#593930" + }, + { + "id": 26, + "type": "StringListToString", + "pos": [ + 1913, + 259 + ], + "size": [ + 268.8372497558594, + 58 + ], + "flags": {}, + "order": 14, + "mode": 0, + "inputs": [ + { + "name": "string_list", + "type": "STRING", + "widget": { + "name": "string_list" + }, + "link": 39 + } + ], + "outputs": [ + { + "name": "STRING", + "type": "STRING", + "shape": 3, + "links": [ + 48 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "StringListToString" + }, + "widgets_values": [ + "\\n", + "" + ], + "color": "#332922", + "bgcolor": "#593930" + }, + { + "id": 30, + "type": "WildcardPromptFromString", + "pos": [ + 2396.2451171875, + -266.2974548339844 + ], + "size": [ + 315, + 198 + ], + "flags": {}, + "order": 15, + "mode": 0, + "inputs": [ + { + "name": "string", + "type": "STRING", + "widget": { + "name": "string" + }, + "link": 48 + }, + { + "name": "restrict_to_tags", + "type": "STRING", + "widget": { + "name": "restrict_to_tags" + }, + "link": 47 + } + ], + "outputs": [ + { + "name": "wildcard", + "type": "STRING", + "shape": 3, + "links": [ + 49 + ], + "slot_index": 0 + }, + { + "name": "segs_labels", + "type": "STRING", + "shape": 3, + "links": [ + 50 + ], + "slot_index": 1 + } + ], + "properties": { + "Node name for S&R": "WildcardPromptFromString" + }, + "widgets_values": [ + "", + "\\n", + "", + ", realistic, high details, high saturation", + "", + "1girl, 1boy, 2girls, multiple girls, realistic" + ], + "color": "#2a363b", + "bgcolor": "#3f5159" + } + ], + "links": [ + [ + 1, + 4, + 0, + 3, + 0, + "MODEL" + ], + [ + 2, + 5, + 0, + 3, + 3, + "LATENT" + ], + [ + 3, + 4, + 1, + 6, + 0, + "CLIP" + ], + [ + 4, + 6, + 0, + 3, + 1, + "CONDITIONING" + ], + [ + 5, + 4, + 1, + 7, + 0, + "CLIP" + ], + [ + 6, + 7, + 0, + 3, + 2, + "CONDITIONING" + ], + [ + 7, + 3, + 0, + 8, + 0, + "LATENT" + ], + [ + 8, + 4, + 2, + 8, + 1, + "VAE" + ], + [ + 10, + 14, + 0, + 11, + 0, + "IMAGE" + ], + [ + 12, + 15, + 0, + 12, + 1, + "SEGS" + ], + [ + 14, + 10, + 0, + 14, + 0, + "SEGS" + ], + [ + 15, + 10, + 0, + 15, + 0, + "SEGS" + ], + [ + 17, + 12, + 0, + 16, + 0, + "IMAGE" + ], + [ + 18, + 14, + 0, + 17, + 0, + "IMAGE" + ], + [ + 21, + 8, + 0, + 19, + 0, + "IMAGE" + ], + [ + 22, + 8, + 0, + 13, + 0, + "IMAGE" + ], + [ + 25, + 4, + 0, + 12, + 2, + "MODEL" + ], + [ + 26, + 4, + 1, + 12, + 3, + "CLIP" + ], + [ + 27, + 4, + 2, + 12, + 4, + "VAE" + ], + [ + 28, + 6, + 0, + 12, + 5, + "CONDITIONING" + ], + [ + 29, + 7, + 0, + 12, + 6, + "CONDITIONING" + ], + [ + 30, + 8, + 0, + 21, + 0, + "IMAGE" + ], + [ + 31, + 21, + 0, + 10, + 0, + "IMAGE" + ], + [ + 32, + 21, + 0, + 12, + 0, + "IMAGE" + ], + [ + 33, + 21, + 0, + 14, + 1, + "IMAGE" + ], + [ + 34, + 13, + 0, + 25, + 0, + "STRING" + ], + [ + 39, + 11, + 0, + 26, + 0, + "STRING" + ], + [ + 47, + 25, + 0, + 30, + 1, + "STRING" + ], + [ + 48, + 26, + 0, + 30, + 0, + "STRING" + ], + [ + 49, + 30, + 0, + 12, + 8, + "STRING" + ], + [ + 50, + 30, + 1, + 15, + 1, + "STRING" + ] + ], + "groups": [ + { + "id": 1, + "title": "Base Image", + "bounding": [ + -620, + -374, + 1311, + 872 + ], + "color": "#3f789e", + "font_size": 24, + "flags": {} + }, + { + "id": 2, + "title": "Upscale and Create Tiles", + "bounding": [ + 745, + -375, + 515, + 1202 + ], + "color": "#8AA", + "font_size": 24, + "flags": {} + }, + { + "id": 3, + "title": "Tag Base Image", + "bounding": [ + 1311, + -374, + 431, + 668 + ], + "color": "#b06634", + "font_size": 24, + "flags": {} + }, + { + "id": 4, + "title": "Tag Tiles", + "bounding": [ + 1815, + -378, + 460, + 750 + ], + "color": "#b06634", + "font_size": 24, + "flags": {} + }, + { + "id": 5, + "title": "Assign Prompts to Tiles", + "bounding": [ + 2367, + -381, + 380, + 615 + ], + "color": "#8AA", + "font_size": 24, + "flags": {} + }, + { + "id": 6, + "title": "Add Details", + "bounding": [ + 2834, + -382, + 1359, + 1377 + ], + "color": "#3f789e", + "font_size": 24, + "flags": {} + } + ], + "config": {}, + "extra": { + "ds": { + "scale": 1, + "offset": [ + 711, + 400 + ] + }, + "groupNodes": {}, + "controller_panel": { + "controllers": {}, + "hidden": true, + "highlight": true, + "version": 2, + "default_order": [] + }, + "node_versions": { + "comfy-core": "0.3.14", + "comfyui-impact-pack": "1ae7cae2df8cca06027edfa3a24512671239d6c4", + "comfyui-wd14-tagger": "1.0.0" + }, + "ue_links": [], + "VHS_latentpreview": false, + "VHS_latentpreviewrate": 0, + "VHS_MetadataImage": true, + "VHS_KeepIntermediate": true + }, + "version": 0.4 +} \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/6-DetailerWildcard.jpg b/custom_nodes/comfyui-impact-pack/example_workflows/6-DetailerWildcard.jpg new file mode 100644 index 0000000000000000000000000000000000000000..0e33a8423593abf7d41e5e8ce7975abbe7ba060c --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/6-DetailerWildcard.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f80a2add23387329e86abfb00382d1efd818df33a8fa19c93cdf75a478a491df +size 538538 diff --git a/custom_nodes/comfyui-impact-pack/example_workflows/6-DetailerWildcard.json b/custom_nodes/comfyui-impact-pack/example_workflows/6-DetailerWildcard.json new file mode 100644 index 0000000000000000000000000000000000000000..4dd684a664dacd76cc79d7c471b69c276648fbfb --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/example_workflows/6-DetailerWildcard.json @@ -0,0 +1,1084 @@ +{ + "last_node_id": 57, + "last_link_id": 116, + "nodes": [ + { + "id": 38, + "type": "SAMLoader", + "pos": [ + 870, + 1120 + ], + "size": [ + 315, + 82 + ], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "SAM_MODEL", + "type": "SAM_MODEL", + "links": [ + 81 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "SAMLoader" + }, + "widgets_values": [ + "sam_vit_b_01ec64.pth", + "AUTO" + ], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 50, + "type": "Reroute", + "pos": [ + 1100, + 30 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 6, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 110 + } + ], + "outputs": [ + { + "name": "", + "type": "VAE", + "links": [ + 109 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 48, + "type": "FromBasicPipe", + "pos": [ + 910, + 300 + ], + "size": [ + 221.4781951904297, + 106 + ], + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [ + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "link": 116 + } + ], + "outputs": [ + { + "name": "model", + "type": "MODEL", + "links": [ + 99 + ], + "slot_index": 0 + }, + { + "name": "clip", + "type": "CLIP", + "links": null + }, + { + "name": "vae", + "type": "VAE", + "links": [ + 110 + ], + "slot_index": 2 + }, + { + "name": "positive", + "type": "CONDITIONING", + "links": [ + 100 + ], + "slot_index": 3 + }, + { + "name": "negative", + "type": "CONDITIONING", + "links": [ + 101 + ], + "slot_index": 4 + } + ], + "properties": { + "Node name for S&R": "FromBasicPipe" + }, + "widgets_values": [] + }, + { + "id": 5, + "type": "EmptyLatentImage", + "pos": [ + 490, + 190 + ], + "size": [ + 315, + 106 + ], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [ + 2 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "EmptyLatentImage" + }, + "widgets_values": [ + 768, + 512, + 1 + ] + }, + { + "id": 42, + "type": "PreviewImage", + "pos": [ + 2140, + 130 + ], + "size": [ + 793.8984985351562, + 562.4002685546875 + ], + "flags": {}, + "order": 11, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 107 + } + ], + "outputs": [], + "title": "Refined", + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 52, + "type": "PreviewImage", + "pos": [ + 2140, + 770 + ], + "size": [ + 800, + 580 + ], + "flags": {}, + "order": 12, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 113 + } + ], + "outputs": [], + "title": "Original", + "properties": { + "Node name for S&R": "PreviewImage" + }, + "widgets_values": [], + "color": "#432", + "bgcolor": "#653" + }, + { + "id": 44, + "type": "BasicPipeToDetailerPipe", + "pos": [ + 1230, + 950 + ], + "size": [ + 380, + 240 + ], + "flags": {}, + "order": 4, + "mode": 0, + "inputs": [ + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "link": 115, + "slot_index": 0 + }, + { + "name": "bbox_detector", + "type": "BBOX_DETECTOR", + "link": 114 + }, + { + "name": "sam_model_opt", + "type": "SAM_MODEL", + "shape": 7, + "link": 81 + }, + { + "name": "segm_detector_opt", + "type": "SEGM_DETECTOR", + "shape": 7, + "link": null + }, + { + "name": "detailer_hook", + "type": "DETAILER_HOOK", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "detailer_pipe", + "type": "DETAILER_PIPE", + "links": [ + 105 + ], + "slot_index": 0 + } + ], + "title": "BasicPipe -> DetailerPipe (NEW!!)", + "properties": { + "Node name for S&R": "BasicPipeToDetailerPipe" + }, + "widgets_values": [ + "{blue eyes, (angry:1.2)|{green eyes, mouth open|red eyes}| smile}", + "Select the LoRA to add to the text", + "Select the Wildcard to add to the text" + ], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 51, + "type": "Reroute", + "pos": [ + 2380, + 30 + ], + "size": [ + 82, + 26 + ], + "flags": {}, + "order": 10, + "mode": 2, + "inputs": [ + { + "name": "", + "type": "*", + "link": 111 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [ + 113 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": true, + "horizontal": false + } + }, + { + "id": 8, + "type": "VAEDecode", + "pos": [ + 1430, + 60 + ], + "size": [ + 140, + 46 + ], + "flags": { + "collapsed": true + }, + "order": 8, + "mode": 0, + "inputs": [ + { + "name": "samples", + "type": "LATENT", + "link": 7 + }, + { + "name": "vae", + "type": "VAE", + "link": 109 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [ + 106, + 111 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "VAEDecode" + }, + "widgets_values": [], + "color": "#432", + "bgcolor": "#653" + }, + { + "id": 3, + "type": "KSampler", + "pos": [ + 1209, + 126 + ], + "size": [ + 400, + 650 + ], + "flags": {}, + "order": 7, + "mode": 0, + "inputs": [ + { + "name": "model", + "type": "MODEL", + "link": 99 + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": 100 + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": 101 + }, + { + "name": "latent_image", + "type": "LATENT", + "link": 2 + } + ], + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [ + 7 + ], + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "KSampler" + }, + "widgets_values": [ + 497844439625000, + "fixed", + 20, + 8, + "euler", + "normal", + 1 + ], + "color": "#432", + "bgcolor": "#653" + }, + { + "id": 56, + "type": "UltralyticsDetectorProvider", + "pos": [ + 870, + 970 + ], + "size": [ + 315, + 78 + ], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "BBOX_DETECTOR", + "type": "BBOX_DETECTOR", + "shape": 3, + "links": [ + 114 + ], + "slot_index": 0 + }, + { + "name": "SEGM_DETECTOR", + "type": "SEGM_DETECTOR", + "shape": 3, + "links": null + } + ], + "properties": { + "Node name for S&R": "UltralyticsDetectorProvider" + }, + "widgets_values": [ + "bbox/face_yolov8m.pt" + ], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 57, + "type": "workflow>MAKE_BASIC_PIPE", + "pos": [ + 50, + 470 + ], + "size": [ + 410, + 360 + ], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "VAE", + "type": "VAE", + "shape": 3, + "links": null + }, + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "shape": 3, + "links": [ + 115, + 116 + ] + } + ], + "properties": { + "Node name for S&R": "workflow/MAKE_BASIC_PIPE" + }, + "widgets_values": [ + "vae-ft-mse-840000-ema-pruned.safetensors", + "SD1.5/V07_v07.safetensors", + "RAW photo, delicate, best quality, colorful, 2girls, 8k uhd, film grain, soft lighting, dslr, (Fujifilm XT3), (photorealistic:1.4), (detailed skin), soft lips, (very detailed long ponytail), aged down, studio lighting, from top, colorful sports wear, happy face, spread lips, (walking), (central park, cloud, sunshine), small breast", + "(low quality:1.4), (worst quality:1.4), bad anatomy, (nsfw:1.2), muscle, from back, from front, monochrome, (bikini:1.2)" + ], + "color": "#222", + "bgcolor": "#000" + }, + { + "id": 49, + "type": "FaceDetailerPipe", + "pos": [ + 1630, + 130 + ], + "size": [ + 480, + 1220 + ], + "flags": {}, + "order": 9, + "mode": 0, + "inputs": [ + { + "name": "image", + "type": "IMAGE", + "link": 106 + }, + { + "name": "detailer_pipe", + "type": "DETAILER_PIPE", + "link": 105 + }, + { + "name": "scheduler_func_opt", + "type": "SCHEDULER_FUNC", + "shape": 7, + "link": null + } + ], + "outputs": [ + { + "name": "image", + "type": "IMAGE", + "shape": 3, + "links": [ + 107 + ], + "slot_index": 0 + }, + { + "name": "cropped_refined", + "type": "IMAGE", + "shape": 6, + "links": null + }, + { + "name": "cropped_enhanced_alpha", + "type": "IMAGE", + "shape": 6, + "links": null + }, + { + "name": "mask", + "type": "MASK", + "shape": 3, + "links": null + }, + { + "name": "detailer_pipe", + "type": "DETAILER_PIPE", + "shape": 3, + "links": null + }, + { + "name": "cnet_images", + "type": "IMAGE", + "shape": 6, + "links": null + } + ], + "properties": { + "Node name for S&R": "FaceDetailerPipe" + }, + "widgets_values": [ + 256, + true, + 768, + 307405256705890, + "fixed", + 20, + 8, + "euler", + "normal", + 0.5, + 5, + true, + false, + 0.5, + 10, + 3, + "center-1", + 0, + 0.93, + 0, + 0.7, + "False", + 10, + 0.2, + 1, + false, + 0, + false, + false + ], + "color": "#223", + "bgcolor": "#335" + } + ], + "links": [ + [ + 2, + 5, + 0, + 3, + 3, + "LATENT" + ], + [ + 7, + 3, + 0, + 8, + 0, + "LATENT" + ], + [ + 81, + 38, + 0, + 44, + 2, + "SAM_MODEL" + ], + [ + 99, + 48, + 0, + 3, + 0, + "MODEL" + ], + [ + 100, + 48, + 3, + 3, + 1, + "CONDITIONING" + ], + [ + 101, + 48, + 4, + 3, + 2, + "CONDITIONING" + ], + [ + 105, + 44, + 0, + 49, + 1, + "DETAILER_PIPE" + ], + [ + 106, + 8, + 0, + 49, + 0, + "IMAGE" + ], + [ + 107, + 49, + 0, + 42, + 0, + "IMAGE" + ], + [ + 109, + 50, + 0, + 8, + 1, + "VAE" + ], + [ + 110, + 48, + 2, + 50, + 0, + "*" + ], + [ + 111, + 8, + 0, + 51, + 0, + "*" + ], + [ + 113, + 51, + 0, + 52, + 0, + "IMAGE" + ], + [ + 114, + 56, + 0, + 44, + 1, + "BBOX_DETECTOR" + ], + [ + 115, + 57, + 1, + 44, + 0, + "BASIC_PIPE" + ], + [ + 116, + 57, + 1, + 48, + 0, + "BASIC_PIPE" + ] + ], + "groups": [], + "config": {}, + "extra": { + "groupNodes": { + "MAKE_BASIC_PIPE": { + "nodes": [ + { + "type": "VAELoader", + "pos": [ + -200, + 600 + ], + "size": { + "0": 315, + "1": 58 + }, + "flags": {}, + "order": 1, + "mode": 0, + "outputs": [ + { + "name": "VAE", + "type": "VAE", + "links": [], + "slot_index": 0, + "localized_name": "VAE" + } + ], + "properties": { + "Node name for S&R": "VAELoader" + }, + "widgets_values": [ + "vae-ft-mse-840000-ema-pruned.safetensors" + ], + "index": 0, + "inputs": [] + }, + { + "type": "CheckpointLoaderSimple", + "pos": [ + -660, + 680 + ], + "size": { + "0": 315, + "1": 98 + }, + "flags": {}, + "order": 6, + "mode": 0, + "outputs": [ + { + "name": "MODEL", + "type": "MODEL", + "links": [], + "slot_index": 0, + "localized_name": "MODEL" + }, + { + "name": "CLIP", + "type": "CLIP", + "links": [], + "slot_index": 1, + "localized_name": "CLIP" + }, + { + "name": "VAE", + "type": "VAE", + "links": [], + "slot_index": 2, + "localized_name": "VAE" + } + ], + "properties": { + "Node name for S&R": "CheckpointLoaderSimple" + }, + "widgets_values": [ + "SD1.5/V07_v07.safetensors" + ], + "index": 1, + "inputs": [] + }, + { + "type": "CLIPTextEncode", + "pos": [ + -260, + 750 + ], + "size": { + "0": 411.9563903808594, + "1": 162.07196044921875 + }, + "flags": {}, + "order": 7, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": null, + "localized_name": "clip" + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [], + "slot_index": 0, + "localized_name": "CONDITIONING" + } + ], + "title": "Positive", + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "RAW photo, delicate, best quality, colorful, 2girls, 8k uhd, film grain, soft lighting, dslr, (Fujifilm XT3), (photorealistic:1.4), (detailed skin), soft lips, (very detailed long ponytail), aged down, studio lighting, from top, colorful sports wear, happy face, spread lips, (walking), (central park, cloud, sunshine), small breast" + ], + "index": 2 + }, + { + "type": "CLIPTextEncode", + "pos": [ + -260, + 960 + ], + "size": { + "0": 410, + "1": 130 + }, + "flags": {}, + "order": 8, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": null, + "localized_name": "clip" + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [], + "slot_index": 0, + "localized_name": "CONDITIONING" + } + ], + "title": "Negative", + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "(low quality:1.4), (worst quality:1.4), bad anatomy, (nsfw:1.2), muscle, from back, from front, monochrome, (bikini:1.2)" + ], + "index": 3 + }, + { + "type": "ToBasicPipe", + "pos": [ + 210, + 680 + ], + "size": { + "0": 241.79998779296875, + "1": 106 + }, + "flags": {}, + "order": 9, + "mode": 0, + "inputs": [ + { + "name": "model", + "type": "MODEL", + "link": null, + "localized_name": "model" + }, + { + "name": "clip", + "type": "CLIP", + "link": null, + "localized_name": "clip" + }, + { + "name": "vae", + "type": "VAE", + "link": null, + "localized_name": "vae" + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": null, + "localized_name": "positive" + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": null, + "localized_name": "negative" + } + ], + "outputs": [ + { + "name": "basic_pipe", + "type": "BASIC_PIPE", + "links": [], + "slot_index": 0, + "localized_name": "basic_pipe" + } + ], + "properties": { + "Node name for S&R": "ToBasicPipe" + }, + "index": 4 + } + ], + "links": [ + [ + 1, + 1, + 2, + 0, + 4, + "CLIP" + ], + [ + 1, + 1, + 3, + 0, + 4, + "CLIP" + ], + [ + 1, + 0, + 4, + 0, + 4, + "MODEL" + ], + [ + 1, + 1, + 4, + 1, + 4, + "CLIP" + ], + [ + 0, + 0, + 4, + 2, + 14, + "VAE" + ], + [ + 2, + 0, + 4, + 3, + 6, + "CONDITIONING" + ], + [ + 3, + 0, + 4, + 4, + 7, + "CONDITIONING" + ] + ], + "external": [ + [ + 4, + 0, + "BASIC_PIPE" + ] + ] + } + }, + "controller_panel": { + "controllers": {}, + "hidden": true, + "highlight": true, + "version": 2, + "default_order": [] + }, + "ds": { + "scale": 0.7513148009015777, + "offset": { + "0": 149.68603515625, + "1": 245.33897399902344 + } + }, + "node_versions": { + "comfyui-impact-pack": "1ae7cae2df8cca06027edfa3a24512671239d6c4", + "comfy-core": "0.3.14", + "comfyui-impact-subpack": "74db20c95eca152a6d686c914edc0ef4e4762cb8" + }, + "ue_links": [], + "VHS_latentpreview": false, + "VHS_latentpreviewrate": 0, + "VHS_MetadataImage": true, + "VHS_KeepIntermediate": true + }, + "version": 0.4 +} \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/impact-pack.ini b/custom_nodes/comfyui-impact-pack/impact-pack.ini new file mode 100644 index 0000000000000000000000000000000000000000..eca36e28cc02c6d5c2a46cfd5e52b20ee319d80e --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/impact-pack.ini @@ -0,0 +1,6 @@ +[default] +sam_editor_cpu = False +sam_editor_model = sam_vit_b_01ec64.pth +custom_wildcards = C:\Users\Liam\Documents\ComfyUI\custom_nodes\comfyui-impact-pack\custom_wildcards +disable_gpu_opencv = True + diff --git a/custom_nodes/comfyui-impact-pack/install.py b/custom_nodes/comfyui-impact-pack/install.py new file mode 100644 index 0000000000000000000000000000000000000000..592c2c86a290b6806f2a2f208baaeb7ae689165a --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/install.py @@ -0,0 +1,116 @@ +import os +import shutil +import sys +import subprocess +import threading +import locale +import traceback + + +if sys.argv[0] == 'install.py': + sys.path.append('.') # for portable version + + +impact_path = os.path.join(os.path.dirname(__file__), "modules") + + +comfy_path = os.environ.get('COMFYUI_PATH') +if comfy_path is None: + print(f"\nWARN: The `COMFYUI_PATH` environment variable is not set. Assuming `{os.path.dirname(__file__)}/../../` as the ComfyUI path.", file=sys.stderr) + comfy_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) + +model_path = os.environ.get('COMFYUI_MODEL_PATH') +if model_path is None: + try: + import folder_paths + model_path = folder_paths.models_dir + except: + pass + + if model_path is None: + model_path = os.path.abspath(os.path.join(comfy_path, 'models')) + print(f"\nWARN: The `COMFYUI_MODEL_PATH` environment variable is not set. Assuming `{model_path}` as the ComfyUI path.", file=sys.stderr) + + +sys.path.append(impact_path) +sys.path.append(comfy_path) + + +# --- +def handle_stream(stream, is_stdout): + stream.reconfigure(encoding=locale.getpreferredencoding(), errors='replace') + + for msg in stream: + if is_stdout: + print(msg, end="", file=sys.stdout) + else: + print(msg, end="", file=sys.stderr) + + +def process_wrap(cmd_str, cwd=None, handler=None, env=None): + print(f"[Impact Pack] EXECUTE: {cmd_str} in '{cwd}'") + process = subprocess.Popen(cmd_str, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, text=True, bufsize=1) + + if handler is None: + handler = handle_stream + + stdout_thread = threading.Thread(target=handler, args=(process.stdout, True)) + stderr_thread = threading.Thread(target=handler, args=(process.stderr, False)) + + stdout_thread.start() + stderr_thread.start() + + stdout_thread.join() + stderr_thread.join() + + return process.wait() +# --- + + +try: + from torchvision.datasets.utils import download_url + import impact.config + + print("### ComfyUI-Impact-Pack: Check dependencies") + def install(): + new_env = os.environ.copy() + new_env["COMFYUI_PATH"] = comfy_path + new_env["COMFYUI_MODEL_PATH"] = model_path + + # Download model + print("### ComfyUI-Impact-Pack: Check basic models") + sam_path = os.path.join(model_path, "sams") + onnx_path = os.path.join(model_path, "onnx") + + if not os.path.exists(os.path.join(os.path.dirname(__file__), '..', 'skip_download_model')): + try: + if not os.path.exists(os.path.join(sam_path, "sam_vit_b_01ec64.pth")): + download_url("https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth", sam_path) + except: + print("[Impact Pack] Failed to auto-download model files. Please download them manually.") + + if not os.path.exists(onnx_path): + print(f"### ComfyUI-Impact-Pack: onnx model directory created ({onnx_path})") + os.mkdir(onnx_path) + + impact.config.write_config() + + # Remove legacy subpack + try: + subpack_path = os.path.join(os.path.dirname(__file__), 'impact_subpack') + if os.path.exists(subpack_path): + shutil.rmtree(subpack_path) + print(f"Legacy subpack is detected. '{subpack_path}' is removed.") + + subpack_path = os.path.join(os.path.dirname(__file__), 'subpack') + if os.path.exists(subpack_path): + shutil.rmtree(subpack_path) + print(f"Legacy subpack is detected. '{subpack_path}' is removed.") + except: + print(f"ERROT: Failed to delete legacy subpack '{subpack_path}'\nPlease delete the folder after terminate ComfyUI.") + + install() + +except Exception: + print("[ERROR] ComfyUI-Impact-Pack: Dependency installation has failed. Please install manually.") + traceback.print_exc() diff --git a/custom_nodes/comfyui-impact-pack/js/common.js b/custom_nodes/comfyui-impact-pack/js/common.js new file mode 100644 index 0000000000000000000000000000000000000000..561917dffeb69d3f8d194de79398cc14da586aa0 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/js/common.js @@ -0,0 +1,137 @@ +import { api } from "../../scripts/api.js"; +import { app } from "../../scripts/app.js"; + +let original_show = app.ui.dialog.show; + +export function customAlert(message) { + try { + app.extensionManager.toast.addAlert(message); + } + catch { + alert(message); + } +} + +export function isBeforeFrontendVersion(compareVersion) { + try { + const frontendVersion = window['__COMFYUI_FRONTEND_VERSION__']; + if (typeof frontendVersion !== 'string') { + return false; + } + + function parseVersion(versionString) { + const parts = versionString.split('.').map(Number); + return parts.length === 3 && parts.every(part => !isNaN(part)) ? parts : null; + } + + const currentVersion = parseVersion(frontendVersion); + const comparisonVersion = parseVersion(compareVersion); + + if (!currentVersion || !comparisonVersion) { + return false; + } + + for (let i = 0; i < 3; i++) { + if (currentVersion[i] > comparisonVersion[i]) { + return false; + } else if (currentVersion[i] < comparisonVersion[i]) { + return true; + } + } + + return false; + } catch { + return true; + } +} + +function dialog_show_wrapper(html) { + if (typeof html === "string") { + if(html.includes("IMPACT-PACK-SIGNAL: STOP CONTROL BRIDGE")) { + return; + } + + this.textElement.innerHTML = html; + } else { + this.textElement.replaceChildren(html); + } + this.element.style.display = "flex"; +} + +app.ui.dialog.show = dialog_show_wrapper; + + +function nodeFeedbackHandler(event) { + let nodes = app.graph._nodes_by_id; + let node = nodes[event.detail.node_id]; + if(node) { + const w = node.widgets.find((w) => event.detail.widget_name === w.name); + if(w) { + w.value = event.detail.value; + } + } +} + +api.addEventListener("impact-node-feedback", nodeFeedbackHandler); + + +function setMuteState(event) { + let nodes = app.graph._nodes_by_id; + let node = nodes[event.detail.node_id]; + if(node) { + if(event.detail.is_active) + node.mode = 0; + else + node.mode = 2; + } +} + +api.addEventListener("impact-node-mute-state", setMuteState); + + +async function bridgeContinue(event) { + let nodes = app.graph._nodes_by_id; + let node = nodes[event.detail.node_id]; + if(node) { + const mutes = new Set(event.detail.mutes); + const actives = new Set(event.detail.actives); + const bypasses = new Set(event.detail.bypasses); + + for(let i in app.graph._nodes_by_id) { + let this_node = app.graph._nodes_by_id[i]; + if(mutes.has(i)) { + this_node.mode = 2; + } + else if(actives.has(i)) { + this_node.mode = 0; + } + else if(bypasses.has(i)) { + this_node.mode = 4; + } + } + + await app.queuePrompt(0, 1); + } +} + +api.addEventListener("impact-bridge-continue", bridgeContinue); + + +function addQueue(event) { + app.queuePrompt(0, 1); +} + +api.addEventListener("impact-add-queue", addQueue); + + +function refreshPreview(event) { + let node_id = event.detail.node_id; + let item = event.detail.item; + let img = new Image(); + img.src = `/view?filename=${item.filename}&subfolder=${item.subfolder}&type=${item.type}&no-cache=${Date.now()}`; + let node = app.graph._nodes_by_id[node_id]; + if(node) + node.imgs = [img]; +} + +api.addEventListener("impact-preview", refreshPreview); diff --git a/custom_nodes/comfyui-impact-pack/js/impact-image-util.js b/custom_nodes/comfyui-impact-pack/js/impact-image-util.js new file mode 100644 index 0000000000000000000000000000000000000000..4985a9e5ca0bc19be6159449c87eceeee8ef2192 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/js/impact-image-util.js @@ -0,0 +1,229 @@ +import { ComfyApp, app } from "../../scripts/app.js"; +import { api } from "../../scripts/api.js"; + +function load_image(str) { + let base64String = canvas.toDataURL('image/png'); + let img = new Image(); + img.src = base64String; +} + +function getFileItem(baseType, path) { + try { + let pathType = baseType; + + if (path.endsWith("[output]")) { + pathType = "output"; + path = path.slice(0, -9); + } else if (path.endsWith("[input]")) { + pathType = "input"; + path = path.slice(0, -8); + } else if (path.endsWith("[temp]")) { + pathType = "temp"; + path = path.slice(0, -7); + } + + const subfolder = path.substring(0, path.lastIndexOf('/')); + const filename = path.substring(path.lastIndexOf('/') + 1); + + return { + filename: filename, + subfolder: subfolder, + type: pathType + }; + } + catch(exception) { + return null; + } +} + +async function loadImageFromUrl(image, node_id, v, need_to_load) { + let item = getFileItem('temp', v); + + if(item) { + let params = `?node_id=${node_id}&filename=${item.filename}&type=${item.type}&subfolder=${item.subfolder}`; + + let res = await api.fetchApi('/impact/set/pb_id_image'+params, { cache: "no-store" }); + if(res.status == 200) { + let pb_id = await res.text(); + if(need_to_load) {; + image.src = api.apiURL(`/view?filename=${item.filename}&type=${item.type}&subfolder=${item.subfolder}`); + } + return pb_id; + } + else { + return `$${node_id}-0`; + } + } + else { + return `$${node_id}-0`; + } +} + +async function loadImageFromId(image, v) { + let res = await api.fetchApi('/impact/get/pb_id_image?id='+v, { cache: "no-store" }); + if(res.status == 200) { + let item = await res.json(); + image.src = api.apiURL(`/view?filename=${item.filename}&type=${item.type}&subfolder=${item.subfolder}`); + return true; + } + + return false; +} + +app.registerExtension({ + name: "Comfy.Impact.img", + + nodeCreated(node, app) { + if(node.comfyClass == "PreviewBridge" || node.comfyClass == "PreviewBridgeLatent") { + let w = node.widgets.find(obj => obj.name === 'image'); + node._imgs = [new Image()]; + node.imageIndex = 0; + + Object.defineProperty(w, 'value', { + async set(v) { + if(w._lock) + return; + + const stackTrace = new Error().stack; + if(stackTrace.includes('presetText.js')) + return; + + var image = new Image(); + if(v && v.constructor == String && v.startsWith('$')) { + // from node feedback + let need_to_load = node._imgs[0].src == ''; + if(await loadImageFromId(image, v, need_to_load)) { + w._value = v; + if(node._imgs[0].src == '') { + node._imgs = [image]; + } + } + else { + w._value = `$${node.id}-0`; + } + } + else { + // from clipspace + w._lock = true; + w._value = await loadImageFromUrl(image, node.id, v, false); + w._lock = false; + } + }, + get() { + if(w._value == undefined) { + w._value = `$${node.id}-0`; + } + return w._value; + } + }); + + Object.defineProperty(node, 'imgs', { + set(v) { + const stackTrace = new Error().stack; + if(v && v.length == 0) + return; + else if(stackTrace.includes('pasteFromClipspace')) { + let sp = new URLSearchParams(v[0].src.split("?")[1]); + let str = ""; + if(sp.get('subfolder')) { + str += sp.get('subfolder') + '/'; + } + str += `${sp.get("filename")} [${sp.get("type")}]`; + + w.value = str; + } + + node._imgs = v; + }, + get() { + return node._imgs; + } + }); + } + + if(node.comfyClass == "ImageReceiver") { + let path_widget = node.widgets.find(obj => obj.name === 'image'); + let w = node.widgets.find(obj => obj.name === 'image_data'); + let stw_widget = node.widgets.find(obj => obj.name === 'save_to_workflow'); + w._value = ""; + + Object.defineProperty(w, 'value', { + set(v) { + if(v != '[IMAGE DATA]') + w._value = v; + }, + get() { + const stackTrace = new Error().stack; + if(!stackTrace.includes('draw') && !stackTrace.includes('graphToPrompt') && stackTrace.includes('app.js')) { + return "[IMAGE DATA]"; + } + else { + if(stw_widget.value) + return w._value; + else + return ""; + } + } + }); + + let set_img_act = (v) => { + node._img = v; + var canvas = document.createElement('canvas'); + canvas.width = v[0].width; + canvas.height = v[0].height; + + var context = canvas.getContext('2d'); + context.drawImage(v[0], 0, 0, v[0].width, v[0].height); + + var base64Image = canvas.toDataURL('image/png'); + w.value = base64Image; + }; + + Object.defineProperty(node, 'imgs', { + set(v) { + if (v && !v[0].complete) { + let orig_onload = v[0].onload; + v[0].onload = function(v2) { + if(orig_onload) + orig_onload(); + set_img_act(v); + }; + } + else { + set_img_act(v); + } + }, + get() { + if(this._img == undefined && w.value != '') { + this._img = [new Image()]; + if(stw_widget.value && w.value != '[IMAGE DATA]') + this._img[0].src = w.value; + } + else if(this._img == undefined && path_widget.value) { + let image = new Image(); + image.src = path_widget.value; + + try { + let item = getFileItem('temp', path_widget.value); + let params = `?filename=${item.filename}&type=${item.type}&subfolder=${item.subfolder}`; + + let res = api.fetchApi('/view/validate'+params, { cache: "no-store" }).then(response => response); + if(res.status == 200) { + image.src = api.apiURL('/view'+params); + } + + this._img = [new Image()]; // placeholder + image.onload = function(v) { + set_img_act([image]); + }; + } + catch { + + } + } + return this._img; + } + }); + } + } +}) diff --git a/custom_nodes/comfyui-impact-pack/js/impact-pack.js b/custom_nodes/comfyui-impact-pack/js/impact-pack.js new file mode 100644 index 0000000000000000000000000000000000000000..b2f9d706bfbbf8c82ad015cd23650df366960dfd --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/js/impact-pack.js @@ -0,0 +1,933 @@ +import { ComfyApp, app } from "../../scripts/app.js"; +import { ComfyDialog, $el } from "../../scripts/ui.js"; +import { api } from "../../scripts/api.js"; +import { customAlert, isBeforeFrontendVersion } from "./common.js"; + +const is_legacy_front = () => isBeforeFrontendVersion('1.16.9'); + +if(is_legacy_front()) { + customAlert("An outdated version(<1.16.9) of the `comfyui-frontend-package` is installed. It is not compatible with the current version of the Impact Pack."); +} + +let wildcards_list = []; +async function load_wildcards() { + let res = await api.fetchApi('/impact/wildcards/list'); + let data = await res.json(); + wildcards_list = data.data; +} + +load_wildcards(); + +export function get_wildcards_list() { + return wildcards_list; +} + +// temporary implementation (copying from https://github.com/pythongosssss/ComfyUI-WD14-Tagger) +// I think this should be included into master!! +class ImpactProgressBadge { + constructor() { + if (!window.__progress_badge__) { + window.__progress_badge__ = Symbol("__impact_progress_badge__"); + } + this.symbol = window.__progress_badge__; + } + + getState(node) { + return node[this.symbol] || {}; + } + + setState(node, state) { + node[this.symbol] = state; + app.canvas.setDirty(true); + } + + addStatusHandler(nodeType) { + if (nodeType[this.symbol]?.statusTagHandler) { + return; + } + if (!nodeType[this.symbol]) { + nodeType[this.symbol] = {}; + } + nodeType[this.symbol] = { + statusTagHandler: true, + }; + + api.addEventListener("impact/update_status", ({ detail }) => { + let { node, progress, text } = detail; + const n = app.graph.getNodeById(+(node || app.runningNodeId)); + if (!n) return; + const state = this.getState(n); + state.status = Object.assign(state.status || {}, { progress: text ? progress : null, text: text || null }); + this.setState(n, state); + }); + + const self = this; + const onDrawForeground = nodeType.prototype.onDrawForeground; + nodeType.prototype.onDrawForeground = function (ctx) { + const r = onDrawForeground?.apply?.(this, arguments); + const state = self.getState(this); + if (!state?.status?.text) { + return r; + } + + const { fgColor, bgColor, text, progress, progressColor } = { ...state.status }; + + ctx.save(); + ctx.font = "12px sans-serif"; + const sz = ctx.measureText(text); + ctx.fillStyle = bgColor || "dodgerblue"; + ctx.beginPath(); + ctx.roundRect(0, -LiteGraph.NODE_TITLE_HEIGHT - 20, sz.width + 12, 20, 5); + ctx.fill(); + + if (progress) { + ctx.fillStyle = progressColor || "green"; + ctx.beginPath(); + ctx.roundRect(0, -LiteGraph.NODE_TITLE_HEIGHT - 20, (sz.width + 12) * progress, 20, 5); + ctx.fill(); + } + + ctx.fillStyle = fgColor || "#fff"; + ctx.fillText(text, 6, -LiteGraph.NODE_TITLE_HEIGHT - 6); + ctx.restore(); + return r; + }; + } +} + +const input_tracking = {}; +const input_dirty = {}; +const output_tracking = {}; + +function progressExecuteHandler(event) { + if(event.detail?.output?.aux){ + const id = event.detail.node; + if(input_tracking.hasOwnProperty(id)) { + if(input_tracking.hasOwnProperty(id) && input_tracking[id][0] != event.detail.output.aux[0]) { + input_dirty[id] = true; + } + else{ + + } + } + + input_tracking[id] = event.detail.output.aux; + } +} + +function imgSendHandler(event) { + if(event.detail.images.length > 0){ + let data = event.detail.images[0]; + let filename = `${data.filename} [${data.type}]`; + + let nodes = app.graph._nodes; + for(let i in nodes) { + if(nodes[i].type == 'ImageReceiver') { + let is_linked = false; + + if(nodes[i].widgets[1].type == 'converted-widget') { + for(let j in nodes[i].inputs) { + let input = nodes[i].inputs[j]; + if(input.name === 'link_id') { + if(input.link) { + let src_node = app.graph._nodes_by_id[app.graph.links[input.link].origin_id]; + if(src_node.type == 'ImpactInt' || src_node.type == 'PrimitiveNode') { + is_linked = true; + } + } + break; + } + } + } + else if(nodes[i].widgets[1].value == event.detail.link_id) { + is_linked = true; + } + + if(is_linked) { + if(data.subfolder) + nodes[i].widgets[0].value = `${data.subfolder}/${data.filename} [${data.type}]`; + else + nodes[i].widgets[0].value = `${data.filename} [${data.type}]`; + + let img = new Image(); + img.onload = (event) => { + nodes[i].imgs = [img]; + nodes[i].size[1] = Math.max(200, nodes[i].size[1]); + app.canvas.setDirty(true); + }; + img.src = `/view?filename=${data.filename}&type=${data.type}&subfolder=${data.subfolder}`+app.getPreviewFormatParam(); + } + } + } + } +} + + +function latentSendHandler(event) { + if(event.detail.images.length > 0){ + let data = event.detail.images[0]; + let filename = `${data.filename} [${data.type}]`; + + let nodes = app.graph._nodes; + for(let i in nodes) { + if(nodes[i].type == 'LatentReceiver') { + if(nodes[i].widgets[1].value == event.detail.link_id) { + if(data.subfolder) + nodes[i].widgets[0].value = `${data.subfolder}/${data.filename} [${data.type}]`; + else + nodes[i].widgets[0].value = `${data.filename} [${data.type}]`; + + let img = new Image(); + img.src = `/view?filename=${data.filename}&type=${data.type}&subfolder=${data.subfolder}`+app.getPreviewFormatParam(); + nodes[i].imgs = [img]; + nodes[i].size[1] = Math.max(200, nodes[i].size[1]); + } + } + } + } +} + + +function valueSendHandler(event) { + let nodes = app.graph._nodes; + for(let i in nodes) { + if(nodes[i].type == 'ImpactValueReceiver') { + if(nodes[i].widgets[2].value == event.detail.link_id) { + nodes[i].widgets[1].value = event.detail.value; + + let typ = typeof event.detail.value; + if(typ == 'string') { + nodes[i].widgets[0].value = "STRING"; + } + else if(typ == "boolean") { + nodes[i].widgets[0].value = "BOOLEAN"; + } + else if(typ != "number") { + nodes[i].widgets[0].value = typeof event.detail.value; + } + else if(Number.isInteger(event.detail.value)) { + nodes[i].widgets[0].value = "INT"; + } + else { + nodes[i].widgets[0].value = "FLOAT"; + } + } + } + } +} + + +const impactProgressBadge = new ImpactProgressBadge(); + +api.addEventListener("stop-iteration", () => { + document.getElementById("autoQueueCheckbox").checked = false; +}); +api.addEventListener("value-send", valueSendHandler); +api.addEventListener("img-send", imgSendHandler); +api.addEventListener("latent-send", latentSendHandler); +api.addEventListener("executed", progressExecuteHandler); + +app.registerExtension({ + name: "Comfy.Impack", + + commands: [ + { + id: 'refresh-impact-wildcard', + label: 'Impact: Refresh Wildcard', + function: async () => { + await api.fetchApi('/impact/wildcards/refresh'); + await load_wildcards(); + app.extensionManager.toast.add({ + severity: 'info', + summary: 'Refreshed!', + detail: 'Impact Wildcard List is refreshed!!', + life: 3000 + }); + } + } + ], + + menuCommands: [ + { + path: ['Edit'], + commands: ['refresh-impact-wildcard'] + } + ], + + loadedGraphNode(node, app) { + if (node.comfyClass == "MaskPainter") { + input_dirty[node.id + ""] = true; + } + }, + + async beforeRegisterNodeDef(nodeType, nodeData, app) { + if (nodeData.name == "IterativeLatentUpscale" || nodeData.name == "IterativeImageUpscale" + || nodeData.name == "RegionalSampler"|| nodeData.name == "RegionalSamplerAdvanced") { + impactProgressBadge.addStatusHandler(nodeType); + } + + if(nodeData.name == "ImpactControlBridge") { + const onConnectionsChange = nodeType.prototype.onConnectionsChange; + nodeType.prototype.onConnectionsChange = function (type, index, connected, link_info) { + if(index != 0 || !link_info || this.inputs[0].type != '*') + return; + + // assign type + let slot_type = '*'; + + if(type == 2) { + slot_type = link_info.type; + } + else { + const node = app.graph.getNodeById(link_info.origin_id); + slot_type = node.outputs[link_info.origin_slot]?.type; + } + + this.inputs[0].type = slot_type; + this.outputs[0].type = slot_type; + this.outputs[0].label = slot_type; + } + } + + if(nodeData.name == "ImpactConditionalBranch" || nodeData.name == "ImpactConditionalBranchSelMode") { + const onConnectionsChange = nodeType.prototype.onConnectionsChange; + nodeType.prototype.onConnectionsChange = function (type, index, connected, link_info) { + if(!link_info || this.inputs[0].type != '*') + return; + + if(index >= 2) + return; + + // assign type + let slot_type = '*'; + + if(type == 2) { + slot_type = link_info.type; + } + else { + const node = app.graph.getNodeById(link_info.origin_id); + slot_type = node.outputs[link_info.origin_slot].type; + } + + this.inputs[0].type = slot_type; + this.inputs[1].type = slot_type; + this.outputs[0].type = slot_type; + this.outputs[0].label = slot_type; + } + } + + if(nodeData.name == "ImpactCompare") { + const onConnectionsChange = nodeType.prototype.onConnectionsChange; + nodeType.prototype.onConnectionsChange = function (type, index, connected, link_info) { + if(!link_info || this.inputs[0].type != '*' || type == 2) + return; + + // assign type + const node = app.graph.getNodeById(link_info.origin_id); + let slot_type = node.outputs[link_info.origin_slot].type; + + this.inputs[0].type = slot_type; + this.inputs[1].type = slot_type; + } + } + + if(nodeData.name == "ImpactSelectNthItemOfAnyList") { + const onConnectionsChange = nodeType.prototype.onConnectionsChange; + nodeType.prototype.onConnectionsChange = function (type, index, connected, link_info) { + if(!link_info || this.inputs[0].type != '*') + return; + + if(index >= 2) + return; + + // assign type + let slot_type = '*'; + + if(type == 2) { + slot_type = link_info.type; + } + else { + const node = app.graph.getNodeById(link_info.origin_id); + slot_type = node.outputs[link_info.origin_slot].type; + } + + this.inputs[0].type = slot_type; + this.outputs[0].type = slot_type; + this.outputs[0].label = slot_type; + } + } + + if(nodeData.name === 'ImpactInversedSwitch') { + nodeData.output = ['*']; + nodeData.output_is_list = [false]; + nodeData.output_name = ['output1']; + + const onConnectionsChange = nodeType.prototype.onConnectionsChange; + nodeType.prototype.onConnectionsChange = function (type, index, connected, link_info) { + if(!link_info) + return; + + // HOTFIX: subgraph + const stackTrace = new Error().stack; + + if(stackTrace.includes('convertToSubgraph') || stackTrace.includes('Subgraph.configure')) { + return; + } + + if(type == 2) { + // connect output + if(connected){ + if(app.graph._nodes_by_id[link_info.target_id]?.type == 'Reroute') { + app.graph._nodes_by_id[link_info.target_id].disconnectInput(link_info.target_slot); + } + + if(this.outputs[0].type == '*'){ + if(link_info.type == '*' && app.graph.getNodeById(link_info.target_id).slots[link_info.target_slot].type != '*') { + app.graph._nodes_by_id[link_info.target_id].disconnectInput(link_info.target_slot); + } + else { + // propagate type + this.outputs[0].type = link_info.type; + this.outputs[0].name = link_info.type; + + for(let i in this.inputs) { + if(this.inputs[i].name != 'select') + this.inputs[i].type = link_info.type; + } + } + } + } + } + else { + if(app.graph._nodes_by_id[link_info.origin_id]?.type == 'Reroute') + this.disconnectInput(link_info.target_slot); + + // connect input + if(this.inputs[0].type == '*'){ + const node = app.graph.getNodeById(link_info.origin_id); + let origin_type = node.outputs[link_info.origin_slot]?.type; + + if(origin_type==undefined) { + return; // fallback + } + + if(origin_type == '*' && app.graph.getNodeById(link_info.origin_id).slots[link_info.origin_slot].type != '*') { + this.disconnectInput(link_info.target_slot); + return; + } + + for(let i in this.inputs) { + if(this.inputs[i].name != 'select') + this.inputs[i].type = origin_type; + } + + this.outputs[0].type = origin_type; + this.outputs[0].name = 'output1'; + } + + return; + } + + if (!connected && this.outputs.length > 1) { + const stackTrace = new Error().stack; + + if( + !stackTrace.includes('LGraphNode.prototype.connect') && // for touch device + !stackTrace.includes('LGraphNode.connect') && // for mouse device + !stackTrace.includes('loadGraphData')) { + if(this.outputs[link_info.origin_slot].links.length == 0) { + this.removeOutput(link_info.origin_slot); + } + } + } + + let slot_i = 1; + for (let i = 0; i < this.outputs.length; i++) { + this.outputs[i].name = `output${slot_i}` + if (this.outputs[i].slot_index === undefined) { + this.outputs[i].slot_index = i; + } + slot_i++; + } + + if(connected) { + // NOTE: node.slot_index is different with link_info.origin_slot + let last_slot_index = this.outputs.length - 1; + if (last_slot_index == link_info.origin_slot) { + this.addOutput(`output${slot_i}`, this.outputs[0].type); + } + } + + let select_slot = this.inputs.find(x => x.name == "select"); + if(this.widgets?.length) { + this.widgets[0].options.max = select_slot?this.outputs.length-1:this.outputs.length; + this.widgets[0].value = Math.min(this.widgets[0].value, this.widgets[0].options.max); + if(this.widgets[0].options.max > 0 && this.widgets[0].value == 0) + this.widgets[0].value = 1; + } + } + } + + if (nodeData.name === 'ImpactMakeImageList' || nodeData.name === 'ImpactMakeImageBatch' || + nodeData.name === 'ImpactMakeMaskList' || nodeData.name === 'ImpactMakeMaskBatch' || + nodeData.name === 'ImpactMakeAnyList' || nodeData.name === 'CombineRegionalPrompts' || + nodeData.name === 'ImpactCombineConditionings' || nodeData.name === 'ImpactConcatConditionings' || + nodeData.name === 'ImpactSEGSConcat' || + nodeData.name === 'ImpactSwitch' || nodeData.name === 'LatentSwitch' || nodeData.name == 'SEGSSwitch') { + var input_name = "input"; + + switch(nodeData.name) { + case 'ImpactMakeImageList': + case 'ImpactMakeImageBatch': + input_name = "image"; + break; + + case 'ImpactMakeMaskList': + case 'ImpactMakeMaskBatch': + input_name = "mask"; + break; + + case 'ImpactMakeAnyList': + input_name = "value"; + break; + + case 'ImpactSEGSConcat': + input_name = "segs"; + break; + + case 'CombineRegionalPrompts': + input_name = "regional_prompts"; + break; + + case 'ImpactCombineConditionings': + case 'ImpactConcatConditionings': + input_name = "conditioning"; + break; + + case 'LatentSwitch': + input_name = "input"; + break; + + case 'SEGSSwitch': + input_name = "input"; + break; + + case 'ImpactSwitch': + input_name = "input"; + } + + const onConnectionsChange = nodeType.prototype.onConnectionsChange; + nodeType.prototype.onConnectionsChange = function (type, index, connected, link_info) { + const stackTrace = new Error().stack; + + // HOTFIX: subgraph + if(stackTrace.includes('convertToSubgraph') || stackTrace.includes('Subgraph.configure')) { + return; + } + + if(stackTrace.includes('loadGraphData')) { + if(this.widgets?.[0]) { + this.widgets[0].options.max = this.inputs.length-3; + this.widgets[0].value = Math.min(this.widgets[0].value, this.widgets[0].options.max); + } + return; + } + + if(stackTrace.includes('pasteFromClipboard')) { + if(this.widgets?.[0]) { + this.widgets[0].options.max = this.inputs.length-3; + this.widgets[0].value = Math.min(this.widgets[0].value, this.widgets[0].options.max); + } + return; + } + + if(!link_info) + return; + + if(type == 2) { + // connect output + if(connected && index == 0){ + if(nodeData.name == 'ImpactSwitch' && app.graph._nodes_by_id[link_info.target_id]?.type == 'Reroute') { + app.graph._nodes_by_id[link_info.target_id].disconnectInput(link_info.target_slot); + } + + if(this.outputs[0].type == '*'){ + if(link_info.type == '*' && app.graph.getNodeById(link_info.target_id).slots[link_info.target_slot].type != '*') { + app.graph._nodes_by_id[link_info.target_id].disconnectInput(link_info.target_slot); + } + else { + // propagate type + this.outputs[0].type = link_info.type; + this.outputs[0].label = link_info.type; + this.outputs[0].name = link_info.type; + + for(let i in this.inputs) { + let input_i = this.inputs[i]; + if(input_i.name != 'select' && input_i.name != 'sel_mode') + input_i.type = link_info.type; + } + } + } + } + + return; + } + else { + if(nodeData.name == 'ImpactSwitch' && app.graph._nodes_by_id[link_info.origin_id]?.type == 'Reroute') + this.disconnectInput(link_info.target_slot); + + // connect input + if(this.inputs[index].name == 'select' || this.inputs[index].name == 'sel_mode') + return; + + if(this.inputs[0].type == '*'){ + const node = app.graph.getNodeById(link_info.origin_id); + + // NOTE: node is undefined when subgraph editing mode + if(node) { + let origin_type = node.outputs[link_info.origin_slot]?.type; + if(link_info.target_slot == 0 && this.inputs.length > 3) { // NOTE: widgets are regarded as input since new front + origin_type = this.inputs[1].type; + node.connect(link_info.origin_slot, node.id, 'input1'); + } + + if(origin_type == '*' && app.graph.getNodeById(link_info.origin_id).slots[link_info.origin_slot].type != '*') { + this.disconnectInput(link_info.target_slot); + return; + } + + for(let i in this.inputs) { + let input_i = this.inputs[i]; + if(input_i.name != 'select' && input_i.name != 'sel_mode') + input_i.type = origin_type; + } + + this.outputs[0].type = origin_type; + this.outputs[0].label = origin_type; + this.outputs[0].name = origin_type; + } + } + } + + let widget_count = 0; + if(nodeData.name == 'ImpactSwitch' || nodeData.name == 'LatentSwitch' || nodeData.name == 'SEGSSwitch') { + widget_count += 1; + } + + if (!connected && (this.inputs.length > widget_count+1)) { + if( + !stackTrace.includes('LGraphNode.prototype.connect') && // for touch device + !stackTrace.includes('LGraphNode.connect') && // for mouse device + !stackTrace.includes('loadGraphData') && + this.inputs[index].name != 'select') { + this.removeInput(index); + } + } + + let slot_i = 1; + for (let i = 0; i < this.inputs.length; i++) { + let input_i = this.inputs[i]; + if(input_i.name != 'select'&& input_i.name != 'sel_mode') { + input_i.name = `${input_name}${slot_i}` + slot_i++; + } + } + + if(connected) { + this.addInput(`${input_name}${slot_i}`, this.outputs[0].type); + } + + if(this.widgets?.[0]) { + this.widgets[0].options.max = this.inputs.length-3; + this.widgets[0].value = Math.min(this.widgets[0].value, this.widgets[0].options.max); + } + } + } + }, + + nodeCreated(node, app) { + if(node.comfyClass == "MaskPainter") { + node.addWidget("button", "Edit mask", null, () => { + ComfyApp.copyToClipspace(node); + ComfyApp.clipspace_return_node = node; + ComfyApp.open_maskeditor(); + }); + } + + switch(node.comfyClass) { + case "ToDetailerPipe": + case "ToDetailerPipeSDXL": + case "BasicPipeToDetailerPipe": + case "BasicPipeToDetailerPipeSDXL": + case "EditDetailerPipe": + case "FaceDetailer": + case "DetailerForEach": + case "DetailerForEachDebug": + case "DetailerForEachPipe": + case "DetailerForEachDebugPipe": + { + for(let i in node.widgets) { + let widget = node.widgets[i]; + if(widget.type === "customtext") { + widget.dynamicPrompts = false; + widget.inputEl.placeholder = "wildcard spec: if kept empty, this option will be ignored"; + widget.serializeValue = () => { + return node.widgets[i].value; + }; + } + } + } + break; + } + + if(node.comfyClass == "ImpactSEGSLabelFilter" || node.comfyClass == "SEGSLabelFilterDetailerHookProvider") { + node.widgets[0].callback = (value, canvas, node, pos, e) => { + if(node) { + if(node.widgets[1].value.trim() != "" && !node.widgets[1].value.trim().endsWith(",")) + node.widgets[1].value += ", " + + node.widgets[1].value += value; + if(node.widgets_values) + node.widgets_values[1] = node.widgets[1].value; + } + } + + Object.defineProperty(node.widgets[0], "value", { + set: (value) => { + node._value = value; + }, + get: () => { + return node._value; + } + }); + } + + if(node.comfyClass == "UltralyticsDetectorProvider") { + let model_name_widget = node.widgets.find((w) => w.name === "model_name"); + let orig_draw = node.onDrawForeground; + node.onDrawForeground = function (ctx) { + const r = orig_draw?.apply?.(this, arguments); + + let is_seg = model_name_widget.value?.startsWith('segm/') || model_name_widget.value?.includes('-seg'); + if(!is_seg) { + var slot_pos = new Float32Array(2); + var pos = node.getConnectionPos(false, 1, slot_pos); + + pos[0] -= node.pos[0] - 10; + pos[1] -= node.pos[1]; + + ctx.beginPath(); + ctx.strokeStyle = "red"; + ctx.lineWidth = 4; + ctx.moveTo(pos[0] - 5, pos[1] - 5); + ctx.lineTo(pos[0] + 5, pos[1] + 5); + ctx.moveTo(pos[0] + 5, pos[1] - 5); + ctx.lineTo(pos[0] - 5, pos[1] + 5); + ctx.stroke(); + } + } + } + + if( + node.comfyClass == "ImpactWildcardEncode" || node.comfyClass == "ImpactWildcardProcessor" + || node.comfyClass == "ToDetailerPipe" || node.comfyClass == "ToDetailerPipeSDXL" + || node.comfyClass == "EditDetailerPipe" || node.comfyClass == "EditDetailerPipeSDXL" + || node.comfyClass == "BasicPipeToDetailerPipe" || node.comfyClass == "BasicPipeToDetailerPipeSDXL") { + node._value = "Select the LoRA to add to the text"; + node._wvalue = "Select the Wildcard to add to the text"; + + var tbox_id = 0; + var combo_id = 3; + var has_lora = true; + + switch(node.comfyClass){ + case "ImpactWildcardEncode": + tbox_id = 0; + combo_id = 3; + break; + + case "ImpactWildcardProcessor": + tbox_id = 0; + combo_id = 4; + has_lora = false; + break; + + case "ToDetailerPipe": + case "ToDetailerPipeSDXL": + case "EditDetailerPipe": + case "EditDetailerPipeSDXL": + case "BasicPipeToDetailerPipe": + case "BasicPipeToDetailerPipeSDXL": + tbox_id = 0; + combo_id = 1; + break; + } + + node.widgets[combo_id+1].callback = (value, canvas, node, pos, e) => { + if(node) { + if(node.widgets[tbox_id].value != '') + node.widgets[tbox_id].value += ', ' + + node.widgets[tbox_id].value += node._wildcard_value; + } + } + + Object.defineProperty(node.widgets[combo_id+1], "value", { + set: (value) => { + if (value !== "Select the Wildcard to add to the text") + node._wildcard_value = value; + }, + get: () => { return "Select the Wildcard to add to the text"; } + }); + + Object.defineProperty(node.widgets[combo_id+1].options, "values", { + set: (x) => {}, + get: () => { + return wildcards_list; + } + }); + + if(has_lora) { + node.widgets[combo_id].callback = (value, canvas, node, pos, e) => { + if(node) { + let lora_name = node._value; + if(lora_name.endsWith('.safetensors')) { + lora_name = lora_name.slice(0, -12); + } + + node.widgets[tbox_id].value += ``; + if(node.widgets_values) { + node.widgets_values[tbox_id] = node.widgets[tbox_id].value; + } + } + } + + Object.defineProperty(node.widgets[combo_id], "value", { + set: (value) => { + if (value !== "Select the LoRA to add to the text") + node._value = value; + }, + + get: () => { return "Select the LoRA to add to the text"; } + }); + } + + // Preventing validation errors from occurring in any situation. + if(has_lora) { + node.widgets[combo_id].serializeValue = () => { return "Select the LoRA to add to the text"; } + } + node.widgets[combo_id+1].serializeValue = () => { return "Select the Wildcard to add to the text"; } + } + + if(node.comfyClass == "ImpactWildcardProcessor" || node.comfyClass == "ImpactWildcardEncode") { + node.widgets[0].inputEl.placeholder = "Wildcard Prompt (User input)"; + node.widgets[1].inputEl.placeholder = "Populated Prompt (Will be generated automatically)"; + node.widgets[1].inputEl.disabled = true; + + const populated_text_widget = node.widgets.find((w) => w.name == 'populated_text'); + const mode_widget = node.widgets.find((w) => w.name == 'mode'); + + // mode combo + Object.defineProperty(mode_widget, "value", { + set: (value) => { + if(value == true) + node._mode_value = "populate"; + else if(value == false) + node._mode_value = "fixed"; + else + node._mode_value = value; // combo value + + populated_text_widget.inputEl.disabled = node._mode_value == 'populate'; + }, + get: () => { + if(node._mode_value != undefined) + return node._mode_value; + else + return 'populate'; + } + }); + } + + if (node.comfyClass == "MaskPainter") { + node.widgets[0].value = '#placeholder'; + + Object.defineProperty(node, "images", { + set: function(value) { + node._images = value; + }, + get: function() { + const id = node.id+""; + if(node.widgets[0].value != '#placeholder') { + var need_invalidate = false; + + if(input_dirty.hasOwnProperty(id) && input_dirty[id]) { + node.widgets[0].value = {...input_tracking[id][1]}; + input_dirty[id] = false; + need_invalidate = true + this._images = app.nodeOutputs[id].images; + } + + let filename = app.nodeOutputs[id]['aux'][1][0]['filename']; + let subfolder = app.nodeOutputs[id]['aux'][1][0]['subfolder']; + let type = app.nodeOutputs[id]['aux'][1][0]['type']; + + let item = + { + image_hash: app.nodeOutputs[id]['aux'][0], + forward_filename: app.nodeOutputs[id]['aux'][1][0]['filename'], + forward_subfolder: app.nodeOutputs[id]['aux'][1][0]['subfolder'], + forward_type: app.nodeOutputs[id]['aux'][1][0]['type'] + }; + + if(node._images) { + app.nodeOutputs[id].images = [{ + ...node._images[0], + ...item + }]; + + node.widgets[0].value = + { + ...node._images[0], + ...item + }; + } + else { + app.nodeOutputs[id].images = [{ + ...item + }]; + + node.widgets[0].value = + { + ...item + }; + } + + if(need_invalidate) { + Promise.all( + app.nodeOutputs[id].images.map((src) => { + return new Promise((r) => { + const img = new Image(); + img.onload = () => r(img); + img.onerror = () => r(null); + img.src = "/view?" + new URLSearchParams(src).toString(); + }); + }) + ).then((imgs) => { + this.imgs = imgs.filter(Boolean); + this.setSizeForImage?.(); + app.graph.setDirtyCanvas(true); + }); + + app.nodeOutputs[id].images[0] = { ...node.widgets[0].value }; + } + + return app.nodeOutputs[id].images; + } + else { + return node._images; + } + } + }); + } + } +}); diff --git a/custom_nodes/comfyui-impact-pack/js/impact-sam-editor.js b/custom_nodes/comfyui-impact-pack/js/impact-sam-editor.js new file mode 100644 index 0000000000000000000000000000000000000000..371987c0b8f9fa7149d48711c3540f14d0768db1 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/js/impact-sam-editor.js @@ -0,0 +1,641 @@ +import { app } from "../../scripts/app.js"; +import { api } from "../../scripts/api.js"; +import { ComfyDialog, $el } from "../../scripts/ui.js"; +import { ComfyApp } from "../../scripts/app.js"; +import { ClipspaceDialog } from "../../extensions/core/clipspace.js"; + +function addMenuHandler(nodeType, cb) { + const getOpts = nodeType.prototype.getExtraMenuOptions; + nodeType.prototype.getExtraMenuOptions = function () { + const r = getOpts.apply(this, arguments); + cb.apply(this, arguments); + return r; + }; +} + +// Helper function to convert a data URL to a Blob object +function dataURLToBlob(dataURL) { + const parts = dataURL.split(';base64,'); + const contentType = parts[0].split(':')[1]; + const byteString = atob(parts[1]); + const arrayBuffer = new ArrayBuffer(byteString.length); + const uint8Array = new Uint8Array(arrayBuffer); + for (let i = 0; i < byteString.length; i++) { + uint8Array[i] = byteString.charCodeAt(i); + } + return new Blob([arrayBuffer], { type: contentType }); +} + +function loadedImageToBlob(image) { + const canvas = document.createElement('canvas'); + + canvas.width = image.width; + canvas.height = image.height; + + const ctx = canvas.getContext('2d'); + + ctx.drawImage(image, 0, 0); + + const dataURL = canvas.toDataURL('image/png', 1); + const blob = dataURLToBlob(dataURL); + + return blob; +} + +async function uploadMask(filepath, formData) { + await api.fetchApi('/upload/mask', { + method: 'POST', + body: formData + }).then(response => {}).catch(error => { + console.error('Error:', error); + }); + + ComfyApp.clipspace.imgs[ComfyApp.clipspace['selectedIndex']] = new Image(); + ComfyApp.clipspace.imgs[ComfyApp.clipspace['selectedIndex']].src = `view?filename=${filepath.filename}&type=${filepath.type}`; + + if(ComfyApp.clipspace.images) + ComfyApp.clipspace.images[ComfyApp.clipspace['selectedIndex']] = filepath; + + ClipspaceDialog.invalidatePreview(); +} + +class ImpactSamEditorDialog extends ComfyDialog { + static instance = null; + + static getInstance() { + if(!ImpactSamEditorDialog.instance) { + ImpactSamEditorDialog.instance = new ImpactSamEditorDialog(); + } + + return ImpactSamEditorDialog.instance; + } + + constructor() { + super(); + this.element = $el("div.comfy-modal", { parent: document.body }, + [ $el("div.comfy-modal-content", + [...this.createButtons()]), + ]); + } + + createButtons() { + return []; + } + + createButton(name, callback) { + var button = document.createElement("button"); + button.innerText = name; + button.addEventListener("click", callback); + return button; + } + + createLeftButton(name, callback) { + var button = this.createButton(name, callback); + button.style.cssFloat = "left"; + button.style.marginRight = "4px"; + return button; + } + + createRightButton(name, callback) { + var button = this.createButton(name, callback); + button.style.cssFloat = "right"; + button.style.marginLeft = "4px"; + return button; + } + + createLeftSlider(self, name, callback) { + const divElement = document.createElement('div'); + divElement.id = "sam-confidence-slider"; + divElement.style.cssFloat = "left"; + divElement.style.fontFamily = "sans-serif"; + divElement.style.marginRight = "4px"; + divElement.style.color = "var(--input-text)"; + divElement.style.backgroundColor = "var(--comfy-input-bg)"; + divElement.style.borderRadius = "8px"; + divElement.style.borderColor = "var(--border-color)"; + divElement.style.borderStyle = "solid"; + divElement.style.fontSize = "15px"; + divElement.style.height = "21px"; + divElement.style.padding = "1px 6px"; + divElement.style.display = "flex"; + divElement.style.position = "relative"; + divElement.style.top = "2px"; + self.confidence_slider_input = document.createElement('input'); + self.confidence_slider_input.setAttribute('type', 'range'); + self.confidence_slider_input.setAttribute('min', '0'); + self.confidence_slider_input.setAttribute('max', '100'); + self.confidence_slider_input.setAttribute('value', '70'); + const labelElement = document.createElement("label"); + labelElement.textContent = name; + + divElement.appendChild(labelElement); + divElement.appendChild(self.confidence_slider_input); + + self.confidence_slider_input.addEventListener("change", callback); + + return divElement; + } + + async detect_and_invalidate_mask_canvas(self) { + const mask_img = await self.detect(self); + + const canvas = self.maskCtx.canvas; + const ctx = self.maskCtx; + + ctx.clearRect(0, 0, canvas.width, canvas.height); + + await new Promise((resolve, reject) => { + self.mask_image = new Image(); + self.mask_image.onload = function() { + ctx.drawImage(self.mask_image, 0, 0, canvas.width, canvas.height); + resolve(); + }; + self.mask_image.onerror = reject; + self.mask_image.src = mask_img.src; + }); + } + + setlayout(imgCanvas, maskCanvas, pointsCanvas) { + const self = this; + + // If it is specified as relative, using it only as a hidden placeholder for padding is recommended + // to prevent anomalies where it exceeds a certain size and goes outside of the window. + var placeholder = document.createElement("div"); + placeholder.style.position = "relative"; + placeholder.style.height = "50px"; + + var bottom_panel = document.createElement("div"); + bottom_panel.style.position = "absolute"; + bottom_panel.style.bottom = "0px"; + bottom_panel.style.left = "20px"; + bottom_panel.style.right = "20px"; + bottom_panel.style.height = "50px"; + + var brush = document.createElement("div"); + brush.id = "sam-brush"; + brush.style.backgroundColor = "blue"; + brush.style.outline = "2px solid pink"; + brush.style.borderRadius = "50%"; + brush.style.MozBorderRadius = "50%"; + brush.style.WebkitBorderRadius = "50%"; + brush.style.position = "absolute"; + brush.style.zIndex = 100; + brush.style.pointerEvents = "none"; + this.brush = brush; + this.element.appendChild(imgCanvas); + this.element.appendChild(maskCanvas); + this.element.appendChild(pointsCanvas); + this.element.appendChild(placeholder); // must below z-index than bottom_panel to avoid covering button + this.element.appendChild(bottom_panel); + document.body.appendChild(brush); + this.brush_size = 5; + + var confidence_slider = this.createLeftSlider(self, "Confidence", (event) => { + self.confidence = event.target.value; + }); + + var clearButton = this.createLeftButton("Clear", () => { + self.maskCtx.clearRect(0, 0, self.maskCanvas.width, self.maskCanvas.height); + self.pointsCtx.clearRect(0, 0, self.pointsCanvas.width, self.pointsCanvas.height); + + self.prompt_points = []; + + self.invalidatePointsCanvas(self); + }); + + var detectButton = this.createLeftButton("Detect", () => self.detect_and_invalidate_mask_canvas(self)); + + var cancelButton = this.createRightButton("Cancel", () => { + document.removeEventListener("mouseup", ImpactSamEditorDialog.handleMouseUp); + document.removeEventListener("keydown", ImpactSamEditorDialog.handleKeyDown); + self.close(); + }); + + self.saveButton = this.createRightButton("Save", () => { + document.removeEventListener("mouseup", ImpactSamEditorDialog.handleMouseUp); + document.removeEventListener("keydown", ImpactSamEditorDialog.handleKeyDown); + self.save(self); + }); + + var undoButton = this.createLeftButton("Undo", () => { + if(self.prompt_points.length > 0) { + self.prompt_points.pop(); + self.pointsCtx.clearRect(0, 0, self.pointsCanvas.width, self.pointsCanvas.height); + self.invalidatePointsCanvas(self); + } + }); + + bottom_panel.appendChild(clearButton); + bottom_panel.appendChild(detectButton); + bottom_panel.appendChild(self.saveButton); + bottom_panel.appendChild(cancelButton); + bottom_panel.appendChild(confidence_slider); + bottom_panel.appendChild(undoButton); + + imgCanvas.style.position = "relative"; + imgCanvas.style.top = "200"; + imgCanvas.style.left = "0"; + + maskCanvas.style.position = "absolute"; + maskCanvas.style.opacity = 0.5; + pointsCanvas.style.position = "absolute"; + } + + show() { + this.mask_image = null; + self.prompt_points = []; + + this.message_box = $el("p", ["Please wait a moment while the SAM model and the image are being loaded."]); + this.element.appendChild(this.message_box); + + if(self.imgCtx) { + self.imgCtx.clearRect(0, 0, self.imageCanvas.width, self.imageCanvas.height); + } + + const target_image_path = ComfyApp.clipspace.imgs[ComfyApp.clipspace['selectedIndex']].src; + this.load_sam(target_image_path); + + if(!this.is_layout_created) { + // layout + const imgCanvas = document.createElement('canvas'); + const maskCanvas = document.createElement('canvas'); + const pointsCanvas = document.createElement('canvas'); + + imgCanvas.id = "imageCanvas"; + maskCanvas.id = "samEditorMaskCanvas"; + pointsCanvas.id = "pointsCanvas"; + + this.setlayout(imgCanvas, maskCanvas, pointsCanvas); + + // prepare content + this.imgCanvas = imgCanvas; + this.maskCanvas = maskCanvas; + this.pointsCanvas = pointsCanvas; + this.maskCtx = maskCanvas.getContext('2d'); + this.pointsCtx = pointsCanvas.getContext('2d'); + + this.is_layout_created = true; + + // replacement of onClose hook since close is not real close + const self = this; + const observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + if (mutation.type === 'attributes' && mutation.attributeName === 'style') { + if(self.last_display_style && self.last_display_style != 'none' && self.element.style.display == 'none') { + ComfyApp.onClipspaceEditorClosed(); + } + + self.last_display_style = self.element.style.display; + } + }); + }); + + const config = { attributes: true }; + observer.observe(this.element, config); + } + + this.setImages(target_image_path, this.imgCanvas, this.pointsCanvas); + + if(ComfyApp.clipspace_return_node) { + this.saveButton.innerText = "Save to node"; + } + else { + this.saveButton.innerText = "Save"; + } + this.saveButton.disabled = true; + + this.element.style.display = "block"; + this.element.style.zIndex = 8888; // NOTE: alert dialog must be high priority. + } + + updateBrushPreview(self, event) { + event.preventDefault(); + + const centerX = event.pageX; + const centerY = event.pageY; + + const brush = self.brush; + + brush.style.width = self.brush_size * 2 + "px"; + brush.style.height = self.brush_size * 2 + "px"; + brush.style.left = (centerX - self.brush_size) + "px"; + brush.style.top = (centerY - self.brush_size) + "px"; + } + + setImages(target_image_path, imgCanvas, pointsCanvas) { + const imgCtx = imgCanvas.getContext('2d'); + const maskCtx = this.maskCtx; + const maskCanvas = this.maskCanvas; + + const self = this; + + // image load + const orig_image = new Image(); + window.addEventListener("resize", () => { + // repositioning + imgCanvas.width = window.innerWidth - 250; + imgCanvas.height = window.innerHeight - 200; + + // redraw image + let drawWidth = orig_image.width; + let drawHeight = orig_image.height; + + if (orig_image.width > imgCanvas.width) { + drawWidth = imgCanvas.width; + drawHeight = (drawWidth / orig_image.width) * orig_image.height; + } + + if (drawHeight > imgCanvas.height) { + drawHeight = imgCanvas.height; + drawWidth = (drawHeight / orig_image.height) * orig_image.width; + } + + imgCtx.drawImage(orig_image, 0, 0, drawWidth, drawHeight); + + // update mask + let w = (drawWidth * imgCanvas.clientWidth/imgCanvas.width) + "px"; + let h = (drawHeight * imgCanvas.clientHeight/imgCanvas.height) + "px"; + + pointsCanvas.width = drawWidth * imgCanvas.clientWidth/imgCanvas.width; + pointsCanvas.height = drawHeight * imgCanvas.clientHeight/imgCanvas.height; + pointsCanvas.style.top = imgCanvas.offsetTop + "px"; + pointsCanvas.style.left = imgCanvas.offsetLeft + "px"; + + maskCanvas.width = pointsCanvas.width; + maskCanvas.height = pointsCanvas.height; + maskCanvas.style.top = imgCanvas.offsetTop + "px"; + maskCanvas.style.left = imgCanvas.offsetLeft + "px"; + + self.invalidateMaskCanvas(self); + self.invalidatePointsCanvas(self); + }); + + // original image load + orig_image.onload = () => self.onLoaded(self); + const rgb_url = new URL(target_image_path); + rgb_url.searchParams.delete('channel'); + rgb_url.searchParams.set('channel', 'rgb'); + orig_image.src = rgb_url; + self.image = orig_image; + } + + onLoaded(self) { + if(self.message_box) { + self.element.removeChild(self.message_box); + self.message_box = null; + } + + window.dispatchEvent(new Event('resize')); + + self.setEventHandler(pointsCanvas); + self.saveButton.disabled = false; + } + + setEventHandler(targetCanvas) { + targetCanvas.addEventListener("contextmenu", (event) => { + event.preventDefault(); + }); + + const self = this; + targetCanvas.addEventListener('pointermove', (event) => this.updateBrushPreview(self,event)); + targetCanvas.addEventListener('pointerdown', (event) => this.handlePointerDown(self,event)); + targetCanvas.addEventListener('pointerover', (event) => { this.brush.style.display = "block"; }); + targetCanvas.addEventListener('pointerleave', (event) => { this.brush.style.display = "none"; }); + document.addEventListener('keydown', ImpactSamEditorDialog.handleKeyDown); + } + + static handleKeyDown(event) { + const self = ImpactSamEditorDialog.instance; + if (event.key === '=') { // positive + brush.style.backgroundColor = "blue"; + brush.style.outline = "2px solid pink"; + self.is_positive_mode = true; + } else if (event.key === '-') { // negative + brush.style.backgroundColor = "red"; + brush.style.outline = "2px solid skyblue"; + self.is_positive_mode = false; + } + } + + is_positive_mode = true; + prompt_points = []; + confidence = 70; + + invalidatePointsCanvas(self) { + const ctx = self.pointsCtx; + + for (const i in self.prompt_points) { + const [is_positive, x, y] = self.prompt_points[i]; + + const scaledX = x * ctx.canvas.width / self.image.width; + const scaledY = y * ctx.canvas.height / self.image.height; + + if(is_positive) + ctx.fillStyle = "blue"; + else + ctx.fillStyle = "red"; + ctx.beginPath(); + ctx.arc(scaledX, scaledY, 3, 0, 3 * Math.PI); + ctx.fill(); + } + } + + invalidateMaskCanvas(self) { + if(self.mask_image) { + self.maskCtx.clearRect(0, 0, self.maskCanvas.width, self.maskCanvas.height); + self.maskCtx.drawImage(self.mask_image, 0, 0, self.maskCanvas.width, self.maskCanvas.height); + } + } + + async load_sam(url) { + const parsedUrl = new URL(url); + const searchParams = new URLSearchParams(parsedUrl.search); + + const filename = searchParams.get("filename") || ""; + const fileType = searchParams.get("type") || ""; + const subfolder = searchParams.get("subfolder") || ""; + + const data = { + sam_model_name: "auto", + filename: filename, + type: fileType, + subfolder: subfolder + }; + + api.fetchApi('/sam/prepare', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(data) + }); + } + + async detect(self) { + const positive_points = []; + const negative_points = []; + + for(const i in self.prompt_points) { + const [is_positive, x, y] = self.prompt_points[i]; + const point = [x,y]; + if(is_positive) { + positive_points.push(point); + } + else + negative_points.push(point); + } + + const data = { + positive_points: positive_points, + negative_points: negative_points, + threshold: self.confidence/100 + }; + + const response = await api.fetchApi('/sam/detect', { + method: 'POST', + headers: { 'Content-Type': 'image/png' }, + body: JSON.stringify(data) + }); + + const blob = await response.blob(); + const url = URL.createObjectURL(blob); + + return new Promise((resolve, reject) => { + const image = new Image(); + image.onload = () => resolve(image); + image.onerror = reject; + image.src = url; + }); + } + + handlePointerDown(self, event) { + if ([0, 2, 5].includes(event.button)) { + event.preventDefault(); + const x = event.offsetX || event.targetTouches[0].clientX - maskRect.left; + const y = event.offsetY || event.targetTouches[0].clientY - maskRect.top; + + const originalX = x * self.image.width / self.pointsCanvas.clientWidth; + const originalY = y * self.image.height / self.pointsCanvas.clientHeight; + + var point = null; + if (event.button == 0) { + // positive + point = [true, originalX, originalY]; + } else { + // negative + point = [false, originalX, originalY]; + } + + self.prompt_points.push(point); + + self.invalidatePointsCanvas(self); + } + } + + async save(self) { + if(!self.mask_image) { + this.close(); + return; + } + + const save_canvas = document.createElement('canvas'); + + const save_ctx = save_canvas.getContext('2d', {willReadFrequently:true}); + save_canvas.width = self.mask_image.width; + save_canvas.height = self.mask_image.height; + + save_ctx.drawImage(self.mask_image, 0, 0, save_canvas.width, save_canvas.height); + + const save_data = save_ctx.getImageData(0, 0, save_canvas.width, save_canvas.height); + + // refine mask image + for (let i = 0; i < save_data.data.length; i += 4) { + if(save_data.data[i]) { + save_data.data[i+3] = 0; + } + else { + save_data.data[i+3] = 255; + } + + save_data.data[i] = 0; + save_data.data[i+1] = 0; + save_data.data[i+2] = 0; + } + + save_ctx.globalCompositeOperation = 'source-over'; + save_ctx.putImageData(save_data, 0, 0); + + const formData = new FormData(); + const filename = "clipspace-mask-" + performance.now() + ".png"; + + const item = + { + "filename": filename, + "subfolder": "", + "type": "temp", + }; + + if(ComfyApp.clipspace.images) + ComfyApp.clipspace.images[0] = item; + + if(ComfyApp.clipspace.widgets) { + const index = ComfyApp.clipspace.widgets.findIndex(obj => obj.name === 'image'); + + if(index >= 0) + ComfyApp.clipspace.widgets[index].value = `${filename} [temp]`; + } + + const dataURL = save_canvas.toDataURL(); + const blob = dataURLToBlob(dataURL); + + let original_url = new URL(this.image.src); + + const original_ref = { filename: original_url.searchParams.get('filename') }; + + let original_subfolder = original_url.searchParams.get("subfolder"); + if(original_subfolder) + original_ref.subfolder = original_subfolder; + + let original_type = original_url.searchParams.get("type"); + if(original_type) + original_ref.type = original_type; + + formData.append('image', blob, filename); + formData.append('original_ref', JSON.stringify(original_ref)); + formData.append('type', "temp"); + + await uploadMask(item, formData); + ComfyApp.onClipspaceEditorSave(); + this.close(); + } +} + +app.registerExtension({ + name: "Comfy.Impact.SAMEditor", + init(app) { + const callback = + function () { + let dlg = ImpactSamEditorDialog.getInstance(); + dlg.show(); + }; + + const context_predicate = () => ComfyApp.clipspace && ComfyApp.clipspace.imgs && ComfyApp.clipspace.imgs.length > 0 + ClipspaceDialog.registerButton("Impact SAM Detector", context_predicate, callback); + }, + + async beforeRegisterNodeDef(nodeType, nodeData, app) { + if (Array.isArray(nodeData.output) && (nodeData.output.includes("MASK") || nodeData.output.includes("IMAGE"))) { + addMenuHandler(nodeType, function (_, options) { + options.unshift({ + content: "Open in SAM Detector", + callback: () => { + ComfyApp.copyToClipspace(this); + ComfyApp.clipspace_return_node = this; + + let dlg = ImpactSamEditorDialog.getInstance(); + dlg.show(); + }, + }); + }); + } + } +}); + diff --git a/custom_nodes/comfyui-impact-pack/js/impact-segs-picker.js b/custom_nodes/comfyui-impact-pack/js/impact-segs-picker.js new file mode 100644 index 0000000000000000000000000000000000000000..01319f072923294d9a531aa296435ffa78eafe2a --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/js/impact-segs-picker.js @@ -0,0 +1,182 @@ +import { ComfyApp, app } from "../../scripts/app.js"; +import { ComfyDialog, $el } from "../../scripts/ui.js"; +import { api } from "../../scripts/api.js"; + +async function open_picker(node) { + const resp = await api.fetchApi(`/impact/segs/picker/count?id=${node.id}`); + const body = await resp.text(); + + let cnt = parseInt(body); + + var existingPicker = document.getElementById('impact-picker'); + if (existingPicker) { + existingPicker.parentNode.removeChild(existingPicker); + } + + var gallery = document.createElement('div'); + gallery.id = 'impact-picker'; + + gallery.style.position = "absolute"; + gallery.style.height = "80%"; + gallery.style.width = "80%"; + gallery.style.top = "10%"; + gallery.style.left = "10%"; + gallery.style.display = 'flex'; + gallery.style.flexWrap = 'wrap'; + gallery.style.maxHeight = '600px'; + gallery.style.overflow = 'auto'; + gallery.style.backgroundColor = 'rgba(0,0,0,0.3)'; + gallery.style.padding = '20px'; + gallery.draggable = false; + gallery.style.zIndex = 5000; + + var doneButton = document.createElement('button'); + doneButton.textContent = 'Done'; + doneButton.style.padding = '10px 10px'; + doneButton.style.border = 'none'; + doneButton.style.borderRadius = '5px'; + doneButton.style.fontFamily = 'Arial, sans-serif'; + doneButton.style.fontSize = '16px'; + doneButton.style.fontWeight = 'bold'; + doneButton.style.color = '#fff'; + doneButton.style.background = 'linear-gradient(to bottom, #0070B8, #003D66)'; + doneButton.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.4)'; + doneButton.style.margin = "20px"; + doneButton.style.height = "40px"; + + var cancelButton = document.createElement('button'); + cancelButton.textContent = 'Cancel'; + cancelButton.style.padding = '10px 10px'; + cancelButton.style.border = 'none'; + cancelButton.style.borderRadius = '5px'; + cancelButton.style.fontFamily = 'Arial, sans-serif'; + cancelButton.style.fontSize = '16px'; + cancelButton.style.fontWeight = 'bold'; + cancelButton.style.color = '#fff'; + cancelButton.style.background = 'linear-gradient(to bottom, #ff70B8, #ff3D66)'; + cancelButton.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.4)'; + cancelButton.style.margin = "20px"; + cancelButton.style.height = "40px"; + + const w = node.widgets.find((w) => w.name == 'picks'); + let prev_selected = w.value.split(',').map(function(item) { + return parseInt(item, 10); + }); + + let images = []; + doneButton.onclick = () => { + var result = ''; + for(let i in images) { + if(images[i].isSelected) { + if(result != '') + result += ', '; + + result += (parseInt(i)+1); + } + } + + w.value = result; + + gallery.parentNode.removeChild(gallery); + } + + cancelButton.onclick = () => { + gallery.parentNode.removeChild(gallery); + } + + var panel = document.createElement('div'); + panel.style.clear = 'both'; + panel.style.width = '100%'; + panel.style.height = '40px'; + panel.style.justifyContent = 'center'; + panel.style.alignItems = 'center'; + panel.style.display = 'flex'; + panel.appendChild(doneButton); + panel.appendChild(cancelButton); + gallery.appendChild(panel); + + var hint = document.createElement('label'); + hint.style.position = 'absolute'; + hint.innerHTML = 'Click: Toggle Selection
Ctrl-click: Single Selection'; + gallery.appendChild(hint); + + let max_size = 300; + + for(let i=0; i image.naturalHeight) { + ratio = max_size/image.naturalWidth; + } + else { + ratio = max_size/image.naturalHeight; + } + + let width = image.naturalWidth * ratio; + let height = image.naturalHeight * ratio; + + if(width < height) { + this.style.marginLeft = (200-width)/2+"px"; + } + else{ + this.style.marginTop = (200-height)/2+"px"; + } + + this.style.width = width+"px"; + this.style.height = height+"px"; + this.style.objectFit = 'cover'; + } + + image.addEventListener('click', function(event) { + if(event.ctrlKey) { + for(let i in images) { + if(images[i].isSelected) { + images[i].style.border = 'none'; + images[i].isSelected = false; + } + } + + image.style.border = '2px solid #006699'; + image.isSelected = true; + + return; + } + + if(image.isSelected) { + image.style.border = 'none'; + image.isSelected = false; + } + else { + image.style.border = '2px solid #006699'; + image.isSelected = true; + } + }); + + gallery.appendChild(image); + } + + document.body.appendChild(gallery); +} + + +app.registerExtension({ + name: "Comfy.Impack.Picker", + + nodeCreated(node, app) { + if(node.comfyClass == "ImpactSEGSPicker") { + node.addWidget("button", "pick", "image", () => { + open_picker(node); + }); + } + } +}); \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/js/mask-rect-area-advanced.js b/custom_nodes/comfyui-impact-pack/js/mask-rect-area-advanced.js new file mode 100644 index 0000000000000000000000000000000000000000..7f7e638d6eb5b6509e4a6d75c69de24a2545b0e1 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/js/mask-rect-area-advanced.js @@ -0,0 +1,381 @@ +import { app } from "../../scripts/app.js"; +function showPreviewCanvas(node, app) { + + const widget = { + type: "customCanvas", + name: "mask-rect-area-canvas", + get value() { + return this.canvas.value; + }, + set value(x) { + this.canvas.value = x; + }, + draw: function (ctx, node, widgetWidth, widgetY) { + + // If we are initially offscreen when created we wont have received a resize event + // Calculate it here instead + if (!node.canvasHeight) { + computeCanvasSize(node, node.size); + } + + const visible = true; + const t = ctx.getTransform(); + const margin = 12; + const border = 2; + const widgetHeight = node.canvasHeight; + const width = Math.round(node.properties["width"]); + const height = Math.round(node.properties["height"]); + const scale = Math.min((widgetWidth - margin * 3) / width, (widgetHeight - margin * 3) / height); + const blurRadius = node.properties["blur_radius"] || 0; + const index = 0; + + Object.assign(this.canvas.style, { + left: `${t.e}px`, + top: `${t.f + (widgetY * t.d)}px`, + width: `${widgetWidth * t.a}px`, + height: `${widgetHeight * t.d}px`, + position: "absolute", + zIndex: 1, + fontSize: `${t.d * 10.0}px`, + pointerEvents: "none" + }); + + this.canvas.hidden = !visible; + + let backgroundWidth = width * scale; + let backgroundHeight = height * scale; + + let xOffset = margin; + if (backgroundWidth < widgetWidth) { + xOffset += (widgetWidth - backgroundWidth) / 2 - margin; + } + let yOffset = (margin / 2); + if (backgroundHeight < widgetHeight) { + yOffset += (widgetHeight - backgroundHeight) / 2 - margin; + } + + let widgetX = xOffset; + widgetY = widgetY + yOffset; + + // Draw the background border + ctx.fillStyle = globalThis.LiteGraph.WIDGET_OUTLINE_COLOR; + ctx.fillRect(widgetX - border, widgetY - border, backgroundWidth + border * 2, backgroundHeight + border * 2) + + // Draw the main background area + ctx.fillStyle = globalThis.LiteGraph.WIDGET_BGCOLOR; + ctx.fillRect(widgetX, widgetY, backgroundWidth, backgroundHeight); + + // Draw the conditioning zone + let [x, y, w, h] = getDrawArea(node, backgroundWidth, backgroundHeight); + + ctx.fillStyle = getDrawColor(0, "80"); + ctx.fillRect(widgetX + x, widgetY + y, w, h); + ctx.beginPath(); + ctx.lineWidth = 1; + + // Draw grid lines + for (let x = 0; x <= width / 64; x += 1) { + ctx.moveTo(widgetX + x * 64 * scale, widgetY); + ctx.lineTo(widgetX + x * 64 * scale, widgetY + backgroundHeight); + } + + for (let y = 0; y <= height / 64; y += 1) { + ctx.moveTo(widgetX, widgetY + y * 64 * scale); + ctx.lineTo(widgetX + backgroundWidth, widgetY + y * 64 * scale); + } + + ctx.strokeStyle = "#66666650"; + ctx.stroke(); + ctx.closePath(); + + // Draw current zone + let [sx, sy, sw, sh] = getDrawArea(node, backgroundWidth, backgroundHeight); + + ctx.fillStyle = getDrawColor(0, "80"); + ctx.fillRect(widgetX + sx, widgetY + sy, sw, sh); + + ctx.fillStyle = getDrawColor(0, "40"); + ctx.fillRect(widgetX + sx + border, widgetY + sy + border, sw - border * 2, sh - border * 2); + + // Draw white border around the current zone + ctx.strokeStyle = globalThis.LiteGraph.NODE_SELECTED_TITLE_COLOR; + ctx.lineWidth = 2; + ctx.strokeRect(widgetX + sx, widgetY + sy, sw, sh); + + // Display + ctx.beginPath(); + + ctx.arc(LiteGraph.NODE_SLOT_HEIGHT * 0.5, LiteGraph.NODE_SLOT_HEIGHT * (index + 0.5) + 4, 4, 0, Math.PI * 2); + ctx.fill(); + + ctx.lineWidth = 1; + ctx.strokeStyle = "white"; + ctx.stroke(); + + ctx.lineWidth = 1; + ctx.closePath(); + + // Draw progress bar canvas + if (backgroundWidth < widgetWidth) { + xOffset += (widgetWidth - backgroundWidth) / 2 - margin; + } + + // Ajustar las coordenadas X e Y + const barHeight = 8; + let widgetYBar = widgetY + backgroundHeight + margin; + + // Dibujar el borde negro alrededor de la barra + ctx.fillStyle = globalThis.LiteGraph.WIDGET_OUTLINE_COLOR; + ctx.fillRect( + widgetX - border, + widgetYBar - border, + backgroundWidth + border * 2, + barHeight + border * 2 + ); + + // Dibujar el área principal de la barra (fondo) + ctx.fillStyle = globalThis.LiteGraph.WIDGET_BGCOLOR; // Mismo color de fondo que el canvas + ctx.fillRect( + widgetX, + widgetYBar, + backgroundWidth, + barHeight + ); + + + // Draw progress bar grid + ctx.beginPath(); + ctx.lineWidth = 1; + ctx.strokeStyle = "#66666650"; + + // Calcular el número de líneas en función del tamaño de la barra + const numLines = Math.floor(backgroundWidth / 64); + + // Dibujar líneas del grid + for (let x = 0; x <= width / 64; x += 1) { + ctx.moveTo(widgetX + x * 64 * scale, widgetYBar); + ctx.lineTo(widgetX + x * 64 * scale, widgetYBar + barHeight); + } + ctx.stroke(); + ctx.closePath(); + + // Dibujar progreso (basado en blur_radius) + const progress = Math.min(blurRadius / 255, 1); + ctx.fillStyle = "rgba(0, 120, 255, 0.5)"; + + ctx.fillRect( + widgetX, + widgetYBar, + backgroundWidth * progress, + barHeight + ); + } + }; + + widget.canvas = document.createElement("canvas"); + widget.canvas.className = "mask-rect-area-canvas"; + widget.parent = node; + + document.body.appendChild(widget.canvas); + node.addCustomWidget(widget); + + app.canvas.onDrawBackground = function () { + // Draw node isnt fired once the node is off the screen + // if it goes off screen quickly, the input may not be removed + // this shifts it off screen so it can be moved back if the node is visible. + for (let n in app.graph._nodes) { + n = app.graph._nodes[n]; + for (let w in n.widgets) { + let wid = n.widgets[w]; + if (Object.hasOwn(wid, "canvas")) { + wid.canvas.style.left = -8000 + "px"; + wid.canvas.style.position = "absolute"; + } + } + } + }; + + node.onResize = function (size) { + computeCanvasSize(node, size); + }; + + return {minWidth: 200, minHeight: 200, widget}; +} + +app.registerExtension({ + name: 'drltdata.MaskRectAreaAdvanced', + async beforeRegisterNodeDef(nodeType, nodeData, app) { + if (nodeData.name === "MaskRectAreaAdvanced") { + const onNodeCreated = nodeType.prototype.onNodeCreated; + nodeType.prototype.onNodeCreated = function () { + const r = onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined; + + this.setProperty("width", 512); + this.setProperty("height", 512); + this.setProperty("x", 0); + this.setProperty("y", 0); + this.setProperty("w", 256); + this.setProperty("h", 256); + this.setProperty("blur_radius", 0); + + this.selected = false; + this.index = 3; + this.serialize_widgets = true; + + CUSTOM_INT(this, "x", 0, function (v, _, node) { + const s = this.options.step / 10; + this.value = Math.round(v / s) * s; + node.properties["x"] = this.value; + }); + CUSTOM_INT(this, "y", 0, function (v, _, node) { + const s = this.options.step / 10; + this.value = Math.round(v / s) * s; + node.properties["y"] = this.value; + }); + CUSTOM_INT(this, "width", 256, function (v, _, node) { + const s = this.options.step / 10; + this.value = Math.round(v / s) * s; + node.properties["w"] = this.value; + }); + CUSTOM_INT(this, "height", 256, function (v, _, node) { + const s = this.options.step / 10; + this.value = Math.round(v / s) * s; + node.properties["h"] = this.value; + }); + CUSTOM_INT(this, "image_width", 512, function (v, _, node) { + const s = this.options.step / 10; + this.value = Math.round(v / s) * s; + node.properties["width"] = this.value; + }); + CUSTOM_INT(this, "image_height", 512, function (v, _, node) { + const s = this.options.step / 10; + this.value = Math.round(v / s) * s; + node.properties["height"] = this.value; + }); + CUSTOM_INT(this, "blur_radius", 0, function (v, _, node) { + this.value = Math.round(v) || 0; + node.properties["blur_radius"] = this.value; + }, + {"min": 0, "max": 255, "step": 10} + ); + + showPreviewCanvas(this, app); + + this.onSelected = function () { + this.selected = true; + }; + this.onDeselected = function () { + this.selected = false; + }; + + return r; + }; + } + } +}); + +// Calculate the drawing area using individual properties. +function getDrawArea(node, backgroundWidth, backgroundHeight) { + let x = node.properties["x"] * backgroundWidth / node.properties["width"]; + let y = node.properties["y"] * backgroundHeight / node.properties["height"]; + let w = node.properties["w"] * backgroundWidth / node.properties["width"]; + let h = node.properties["h"] * backgroundHeight / node.properties["height"]; + + if (x > backgroundWidth) { + x = backgroundWidth; + } + if (y > backgroundHeight) { + y = backgroundHeight; + } + + if (x + w > backgroundWidth) { + w = Math.max(0, backgroundWidth - x); + } + + if (y + h > backgroundHeight) { + h = Math.max(0, backgroundHeight - y); + } + + return [x, y, w, h]; +} + +function CUSTOM_INT(node, inputName, val, func, config = {}) { + return { + widget: node.addWidget( + "number", + inputName, + val, + func, + Object.assign({}, {min: 0, max: 4096, step: 640, precision: 0}, config) + ) + }; +} + +function getDrawColor(percent, alpha) { + let h = 360 * percent; + let s = 50; + let l = 50; + l /= 100; + const a = s * Math.min(l, 1 - l) / 100; + const f = n => { + const k = (n + h / 30) % 12; + const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); + return Math.round(255 * color).toString(16).padStart(2, '0'); // convert to Hex and prefix "0" if needed + }; + return `#${f(0)}${f(8)}${f(4)}${alpha}`; +} + +function computeCanvasSize(node, size) { + if (node.widgets[0].last_y == null) { + return; + } + + const MIN_HEIGHT = 220; + const MIN_WIDTH = 240; + + let y = LiteGraph.NODE_WIDGET_HEIGHT * Math.max(node.inputs.length, node.outputs.length) + 5; + let freeSpace = size[1] - y; + + // Compute the height of all non-customCanvas widgets + let widgetHeight = 0; + for (let i = 0; i < node.widgets.length; i++) { + const w = node.widgets[i]; + if (w.type !== "customCanvas") { + if (w.computeSize) { + widgetHeight += w.computeSize()[1] + 4; + } else { + widgetHeight += LiteGraph.NODE_WIDGET_HEIGHT + 5; + } + } + } + + // Ensure there is enough vertical space + freeSpace -= widgetHeight; + + // Adjust the height of the node if needed + if (freeSpace < MIN_HEIGHT) { + freeSpace = MIN_HEIGHT; + node.size[1] = y + widgetHeight + freeSpace; + node.graph.setDirtyCanvas(true); + } + + // Ensure the node width meets the minimum width requirement + if (node.size[0] < MIN_WIDTH) { + node.size[0] = MIN_WIDTH; + node.graph.setDirtyCanvas(true); + } + + // Position each of the widgets + for (const w of node.widgets) { + w.y = y; + if (w.type === "customCanvas") { + y += freeSpace; + } else if (w.computeSize) { + y += w.computeSize()[1] + 4; + } else { + y += LiteGraph.NODE_WIDGET_HEIGHT + 4; + } + } + + node.canvasHeight = freeSpace; +} diff --git a/custom_nodes/comfyui-impact-pack/js/mask-rect-area.js b/custom_nodes/comfyui-impact-pack/js/mask-rect-area.js new file mode 100644 index 0000000000000000000000000000000000000000..8605f719758dbfcfcd6f916c007b8fcf490d0654 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/js/mask-rect-area.js @@ -0,0 +1,366 @@ +import { app } from "../../scripts/app.js"; +function showPreviewCanvas(node, app) { + + const widget = { + type: "customCanvas", + name: "mask-rect-area-canvas", + get value() { + return this.canvas.value; + }, + set value(x) { + this.canvas.value = x; + }, + draw: function (ctx, node, widgetWidth, widgetY) { + + // If we are initially offscreen when created we wont have received a resize event + // Calculate it here instead + if (!node.canvasHeight) { + computeCanvasSize(node, node.size); + } + + const visible = true; + const t = ctx.getTransform(); + const margin = 12; + const border = 2; + const widgetHeight = node.canvasHeight; + const width = 512; + const height = 512; + const scale = Math.min((widgetWidth - margin * 3) / width, (widgetHeight - margin * 3) / height); + const blurRadius = node.properties["blur_radius"] || 0; + const index = 0; + + Object.assign(this.canvas.style, { + left: `${t.e}px`, + top: `${t.f + (widgetY * t.d)}px`, + width: `${widgetWidth * t.a}px`, + height: `${widgetHeight * t.d}px`, + position: "absolute", + zIndex: 1, + fontSize: `${t.d * 10.0}px`, + pointerEvents: "none" + }); + + this.canvas.hidden = !visible; + + let backgroundWidth = width * scale; + let backgroundHeight = height * scale; + let xOffset = margin; + if (backgroundWidth < widgetWidth) { + xOffset += (widgetWidth - backgroundWidth) / 2 - margin; + } + let yOffset = (margin / 2); + if (backgroundHeight < widgetHeight) { + yOffset += (widgetHeight - backgroundHeight) / 2 - margin; + } + + let widgetX = xOffset; + widgetY = widgetY + yOffset; + + // Draw the background border + ctx.fillStyle = globalThis.LiteGraph.WIDGET_OUTLINE_COLOR; + ctx.fillRect(widgetX - border, widgetY - border, backgroundWidth + border * 2, backgroundHeight + border * 2); + + // Draw the main background area + ctx.fillStyle = globalThis.LiteGraph.WIDGET_BGCOLOR; + ctx.fillRect(widgetX, widgetY, backgroundWidth, backgroundHeight); + + // Draw the conditioning zone + let [x, y, w, h] = getDrawArea(node, backgroundWidth, backgroundHeight); + + ctx.fillStyle = getDrawColor(0, "80"); + ctx.fillRect(widgetX + x, widgetY + y, w, h); + ctx.beginPath(); + ctx.lineWidth = 1; + + // Draw grid lines + for (let x = 0; x <= width / 64; x += 1) { + ctx.moveTo(widgetX + x * 64 * scale, widgetY); + ctx.lineTo(widgetX + x * 64 * scale, widgetY + backgroundHeight); + } + + for (let y = 0; y <= height / 64; y += 1) { + ctx.moveTo(widgetX, widgetY + y * 64 * scale); + ctx.lineTo(widgetX + backgroundWidth, widgetY + y * 64 * scale); + } + + ctx.strokeStyle = "#66666650"; + ctx.stroke(); + ctx.closePath(); + + // Draw current zone + let [sx, sy, sw, sh] = getDrawArea(node, backgroundWidth, backgroundHeight); + + ctx.fillStyle = getDrawColor(0, "80"); + ctx.fillRect(widgetX + sx, widgetY + sy, sw, sh); + + ctx.fillStyle = getDrawColor(0, "40"); + ctx.fillRect(widgetX + sx + border, widgetY + sy + border, sw - border * 2, sh - border * 2); + + // Draw white border around the current zone + ctx.strokeStyle = globalThis.LiteGraph.NODE_SELECTED_TITLE_COLOR; + ctx.lineWidth = 2; + ctx.strokeRect(widgetX + sx, widgetY + sy, sw, sh); + //ctx.strokeRect(finalSX, finalSY, finalSW, finalSH); + + // Display + ctx.beginPath(); + + ctx.arc(LiteGraph.NODE_SLOT_HEIGHT * 0.5, LiteGraph.NODE_SLOT_HEIGHT * (index + 0.5) + 4, 4, 0, Math.PI * 2); + ctx.fill(); + + ctx.lineWidth = 1; + ctx.strokeStyle = "white"; + ctx.stroke(); + ctx.lineWidth = 1; + ctx.closePath(); + + // Draw progress bar canvas + if (backgroundWidth < widgetWidth) { + xOffset += (widgetWidth - backgroundWidth) / 2 - margin; + } + + const barHeight = 8; + let widgetYBar = widgetY + backgroundHeight + margin; + + // Draw progress bar border + ctx.fillStyle = globalThis.LiteGraph.WIDGET_OUTLINE_COLOR; + ctx.fillRect( + widgetX - border, + widgetYBar - border, + backgroundWidth + border * 2, + barHeight + border * 2 + ); + + // Draw progress bar area + ctx.fillStyle = globalThis.LiteGraph.WIDGET_BGCOLOR; // Mismo color de fondo que el canvas + ctx.fillRect( + widgetX, + widgetYBar, + backgroundWidth, + barHeight + ); + + // Draw progress bar grid + ctx.beginPath(); + ctx.lineWidth = 1; + ctx.strokeStyle = "#66666650"; + + // Determine max lines + const numLines = Math.floor(backgroundWidth / 64); + + // Draw progress bar grid + for (let x = 0; x <= width / 64; x += 1) { + ctx.moveTo(widgetX + x * 64 * scale, widgetYBar); + ctx.lineTo(widgetX + x * 64 * scale, widgetYBar + barHeight); + } + ctx.stroke(); + ctx.closePath(); + + // Draw progress bar + const progress = Math.min(blurRadius / 255, 1); + ctx.fillStyle = "rgba(0, 120, 255, 0.5)"; + + ctx.fillRect( + widgetX, + widgetYBar, + backgroundWidth * progress, + barHeight + ); + } + }; + + widget.canvas = document.createElement("canvas"); + widget.canvas.className = "mask-rect-area-canvas"; + widget.parent = node; + + document.body.appendChild(widget.canvas); + node.addCustomWidget(widget); + + app.canvas.onDrawBackground = function () { + // Draw node isnt fired once the node is off the screen + // if it goes off screen quickly, the input may not be removed + // this shifts it off screen so it can be moved back if the node is visible. + for (let n in app.graph._nodes) { + n = app.graph._nodes[n]; + for (let w in n.widgets) { + let wid = n.widgets[w]; + if (Object.hasOwn(wid, "canvas")) { + wid.canvas.style.left = -8000 + "px"; + wid.canvas.style.position = "absolute"; + } + } + } + }; + + node.onResize = function (size) { + computeCanvasSize(node, size); + }; + + return {minWidth: 200, minHeight: 200, widget}; +} + +app.registerExtension({ + name: 'drltdata.MaskRectArea', + async beforeRegisterNodeDef(nodeType, nodeData, app) { + if (nodeData.name === "MaskRectArea") { + const onNodeCreated = nodeType.prototype.onNodeCreated; + nodeType.prototype.onNodeCreated = function () { + const r = onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined; + + this.setProperty("width", 512); + this.setProperty("height", 512); + this.setProperty("x", 0); + this.setProperty("y", 0); + this.setProperty("w", 50); + this.setProperty("h", 50); + this.setProperty("blur_radius", 0); + + this.selected = false; + this.index = 3; + this.serialize_widgets = true; + + CUSTOM_INT(this, "x", 0, function (v, _, node) { + this.value = Math.max(0, Math.min(100, Math.round(v))); // Limitar entre 0 y 100 + node.properties["x"] = this.value; + }); + CUSTOM_INT(this, "y", 0, function (v, _, node) { + this.value = Math.max(0, Math.min(100, Math.round(v))); + node.properties["y"] = this.value; + }); + CUSTOM_INT(this, "w", 50, function (v, _, node) { + this.value = Math.max(0, Math.min(100, Math.round(v))); + node.properties["w"] = this.value; + }); + CUSTOM_INT(this, "h", 50, function (v, _, node) { + this.value = Math.max(0, Math.min(100, Math.round(v))); + node.properties["h"] = this.value; + }); + CUSTOM_INT(this, "blur_radius", 0, function (v, _, node) { + this.value = Math.round(v) || 0; + node.properties["blur_radius"] = this.value; + }, + {"min": 0, "max": 255, "step": 10} + ); + + showPreviewCanvas(this, app); + + this.onSelected = function () { + this.selected = true; + }; + this.onDeselected = function () { + this.selected = false; + }; + + return r; + }; + } + } +}); + +// Calculate the drawing area using percentage-based properties. +function getDrawArea(node, backgroundWidth, backgroundHeight) { + // Convert percentages to actual pixel values based on the background dimensions + let x = (node.properties["x"] / 100) * backgroundWidth; + let y = (node.properties["y"] / 100) * backgroundHeight; + let w = (node.properties["w"] / 100) * backgroundWidth; + let h = (node.properties["h"] / 100) * backgroundHeight; + + // Ensure the values do not exceed the background boundaries + if (x > backgroundWidth) { + x = backgroundWidth; + } + if (y > backgroundHeight) { + y = backgroundHeight; + } + + // Adjust width and height to fit within the background dimensions + if (x + w > backgroundWidth) { + w = Math.max(0, backgroundWidth - x); + } + if (y + h > backgroundHeight) { + h = Math.max(0, backgroundHeight - y); + } + + return [x, y, w, h]; +} + +function CUSTOM_INT(node, inputName, val, func, config = {}) { + return { + widget: node.addWidget( + "number", + inputName, + val, + func, + Object.assign({}, {min: 0, max: 100, step: 10, precision: 0}, config) + ) + }; +} + +function getDrawColor(percent, alpha) { + let h = 360 * percent; + let s = 50; + let l = 50; + l /= 100; + const a = s * Math.min(l, 1 - l) / 100; + const f = n => { + const k = (n + h / 30) % 12; + const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); + return Math.round(255 * color).toString(16).padStart(2, '0'); // convert to Hex and prefix "0" if needed + }; + return `#${f(0)}${f(8)}${f(4)}${alpha}`; +} + +function computeCanvasSize(node, size) { + if (node.widgets[0].last_y == null) { + return; + } + + const MIN_HEIGHT = 200; + const MIN_WIDTH = 200; + + let y = LiteGraph.NODE_WIDGET_HEIGHT * Math.max(node.inputs.length, node.outputs.length) + 5; + let freeSpace = size[1] - y; + + // Compute the height of all non-customCanvas widgets + let widgetHeight = 0; + for (let i = 0; i < node.widgets.length; i++) { + const w = node.widgets[i]; + if (w.type !== "customCanvas") { + if (w.computeSize) { + widgetHeight += w.computeSize()[1] + 4; + } else { + widgetHeight += LiteGraph.NODE_WIDGET_HEIGHT + 5; + } + } + } + + // Ensure there is enough vertical space + freeSpace -= widgetHeight; + + // Adjust the height of the node if needed + if (freeSpace < MIN_HEIGHT) { + freeSpace = MIN_HEIGHT; + node.size[1] = y + widgetHeight + freeSpace; + node.graph.setDirtyCanvas(true); + } + + // Ensure the node width meets the minimum width requirement + if (node.size[0] < MIN_WIDTH) { + node.size[0] = MIN_WIDTH; + node.graph.setDirtyCanvas(true); + } + + // Position each of the widgets + for (const w of node.widgets) { + w.y = y; + if (w.type === "customCanvas") { + y += freeSpace; + } else if (w.computeSize) { + y += w.computeSize()[1] + 4; + } else { + y += LiteGraph.NODE_WIDGET_HEIGHT + 4; + } + } + + node.canvasHeight = freeSpace; +} diff --git a/custom_nodes/comfyui-impact-pack/latent.png b/custom_nodes/comfyui-impact-pack/latent.png new file mode 100644 index 0000000000000000000000000000000000000000..a961c8dbc4a8f237ddd13eea00a1ee4c8055b6de --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/latent.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1faf0cc926b0d65c8ab93e7485aea816283eb22fdeefe9682c206948c1de2043 +size 2814 diff --git a/custom_nodes/comfyui-impact-pack/locales/ko/nodeDefs.json b/custom_nodes/comfyui-impact-pack/locales/ko/nodeDefs.json new file mode 100644 index 0000000000000000000000000000000000000000..1626d6cb14c1defb57640264d02ad7e968a42483 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/locales/ko/nodeDefs.json @@ -0,0 +1,1241 @@ +{ + "FaceDetailer": { + "description": "감지 모델(bbox, segm, sam) 모델을 이용해서 입력 이미지에서 자동으로 특정 객체를 감지하고, 감지 영역을 가이드 크기를 기반으로 확대해서 인페이트하는 방법으로 디테일을 강화합니다.\n사용자들이 자주 사용하는 얼굴 디테일 강화 워크플로를 단순화하기 위해 특화 시킨 노드이긴 하지만, 감지 모델에 따라서 다양한 자동 인페인트 용도로 사용 가능합니다.", + "display_name": "얼굴 디테일러", + "inputs": { + "image": { + "name": "이미지" + }, + "model": { + "name": "모델", + "tooltip": "만약 `ImpactDummyInput` 을 연결 하면, 인페인트 단계를 건너 뜁니다." + }, + "guide_size": { + "name": "가이드 크기", + "tooltip": "'가이드 크기 대상'으로 지정된 크기의 가장 짧은면을 이 크기까지 확대합니다." + }, + "guide_size_for": { + "name": "가이드 크기 대상", + "tooltip": "bbox: 감지된 사각 영역(bbox)\ncrop_region: 잘라낸 영역" + }, + "max_size": { + "name": "최대 크기", + "tooltip": "'가이드 크기'로 확대 할 때, 가장 긴 면의 길이를 이 크기로 제한합니다. 너무 크게 확대 되는 것을 막아줍니다." + }, + "seed": { + "name": "시드" + }, + "steps": { + "name": "스텝수" + }, + "sampler_name": { + "name": "샘플러 이름" + }, + "scheduler": { + "name": "스케쥴러" + }, + "positive": { + "name": "긍정 조건" + }, + "negative": { + "name": "부정 조건" + }, + "denoise": { + "name": "노이즈 제거양" + }, + "feather": { + "name": "가장자리 흐림", + "tooltip": "확대 해서 인페인트 된 이미지를 원본 이미지에 붙여넣을 때, 이 수치로 마스크의 가장 자리를 흐리게 해서 붙여넣어 이음매의 위화감을 줄여줍니다." + }, + "noise_mask": { + "name": "노이즈 마스크 사용", + "tooltip": "인페인트 할 때, 마스크를 적용해서 마스크 영역만 인페인트합니다. 이 옵션을 적용하지 않으면, 잘라낸 이미지 전체가 재생성되어 노이즈 제거양이 클 때 위화감이 나타나게 됩니다." + }, + "force_inpaint": { + "name": "인페인트 강제 적용", + "tooltip": "가이드 크기와 상관 없이 인페인팅을 무조건 적용 합니다. 이 옵션이 꺼져있는 경우 가이드 크기보다 이미 큰 감지 영역은 인페인팅을 건너 뜁니다." + }, + "bbox_threshold": { + "name": "bbox 감지 임계치", + "tooltip": "사각 영역(bbox) 감지 모델의 최소 감지 임계치를 설정합니다. 임계치가 높을수록 확실한 객체만 감지하지만, 객체를 감지하지 못할 확률이 증가합니다." + }, + "bbox_dilation": { + "name": "bbox 확장", + "tooltip": "감지된 사각 영역(bbox)을 확장 합니다. 이 옵션은 감지된 영역보다 더 넓은 영역을 인페인트 할 경우 사용합니다.\n주의: sam 모델을 사용할 경우 잘라낸 영역내에서 bbox 확장을 하더라도, sam 감지 영역이 작으면 여전히 제한됩니다." + }, + "bbox_crop_factor": { + "name": "bbox 자르기 배율", + "tooltip": "감지된 사각 영역(bbox)의 몇배 크기의 영역을 잘라낼 것 인지를 설정합니다. 이 크기가 너무 작으면, 인페인트 할 이미지의 주변 정보가 부족해서 위화감이 강한 이미지가 생성됩니다. 이 크기가 너무 크면, 인페인팅에 너무 오랜 시간이 걸릴 수 있으며, 모델의 역량을 초과할 정도로 클 경우 올바르지 않은 이미지를 생성하게 됩니다." + }, + "sam_detection_hint": { + "name": "sam 감지 힌트", + "tooltip": "[실험기능] 오래된 실험 기능으로, sam 모델의 감지 힌트를 제공하는 방식입니다. cetner-1 (중앙점 1개) 외에는 사용하지 않을 것을 권장합니다." + }, + "sam_dilation": { + "name": "sam 마스크 확장", + "tooltip": "sam 모델로 감지된 실루엣 마스크를 확장합니다." + }, + "sam_threshold": { + "name": "sam 감지 임계치", + "tooltip": "sam 모델의 최소 감지 임계치를 설정합니다. 임계치가 높을수록 확실한 객체만 감지하지만, 객체를 감지하지 못할 확률이 증가합니다." + }, + "sam_bbox_expansion": { + "name": "sam 영역 확장", + "tooltip": "sam 의 감지 영역을 확장합니다. 감지 영역은 마스크를 포함하는 전체 사각 영역입니다.\n주의1:sam 마스크를 확장하더라도, 감지 영역을 벗어날 수 없습니다.\n주의2: bbox 확장을 하더라도, sam 감지 영역이 작으면 여전히 제한됩니다." + }, + "sam_mask_hint_threshold": { + "name": "sam 마스크 힌트 임계치", + "tooltip": "[실험기능] 오래된 실험 기능으로, mask-hint 모드에서만 사용되는 옵션으로, 마스크에서 이 크기 이상의 점 마스크를 sam의 힌트로 사용합니다." + }, + "sam_mask_hint_use_negative": { + "name": "sam 마스크 힌트에 제외 힌트 사용", + "tooltip": "[실험기능] 오래된 실험 기능으로, mask-hitn 모드에서만 사용되는 옵션으로, sam 마스크 힌트 임계치보다 작은 점 마스크를 sam의 제외 힌트로 사용합니다." + }, + "drop_size": { + "name": "감지 최소 크기", + "tooltip": "사각 영역(bbox) 감지기로 감지한 크기가 이 설정값 보다 작을 경우 무시합니다." + }, + "bbox_detector": { + "name": "bbox 감지기", + "tooltip": "디테일 개선 대상을 자동으로 감지해주는 사각 영역(bbox) 감지기 입력.\n이 감지기로 감지된 감지 정보가 기준 정보입니다." + }, + "wildcard": { + "name": "와일드카드 프롬프트", + "tooltip": "'와일드카드 인코더 (Impact)'와 유사한 기능을 수행하여, 와일드카드 기능과 로라 로딩 기능을 제공합니다. 또한, 감지된 영역별로 다른 프롬프트를 적용하는 기능들을 제공합니다.\n더 자세한 정보는 튜토리얼 페이지를 참고하세요.\n주의:이 입력을 비워두면, 이 입력은 무시됩니다." + }, + "cycle": { + "name": "반복수", + "tooltip": "설정된 값만큼 인페인팅을 반복 적용합니다. 인코딩/디코딩 없이 확대된 잠재 이미지 단계에서 반복됩니다." + }, + "sam_model_opt": { + "name": "sam 모델", + "tooltip": "이 모델을 제공할 경우 sam 모델을 감지 보조 모델로 사용합니다. bbox 감지기로 감지된 사각 영역에 sam 모델을 적용해서 정교한 실루엣 마스크를 생성합니다.\n주의: 이 입력이 연결될 경우 segm 감지기는 무시됩니다." + }, + "segm_detector_opt": { + "name": "segm 감지기", + "tooltip": "이 모델을 제공할 경우 segm 모델을 감지 보조 모델로 사용합니다. bbox 감지기로 감지된 사각 영역에 segm 감지기로 감지된 실루엣 마스크를 생성합니다.\n주의: 이 입력은 sam 모델이 연결될 경우 무시됩니다." + }, + "detailer_hook": { + "name": "디테일러 후크", + "tooltip": "이 노드의 실행 중간단계에서 여러가지 기능을 수행할 수 있는 후크를 연결합니다." + }, + "inpaint_model": { + "name": "인페인트 모델 모드", + "tooltip": "인페인트 전용 모델을 사용할 경우 이 옵션을 켜면, 인페인팅시에 '인페인트 모델 조건 설정'이 적용되어 수행됩니다." + }, + "noise_mask_feather": { + "name": "노이즈 마스크 가장자리 흐림", + "tooltip": "인페인트시에 적용되는 노이즈 마스크의 가장자리를 흐리게 합니다. 이 설정값이 0을 초과할 경우, 내부적으로 자동으로 '차등 확산' 노드를 적용합니다." + }, + "scheduler_func_opt": { + "name": "스케쥴러 함수", + "tooltip": "GITS 스케쥴러 처럼 기본 스케쥴러 리스트에서 선택할 수 없는 특수 스케쥴러를 사용할 수 있게 해줍니다. 이 입력이 연결되면, 기본 스케쥴러 선택은 무시됩니다." + }, + "tiled_encode": { + "name": "타일 인코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 인코드'를 사용할 경우, 기본 'VAE 인코드' 대신 'VAE 인코드 (타일)' 을 적용합니다." + }, + "tiled_decode": { + "name": "타일 디코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 다코드'를 사용할 경우, 기본 'VAE 디코드' 대신 'VAE 디코드 (타일)' 을 적용합니다." + } + }, + "outputs": { + "0": { + "name": "개선 이미" + }, + "1": { + "name": "잘라낸 이미지" + }, + "2": { + "name": "잘라낸 투명 이미지" + }, + "3": { + "name": "마스크" + }, + "4": { + "name": "디테일러 파이프" + }, + "5": { + "name": "컨트롤넷 이미지" + } + } + }, + "FaceDetailerPipe": { + "description": "감지 모델(bbox, segm, sam) 모델을 이용해서 입력 이미지에서 자동으로 특정 객체를 감지하고, 감지 영역을 가이드 크기를 기반으로 확대해서 인페이트하는 방법으로 디테일을 강화합니다.\n사용자들이 자주 사용하는 얼굴 디테일 강화 워크플로를 단순화하기 위해 특화 시킨 노드이긴 하지만, 감지 모델에 따라서 다양한 자동 인페인트 용도로 사용 가능합니다.", + "display_name": "얼굴 디테일러 (파이프)", + "inputs": { + "image": { + "name": "이미지" + }, + "detailer_pipe": { + "name": "디테일러 파이프", + "tooltip": "만약 디테일러 파이프 내의 모델에 `ImpactDummyInput` 가 설정된 경우, 인페인트 단계를 건너 뜁니다." + }, + "guide_size": { + "name": "가이드 크기", + "tooltip": "'가이드 크기 대상'으로 지정된 크기의 가장 짧은면을 이 크기까지 확대합니다." + }, + "guide_size_for": { + "name": "가이드 크기 대상", + "tooltip": "bbox: 감지된 사각 영역(bbox)\ncrop_region: 잘라낸 영역" + }, + "max_size": { + "name": "최대 크기", + "tooltip": "'가이드 크기'로 확대 할 때, 가장 긴 면의 길이를 이 크기로 제한합니다. 너무 크게 확대 되는 것을 막아줍니다." + }, + "seed": { + "name": "시드" + }, + "steps": { + "name": "스텝수" + }, + "sampler_name": { + "name": "샘플러 이름" + }, + "scheduler": { + "name": "스케쥴러" + }, + "denoise": { + "name": "노이즈 제거양" + }, + "feather": { + "name": "가장자리 흐림", + "tooltip": "확대 해서 인페인트 된 이미지를 원본 이미지에 붙여넣을 때, 이 수치로 마스크의 가장 자리를 흐리게 해서 붙여넣어 이음매의 위화감을 줄여줍니다." + }, + "noise_mask": { + "name": "노이즈 마스크 사용", + "tooltip": "인페인트 할 때, 마스크를 적용해서 마스크 영역만 인페인트합니다. 이 옵션을 적용하지 않으면, 잘라낸 이미지 전체가 재생성되어 노이즈 제거양이 클 때 위화감이 나타나게 됩니다." + }, + "force_inpaint": { + "name": "인페인트 강제 적용", + "tooltip": "가이드 크기와 상관 없이 인페인팅을 무조건 적용 합니다. 이 옵션이 꺼져있는 경우 가이드 크기보다 이미 큰 감지 영역은 인페인팅을 건너 뜁니다." + }, + "bbox_threshold": { + "name": "bbox 감지 임계치", + "tooltip": "사각 영역(bbox) 감지 모델의 최소 감지 임계치를 설정합니다. 임계치가 높을수록 확실한 객체만 감지하지만, 객체를 감지하지 못할 확률이 증가합니다." + }, + "bbox_dilation": { + "name": "bbox 확장", + "tooltip": "감지된 사각 영역(bbox)을 확장 합니다. 이 옵션은 감지된 영역보다 더 넓은 영역을 인페인트 할 경우 사용합니다.\n주의: sam 모델을 사용할 경우 잘라낸 영역내에서 bbox 확장을 하더라도, sam 감지 영역이 작으면 여전히 제한됩니다." + }, + "bbox_crop_factor": { + "name": "bbox 자르기 배율", + "tooltip": "감지된 사각 영역(bbox)의 몇배 크기의 영역을 잘라낼 것 인지를 설정합니다. 이 크기가 너무 작으면, 인페인트 할 이미지의 주변 정보가 부족해서 위화감이 강한 이미지가 생성됩니다. 이 크기가 너무 크면, 인페인팅에 너무 오랜 시간이 걸릴 수 있으며, 모델의 역량을 초과할 정도로 클 경우 올바르지 않은 이미지를 생성하게 됩니다." + }, + "sam_detection_hint": { + "name": "sam 감지 힌트", + "tooltip": "[실험기능] 오래된 실험 기능으로, sam 모델의 감지 힌트를 제공하는 방식입니다. cetner-1 (중앙점 1개) 외에는 사용하지 않을 것을 권장합니다." + }, + "sam_dilation": { + "name": "sam 마스크 확장", + "tooltip": "sam 모델로 감지된 실루엣 마스크를 확장합니다." + }, + "sam_threshold": { + "name": "sam 감지 임계치", + "tooltip": "sam 모델의 최소 감지 임계치를 설정합니다. 임계치가 높을수록 확실한 객체만 감지하지만, 객체를 감지하지 못할 확률이 증가합니다." + }, + "sam_bbox_expansion": { + "name": "sam 영역 확장", + "tooltip": "sam 의 감지 영역을 확장합니다. 감지 영역은 마스크를 포함하는 전체 사각 영역입니다.\n주의1:sam 마스크를 확장하더라도, 감지 영역을 벗어날 수 없습니다.\n주의2: bbox 확장을 하더라도, sam 감지 영역이 작으면 여전히 제한됩니다." + }, + "sam_mask_hint_threshold": { + "name": "sam 마스크 힌트 임계치", + "tooltip": "[실험기능] 오래된 실험 기능으로, mask-hint 모드에서만 사용되는 옵션으로, 마스크에서 이 크기 이상의 점 마스크를 sam의 힌트로 사용합니다." + }, + "sam_mask_hint_use_negative": { + "name": "sam 마스크 힌트에 제외 힌트 사용", + "tooltip": "[실험기능] 오래된 실험 기능으로, mask-hitn 모드에서만 사용되는 옵션으로, sam 마스크 힌트 임계치보다 작은 점 마스크를 sam의 제외 힌트로 사용합니다." + }, + "drop_size": { + "name": "감지 최소 크기", + "tooltip": "사각 영역(bbox) 감지기로 감지한 크기가 이 설정값 보다 작을 경우 무시합니다." + }, + "refiner_ratio": { + "name": "라파이너 적용 비율", + "tooltip": "SDXL 리파이너 모델을 사용할 경우 적용될 후반 스텝수 비율을 설정합니다." + }, + "cycle": { + "name": "반복수", + "tooltip": "설정된 값만큼 인페인팅을 반복 적용합니다. 인코딩/디코딩 없이 확대된 잠재 이미지 단계에서 반복됩니다." + }, + "inpaint_model": { + "name": "인페인트 모델 모드", + "tooltip": "인페인트 전용 모델을 사용할 경우 이 옵션을 켜면, 인페인팅시에 '인페인트 모델 조건 설정'이 적용되어 수행됩니다." + }, + "noise_mask_feather": { + "name": "노이즈 마스크 가장자리 흐림", + "tooltip": "인페인트시에 적용되는 노이즈 마스크의 가장자리를 흐리게 합니다. 이 설정값이 0을 초과할 경우, 내부적으로 자동으로 '차등 확산' 노드를 적용합니다." + }, + "scheduler_func_opt": { + "name": "스케쥴러 함수", + "tooltip": "GITS 스케쥴러 처럼 기본 스케쥴러 리스트에서 선택할 수 없는 특수 스케쥴러를 사용할 수 있게 해줍니다. 이 입력이 연결되면, 기본 스케쥴러 선택은 무시됩니다." + }, + "tiled_encode": { + "name": "타일 인코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 인코드'를 사용할 경우, 기본 'VAE 인코드' 대신 'VAE 인코드 (타일)' 을 적용합니다." + }, + "tiled_decode": { + "name": "타일 디코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 다코드'를 사용할 경우, 기본 'VAE 디코드' 대신 'VAE 디코드 (타일)' 을 적용합니다." + } + }, + "outputs": { + "0": { + "name": "개선 이미" + }, + "1": { + "name": "잘라낸 이미지" + }, + "2": { + "name": "잘라낸 투명 이미지" + }, + "3": { + "name": "마스크" + }, + "4": { + "name": "디테일러 파이프" + }, + "5": { + "name": "컨트롤넷 이미지" + } + } + }, + "DetailerForEach": { + "description": "감지 영역 정보 묶음(SEGS)내의 각 영역들에 대해 가이드 크기를 기반으로 확대해서 인페이트하는 방법으로 디테일을 강화합니다.", + "display_name": "디테일러 (SEGS)", + "inputs": { + "image": { + "name": "이미지" + }, + "segs": { + "name": "segs", + "tooltip": "감지 영역 정보를 담고 있는 묶음.\n이 영역들을 대상으로 인페인트가 적용됩니다." + }, + "model": { + "name": "모델", + "tooltip": "만약 `ImpactDummyInput` 을 연결 하면, 인페인트 단계를 건너 뜁니다." + }, + "guide_size": { + "name": "가이드 크기", + "tooltip": "'가이드 크기 대상'으로 지정된 크기의 가장 짧은면을 이 크기까지 확대합니다." + }, + "guide_size_for": { + "name": "가이드 크기 대상", + "tooltip": "bbox: 감지된 사각 영역(bbox)\ncrop_region: 잘라낸 영역" + }, + "max_size": { + "name": "최대 크기", + "tooltip": "'가이드 크기'로 확대 할 때, 가장 긴 면의 길이를 이 크기로 제한합니다. 너무 크게 확대 되는 것을 막아줍니다." + }, + "seed": { + "name": "시드" + }, + "steps": { + "name": "스텝수" + }, + "sampler_name": { + "name": "샘플러 이름" + }, + "scheduler": { + "name": "스케쥴러" + }, + "positive": { + "name": "긍정 조건" + }, + "negative": { + "name": "부정 조건" + }, + "denoise": { + "name": "노이즈 제거양" + }, + "feather": { + "name": "가장자리 흐림", + "tooltip": "확대 해서 인페인트 된 이미지를 원본 이미지에 붙여넣을 때, 이 수치로 마스크의 가장 자리를 흐리게 해서 붙여넣어 이음매의 위화감을 줄여줍니다." + }, + "noise_mask": { + "name": "노이즈 마스크 사용", + "tooltip": "인페인트 할 때, 마스크를 적용해서 마스크 영역만 인페인트합니다. 이 옵션을 적용하지 않으면, 잘라낸 이미지 전체가 재생성되어 노이즈 제거양이 클 때 위화감이 나타나게 됩니다." + }, + "force_inpaint": { + "name": "인페인트 강제 적용", + "tooltip": "가이드 크기와 상관 없이 인페인팅을 무조건 적용 합니다. 이 옵션이 꺼져있는 경우 가이드 크기보다 이미 큰 감지 영역은 인페인팅을 건너 뜁니다." + }, + "wildcard": { + "name": "와일드카드 프롬프트", + "tooltip": "'와일드카드 인코더 (Impact)'와 유사한 기능을 수행하여, 와일드카드 기능과 로라 로딩 기능을 제공합니다. 또한, 감지된 영역별로 다른 프롬프트를 적용하는 기능들을 제공합니다.\n더 자세한 정보는 튜토리얼 페이지를 참고하세요.\n주의:이 입력을 비워두면, 이 입력은 무시됩니다." + }, + "cycle": { + "name": "반복수", + "tooltip": "설정된 값만큼 인페인팅을 반복 적용합니다. 인코딩/디코딩 없이 확대된 잠재 이미지 단계에서 반복됩니다." + }, + "detailer_hook": { + "name": "디테일러 후크", + "tooltip": "이 노드의 실행 중간단계에서 여러가지 기능을 수행할 수 있는 후크를 연결합니다." + }, + "inpaint_model": { + "name": "인페인트 모델 모드", + "tooltip": "인페인트 전용 모델을 사용할 경우 이 옵션을 켜면, 인페인팅시에 '인페인트 모델 조건 설정'이 적용되어 수행됩니다." + }, + "noise_mask_feather": { + "name": "노이즈 마스크 가장자리 흐림", + "tooltip": "인페인트시에 적용되는 노이즈 마스크의 가장자리를 흐리게 합니다. 이 설정값이 0을 초과할 경우, 내부적으로 자동으로 '차등 확산' 노드를 적용합니다." + }, + "scheduler_func_opt": { + "name": "스케쥴러 함수", + "tooltip": "GITS 스케쥴러 처럼 기본 스케쥴러 리스트에서 선택할 수 없는 특수 스케쥴러를 사용할 수 있게 해줍니다. 이 입력이 연결되면, 기본 스케쥴러 선택은 무시됩니다." + }, + "tiled_encode": { + "name": "타일 인코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 인코드'를 사용할 경우, 기본 'VAE 인코드' 대신 'VAE 인코드 (타일)' 을 적용합니다." + }, + "tiled_decode": { + "name": "타일 디코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 다코드'를 사용할 경우, 기본 'VAE 디코드' 대신 'VAE 디코드 (타일)' 을 적용합니다." + } + }, + "outputs": { + "0": { + "name": "개선 이미지" + } + } + }, + "DetailerForEachPipe": { + "description": "감지 영역 정보 묶음(SEGS)내의 각 영역들에 대해 가이드 크기를 기반으로 확대해서 인페이트하는 방법으로 디테일을 강화합니다.", + "display_name": "디테일러 (상세/SEGS/파이프)", + "inputs": { + "image": { + "name": "이미지" + }, + "segs": { + "name": "segs", + "tooltip": "감지 영역 정보를 담고 있는 묶음.\n이 영역들을 대상으로 인페인트가 적용됩니다." + }, + "guide_size": { + "name": "가이드 크기", + "tooltip": "'가이드 크기 대상'으로 지정된 크기의 가장 짧은면을 이 크기까지 확대합니다." + }, + "guide_size_for": { + "name": "가이드 크기 대상", + "tooltip": "bbox: 감지된 사각 영역(bbox)\ncrop_region: 잘라낸 영역" + }, + "max_size": { + "name": "최대 크기", + "tooltip": "'가이드 크기'로 확대 할 때, 가장 긴 면의 길이를 이 크기로 제한합니다. 너무 크게 확대 되는 것을 막아줍니다." + }, + "seed": { + "name": "시드" + }, + "steps": { + "name": "스텝수" + }, + "sampler_name": { + "name": "샘플러 이름" + }, + "scheduler": { + "name": "스케쥴러" + }, + "denoise": { + "name": "노이즈 제거양" + }, + "feather": { + "name": "가장자리 흐림", + "tooltip": "확대 해서 인페인트 된 이미지를 원본 이미지에 붙여넣을 때, 이 수치로 마스크의 가장 자리를 흐리게 해서 붙여넣어 이음매의 위화감을 줄여줍니다." + }, + "noise_mask": { + "name": "노이즈 마스크 사용", + "tooltip": "인페인트 할 때, 마스크를 적용해서 마스크 영역만 인페인트합니다. 이 옵션을 적용하지 않으면, 잘라낸 이미지 전체가 재생성되어 노이즈 제거양이 클 때 위화감이 나타나게 됩니다." + }, + "force_inpaint": { + "name": "인페인트 강제 적용", + "tooltip": "가이드 크기와 상관 없이 인페인팅을 무조건 적용 합니다. 이 옵션이 꺼져있는 경우 가이드 크기보다 이미 큰 감지 영역은 인페인팅을 건너 뜁니다." + }, + "basic_pipe": { + "name": "기본 파이프", + "tooltip": "만약 기본 파이프 내의 모델에 `ImpactDummyInput` 가 설정된 경우, 인페인트 단계를 건너 뜁니다." + }, + "refiner_ratio": { + "name": "라파이너 적용 비율", + "tooltip": "SDXL 리파이너 모델을 사용할 경우 적용될 후반 스텝수 비율을 설정합니다." + }, + "cycle": { + "name": "반복수", + "tooltip": "설정된 값만큼 인페인팅을 반복 적용합니다. 인코딩/디코딩 없이 확대된 잠재 이미지 단계에서 반복됩니다." + }, + "detailer_hook": { + "name": "디테일러 후크", + "tooltip": "이 노드의 실행 중간단계에서 여러가지 기능을 수행할 수 있는 후크를 연결합니다." + }, + "refiner_basic_pipe_opt": { + "name": "리파이너 기본 파이프", + "tooltip": "SDXL 리파이너 단계에 적용할 기본 파이프를 연결합니다." + }, + "inpaint_model": { + "name": "인페인트 모델 모드", + "tooltip": "인페인트 전용 모델을 사용할 경우 이 옵션을 켜면, 인페인팅시에 '인페인트 모델 조건 설정'이 적용되어 수행됩니다." + }, + "noise_mask_feather": { + "name": "노이즈 마스크 가장자리 흐림", + "tooltip": "인페인트시에 적용되는 노이즈 마스크의 가장자리를 흐리게 합니다. 이 설정값이 0을 초과할 경우, 내부적으로 자동으로 '차등 확산' 노드를 적용합니다." + }, + "scheduler_func_opt": { + "name": "스케쥴러 함수", + "tooltip": "GITS 스케쥴러 처럼 기본 스케쥴러 리스트에서 선택할 수 없는 특수 스케쥴러를 사용할 수 있게 해줍니다. 이 입력이 연결되면, 기본 스케쥴러 선택은 무시됩니다." + }, + "tiled_encode": { + "name": "타일 인코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 인코드'를 사용할 경우, 기본 'VAE 인코드' 대신 'VAE 인코드 (타일)' 을 적용합니다." + }, + "tiled_decode": { + "name": "타일 디코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 다코드'를 사용할 경우, 기본 'VAE 디코드' 대신 'VAE 디코드 (타일)' 을 적용합니다." + } + }, + "outputs": { + "0": { + "name": "개선 이미지" + }, + "1": { + "name": "segs" + }, + "2": { + "name": "기본 파이프" + }, + "3": { + "name": "컨트롤넷 이미지" + } + } + }, + "DetailerForEachDebug": { + "description": "감지 영역 정보 묶음(SEGS)내의 각 영역들에 대해 가이드 크기를 기반으로 확대해서 인페이트하는 방법으로 디테일을 강화합니다.", + "display_name": "디테일러 (상세/SEGS)", + "inputs": { + "image": { + "name": "이미지" + }, + "segs": { + "name": "segs", + "tooltip": "감지 영역 정보를 담고 있는 묶음.\n이 영역들을 대상으로 인페인트가 적용됩니다." + }, + "model": { + "name": "모델", + "tooltip": "만약 `ImpactDummyInput` 을 연결 하면, 인페인트 단계를 건너 뜁니다." + }, + "guide_size": { + "name": "가이드 크기", + "tooltip": "'가이드 크기 대상'으로 지정된 크기의 가장 짧은면을 이 크기까지 확대합니다." + }, + "guide_size_for": { + "name": "가이드 크기 대상", + "tooltip": "bbox: 감지된 사각 영역(bbox)\ncrop_region: 잘라낸 영역" + }, + "max_size": { + "name": "최대 크기", + "tooltip": "'가이드 크기'로 확대 할 때, 가장 긴 면의 길이를 이 크기로 제한합니다. 너무 크게 확대 되는 것을 막아줍니다." + }, + "seed": { + "name": "시드" + }, + "steps": { + "name": "스텝수" + }, + "sampler_name": { + "name": "샘플러 이름" + }, + "scheduler": { + "name": "스케쥴러" + }, + "positive": { + "name": "긍정 조건" + }, + "negative": { + "name": "부정 조건" + }, + "denoise": { + "name": "노이즈 제거양" + }, + "feather": { + "name": "가장자리 흐림", + "tooltip": "확대 해서 인페인트 된 이미지를 원본 이미지에 붙여넣을 때, 이 수치로 마스크의 가장 자리를 흐리게 해서 붙여넣어 이음매의 위화감을 줄여줍니다." + }, + "noise_mask": { + "name": "노이즈 마스크 사용", + "tooltip": "인페인트 할 때, 마스크를 적용해서 마스크 영역만 인페인트합니다. 이 옵션을 적용하지 않으면, 잘라낸 이미지 전체가 재생성되어 노이즈 제거양이 클 때 위화감이 나타나게 됩니다." + }, + "force_inpaint": { + "name": "인페인트 강제 적용", + "tooltip": "가이드 크기와 상관 없이 인페인팅을 무조건 적용 합니다. 이 옵션이 꺼져있는 경우 가이드 크기보다 이미 큰 감지 영역은 인페인팅을 건너 뜁니다." + }, + "wildcard": { + "name": "와일드카드 프롬프트", + "tooltip": "'와일드카드 인코더 (Impact)'와 유사한 기능을 수행하여, 와일드카드 기능과 로라 로딩 기능을 제공합니다. 또한, 감지된 영역별로 다른 프롬프트를 적용하는 기능들을 제공합니다.\n더 자세한 정보는 튜토리얼 페이지를 참고하세요.\n주의:이 입력을 비워두면, 이 입력은 무시됩니다." + }, + "cycle": { + "name": "반복수", + "tooltip": "설정된 값만큼 인페인팅을 반복 적용합니다. 인코딩/디코딩 없이 확대된 잠재 이미지 단계에서 반복됩니다." + }, + "detailer_hook": { + "name": "디테일러 후크", + "tooltip": "이 노드의 실행 중간단계에서 여러가지 기능을 수행할 수 있는 후크를 연결합니다." + }, + "inpaint_model": { + "name": "인페인트 모델 모드", + "tooltip": "인페인트 전용 모델을 사용할 경우 이 옵션을 켜면, 인페인팅시에 '인페인트 모델 조건 설정'이 적용되어 수행됩니다." + }, + "noise_mask_feather": { + "name": "노이즈 마스크 가장자리 흐림", + "tooltip": "인페인트시에 적용되는 노이즈 마스크의 가장자리를 흐리게 합니다. 이 설정값이 0을 초과할 경우, 내부적으로 자동으로 '차등 확산' 노드를 적용합니다." + }, + "scheduler_func_opt": { + "name": "스케쥴러 함수", + "tooltip": "GITS 스케쥴러 처럼 기본 스케쥴러 리스트에서 선택할 수 없는 특수 스케쥴러를 사용할 수 있게 해줍니다. 이 입력이 연결되면, 기본 스케쥴러 선택은 무시됩니다." + }, + "tiled_encode": { + "name": "타일 인코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 인코드'를 사용할 경우, 기본 'VAE 인코드' 대신 'VAE 인코드 (타일)' 을 적용합니다." + }, + "tiled_decode": { + "name": "타일 디코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 다코드'를 사용할 경우, 기본 'VAE 디코드' 대신 'VAE 디코드 (타일)' 을 적용합니다." + } + }, + "outputs": { + "0": { + "name": "개선 이미지" + }, + "1": { + "name": "잘라낸 이미지" + }, + "2": { + "name": "잘라낸 개선 이미지" + }, + "3": { + "name": "잘라낸 투명 개선 이미지" + }, + "4": { + "name": "컨트롤넷 이미지" + } + } + }, + "DetailerForEachDebugPipe": { + "description": "감지 영역 정보 묶음(SEGS)내의 각 영역들에 대해 가이드 크기를 기반으로 확대해서 인페이트하는 방법으로 디테일을 강화합니다.", + "display_name": "디테일러 (상세/SEGS/파이프)", + "inputs": { + "image": { + "name": "이미지" + }, + "segs": { + "name": "segs", + "tooltip": "감지 영역 정보를 담고 있는 묶음.\n이 영역들을 대상으로 인페인트가 적용됩니다." + }, + "guide_size": { + "name": "가이드 크기", + "tooltip": "'가이드 크기 대상'으로 지정된 크기의 가장 짧은면을 이 크기까지 확대합니다." + }, + "guide_size_for": { + "name": "가이드 크기 대상", + "tooltip": "bbox: 감지된 사각 영역(bbox)\ncrop_region: 잘라낸 영역" + }, + "max_size": { + "name": "최대 크기", + "tooltip": "'가이드 크기'로 확대 할 때, 가장 긴 면의 길이를 이 크기로 제한합니다. 너무 크게 확대 되는 것을 막아줍니다." + }, + "seed": { + "name": "시드" + }, + "steps": { + "name": "스텝수" + }, + "sampler_name": { + "name": "샘플러 이름" + }, + "scheduler": { + "name": "스케쥴러" + }, + "denoise": { + "name": "노이즈 제거양" + }, + "feather": { + "name": "가장자리 흐림", + "tooltip": "확대 해서 인페인트 된 이미지를 원본 이미지에 붙여넣을 때, 이 수치로 마스크의 가장 자리를 흐리게 해서 붙여넣어 이음매의 위화감을 줄여줍니다." + }, + "noise_mask": { + "name": "노이즈 마스크 사용", + "tooltip": "인페인트 할 때, 마스크를 적용해서 마스크 영역만 인페인트합니다. 이 옵션을 적용하지 않으면, 잘라낸 이미지 전체가 재생성되어 노이즈 제거양이 클 때 위화감이 나타나게 됩니다." + }, + "force_inpaint": { + "name": "인페인트 강제 적용", + "tooltip": "가이드 크기와 상관 없이 인페인팅을 무조건 적용 합니다. 이 옵션이 꺼져있는 경우 가이드 크기보다 이미 큰 감지 영역은 인페인팅을 건너 뜁니다." + }, + "basic_pipe": { + "name": "기본 파이프", + "tooltip": "만약 기본 파이프 내의 모델에 `ImpactDummyInput` 가 설정된 경우, 인페인트 단계를 건너 뜁니다." + }, + "refiner_ratio": { + "name": "라파이너 적용 비율", + "tooltip": "SDXL 리파이너 모델을 사용할 경우 적용될 후반 스텝수 비율을 설정합니다." + }, + "cycle": { + "name": "반복수", + "tooltip": "설정된 값만큼 인페인팅을 반복 적용합니다. 인코딩/디코딩 없이 확대된 잠재 이미지 단계에서 반복됩니다." + }, + "detailer_hook": { + "name": "디테일러 후크", + "tooltip": "이 노드의 실행 중간단계에서 여러가지 기능을 수행할 수 있는 후크를 연결합니다." + }, + "refiner_basic_pipe_opt": { + "name": "리파이너 기본 파이프", + "tooltip": "SDXL 리파이너 단계에 적용할 기본 파이프를 연결합니다." + }, + "inpaint_model": { + "name": "인페인트 모델 모드", + "tooltip": "인페인트 전용 모델을 사용할 경우 이 옵션을 켜면, 인페인팅시에 '인페인트 모델 조건 설정'이 적용되어 수행됩니다." + }, + "noise_mask_feather": { + "name": "노이즈 마스크 가장자리 흐림", + "tooltip": "인페인트시에 적용되는 노이즈 마스크의 가장자리를 흐리게 합니다. 이 설정값이 0을 초과할 경우, 내부적으로 자동으로 '차등 확산' 노드를 적용합니다." + }, + "scheduler_func_opt": { + "name": "스케쥴러 함수", + "tooltip": "GITS 스케쥴러 처럼 기본 스케쥴러 리스트에서 선택할 수 없는 특수 스케쥴러를 사용할 수 있게 해줍니다. 이 입력이 연결되면, 기본 스케쥴러 선택은 무시됩니다." + }, + "tiled_encode": { + "name": "타일 인코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 인코드'를 사용할 경우, 기본 'VAE 인코드' 대신 'VAE 인코드 (타일)' 을 적용합니다." + }, + "tiled_decode": { + "name": "타일 디코드 사용", + "tooltip": "이 옵션을 켜면, 내부적으로 'VAE 다코드'를 사용할 경우, 기본 'VAE 디코드' 대신 'VAE 디코드 (타일)' 을 적용합니다." + } + }, + "outputs": { + "0": { + "name": "개선 이미지" + }, + "1": { + "name": "개선 SEGS" + }, + "2": { + "name": "기본 파이프" + }, + "3": { + "name": "잘라낸 이미지" + }, + "4": { + "name": "잘라낸 개선 이미지" + }, + "5": { + "name": "잘라낸 투명 개선 이미지" + }, + "6": { + "name": "컨트롤넷 이미지" + } + } + }, + "DetailerForEachPipeForAnimateDiff": { + "description": "감지 영역 정보 묶음(SEGS)내의 각 영역들에 대해 가이드 크기를 기반으로 확대해서 인페이트하는 방법으로 디테일을 강화합니다.\n이 노드는 AnimateDiff와 같은 동영상의 디테일 개선을 위한 특수 디테일러 노드로써, SEGS가 담고 있는 마스크가 여러 프레임에 걸친 배치 마스크가 되는 경우를 처리할 수 있습니다.", + "display_name": "디테일러 (AnimateDiff/파이프)", + "inputs": { + "image_frames": { + "name": "이미지 프레임 묶음" + }, + "segs": { + "name": "segs", + "tooltip": "감지 영역 정보를 담고 있는 묶음.\n이 영역들을 대상으로 인페인트가 적용됩니다." + }, + "guide_size": { + "name": "가이드 크기", + "tooltip": "'가이드 크기 대상'으로 지정된 크기의 가장 짧은면을 이 크기까지 확대합니다." + }, + "guide_size_for": { + "name": "가이드 크기 대상", + "tooltip": "bbox: 감지된 사각 영역(bbox)\ncrop_region: 잘라낸 영역" + }, + "max_size": { + "name": "최대 크기", + "tooltip": "'가이드 크기'로 확대 할 때, 가장 긴 면의 길이를 이 크기로 제한합니다. 너무 크게 확대 되는 것을 막아줍니다." + }, + "seed": { + "name": "시드" + }, + "steps": { + "name": "스텝수" + }, + "sampler_name": { + "name": "샘플러 이름" + }, + "scheduler": { + "name": "스케쥴러" + }, + "denoise": { + "name": "노이즈 제거양" + }, + "feather": { + "name": "가장자리 흐림", + "tooltip": "확대 해서 인페인트 된 이미지를 원본 이미지에 붙여넣을 때, 이 수치로 마스크의 가장 자리를 흐리게 해서 붙여넣어 이음매의 위화감을 줄여줍니다." + }, + "basic_pipe": { + "name": "기본 파이프", + "tooltip": "만약 기본 파이프 내의 모델에 `ImpactDummyInput` 가 설정된 경우, 인페인트 단계를 건너 뜁니다." + }, + "refiner_ratio": { + "name": "라파이너 적용 비율", + "tooltip": "SDXL 리파이너 모델을 사용할 경우 적용될 후반 스텝수 비율을 설정합니다." + }, + "detailer_hook": { + "name": "디테일러 후크", + "tooltip": "이 노드의 실행 중간단계에서 여러가지 기능을 수행할 수 있는 후크를 연결합니다." + }, + "refiner_basic_pipe_opt": { + "name": "리파이너 기본 파이프", + "tooltip": "SDXL 리파이너 단계에 적용할 기본 파이프를 연결합니다." + }, + "noise_mask_feather": { + "name": "노이즈 마스크 가장자리 흐림", + "tooltip": "인페인트시에 적용되는 노이즈 마스크의 가장자리를 흐리게 합니다. 이 설정값이 0을 초과할 경우, 내부적으로 자동으로 '차등 확산' 노드를 적용합니다." + }, + "scheduler_func_opt": { + "name": "스케쥴러 함수", + "tooltip": "GITS 스케쥴러 처럼 기본 스케쥴러 리스트에서 선택할 수 없는 특수 스케쥴러를 사용할 수 있게 해줍니다. 이 입력이 연결되면, 기본 스케쥴러 선택은 무시됩니다." + } + }, + "outputs": { + "0": { + "name": "개선 SEGS" + }, + "1": { + "name": "개선 이미지" + }, + "2": { + "name": "기본 파이프" + }, + "3": { + "name": "컨트롤넷 이미지" + } + } + }, + "SEGSDetailerForAnimateDiff": { + "description": "감지 영역 정보 묶음(SEGS)내의 각 영역들에 대해 가이드 크기를 기반으로 확대해서 인페이트하는 방법으로 디테일을 강화합니다.\n이 노드는 원본 이미지가 아닌 SEGS를 대상으로 적용되는 노드로 원본 이미지에 적용하려면 'SEGS 붙여넣기' 노드를 사용하세요.\n이 노드는 AnimateDiff와 같은 동영상의 디테일 개선을 위한 특수 디테일러 노드로써, SEGS가 담고 있는 마스크가 여러 프레임에 걸친 배치 마스크가 되는 경우를 처리할 수 있습니다.", + "display_name": "SEGS 디테일러 (AnimateDiff/파이프)", + "inputs": { + "image_frames": { + "name": "이미지 프레임 묶음" + }, + "segs": { + "name": "segs", + "tooltip": "감지 영역 정보를 담고 있는 묶음.\n이 영역들을 대상으로 인페인트가 적용됩니다." + }, + "guide_size": { + "name": "가이드 크기", + "tooltip": "'가이드 크기 대상'으로 지정된 크기의 가장 짧은면을 이 크기까지 확대합니다." + }, + "guide_size_for": { + "name": "가이드 크기 대상", + "tooltip": "bbox: 감지된 사각 영역(bbox)\ncrop_region: 잘라낸 영역" + }, + "max_size": { + "name": "최대 크기", + "tooltip": "'가이드 크기'로 확대 할 때, 가장 긴 면의 길이를 이 크기로 제한합니다. 너무 크게 확대 되는 것을 막아줍니다." + }, + "seed": { + "name": "시드" + }, + "steps": { + "name": "스텝수" + }, + "sampler_name": { + "name": "샘플러 이름" + }, + "scheduler": { + "name": "스케쥴러" + }, + "denoise": { + "name": "노이즈 제거양" + }, + "basic_pipe": { + "name": "기본 파이프", + "tooltip": "만약 기본 파이프 내의 모델에 `ImpactDummyInput` 가 설정된 경우, 인페인트 단계를 건너 뜁니다." + }, + "refiner_ratio": { + "name": "라파이너 적용 비율", + "tooltip": "SDXL 리파이너 모델을 사용할 경우 적용될 후반 스텝수 비율을 설정합니다." + }, + "refiner_basic_pipe_opt": { + "name": "리파이너 기본 파이프", + "tooltip": "SDXL 리파이너 단계에 적용할 기본 파이프를 연결합니다." + }, + "noise_mask_feather": { + "name": "노이즈 마스크 가장자리 흐림", + "tooltip": "인페인트시에 적용되는 노이즈 마스크의 가장자리를 흐리게 합니다. 이 설정값이 0을 초과할 경우, 내부적으로 자동으로 '차등 확산' 노드를 적용합니다." + }, + "scheduler_func_opt": { + "name": "스케쥴러 함수", + "tooltip": "GITS 스케쥴러 처럼 기본 스케쥴러 리스트에서 선택할 수 없는 특수 스케쥴러를 사용할 수 있게 해줍니다. 이 입력이 연결되면, 기본 스케쥴러 선택은 무시됩니다." + } + }, + "outputs": { + "0": { + "name": "개선 SEGS" + }, + "1": { + "name": "개선 이미지" + } + } + }, + "SEGSDetailer": { + "description": "감지 영역 정보 묶음(SEGS)내의 각 영역들에 대해 가이드 크기를 기반으로 확대해서 인페이트하는 방법으로 디테일을 강화합니다.\n이 노드는 원본 이미지가 아닌 SEGS를 대상으로 적용되는 노드로 원본 이미지에 적용하려면 'SEGS 붙여넣기' 노드를 사용하세요.", + "display_name": "SEGS 디테일러 (파이프)", + "inputs": { + "image": { + "name": "이미지" + }, + "segs": { + "name": "segs", + "tooltip": "감지 영역 정보를 담고 있는 묶음.\n이 영역들을 대상으로 인페인트가 적용됩니다." + }, + "guide_size": { + "name": "가이드 크기", + "tooltip": "'가이드 크기 대상'으로 지정된 크기의 가장 짧은면을 이 크기까지 확대합니다." + }, + "guide_size_for": { + "name": "가이드 크기 대상", + "tooltip": "bbox: 감지된 사각 영역(bbox)\ncrop_region: 잘라낸 영역" + }, + "max_size": { + "name": "최대 크기", + "tooltip": "'가이드 크기'로 확대 할 때, 가장 긴 면의 길이를 이 크기로 제한합니다. 너무 크게 확대 되는 것을 막아줍니다." + }, + "seed": { + "name": "시드" + }, + "steps": { + "name": "스텝수" + }, + "sampler_name": { + "name": "샘플러 이름" + }, + "scheduler": { + "name": "스케쥴러" + }, + "denoise": { + "name": "노이즈 제거양" + }, + "noise_mask": { + "name": "노이즈 마스크 사용", + "tooltip": "인페인트 할 때, 마스크를 적용해서 마스크 영역만 인페인트합니다. 이 옵션을 적용하지 않으면, 잘라낸 이미지 전체가 재생성되어 노이즈 제거양이 클 때 위화감이 나타나게 됩니다." + }, + "force_inpaint": { + "name": "인페인트 강제 적용", + "tooltip": "가이드 크기와 상관 없이 인페인팅을 무조건 적용 합니다. 이 옵션이 꺼져있는 경우 가이드 크기보다 이미 큰 감지 영역은 인페인팅을 건너 뜁니다." + }, + "basic_pipe": { + "name": "기본 파이프", + "tooltip": "만약 기본 파이프 내의 모델에 `ImpactDummyInput` 가 설정된 경우, 인페인트 단계를 건너 뜁니다." + }, + "refiner_ratio": { + "name": "라파이너 적용 비율", + "tooltip": "SDXL 리파이너 모델을 사용할 경우 적용될 후반 스텝수 비율을 설정합니다." + }, + "batch_size": { + "name": "배치 갯수", + "tooltip": "대상 SEGS 에 대해서 배치 갯수만큼 여러개의 후보를 생성합니다. 여러개를 생성할 경우 '고르기 (SEGS)'와 함께 사용하세요." + }, + "cycle": { + "name": "반복수", + "tooltip": "설정된 값만큼 인페인팅을 반복 적용합니다. 인코딩/디코딩 없이 확대된 잠재 이미지 단계에서 반복됩니다." + }, + "refiner_basic_pipe_opt": { + "name": "리파이너 기본 파이프", + "tooltip": "SDXL 리파이너 단계에 적용할 기본 파이프를 연결합니다." + }, + "inpaint_model": { + "name": "인페인트 모델 모드", + "tooltip": "인페인트 전용 모델을 사용할 경우 이 옵션을 켜면, 인페인팅시에 '인페인트 모델 조건 설정'이 적용되어 수행됩니다." + }, + "noise_mask_feather": { + "name": "노이즈 마스크 가장자리 흐림", + "tooltip": "인페인트시에 적용되는 노이즈 마스크의 가장자리를 흐리게 합니다. 이 설정값이 0을 초과할 경우, 내부적으로 자동으로 '차등 확산' 노드를 적용합니다." + }, + "scheduler_func_opt": { + "name": "스케쥴러 함수", + "tooltip": "GITS 스케쥴러 처럼 기본 스케쥴러 리스트에서 선택할 수 없는 특수 스케쥴러를 사용할 수 있게 해줍니다. 이 입력이 연결되면, 기본 스케쥴러 선택은 무시됩니다." + } + }, + "outputs": { + "0": { + "name": "개선 SEGS" + }, + "1": { + "name": "컨트롤넷 이미지" + } + } + }, + "MaskDetailerPipe": { + "description": "이 디테일러 노드는 마스크로 설정된 영역을 확대해서 가이드 크기를 기반으로 확대해서 인페인트하는 방법으로 디테일을 강화합니다.", + "display_name": "마스크 디테일러 (파이프)", + "inputs": { + "image": { + "name": "이미지" + }, + "mask": { + "name": "마스크", + "tooltip": "디테일을 강화하고 싶은 대상 영역이 설정된 마스크. 분리된 마스크 영역은 개별적으로 디테일 강화가 이루어집니다." + }, + "basic_pipe": { + "name": "기본 파이프", + "tooltip": "만약 기본 파이프 내의 모델에 `ImpactDummyInput` 가 설정된 경우, 인페인트 단계를 건너 뜁니다." + }, + "guide_size": { + "name": "가이드 크기", + "tooltip": "'가이드 크기 대상'으로 지정된 크기의 가장 짧은면을 이 크기까지 확대합니다." + }, + "guide_size_for": { + "name": "가이드 크기 대상", + "tooltip": "bbox: 감지된 사각 영역(bbox)\ncrop_region: 잘라낸 영역" + }, + "max_size": { + "name": "최대 크기", + "tooltip": "'가이드 크기'로 확대 할 때, 가장 긴 면의 길이를 이 크기로 제한합니다. 너무 크게 확대 되는 것을 막아줍니다." + }, + "mask_mode": { + "name": "마스크 모드", + "tooltip": "마스크로 설정된 영역만을 인페인트 할지, 잘라낸 영역 전체를 인페인트 할 것인지를 설정합니다." + }, + "seed": { + "name": "시드" + }, + "steps": { + "name": "스텝수" + }, + "sampler_name": { + "name": "샘플러 이름" + }, + "scheduler": { + "name": "스케쥴러" + }, + "denoise": { + "name": "노이즈 제거양" + }, + "feather": { + "name": "가장자리 흐림", + "tooltip": "확대 해서 인페인트 된 이미지를 원본 이미지에 붙여넣을 때, 이 수치로 마스크의 가장 자리를 흐리게 해서 붙여넣어 이음매의 위화감을 줄여줍니다." + }, + "crop_factor": { + "name": "자르기 배율", + "tooltip": "각 마스크로 설정된 영역에 대해서 몇배 크기를 잘라내어서 인페인트에 사용할지를 설정합니다. 이 크기가 너무 작으면, 인페인트 할 이미지의 주변 정보가 부족해서 위화감이 강한 이미지가 생성됩니다. 이 크기가 너무 크면, 인페인팅에 너무 오랜 시간이 걸릴 수 있으며, 모델의 역량을 초과할 정도로 클 경우 올바르지 않은 이미지를 생성하게 됩니다." + }, + "drop_size": { + "name": "감지 최소 크기", + "tooltip": "사각 영역(bbox) 감지기로 감지한 크기가 이 설정값 보다 작을 경우 무시합니다." + }, + "refiner_ratio": { + "name": "라파이너 적용 비율", + "tooltip": "SDXL 리파이너 모델을 사용할 경우 적용될 후반 스텝수 비율을 설정합니다." + }, + "batch_size": { + "name": "배치 갯수", + "tooltip": "대상 SEGS 에 대해서 배치 갯수만큼 여러개의 후보를 생성합니다. 여러개를 생성할 경우 '고르기 (SEGS)'와 함께 사용하세요." + }, + "cycle": { + "name": "반복수", + "tooltip": "설정된 값만큼 인페인팅을 반복 적용합니다. 인코딩/디코딩 없이 확대된 잠재 이미지 단계에서 반복됩니다." + }, + "refiner_basic_pipe_opt": { + "name": "리파이너 기본 파이프", + "tooltip": "SDXL 리파이너 단계에 적용할 기본 파이프를 연결합니다." + }, + "detailer_hook": { + "name": "디테일러 후크", + "tooltip": "이 노드의 실행 중간단계에서 여러가지 기능을 수행할 수 있는 후크를 연결합니다." + }, + "inpaint_model": { + "name": "인페인트 모델 모드", + "tooltip": "인페인트 전용 모델을 사용할 경우 이 옵션을 켜면, 인페인팅시에 '인페인트 모델 조건 설정'이 적용되어 수행됩니다." + }, + "noise_mask_feather": { + "name": "노이즈 마스크 가장자리 흐림", + "tooltip": "인페인트시에 적용되는 노이즈 마스크의 가장자리를 흐리게 합니다. 이 설정값이 0을 초과할 경우, 내부적으로 자동으로 '차등 확산' 노드를 적용합니다." + }, + "bbox_fill": { + "name": "bbox 채우기", + "tooltip": "각 마스크 조각들을 해당 마스크를 포함하는 가장 작은 사각 영역의 마스크로 간주합니다." + }, + "contour_fill": { + "name": "윤곽 내부 채우기", + "tooltip": "윤곽선 형태의 마스크 조각들의 경우 마스크 내부가 모두 채워진 것으로 간주합니다." + }, + "scheduler_func_opt": { + "name": "스케쥴러 함수", + "tooltip": "GITS 스케쥴러 처럼 기본 스케쥴러 리스트에서 선택할 수 없는 특수 스케쥴러를 사용할 수 있게 해줍니다. 이 입력이 연결되면, 기본 스케쥴러 선택은 무시됩니다." + } + }, + "outputs": { + "0": { + "name": "개선 이미지" + }, + "1": { + "name": "잘라낸 개선 이미지" + }, + "2": { + "name": "잘라낸 투명 개선 이미지" + }, + "3": { + "name": "기본 파이프" + }, + "4": { + "name": "리파이너 기본 파이프" + } + } + }, + + "SEGSPaste": { + "description": "SEGS 디테일러를 통해 개선된 SEGS를 원본 이미지에 붙여넣는 기능을 제공하기 위한 노드입니다.", + "display_name": "SEGS 붙여넣기", + "inputs": { + "image": { + "name": "원본 이미지" + }, + "segs": { + "name": "segs" + }, + "feather": { + "name": "가장자리 흐림", + "tooltip": "개선된 SEGS의 이미지를 원본 이미지에 붙여넣을 때, 이 수치로 마스크의 가장 자리를 흐리게 해서 붙여넣어 이음매의 위화감을 줄여줍니다." + }, + "alpha": { + "name": "투명도", + "tooltip": "원본에 붙여넣는 이미지에 투명도를 설정합니다." + }, + "ref_image_opt": { + "name": "참조 이미지", + "tooltip": "디테일러를 통과시키거나 'SEGS에 기본 이미지 설정'을 한 경우가 아니라면, SEGS는 이미지가 없이 감지 영역 정보만 있습니다. 이 때 감지영역이 참조할 원본 이미지를 설정합니다." + } + }, + "outputs": { + "0": { + "name": "개선 SEGS" + } + } + }, + + "ImpactSEGSPicker": { + "description": "입력된 SEGS 중에서 선택된 SEGS만을 고를 수 있는 있는 기능을 제공합니다.", + "display_name": "고르기 (SEGS)", + "inputs": { + "picks": { + "name": "선택 목록", + "tooltip": "출력할 SEGS 번호 목록을 나열합니다. 'pick' 버튼을 눌러서 선택하세요." + }, + "segs": { + "name": "segs" + }, + "fallback_image_opt": { + "name": "참조 이미지", + "tooltip": "디테일러를 통과시키거나 'SEGS에 기본 이미지 설정'을 한 경우가 아니라면, SEGS는 이미지가 없이 감지 영역 정보만 있습니다. 이 때 감지영역이 참조할 원본 이미지를 설정합니다." + } + }, + "outputs": { + "0": { + "name": "선택된 SEGS" + } + } + }, + + "SetDefaultImageForSEGS": { + "description": "디테일러를 통과시킨 경우가 아니라면, SEGS는 이미지가 없이 감지 영역 정보만 있습니다. 이 노드는 SEGS에 기본 이미지를 설정해 줍니다.", + "display_name": "SEGS에 기본 이미지 설정", + "inputs": { + "segs": { + "name": "segs" + }, + "image": { + "name": "이미지" + }, + "override": { + "name": "덮어쓰기", + "tooltip": "이미 설정된 이미지가 있는 경우 덮어쓸지 여부를 설정합니다." + } + }, + "outputs": { + "0": { + "name": "segs" + } + } + }, + + "ImpactWildcardProcessor": { + "description": "이 노드는 와일드카드 구문으로 작성된 텍스트 프롬프트를 처리하고, 처리된 텍스트 프롬프트를 출력합니다.\n\nTIP: 워크플로가 실행되기 전에 '와일드카드 텍스트'의 처리 결과가 '채워진(populated) 텍스트'에 표시되며, 이 값은 워크플로와 함께 저장됩니다. 입력으로 변환된 시드를 사용하려면 '와일드카드 텍스트' 대신 '채워진(populated) 텍스트'에 직접 프롬프트를 작성하고, 모드를 '고정(fixed)'로 설정하세요.", + "display_name": "와일드카드 처리기 (Impact)", + "inputs": { + "wildcard_text": { + "name": "와일드카드 텍스트", + "tooltip": "와일드카드 문법으로 작성된 텍스트 프롬프트를 입력하세요." + }, + "populated_text": { + "name": "채워진 텍스트", + "tooltip": "이 노드에 실행 중에 전달되는 실제 값은 여기 표시된 값입니다. 동작은 모드에 따라 약간 다를 수 있으며, '채워진 텍스트'에서도 와일드카드 구문을 사용할 수 있습니다." + }, + "mode": { + "name": "모드", + "tooltip": "채우기(populate): 워크플로를 실행하기 전에 '와일드카드 텍스트'에서 처리된 프롬프트로 '채워진 텍스트'의 기존 값을 덮어씁니다. 이 모드에서는 '채워진 텍스트'를 수정할 수 없습니다.\n\n고정(fixed): '와일드카드 텍스트'를 무시하고 '채워진 텍스트'의 값을 그대로 유지합니다. 이 모드에서는 '채워진 텍스트'를 수정할 수 있습니다.\n\n재현(reproduce): 이 모드는 한 번만 '고정(fixed)' 모드로 작동하여 재현한 후, 이후에는 '채우기(populate)' 모드로 전환됩니다." + }, + "seed": { + "name": "시드", + "tooltip": "와일드카드의 무작위 선택에 사용할 시드 입니다" + }, + "Select to add Wildcard": { + "name": "추가할 와일드카드 선택" + } + }, + "outputs": { + "0": { + "name": "처리된 텍스트" + } + } + }, + + "ImpactWildcardEncode": { + "description": "이 노드는 와일드카드 구문으로 작성된 텍스트 프롬프트를 처리하고 이를 조건으로 출력합니다. 또한 LoRA 구문을 지원하며, 적용된 LoRA는 모델 출력에 반영됩니다.\n\nTIP1: 워크플로가 실행되기 전에 '와일드카드 텍스트'의 처리 결과가 '채워진 텍스트'에 표시되며, 이 값은 워크플로와 함께 저장됩니다. 입력으로 변환된 시드를 사용하려면 '와일드카드 텍스트' 대신 '채워진 텍스트'에 직접 프롬프트를 작성하고, 모드를 '고정(fixed)'로 설정하세요.\nTIP2: 'Inspire Pack'이 설치되어 있으면 LBW(로라 블록 웨이트) 구문도 적용할 수 있습니다.", + "display_name": "와일드카드 인코딩 (Impact)", + "inputs": { + "wildcard_text": { + "name": "와일드카드 텍스트", + "tooltip": "와일드카드 문법으로 작성된 텍스트 프롬프트를 입력하세요." + }, + "populated_text": { + "name": "채워진 텍스트", + "tooltip": "이 노드에 실행 중에 전달되는 실제 값은 여기 표시된 값입니다. 동작은 모드에 따라 약간 다를 수 있으며, '채워진 텍스트'에서도 와일드카드 구문을 사용할 수 있습니다." + }, + "mode": { + "name": "모드", + "tooltip": "채우기(populate): 워크플로를 실행하기 전에 '와일드카드 텍스트'에서 처리된 프롬프트로 '채워진 텍스트'의 기존 값을 덮어씁니다. 이 모드에서는 '채워진 텍스트'를 수정할 수 없습니다.\n\n고정(fixed): '와일드카드 텍스트'를 무시하고 '채워진 텍스트'의 값을 그대로 유지합니다. 이 모드에서는 '채워진 텍스트'를 수정할 수 있습니다.\n\n재현(reproduce): 이 모드는 한 번만 '고정(fixed)' 모드로 작동하여 재현한 후, 이후에는 '채우기(populate)' 모드로 전환됩니다." + }, + "Select to add LoRA": { + "name": "추가할 LoRA 선택" + }, + "Select to add Wildcard": { + "name": "추가할 와일드카드 선택" + }, + "seed": { + "name": "시드", + "tooltip": "와일드카드의 무작위 선택에 사용할 시드 입니다" + } + }, + "outputs": { + "0": { + "name": "model", + "tooltip": "LoRA 적용 문법이 사용된 경우, LoRA 가 적용된 model이 출력됩니다." + }, + "1": { + "name": "clip", + "tooltip": "LoRA 적용 문법이 사용된 경우, LoRA 가 적용된 clip이 출력됩니다." + }, + "2": { + "name": "조건" + }, + "3": { + "name": "채워진 텍스트" + } + } + } +} \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/animatediff_nodes.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/animatediff_nodes.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4631b298dffc9e99f6dcc11ba904cab447b15040 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/animatediff_nodes.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/bridge_nodes.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/bridge_nodes.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ba3878eb49025d19fa1fe64a66461d5cc7f40d77 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/bridge_nodes.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/config.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/config.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..299de5b2c13c1fc63548238c9fb680da3ca006ea Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/config.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/core.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/core.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..31cad13af4f6e0a808d7c1f302a0b1a94ee01428 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/core.cpython-312.pyc @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:891b0b3d0195f5fe60c984af89a06cad6ca45573442d0695d56f7b7f137e4c8d +size 112593 diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/defs.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/defs.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..06629be69fa391ab60df4c1fbcaf66a6c2d97b01 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/defs.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/detectors.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/detectors.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..750d22e47af105d2fd87d6371c23f54d63360ee1 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/detectors.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/hf_nodes.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/hf_nodes.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..50af6868a14903a08e21e47d2da7c8e8d8ac391e Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/hf_nodes.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/hook_nodes.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/hook_nodes.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..54e97cebec8d718d6f8c43b5fa5f38c1e44a3819 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/hook_nodes.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/hooks.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/hooks.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..213dd4824b01575bfb79c417aefa7e0d55e0aeb0 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/hooks.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/impact_pack.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/impact_pack.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3ae4526d3fd2c4e3987b073a924bf88cf47214fa --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/impact_pack.cpython-312.pyc @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2e0a8cd4970969eea4da16cb1f841f059010114d07b6e6973550c94f8cc4c4a4 +size 118304 diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/impact_sampling.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/impact_sampling.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7bcbb6c083b7f13d6b6debbc35b6f6e887a4bf10 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/impact_sampling.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/impact_server.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/impact_server.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..89c2034f83bade8b00395918c609628728d3c571 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/impact_server.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/logics.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/logics.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dfe881f62158facd83584a2a0dcbc1537fb1151d Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/logics.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/pipe.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/pipe.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b88c897d01ee8ac9786a5ea593003f83ca2a9451 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/pipe.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/segs_nodes.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/segs_nodes.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3f5cc7e4b00a2d68b996157d1c4f057387749ac9 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/segs_nodes.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/segs_upscaler.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/segs_upscaler.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a9bb7e135088d9e7e898dcec1c9d29af87584211 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/segs_upscaler.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/special_samplers.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/special_samplers.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..032f13d5474ecefdf659030f946233b3b7e4fd0d Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/special_samplers.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/util_nodes.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/util_nodes.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..67a96160d88cbf8194deaef2da432618e9cc609f Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/util_nodes.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/utils.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/utils.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8a56fb3ea838c82190f82e3765c3de7d44784b40 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/utils.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/wildcards.cpython-312.pyc b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/wildcards.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..15e21d30fc52be7d6eb039c07365eb50f50f24c2 Binary files /dev/null and b/custom_nodes/comfyui-impact-pack/modules/impact/__pycache__/wildcards.cpython-312.pyc differ diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/additional_dependencies.py b/custom_nodes/comfyui-impact-pack/modules/impact/additional_dependencies.py new file mode 100644 index 0000000000000000000000000000000000000000..904c729f05492a185b97b00829807da9313f7164 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/additional_dependencies.py @@ -0,0 +1,12 @@ +import sys +import subprocess + + +def ensure_onnx_package(): + try: + import onnxruntime # noqa: F401 + except Exception: + if "python_embeded" in sys.executable or "python_embedded" in sys.executable: + subprocess.check_call([sys.executable, '-s', '-m', 'pip', 'install', 'onnxruntime']) + else: + subprocess.check_call([sys.executable, '-s', '-m', 'pip', 'install', 'onnxruntime']) diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/animatediff_nodes.py b/custom_nodes/comfyui-impact-pack/modules/impact/animatediff_nodes.py new file mode 100644 index 0000000000000000000000000000000000000000..97941ef74cb8f268972ae26c6dbbb3a891cac238 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/animatediff_nodes.py @@ -0,0 +1,200 @@ +from nodes import MAX_RESOLUTION +import impact.core as core +from impact.core import SEG +from impact.segs_nodes import SEGSPaste +import comfy +from impact import utils +import torch +import nodes +import logging + +try: + from comfy_extras import nodes_differential_diffusion +except Exception: + logging.warning("\n#############################################\n[Impact Pack] ComfyUI is an outdated version.\n#############################################\n") + raise Exception("[Impact Pack] ComfyUI is an outdated version.") + + +class SEGSDetailerForAnimateDiff: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "image_frames": ("IMAGE", ), + "segs": ("SEGS", ), + "guide_size": ("FLOAT", {"default": 512, "min": 64, "max": MAX_RESOLUTION, "step": 8}), + "guide_size_for": ("BOOLEAN", {"default": True, "label_on": "bbox", "label_off": "crop_region"}), + "max_size": ("FLOAT", {"default": 768, "min": 64, "max": MAX_RESOLUTION, "step": 8}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + "basic_pipe": ("BASIC_PIPE", {"tooltip": "If the `ImpactDummyInput` is connected to the model in the basic_pipe, the inference stage is skipped."}), + "refiner_ratio": ("FLOAT", {"default": 0.2, "min": 0.0, "max": 1.0}), + }, + "optional": { + "refiner_basic_pipe_opt": ("BASIC_PIPE",), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + } + } + + RETURN_TYPES = ("SEGS", "IMAGE") + RETURN_NAMES = ("segs", "cnet_images") + OUTPUT_IS_LIST = (False, True) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + DESCRIPTION = "This node enhances details by inpainting each region within the detected area bundle (SEGS) after enlarging them based on the guide size.\nThis node is applied specifically to SEGS rather than the entire image. To apply it to the entire image, use the 'SEGS Paste' node.\nAs a specialized detailer node for improving video details, such as in AnimateDiff, this node can handle cases where the masks contained in SEGS serve as batch masks spanning multiple frames." + + @staticmethod + def do_detail(image_frames, segs, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, scheduler, + denoise, basic_pipe, refiner_ratio=None, refiner_basic_pipe_opt=None, noise_mask_feather=0, scheduler_func_opt=None): + + model, clip, vae, positive, negative = basic_pipe + if refiner_basic_pipe_opt is None: + refiner_model, refiner_clip, refiner_positive, refiner_negative = None, None, None, None + else: + refiner_model, refiner_clip, _, refiner_positive, refiner_negative = refiner_basic_pipe_opt + + segs = core.segs_scale_match(segs, image_frames.shape) + + new_segs = [] + cnet_image_list = [] + + if not (isinstance(model, str) and model == "DUMMY") and noise_mask_feather > 0 and 'denoise_mask_function' not in model.model_options: + model = nodes_differential_diffusion.DifferentialDiffusion().apply(model)[0] + + for seg in segs[1]: + cropped_image_frames = None + + for image in image_frames: + image = image.unsqueeze(0) + cropped_image = seg.cropped_image if seg.cropped_image is not None else utils.crop_tensor4(image, seg.crop_region) + cropped_image = utils.to_tensor(cropped_image) + if cropped_image_frames is None: + cropped_image_frames = cropped_image + else: + cropped_image_frames = torch.concat((cropped_image_frames, cropped_image), dim=0) + + cropped_image_frames = cropped_image_frames.cpu().numpy() + + # It is assumed that AnimateDiff does not support conditioning masks based on test results, but it will be added for future consideration. + cropped_positive = [ + [condition, { + k: core.crop_condition_mask(v, cropped_image_frames, seg.crop_region) if k == "mask" else v + for k, v in details.items() + }] + for condition, details in positive + ] + + cropped_negative = [ + [condition, { + k: core.crop_condition_mask(v, cropped_image_frames, seg.crop_region) if k == "mask" else v + for k, v in details.items() + }] + for condition, details in negative + ] + + if not (isinstance(model, str) and model == "DUMMY"): + enhanced_image_tensor, cnet_images = core.enhance_detail_for_animatediff(cropped_image_frames, model, clip, vae, guide_size, guide_size_for, max_size, + seg.bbox, seed, steps, cfg, sampler_name, scheduler, + cropped_positive, cropped_negative, denoise, seg.cropped_mask, + refiner_ratio=refiner_ratio, refiner_model=refiner_model, + refiner_clip=refiner_clip, refiner_positive=refiner_positive, + refiner_negative=refiner_negative, control_net_wrapper=seg.control_net_wrapper, + noise_mask_feather=noise_mask_feather, scheduler_func=scheduler_func_opt) + else: + enhanced_image_tensor = cropped_image_frames + cnet_images = None + + if cnet_images is not None: + cnet_image_list.extend(cnet_images) + + if enhanced_image_tensor is None: + new_cropped_image = cropped_image_frames + else: + new_cropped_image = enhanced_image_tensor.cpu().numpy() + + new_seg = SEG(new_cropped_image, seg.cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, None) + new_segs.append(new_seg) + + return (segs[0], new_segs), cnet_image_list + + def doit(self, image_frames, segs, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, scheduler, + denoise, basic_pipe, refiner_ratio=None, refiner_basic_pipe_opt=None, inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None): + + segs, cnet_images = SEGSDetailerForAnimateDiff.do_detail(image_frames, segs, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, + scheduler, denoise, basic_pipe, refiner_ratio, refiner_basic_pipe_opt, + noise_mask_feather=noise_mask_feather, scheduler_func_opt=scheduler_func_opt) + + if len(cnet_images) == 0: + cnet_images = [utils.empty_pil_tensor()] + + return (segs, cnet_images) + + +class DetailerForEachPipeForAnimateDiff: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "image_frames": ("IMAGE", ), + "segs": ("SEGS", ), + "guide_size": ("FLOAT", {"default": 512, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "guide_size_for": ("BOOLEAN", {"default": True, "label_on": "bbox", "label_off": "crop_region"}), + "max_size": ("FLOAT", {"default": 1024, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + "feather": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}), + "basic_pipe": ("BASIC_PIPE", {"tooltip": "If the `ImpactDummyInput` is connected to the model in the basic_pipe, the inference stage is skipped."}), + "refiner_ratio": ("FLOAT", {"default": 0.2, "min": 0.0, "max": 1.0}), + }, + "optional": { + "detailer_hook": ("DETAILER_HOOK",), + "refiner_basic_pipe_opt": ("BASIC_PIPE",), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + } + } + + RETURN_TYPES = ("IMAGE", "SEGS", "BASIC_PIPE", "IMAGE") + RETURN_NAMES = ("image", "segs", "basic_pipe", "cnet_images") + OUTPUT_IS_LIST = (False, False, False, True) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + DESCRIPTION = "This node enhances details by inpainting each region within the detected area bundle (SEGS) after enlarging them based on the guide size.\nThis node is a specialized detailer node for enhancing video details, such as in AnimateDiff. It can handle cases where the masks contained in SEGS serve as batch masks spanning multiple frames." + + @staticmethod + def doit(image_frames, segs, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, scheduler, + denoise, feather, basic_pipe, refiner_ratio=None, detailer_hook=None, refiner_basic_pipe_opt=None, + noise_mask_feather=0, scheduler_func_opt=None): + + enhanced_segs = [] + cnet_image_list = [] + + for sub_seg in segs[1]: + single_seg = segs[0], [sub_seg] + enhanced_seg, cnet_images = SEGSDetailerForAnimateDiff().do_detail(image_frames, single_seg, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, scheduler, + denoise, basic_pipe, refiner_ratio, refiner_basic_pipe_opt, noise_mask_feather, scheduler_func_opt=scheduler_func_opt) + + image_frames = SEGSPaste.doit(image_frames, enhanced_seg, feather, alpha=255)[0] + + if cnet_images is not None: + cnet_image_list.extend(cnet_images) + + if detailer_hook is not None: + image_frames = detailer_hook.post_paste(image_frames) + + enhanced_segs += enhanced_seg[1] + + new_segs = segs[0], enhanced_segs + return image_frames, new_segs, basic_pipe, cnet_image_list diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/bridge_nodes.py b/custom_nodes/comfyui-impact-pack/modules/impact/bridge_nodes.py new file mode 100644 index 0000000000000000000000000000000000000000..b3923b58a99bba6afdecdbf143228803c9c16eed --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/bridge_nodes.py @@ -0,0 +1,490 @@ +import os +from PIL import ImageOps +import logging +import folder_paths +import torch +import nodes +from PIL import Image +import numpy as np +from impact import utils + +# NOTE: this should not be `from . import core`. +# I don't know why but... 'from .' and 'from impact' refer to different core modules. +# This separates global variables of the core module and breaks the preview bridge. +from impact import core +# <-- +import random + + +class PreviewBridge: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "images": ("IMAGE",), + "image": ("STRING", {"default": ""}), + }, + "optional": { + "block": ("BOOLEAN", {"default": False, "label_on": "if_empty_mask", "label_off": "never", "tooltip": "is_empty_mask: If the mask is empty, the execution is stopped.\nnever: The execution is never stopped."}), + "restore_mask": (["never", "always", "if_same_size"], {"tooltip": "if_same_size: If the changed input image is the same size as the previous image, restore using the last saved mask\nalways: Whenever the input image changes, always restore using the last saved mask\nnever: Do not restore the mask.\n`restore_mask` has higher priority than `block`"}), + }, + "hidden": {"unique_id": "UNIQUE_ID", "extra_pnginfo": "EXTRA_PNGINFO"}, + } + + RETURN_TYPES = ("IMAGE", "MASK", ) + + FUNCTION = "doit" + + OUTPUT_NODE = True + + CATEGORY = "ImpactPack/Util" + + DESCRIPTION = "This is a feature that allows you to edit and send a Mask over a image.\nIf the block is set to 'is_empty_mask', the execution is stopped when the mask is empty." + + def __init__(self): + super().__init__() + self.output_dir = folder_paths.get_temp_directory() + self.type = "temp" + self.prev_hash = None + + @staticmethod + def load_image(pb_id): + is_fail = False + if pb_id not in core.preview_bridge_image_id_map: + is_fail = True + + if not is_fail: + image_path, ui_item = core.preview_bridge_image_id_map[pb_id] + if not os.path.isfile(image_path): + is_fail = True + + if not is_fail: + i = Image.open(image_path) + i = ImageOps.exif_transpose(i) + image = i.convert("RGB") + image = np.array(image).astype(np.float32) / 255.0 + image = torch.from_numpy(image)[None,] + + if 'A' in i.getbands(): + mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0 + mask = 1. - torch.from_numpy(mask) + else: + mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu") + else: + image = utils.empty_pil_tensor() + mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu") + ui_item = { + "filename": 'empty.png', + "subfolder": '', + "type": 'temp' + } + + return image, mask.unsqueeze(0), ui_item + + @staticmethod + def register_clipspace_image(clipspace_path, node_id): + """Register a clipspace image file in the preview bridge system. + + This handles the case where ComfyUI's mask editor creates clipspace files + that need to be integrated with the preview bridge system. + """ + # Remove [input] suffix if present + clean_path = clipspace_path.replace(" [input]", "").replace("[input]", "") + + # Try to find the actual clipspace file + input_dir = folder_paths.get_input_directory() + potential_paths = [ + clean_path, + os.path.join(input_dir, clean_path), + os.path.join(input_dir, "clipspace", os.path.basename(clean_path)), + os.path.abspath(clean_path), + ] + + actual_file = None + for path in potential_paths: + if os.path.isfile(path): + actual_file = path + break + + if not actual_file: + return False + + # Create ui_item for the clipspace file + ui_item = { + 'filename': os.path.basename(actual_file), + 'subfolder': 'clipspace', + 'type': 'input' + } + + # Register it using the preview bridge system + core.set_previewbridge_image(node_id, actual_file, ui_item) + # Also register under the original clipspace path for compatibility + core.preview_bridge_image_id_map[clipspace_path] = (actual_file, ui_item) + + return True + + def doit(self, images, image, unique_id, block=False, restore_mask="never", prompt=None, extra_pnginfo=None): + need_refresh = False + images_changed = False + + # Check if images have changed (this determines if we start fresh) + if unique_id not in core.preview_bridge_cache: + need_refresh = True + images_changed = True + elif core.preview_bridge_cache[unique_id][0] is not images: + need_refresh = True + images_changed = True + + # If images changed, clear the mask cache to ensure fresh start behavior + # This restores the original behavior where new images start with empty masks + # unless restore_mask is set to "always" or "if_same_size" + if images_changed and restore_mask not in ["always", "if_same_size"] and unique_id in core.preview_bridge_last_mask_cache: + del core.preview_bridge_last_mask_cache[unique_id] + + # Handle clipspace files that aren't registered in the preview bridge system + # This only applies when images haven't changed (same image, new mask scenario) + if not need_refresh and image not in core.preview_bridge_image_id_map: + # Check if this is a clipspace file that needs to be registered + is_clipspace = image and ("clipspace" in image.lower() or "[input]" in image) + if is_clipspace: + if not PreviewBridge.register_clipspace_image(image, unique_id): + need_refresh = True + else: + need_refresh = True + + if not need_refresh: + pixels, mask, path_item = PreviewBridge.load_image(image) + image = [path_item] + else: + # For new images (images_changed=True), we want to start fresh regardless of restore_mask + # For same image with refresh needed, respect the restore_mask setting + # Exception: when restore_mask is "always", restore even with new images + # Exception: when restore_mask is "if_same_size", allow restoration to check size compatibility + if restore_mask != "never" and (not images_changed or restore_mask in ["always", "if_same_size"]): + mask = core.preview_bridge_last_mask_cache.get(unique_id) + if mask is None: + mask = None + elif restore_mask == "if_same_size" and mask.shape[1:] != images.shape[1:3]: + # For if_same_size, clear mask if dimensions don't match + mask = None + # For "always", keep the mask regardless of size + else: + mask = None + + if mask is None: + mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu") + res = nodes.PreviewImage().save_images(images, filename_prefix="PreviewBridge/PB-", prompt=prompt, extra_pnginfo=extra_pnginfo) + else: + masked_images = utils.tensor_convert_rgba(images) + resized_mask = utils.resize_mask(mask, (images.shape[1], images.shape[2])).unsqueeze(3) + resized_mask = 1 - resized_mask + utils.tensor_putalpha(masked_images, resized_mask) + res = nodes.PreviewImage().save_images(masked_images, filename_prefix="PreviewBridge/PB-", prompt=prompt, extra_pnginfo=extra_pnginfo) + + image2 = res['ui']['images'] + pixels = images + + path = os.path.join(folder_paths.get_temp_directory(), 'PreviewBridge', image2[0]['filename']) + core.set_previewbridge_image(unique_id, path, image2[0]) + core.preview_bridge_image_id_map[image] = (path, image2[0]) + core.preview_bridge_image_name_map[unique_id, path] = (image, image2[0]) + core.preview_bridge_cache[unique_id] = (images, image2) + + image = image2 + + is_empty_mask = torch.all(mask == 0) + + if block and is_empty_mask and core.is_execution_model_version_supported(): + from comfy_execution.graph import ExecutionBlocker + result = ExecutionBlocker(None), ExecutionBlocker(None) + elif block and is_empty_mask: + logging.warning("[Impact Pack] PreviewBridge: ComfyUI is outdated - blocking feature is disabled.") + result = pixels, mask + else: + result = pixels, mask + + if not is_empty_mask: + core.preview_bridge_last_mask_cache[unique_id] = mask + + return { + "ui": {"images": image}, + "result": result, + } + + +def decode_latent(latent, preview_method, vae_opt=None): + if vae_opt is not None: + image = nodes.VAEDecode().decode(vae_opt, latent)[0] + return image + + from comfy.cli_args import LatentPreviewMethod + import comfy.latent_formats as latent_formats + + if preview_method.startswith("TAE"): + decoder_name = None + + if preview_method == "TAESD15": + decoder_name = "taesd" + elif preview_method == 'TAESDXL': + decoder_name = "taesdxl" + elif preview_method == 'TAESD3': + decoder_name = "taesd3" + elif preview_method == 'TAEF1': + decoder_name = "taef1" + + if decoder_name: + vae = nodes.VAELoader().load_vae(decoder_name)[0] + image = nodes.VAEDecode().decode(vae, latent)[0] + return image + + if preview_method == "Latent2RGB-SD15": + latent_format = latent_formats.SD15() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-SDXL": + latent_format = latent_formats.SDXL() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-SD3": + latent_format = latent_formats.SD3() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-SD-X4": + latent_format = latent_formats.SD_X4() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-Playground-2.5": + latent_format = latent_formats.SDXL_Playground_2_5() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-SC-Prior": + latent_format = latent_formats.SC_Prior() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-SC-B": + latent_format = latent_formats.SC_B() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-FLUX.1": + latent_format = latent_formats.Flux() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-LTXV": + latent_format = latent_formats.LTXV() + method = LatentPreviewMethod.Latent2RGB + else: + logging.warning(f"[Impact Pack] PreviewBridgeLatent: '{preview_method}' is unsupported preview method.") + latent_format = latent_formats.SD15() + method = LatentPreviewMethod.Latent2RGB + + previewer = core.get_previewer("cpu", latent_format=latent_format, force=True, method=method) + samples = latent_format.process_in(latent['samples']) + + pil_image = previewer.decode_latent_to_preview(samples) + pixels_size = pil_image.size[0]*8, pil_image.size[1]*8 + resized_image = pil_image.resize(pixels_size, resample=utils.LANCZOS) + + return utils.to_tensor(resized_image).unsqueeze(0) + + +class PreviewBridgeLatent: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "latent": ("LATENT",), + "image": ("STRING", {"default": ""}), + "preview_method": (["Latent2RGB-FLUX.1", + "Latent2RGB-SDXL", "Latent2RGB-SD15", "Latent2RGB-SD3", + "Latent2RGB-SD-X4", "Latent2RGB-Playground-2.5", + "Latent2RGB-SC-Prior", "Latent2RGB-SC-B", + "Latent2RGB-LTXV", + "TAEF1", "TAESDXL", "TAESD15", "TAESD3"],), + }, + "optional": { + "vae_opt": ("VAE", ), + "block": ("BOOLEAN", {"default": False, "label_on": "if_empty_mask", "label_off": "never", "tooltip": "is_empty_mask: If the mask is empty, the execution is stopped.\nnever: The execution is never stopped. Instead, it returns a white mask."}), + "restore_mask": (["never", "always", "if_same_size"], {"tooltip": "if_same_size: If the changed input latent is the same size as the previous latent, restore using the last saved mask\nalways: Whenever the input latent changes, always restore using the last saved mask\nnever: Do not restore the mask.\n`restore_mask` has higher priority than `block`\nIf the input latent already has a mask, do not restore mask."}), + }, + "hidden": {"unique_id": "UNIQUE_ID", "prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"}, + } + + RETURN_TYPES = ("LATENT", "MASK", ) + + FUNCTION = "doit" + + OUTPUT_NODE = True + + CATEGORY = "ImpactPack/Util" + + DESCRIPTION = "This is a feature that allows you to edit and send a Mask over a latent image.\nIf the block is set to 'is_empty_mask', the execution is stopped when the mask is empty." + + def __init__(self): + super().__init__() + self.output_dir = folder_paths.get_temp_directory() + self.type = "temp" + self.prev_hash = None + self.prefix_append = "_temp_" + ''.join(random.choice("abcdefghijklmnopqrstupvxyz") for x in range(5)) + + @staticmethod + def load_image(pb_id): + is_fail = False + if pb_id not in core.preview_bridge_image_id_map: + is_fail = True + + if not is_fail: + image_path, ui_item = core.preview_bridge_image_id_map[pb_id] + if not os.path.isfile(image_path): + is_fail = True + + if not is_fail: + i = Image.open(image_path) + i = ImageOps.exif_transpose(i) + image = i.convert("RGB") + image = np.array(image).astype(np.float32) / 255.0 + image = torch.from_numpy(image)[None,] + + if 'A' in i.getbands(): + mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0 + mask = 1. - torch.from_numpy(mask) + else: + mask = None + else: + image = utils.empty_pil_tensor() + mask = None + ui_item = { + "filename": 'empty.png', + "subfolder": '', + "type": 'temp' + } + + return image, mask, ui_item + + def doit(self, latent, image, preview_method, vae_opt=None, block=False, unique_id=None, restore_mask='never', prompt=None, extra_pnginfo=None): + latent_channels = latent['samples'].shape[1] + + if 'SD3' in preview_method or 'SC-Prior' in preview_method or 'FLUX.1' in preview_method or 'TAEF1' == preview_method: + preview_method_channels = 16 + elif 'LTXV' in preview_method: + preview_method_channels = 128 + else: + preview_method_channels = 4 + + if vae_opt is None and latent_channels != preview_method_channels: + logging.warning("[PreviewBridgeLatent] The version of latent is not compatible with preview_method.\nSD3, SD1/SD2, SDXL, SC-Prior, SC-B and FLUX.1 are not compatible with each other.") + raise Exception("The version of latent is not compatible with preview_method.
SD3, SD1/SD2, SDXL, SC-Prior, SC-B and FLUX.1 are not compatible with each other.") + + need_refresh = False + latent_changed = False + + # Check if latent has changed + if unique_id not in core.preview_bridge_cache: + need_refresh = True + latent_changed = True + elif (core.preview_bridge_cache[unique_id][0] is not latent + or (vae_opt is None and core.preview_bridge_cache[unique_id][2] is not None) + or (vae_opt is None and core.preview_bridge_cache[unique_id][1] != preview_method) + or (vae_opt is not None and core.preview_bridge_cache[unique_id][2] is not vae_opt)): + need_refresh = True + latent_changed = True + + # If latent changed, clear the mask cache to ensure fresh start behavior + # unless restore_mask is set to "always" or "if_same_size" + if latent_changed and restore_mask not in ["always", "if_same_size"] and unique_id in core.preview_bridge_last_mask_cache: + del core.preview_bridge_last_mask_cache[unique_id] + + # Handle clipspace files that aren't registered in the preview bridge system + # This only applies when latent hasn't changed (same latent, new mask scenario) + if not need_refresh and image not in core.preview_bridge_image_id_map: + is_clipspace = image and ("clipspace" in image.lower() or "[input]" in image) + if is_clipspace: + if not PreviewBridge.register_clipspace_image(image, unique_id): + need_refresh = True + else: + need_refresh = True + + if not need_refresh: + pixels, mask, path_item = PreviewBridge.load_image(image) + + if mask is None: + mask = torch.ones(latent['samples'].shape[2:], dtype=torch.float32, device="cpu").unsqueeze(0) + if 'noise_mask' in latent: + res_latent = latent.copy() + del res_latent['noise_mask'] + else: + res_latent = latent + + is_empty_mask = True + else: + res_latent = latent.copy() + res_latent['noise_mask'] = mask + + is_empty_mask = torch.all(mask == 1) + + res_image = [path_item] + else: + decoded_image = decode_latent(latent, preview_method, vae_opt) + + if 'noise_mask' in latent: + mask = latent['noise_mask'].squeeze(0) # 4D mask -> 3D mask + + decoded_pil = utils.to_pil(decoded_image) + + inverted_mask = 1 - mask # invert + resized_mask = utils.resize_mask(inverted_mask, (decoded_image.shape[1], decoded_image.shape[2])) + result_pil = utils.apply_mask_alpha_to_pil(decoded_pil, resized_mask) + + full_output_folder, filename, counter, _, _ = folder_paths.get_save_image_path("PreviewBridge/PBL-"+self.prefix_append, folder_paths.get_temp_directory(), result_pil.size[0], result_pil.size[1]) + file = f"{filename}_{counter}.png" + result_pil.save(os.path.join(full_output_folder, file), compress_level=4) + res_image = [{ + 'filename': file, + 'subfolder': 'PreviewBridge', + 'type': 'temp', + }] + + is_empty_mask = False + else: + # For new latents (latent_changed=True), start fresh regardless of restore_mask + # For same latent with refresh needed, respect the restore_mask setting + # Exception: when restore_mask is "always", restore even with new latents + # Exception: when restore_mask is "if_same_size", allow restoration to check size compatibility + if restore_mask != "never" and (not latent_changed or restore_mask in ["always", "if_same_size"]): + mask = core.preview_bridge_last_mask_cache.get(unique_id) + if mask is None: + mask = None + elif restore_mask == "if_same_size" and mask.shape[1:] != decoded_image.shape[1:3]: + # For if_same_size, clear mask if dimensions don't match + mask = None + # For "always", keep the mask regardless of size + else: + mask = None + + if mask is None: + mask = torch.ones(latent['samples'].shape[2:], dtype=torch.float32, device="cpu").unsqueeze(0) + res = nodes.PreviewImage().save_images(decoded_image, filename_prefix="PreviewBridge/PBL-", prompt=prompt, extra_pnginfo=extra_pnginfo) + else: + masked_images = utils.tensor_convert_rgba(decoded_image) + resized_mask = utils.resize_mask(mask, (decoded_image.shape[1], decoded_image.shape[2])).unsqueeze(3) + resized_mask = 1 - resized_mask + utils.tensor_putalpha(masked_images, resized_mask) + res = nodes.PreviewImage().save_images(masked_images, filename_prefix="PreviewBridge/PBL-", prompt=prompt, extra_pnginfo=extra_pnginfo) + + res_image = res['ui']['images'] + + is_empty_mask = torch.all(mask == 1) + + path = os.path.join(folder_paths.get_temp_directory(), 'PreviewBridge', res_image[0]['filename']) + core.set_previewbridge_image(unique_id, path, res_image[0]) + core.preview_bridge_image_id_map[image] = (path, res_image[0]) + core.preview_bridge_image_name_map[unique_id, path] = (image, res_image[0]) + core.preview_bridge_cache[unique_id] = (latent, preview_method, vae_opt, res_image) + + res_latent = latent + + if block and is_empty_mask and core.is_execution_model_version_supported(): + from comfy_execution.graph import ExecutionBlocker + result = ExecutionBlocker(None), ExecutionBlocker(None) + elif block and is_empty_mask: + logging.warning("[Impact Pack] PreviewBridgeLatent: ComfyUI is outdated - blocking feature is disabled.") + result = res_latent, mask + else: + result = res_latent, mask + + if not is_empty_mask: + core.preview_bridge_last_mask_cache[unique_id] = mask + + return { + "ui": {"images": res_image}, + "result": result, + } \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/config.py b/custom_nodes/comfyui-impact-pack/modules/impact/config.py new file mode 100644 index 0000000000000000000000000000000000000000..1f319019a3397a6ec04a807af73e964011481e56 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/config.py @@ -0,0 +1,62 @@ +import configparser +import os +import logging + + +version_code = [8, 22, 2] +version = f"V{version_code[0]}.{version_code[1]}" + (f'.{version_code[2]}' if len(version_code) > 2 else '') + +my_path = os.path.dirname(__file__) +old_config_path = os.path.join(my_path, "impact-pack.ini") +config_path = os.path.join(my_path, "..", "..", "impact-pack.ini") +latent_letter_path = os.path.join(my_path, "..", "..", "latent.png") + + +def write_config(): + config = configparser.ConfigParser() + config['default'] = { + 'sam_editor_cpu': str(get_config()['sam_editor_cpu']), + 'sam_editor_model': get_config()['sam_editor_model'], + 'custom_wildcards': get_config()['custom_wildcards'], + 'disable_gpu_opencv': get_config()['disable_gpu_opencv'], + } + with open(config_path, 'w') as configfile: + config.write(configfile) + + +def read_config(): + try: + config = configparser.ConfigParser() + config.read(config_path) + default_conf = config['default'] + + if not os.path.exists(default_conf['custom_wildcards']): + logging.warning(f"[Impact Pack] custom_wildcards path not found: {default_conf['custom_wildcards']}. Using default path.") + default_conf['custom_wildcards'] = os.path.join(my_path, "..", "..", "custom_wildcards") + + return { + 'sam_editor_cpu': default_conf['sam_editor_cpu'].lower() == 'true' if 'sam_editor_cpu' in default_conf else False, + 'sam_editor_model': default_conf['sam_editor_model'].lower() if 'sam_editor_model' else 'sam_vit_b_01ec64.pth', + 'custom_wildcards': default_conf['custom_wildcards'] if 'custom_wildcards' in default_conf else os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "custom_wildcards")), + 'disable_gpu_opencv': default_conf['disable_gpu_opencv'].lower() == 'true' if 'disable_gpu_opencv' in default_conf else True + } + + except Exception: + return { + 'sam_editor_cpu': False, + 'sam_editor_model': 'sam_vit_b_01ec64.pth', + 'custom_wildcards': os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "custom_wildcards")), + 'disable_gpu_opencv': True + } + + +cached_config = None + + +def get_config(): + global cached_config + + if cached_config is None: + cached_config = read_config() + + return cached_config diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/core.py b/custom_nodes/comfyui-impact-pack/modules/impact/core.py new file mode 100644 index 0000000000000000000000000000000000000000..f14c322d4ced06aa5104dddc658d9978132dfcf8 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/core.py @@ -0,0 +1,2406 @@ +import os +import warnings + +import torch +from segment_anything import SamPredictor + +from comfy_extras.nodes_custom_sampler import Noise_RandomNoise +from collections import namedtuple +import numpy as np +from PIL import ImageOps, Image + +import nodes +import comfy_extras.nodes_upscale_model as model_upscale +from server import PromptServer +import comfy +import impact.wildcards as wildcards +import math +import cv2 +import time +from comfy import model_management +from impact import utils +from impact import impact_sampling +from concurrent.futures import ThreadPoolExecutor +import inspect +from collections import OrderedDict +import torch.nn.functional as F +import logging +import sys +import importlib + + +is_sam2_available = importlib.util.find_spec("sam2") +sam2_unavailable_message = f"\n----------------------------------------------------------------------------\n[Impact Pack] The SAM2 functionality is unavailable because the `facebook/sam2` dependency is not installed.\n\nInstallation command:\n{sys.executable} -m pip install git+https://github.com/facebookresearch/sam2\n----------------------------------------------------------------------------\n" +if is_sam2_available: + from sam2.sam2_image_predictor import SAM2ImagePredictor + from sam2.build_sam import build_sam2, build_sam2_video_predictor +else: + logging.warning(sam2_unavailable_message) + +try: + from comfy_extras import nodes_differential_diffusion +except Exception: + logging.warning("\n#############################################\n[Impact Pack] ComfyUI is an outdated version.\n#############################################\n") + raise Exception("[Impact Pack] ComfyUI is an outdated version.") + + +SEG = namedtuple("SEG", + ['cropped_image', 'cropped_mask', 'confidence', 'crop_region', 'bbox', 'label', 'control_net_wrapper'], + defaults=[None]) + +pb_id_cnt = time.time() +preview_bridge_image_id_map = {} +preview_bridge_image_name_map = {} + +preview_bridge_cache = {} +preview_bridge_last_mask_cache = {} + +current_prompt = None + +SCHEDULERS = comfy.samplers.KSampler.SCHEDULERS + ['AYS SDXL', 'AYS SD1', 'AYS SVD', 'GITS[coeff=1.2]', 'LTXV[default]', 'OSS FLUX', 'OSS Wan', 'OSS Chroma'] + + +def is_execution_model_version_supported(): + try: + import comfy_execution # noqa: F401 + return True + except Exception: + return False + + +def set_previewbridge_image(node_id, file, item): + global pb_id_cnt + + if file in preview_bridge_image_name_map: + pb_id = preview_bridge_image_name_map[node_id, file] + if pb_id.startswith(f"${node_id}"): + return pb_id + + pb_id = f"${node_id}-{pb_id_cnt}" + preview_bridge_image_id_map[pb_id] = (file, item) + preview_bridge_image_name_map[node_id, file] = (pb_id, item) + if os.path.isfile(file): + i = Image.open(file) + i = ImageOps.exif_transpose(i) + if 'A' in i.getbands(): + mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0 + mask = 1. - torch.from_numpy(mask) + preview_bridge_last_mask_cache[node_id] = mask.unsqueeze(0) + pb_id_cnt += 1 + + return pb_id + + +def erosion_mask(mask, grow_mask_by): + mask = utils.make_2d_mask(mask) + + w = mask.shape[1] + h = mask.shape[0] + + device = comfy.model_management.get_torch_device() + mask = mask.clone().to(device) + mask2 = torch.nn.functional.interpolate(mask.reshape((-1, 1, mask.shape[-2], mask.shape[-1])), size=(w, h), mode="bilinear").to(device) + if grow_mask_by == 0: + mask_erosion = mask2 + else: + kernel_tensor = torch.ones((1, 1, grow_mask_by, grow_mask_by)).to(device) + padding = math.ceil((grow_mask_by - 1) / 2) + + mask_erosion = torch.clamp(torch.nn.functional.conv2d(mask2.round(), kernel_tensor, padding=padding), 0, 1) + + return mask_erosion[:, :, :w, :h].round().cpu() + + +# CREDIT: https://github.com/BlenderNeko/ComfyUI_Noise/blob/afb14757216257b12268c91845eac248727a55e2/nodes.py#L68 +# https://discuss.pytorch.org/t/help-regarding-slerp-function-for-generative-model-sampling/32475/3 +def slerp(val, low, high): + dims = low.shape + + low = low.reshape(dims[0], -1) + high = high.reshape(dims[0], -1) + + low_norm = low/torch.norm(low, dim=1, keepdim=True) + high_norm = high/torch.norm(high, dim=1, keepdim=True) + + low_norm[low_norm != low_norm] = 0.0 + high_norm[high_norm != high_norm] = 0.0 + + omega = torch.acos((low_norm*high_norm).sum(1)) + so = torch.sin(omega) + res = (torch.sin((1.0-val)*omega)/so).unsqueeze(1)*low + (torch.sin(val*omega)/so).unsqueeze(1) * high + + return res.reshape(dims) + + +def mix_noise(from_noise, to_noise, strength, variation_method): + if variation_method == 'slerp': + mixed_noise = slerp(strength, from_noise, to_noise) + else: + # linear + mixed_noise = (1 - strength) * from_noise + strength * to_noise + + # NOTE: Since the variance of the Gaussian noise in mixed_noise has changed, it must be corrected through scaling. + scale_factor = math.sqrt((1 - strength) ** 2 + strength ** 2) + mixed_noise /= scale_factor + + return mixed_noise + + +class REGIONAL_PROMPT: + def __init__(self, mask, sampler, variation_seed=0, variation_strength=0.0, variation_method='linear'): + mask = utils.make_2d_mask(mask) + + self.mask = mask + self.sampler = sampler + self.mask_erosion = None + self.erosion_factor = None + self.variation_seed = variation_seed + self.variation_strength = variation_strength + self.variation_method = variation_method + + def clone_with_sampler(self, sampler): + rp = REGIONAL_PROMPT(self.mask, sampler) + rp.mask_erosion = self.mask_erosion + rp.erosion_factor = self.erosion_factor + rp.variation_seed = self.variation_seed + rp.variation_strength = self.variation_strength + rp.variation_method = self.variation_method + return rp + + def get_mask_erosion(self, factor): + if self.mask_erosion is None or self.erosion_factor != factor: + self.mask_erosion = erosion_mask(self.mask, factor) + self.erosion_factor = factor + + return self.mask_erosion + + def touch_noise(self, noise): + if self.variation_strength > 0.0: + mask = utils.make_3d_mask(self.mask) + mask = utils.resize_mask(mask, (noise.shape[2], noise.shape[3])).unsqueeze(0) + + regional_noise = Noise_RandomNoise(self.variation_seed).generate_noise({'samples': noise}) + mixed_noise = mix_noise(noise, regional_noise, self.variation_strength, variation_method=self.variation_method) + + return (mask == 1).float() * mixed_noise + (mask == 0).float() * noise + + return noise + + +class NO_BBOX_DETECTOR: + pass + + +class NO_SEGM_DETECTOR: + pass + + +def create_segmasks(results): + bboxs = results[1] + segms = results[2] + confidence = results[3] + + results = [] + for i in range(len(segms)): + item = (bboxs[i], segms[i].astype(np.float32), confidence[i]) + results.append(item) + return results + + +def gen_detection_hints_from_mask_area(x, y, mask, threshold, use_negative): + mask = utils.make_2d_mask(mask) + + points = [] + plabs = [] + + # minimum sampling step >= 3 + y_step = max(3, int(mask.shape[0] / 20)) + x_step = max(3, int(mask.shape[1] / 20)) + + for i in range(0, len(mask), y_step): + for j in range(0, len(mask[i]), x_step): + if mask[i][j] > threshold: + points.append((x + j, y + i)) + plabs.append(1) + elif use_negative and mask[i][j] == 0: + points.append((x + j, y + i)) + plabs.append(0) + + return points, plabs + + +def gen_negative_hints(w, h, x1, y1, x2, y2): + npoints = [] + nplabs = [] + + # minimum sampling step >= 3 + y_step = max(3, int(w / 20)) + x_step = max(3, int(h / 20)) + + for i in range(10, h - 10, y_step): + for j in range(10, w - 10, x_step): + if not (x1 - 10 <= j and j <= x2 + 10 and y1 - 10 <= i and i <= y2 + 10): + npoints.append((j, i)) + nplabs.append(0) + + return npoints, nplabs + + +def enhance_detail(image, model, clip, vae, guide_size, guide_size_for_bbox, max_size, bbox, seed, steps, cfg, + sampler_name, + scheduler, positive, negative, denoise, noise_mask, force_inpaint, + wildcard_opt=None, wildcard_opt_concat_mode=None, + detailer_hook=None, + refiner_ratio=None, refiner_model=None, refiner_clip=None, refiner_positive=None, + refiner_negative=None, control_net_wrapper=None, cycle=1, + inpaint_model=False, noise_mask_feather=0, scheduler_func=None, + vae_tiled_encode=False, vae_tiled_decode=False): + + if noise_mask is not None: + noise_mask = utils.tensor_gaussian_blur_mask(noise_mask, noise_mask_feather) + noise_mask = noise_mask.squeeze(3) + + if noise_mask_feather > 0 and 'denoise_mask_function' not in model.model_options: + model = nodes_differential_diffusion.DifferentialDiffusion().apply(model)[0] + + if wildcard_opt is not None and wildcard_opt != "": + model, _, wildcard_positive = wildcards.process_with_loras(wildcard_opt, model, clip) + + if wildcard_opt_concat_mode == "concat": + positive = nodes.ConditioningConcat().concat(positive, wildcard_positive)[0] + else: + positive = wildcard_positive + positive = [positive[0].copy()] + if 'pooled_output' in wildcard_positive[0][1]: + positive[0][1]['pooled_output'] = wildcard_positive[0][1]['pooled_output'] + elif 'pooled_output' in positive[0][1]: + del positive[0][1]['pooled_output'] + + h = image.shape[1] + w = image.shape[2] + + bbox_h = bbox[3] - bbox[1] + bbox_w = bbox[2] - bbox[0] + + # Skip processing if the detected bbox is already larger than the guide_size + if not force_inpaint and bbox_h >= guide_size and bbox_w >= guide_size: + logging.info("Detailer: segment skip (enough big)") + return None, None + + if guide_size_for_bbox: # == "bbox" + # Scale up based on the smaller dimension between width and height. + upscale = guide_size / min(bbox_w, bbox_h) + else: + # for cropped_size + upscale = guide_size / min(w, h) + + new_w = int(w * upscale) + new_h = int(h * upscale) + + # safeguard + if 'aitemplate_keep_loaded' in model.model_options: + max_size = min(4096, max_size) + + if new_w > max_size or new_h > max_size: + upscale *= max_size / max(new_w, new_h) + new_w = int(w * upscale) + new_h = int(h * upscale) + + if not force_inpaint: + if upscale <= 1.0: + logging.info(f"Detailer: segment skip [determined upscale factor={upscale}]") + return None, None + + if new_w == 0 or new_h == 0: + logging.info(f"Detailer: segment skip [zero size={new_w, new_h}]") + return None, None + else: + if upscale <= 1.0 or new_w == 0 or new_h == 0: + logging.info("Detailer: force inpaint") + upscale = 1.0 + new_w = w + new_h = h + + if detailer_hook is not None: + new_w, new_h = detailer_hook.touch_scaled_size(new_w, new_h) + + logging.info(f"Detailer: segment upscale for ({bbox_w, bbox_h}) | crop region {w, h} x {upscale} -> {new_w, new_h}") + + # upscale + upscaled_image = utils.tensor_resize(image, new_w, new_h) + + if detailer_hook is not None: + upscaled_image = detailer_hook.post_upscale(upscaled_image, noise_mask) + + cnet_pils = None + if control_net_wrapper is not None: + positive, negative, cnet_pils = control_net_wrapper.apply(positive, negative, upscaled_image, noise_mask) + model, cnet_pils2 = control_net_wrapper.doit_ipadapter(model) + cnet_pils.extend(cnet_pils2) + + # prepare mask + if detailer_hook is None or not detailer_hook.get_skip_sampling(): + if noise_mask is not None and inpaint_model: + imc_encode = nodes.InpaintModelConditioning().encode + if 'noise_mask' in inspect.signature(imc_encode).parameters: + positive, negative, latent_image = imc_encode(positive, negative, upscaled_image, vae, mask=noise_mask, noise_mask=True) + else: + logging.warning("[Impact Pack] ComfyUI is an outdated version.") + positive, negative, latent_image = imc_encode(positive, negative, upscaled_image, vae, noise_mask) + else: + latent_image = utils.to_latent_image(upscaled_image, vae, vae_tiled_encode=vae_tiled_encode) + if noise_mask is not None: + latent_image['noise_mask'] = noise_mask + + if detailer_hook is not None: + latent_image = detailer_hook.post_encode(latent_image) + + refined_latent = latent_image + + sampler_opt=None + if detailer_hook is not None: + sampler_opt = detailer_hook.get_custom_sampler() + + # ksampler + for i in range(0, cycle): + if detailer_hook is not None: + if detailer_hook is not None: + detailer_hook.set_steps((i, cycle)) + + refined_latent = detailer_hook.cycle_latent(refined_latent) + + model2, seed2, steps2, cfg2, sampler_name2, scheduler2, positive2, negative2, upscaled_latent2, denoise2 = \ + detailer_hook.pre_ksample(model, seed+i, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise) + noise, is_touched = detailer_hook.get_custom_noise(seed+i, torch.zeros(latent_image['samples'].size()), is_touched=False) + if not is_touched: + noise = None + else: + model2, seed2, steps2, cfg2, sampler_name2, scheduler2, positive2, negative2, _, denoise2 = \ + model, seed + i, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise + noise = None + + refined_latent = impact_sampling.ksampler_wrapper(model2, seed2, steps2, cfg2, sampler_name2, scheduler2, positive2, negative2, + refined_latent, denoise2, refiner_ratio, refiner_model, refiner_clip, refiner_positive, refiner_negative, + noise=noise, scheduler_func=scheduler_func, sampler_opt=sampler_opt) + + if detailer_hook is not None: + refined_latent = detailer_hook.pre_decode(refined_latent) + + # non-latent downscale - latent downscale cause bad quality + start = time.time() + if vae_tiled_decode: + (refined_image,) = nodes.VAEDecodeTiled().decode(vae, refined_latent, 512) # using default settings + logging.info(f"[Impact Pack] vae decoded (tiled) in {time.time() - start:.1f}s") + else: + try: + refined_image = vae.decode(refined_latent['samples']) + except Exception: + # usually an out-of-memory exception from the decode, so try a tiled approach + logging.warning(f"[Impact Pack] failed after {time.time() - start:.1f}s, doing vae.decode_tiled 64...") + refined_image = vae.decode_tiled(refined_latent["samples"], tile_x=64, tile_y=64, ) + logging.info(f"[Impact Pack] vae decoded in {time.time() - start:.1f}s") + else: + # skipped + refined_image = upscaled_image + + if detailer_hook is not None: + refined_image = detailer_hook.post_decode(refined_image) + + # downscale + + # workaround: support WAN as an i2i model + if len(refined_image.shape) == 5: + refined_image = refined_image.squeeze(0) + + refined_image = utils.tensor_resize(refined_image, w, h) + + # prevent mixing of device + refined_image = refined_image.cpu() + + # don't convert to latent - latent break image + # preserving pil is much better + return refined_image, cnet_pils + + +def enhance_detail_for_animatediff(image_frames, model, clip, vae, guide_size, guide_size_for_bbox, max_size, bbox, seed, steps, cfg, + sampler_name, + scheduler, positive, negative, denoise, noise_mask, + wildcard_opt=None, wildcard_opt_concat_mode=None, + detailer_hook=None, + refiner_ratio=None, refiner_model=None, refiner_clip=None, refiner_positive=None, + refiner_negative=None, control_net_wrapper=None, noise_mask_feather=0, scheduler_func=None): + if noise_mask is not None: + noise_mask = utils.tensor_gaussian_blur_mask(noise_mask, noise_mask_feather) + noise_mask = noise_mask.squeeze(3) + + if noise_mask_feather > 0 and 'denoise_mask_function' not in model.model_options: + model = nodes_differential_diffusion.DifferentialDiffusion().apply(model)[0] + + if wildcard_opt is not None and wildcard_opt != "": + model, _, wildcard_positive = wildcards.process_with_loras(wildcard_opt, model, clip) + + if wildcard_opt_concat_mode == "concat": + positive = nodes.ConditioningConcat().concat(positive, wildcard_positive)[0] + else: + positive = wildcard_positive + + h = image_frames.shape[1] + w = image_frames.shape[2] + + bbox_h = bbox[3] - bbox[1] + bbox_w = bbox[2] - bbox[0] + + # Skip processing if the detected bbox is already larger than the guide_size + if guide_size_for_bbox: # == "bbox" + # Scale up based on the smaller dimension between width and height. + upscale = guide_size / min(bbox_w, bbox_h) + else: + # for cropped_size + upscale = guide_size / min(w, h) + + new_w = int(w * upscale) + new_h = int(h * upscale) + + # safeguard + if 'aitemplate_keep_loaded' in model.model_options: + max_size = min(4096, max_size) + + if new_w > max_size or new_h > max_size: + upscale *= max_size / max(new_w, new_h) + new_w = int(w * upscale) + new_h = int(h * upscale) + + if upscale <= 1.0 or new_w == 0 or new_h == 0: + logging.info("Detailer: force inpaint") + upscale = 1.0 + new_w = w + new_h = h + + if detailer_hook is not None: + new_w, new_h = detailer_hook.touch_scaled_size(new_w, new_h) + + logging.info(f"Detailer: segment upscale for ({bbox_w, bbox_h}) | crop region {w, h} x {upscale} -> {new_w, new_h}") + + # upscale the mask tensor by a factor of 2 using bilinear interpolation + if isinstance(noise_mask, np.ndarray): + noise_mask = torch.from_numpy(noise_mask) + + if len(noise_mask.shape) == 2: + noise_mask = noise_mask.unsqueeze(0) + else: # == 3 + noise_mask = noise_mask + + upscaled_mask = None + + for single_mask in noise_mask: + single_mask = single_mask.unsqueeze(0).unsqueeze(0) + upscaled_single_mask = torch.nn.functional.interpolate(single_mask, size=(new_h, new_w), mode='bilinear', align_corners=False) + upscaled_single_mask = upscaled_single_mask.squeeze(0) + + if upscaled_mask is None: + upscaled_mask = upscaled_single_mask + else: + upscaled_mask = torch.cat((upscaled_mask, upscaled_single_mask), dim=0) + + latent_frames = None + for image in image_frames: + image = torch.from_numpy(image).unsqueeze(0) + + # upscale + upscaled_image = utils.tensor_resize(image, new_w, new_h) + + # ksampler + samples = utils.to_latent_image(upscaled_image, vae)['samples'] + + if latent_frames is None: + latent_frames = samples + else: + latent_frames = torch.concat((latent_frames, samples), dim=0) + + cnet_images = None + if control_net_wrapper is not None: + positive, negative, cnet_images = control_net_wrapper.apply(positive, negative, torch.from_numpy(image_frames), noise_mask, use_acn=True) + + if len(upscaled_mask) != len(image_frames) and len(upscaled_mask) > 1: + logging.warning(f"[Impact Pack] DetailerForAnimateDiff: The number of the mask frames({len(upscaled_mask)}) and the image frames({len(image_frames)}) are different. Combine the mask frames and apply.") + combined_mask = upscaled_mask[0].to(torch.uint8) + + for frame_mask in upscaled_mask[1:]: + combined_mask |= (frame_mask * 255).to(torch.uint8) + + combined_mask = (combined_mask/255.0).to(torch.float32) + + upscaled_mask = combined_mask.expand(len(image_frames), -1, -1) + upscaled_mask = utils.to_binary_mask(upscaled_mask, 0.1) + + latent = { + 'noise_mask': upscaled_mask, + 'samples': latent_frames + } + + + sampler_opt=None + if detailer_hook is not None: + sampler_opt = detailer_hook.get_custom_sampler() + + if detailer_hook is not None: + latent = detailer_hook.post_encode(latent) + + refined_latent = impact_sampling.ksampler_wrapper(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, + latent, denoise, refiner_ratio, refiner_model, refiner_clip, refiner_positive, refiner_negative, scheduler_func=scheduler_func, sampler_opt=sampler_opt) + + if detailer_hook is not None: + refined_latent = detailer_hook.pre_decode(refined_latent) + + refined_image_frames = None + for refined_sample in refined_latent['samples']: + refined_sample = refined_sample.unsqueeze(0) + + # non-latent downscale - latent downscale cause bad quality + refined_image = vae.decode(refined_sample) + + if refined_image_frames is None: + refined_image_frames = refined_image + else: + refined_image_frames = torch.concat((refined_image_frames, refined_image), dim=0) + + if detailer_hook is not None: + refined_image_frames = detailer_hook.post_decode(refined_image_frames) + + refined_image_frames = nodes.ImageScale().upscale(image=refined_image_frames, upscale_method='lanczos', width=w, height=h, crop='disabled')[0] + + return refined_image_frames, cnet_images + + +def composite_to(dest_latent, crop_region, src_latent): + x1 = crop_region[0] + y1 = crop_region[1] + + # composite to original latent + lc = nodes.LatentComposite() + orig_image = lc.composite(dest_latent, src_latent, x1, y1) + + return orig_image[0] + + +def sam_predict(predictor, points, plabs, bbox, threshold): + point_coords = None if not points else np.array(points) + point_labels = None if not plabs else np.array(plabs) + + box = np.array([bbox]) if bbox is not None else None + + cur_masks, scores, _ = predictor.predict(point_coords=point_coords, point_labels=point_labels, box=box) + + total_masks = [] + + selected = False + max_score = 0 + max_mask = None + for idx in range(len(scores)): + if scores[idx] > max_score: + max_score = scores[idx] + max_mask = cur_masks[idx] + + if scores[idx] >= threshold: + selected = True + total_masks.append(cur_masks[idx]) + else: + pass + + if not selected and max_mask is not None: + total_masks.append(max_mask) + + return total_masks + + +class SAMWrapper: + def __init__(self, model, is_auto_mode, safe_to_gpu=None): + self.model = model + self.safe_to_gpu = safe_to_gpu if safe_to_gpu is not None else SafeToGPU_stub() + self.is_auto_mode = is_auto_mode + + def prepare_device(self): + if self.is_auto_mode: + device = comfy.model_management.get_torch_device() + self.safe_to_gpu.to_device(self.model, device=device) + + def release_device(self): + if self.is_auto_mode: + self.model.to(device="cpu") + + def predict(self, image, points, plabs, bbox, threshold): + predictor = SamPredictor(self.model) + predictor.set_image(image, "RGB") + + return sam_predict(predictor, points, plabs, bbox, threshold) + + +class SAM2Wrapper: + def __init__(self, config, modelname, is_auto_mode, safe_to_gpu=None, device_mode="AUTO"): + self.config = config + self.modelname = modelname + self.image_predictor = None + self.video_predictor = None + self.device_mode = device_mode + self.safe_to_gpu = safe_to_gpu if safe_to_gpu is not None else SafeToGPU_stub() + self.is_auto_mode = is_auto_mode + + def prepare_device(self): + pass + + def prepare_image_device(self): + if self.is_auto_mode: + device = comfy.model_management.get_torch_device() + self.safe_to_gpu.to_device(self.image_predictor.model, device=device) + + def prepare_video_device(self): + if self.is_auto_mode: + device = comfy.model_management.get_torch_device() + self.safe_to_gpu.to_device(self.video_predictor, device=device) + + def release_device(self): + if self.is_auto_mode: + if self.image_predictor: + self.image_predictor.model.to(device="cpu") + if self.video_predictor: + self.video_predictor.to(device="cpu") + + def predict(self, image, points, plabs, bbox, threshold): + if not is_sam2_available: + raise Exception(sam2_unavailable_message) + + if self.image_predictor is None: + self.image_predictor = SAM2ImagePredictor(build_sam2(self.config, self.modelname)) + + self.prepare_image_device() + + self.image_predictor.set_image(image) + + return sam_predict(self.image_predictor, points, plabs, bbox, threshold) + + def predict_video_segs(self, image_frames, segs): + if not is_sam2_available: + raise Exception(sam2_unavailable_message) + + if self.video_predictor is None: + self.video_predictor = build_sam2_video_predictor(self.config, self.modelname) + + self.prepare_video_device() + + orig_video_height = image_frames.shape[1] + orig_video_width = image_frames.shape[2] + + image_frames, padding = utils.resize_with_padding(image_frames, self.video_predictor.image_size, self.video_predictor.image_size) + image_frames = image_frames.permute(0, 3, 1, 2) + + inference_state = {} + inference_state["images"] = image_frames + inference_state["num_frames"] = len(image_frames) + inference_state["video_height"] = self.video_predictor.image_size + inference_state["video_width"] = self.video_predictor.image_size + inference_state["offload_video_to_cpu"] = True + inference_state["offload_state_to_cpu"] = self.device_mode == "CPU" + inference_state["device"] = self.video_predictor.device + + if inference_state["offload_state_to_cpu"]: + inference_state["storage_device"] = torch.device("cpu") + else: + inference_state["storage_device"] = self.video_predictor.device + + inference_state["point_inputs_per_obj"] = {} + inference_state["mask_inputs_per_obj"] = {} + inference_state["cached_features"] = {} + inference_state["constants"] = {} + + inference_state["obj_id_to_idx"] = OrderedDict() + inference_state["obj_idx_to_id"] = OrderedDict() + inference_state["obj_ids"] = [] + + inference_state["output_dict_per_obj"] = {} + inference_state["temp_output_dict_per_obj"] = {} + inference_state["frames_tracked_per_obj"] = {} + self.video_predictor._get_image_feature(inference_state, frame_idx=0, batch_size=1) + + temp_masks = {} + for i in range(0, len(segs[1])): + bbox = segs[1][i].bbox + + adjusted_bbox = utils.adjust_bbox_after_resize( + bbox, + (orig_video_height, orig_video_width), + (self.video_predictor.image_size, self.video_predictor.image_size), + padding + ) + + points = [utils.center_of_bbox(adjusted_bbox)] + plabs = [1] + self.video_predictor.add_new_points_or_box(inference_state=inference_state, frame_idx=0, obj_id=i, points=points, labels=plabs, box=adjusted_bbox) + temp_masks[i] = [] + + for frame_idx, object_ids, masks in self.video_predictor.propagate_in_video(inference_state): + for i in object_ids: + m = masks[i] + m = m.permute(1, 2, 0) + temp_masks[i].append(m) + + result = {} + for k, v in temp_masks.items(): + m = torch.stack(v, dim=0) + m = utils.remove_padding(m, padding) + result[k] = utils.resize_with_padding(m, orig_video_width, orig_video_height)[0] + + return result + +class ESAMWrapper: + def __init__(self, model, device): + self.model = model + self.func_inference = nodes.NODE_CLASS_MAPPINGS['Yoloworld_ESAM_Zho'] + self.device = device + + def prepare_device(self): + pass + + def release_device(self): + pass + + def predict(self, image, points, plabs, bbox, threshold): + if self.device == 'CPU': + self.device = 'cpu' + else: + self.device = 'cuda' + + detected_masks = self.func_inference.inference_sam_with_boxes(image=image, xyxy=[bbox], model=self.model, device=self.device) + return [detected_masks.squeeze(0)] + + +def make_sam_mask(sam, segs, image, detection_hint, dilation, + threshold, bbox_expansion, mask_hint_threshold, mask_hint_use_negative): + + if not hasattr(sam, 'sam_wrapper') and not isinstance(sam, SAM2Wrapper): + raise Exception("[Impact Pack] Invalid SAMLoader is connected. Make sure 'SAMLoader (Impact)'.\nKnown issue: The ComfyUI-YOLO node overrides the SAMLoader (Impact), making it unusable. You need to uninstall ComfyUI-YOLO.\n\n\n") + + + if isinstance(sam, SAM2Wrapper): + sam_obj = sam + else: + sam_obj = sam.sam_wrapper + + sam_obj.prepare_device() + + try: + image = np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8) + + total_masks = [] + + use_small_negative = mask_hint_use_negative == "Small" + + # seg_shape = segs[0] + segs = segs[1] + if detection_hint == "mask-points": + points = [] + plabs = [] + + for i in range(len(segs)): + bbox = segs[i].bbox + center = utils.center_of_bbox(segs[i].bbox) + points.append(center) + + # small point is background, big point is foreground + if use_small_negative and bbox[2] - bbox[0] < 10: + plabs.append(0) + else: + plabs.append(1) + + detected_masks = sam_obj.predict(image, points, plabs, None, threshold) + total_masks += detected_masks + + else: + for i in range(len(segs)): + bbox = segs[i].bbox + center = utils.center_of_bbox(bbox) + + x1 = max(bbox[0] - bbox_expansion, 0) + y1 = max(bbox[1] - bbox_expansion, 0) + x2 = min(bbox[2] + bbox_expansion, image.shape[1]) + y2 = min(bbox[3] + bbox_expansion, image.shape[0]) + + dilated_bbox = [x1, y1, x2, y2] + + points = [] + plabs = [] + if detection_hint == "center-1": + points.append(center) + plabs = [1] # 1 = foreground point, 0 = background point + + elif detection_hint == "horizontal-2": + gap = (x2 - x1) / 3 + points.append((x1 + gap, center[1])) + points.append((x1 + gap * 2, center[1])) + plabs = [1, 1] + + elif detection_hint == "vertical-2": + gap = (y2 - y1) / 3 + points.append((center[0], y1 + gap)) + points.append((center[0], y1 + gap * 2)) + plabs = [1, 1] + + elif detection_hint == "rect-4": + x_gap = (x2 - x1) / 3 + y_gap = (y2 - y1) / 3 + points.append((x1 + x_gap, center[1])) + points.append((x1 + x_gap * 2, center[1])) + points.append((center[0], y1 + y_gap)) + points.append((center[0], y1 + y_gap * 2)) + plabs = [1, 1, 1, 1] + + elif detection_hint == "diamond-4": + x_gap = (x2 - x1) / 3 + y_gap = (y2 - y1) / 3 + points.append((x1 + x_gap, y1 + y_gap)) + points.append((x1 + x_gap * 2, y1 + y_gap)) + points.append((x1 + x_gap, y1 + y_gap * 2)) + points.append((x1 + x_gap * 2, y1 + y_gap * 2)) + plabs = [1, 1, 1, 1] + + elif detection_hint == "mask-point-bbox": + center = utils.center_of_bbox(segs[i].bbox) + points.append(center) + plabs = [1] + + elif detection_hint == "mask-area": + points, plabs = gen_detection_hints_from_mask_area(segs[i].crop_region[0], segs[i].crop_region[1], + segs[i].cropped_mask, + mask_hint_threshold, use_small_negative) + + if mask_hint_use_negative == "Outter": + npoints, nplabs = gen_negative_hints(image.shape[0], image.shape[1], + segs[i].crop_region[0], segs[i].crop_region[1], + segs[i].crop_region[2], segs[i].crop_region[3]) + + points += npoints + plabs += nplabs + + detected_masks = sam_obj.predict(image, points, plabs, dilated_bbox, threshold) + total_masks += detected_masks + + # merge every collected masks + mask = utils.combine_masks2(total_masks) + + finally: + sam_obj.release_device() + + if mask is not None: + mask = mask.float() + mask = utils.dilate_mask(mask.cpu().numpy(), dilation) + mask = torch.from_numpy(mask) + else: + size = image.shape[0], image.shape[1] + mask = torch.zeros(size, dtype=torch.float32, device="cpu") # empty mask + + mask = utils.make_3d_mask(mask) + return mask + + +def generate_detection_hints(image, seg, center, detection_hint, dilated_bbox, mask_hint_threshold, use_small_negative, + mask_hint_use_negative): + [x1, y1, x2, y2] = dilated_bbox + + points = [] + plabs = [] + if detection_hint == "center-1": + points.append(center) + plabs = [1] # 1 = foreground point, 0 = background point + + elif detection_hint == "horizontal-2": + gap = (x2 - x1) / 3 + points.append((x1 + gap, center[1])) + points.append((x1 + gap * 2, center[1])) + plabs = [1, 1] + + elif detection_hint == "vertical-2": + gap = (y2 - y1) / 3 + points.append((center[0], y1 + gap)) + points.append((center[0], y1 + gap * 2)) + plabs = [1, 1] + + elif detection_hint == "rect-4": + x_gap = (x2 - x1) / 3 + y_gap = (y2 - y1) / 3 + points.append((x1 + x_gap, center[1])) + points.append((x1 + x_gap * 2, center[1])) + points.append((center[0], y1 + y_gap)) + points.append((center[0], y1 + y_gap * 2)) + plabs = [1, 1, 1, 1] + + elif detection_hint == "diamond-4": + x_gap = (x2 - x1) / 3 + y_gap = (y2 - y1) / 3 + points.append((x1 + x_gap, y1 + y_gap)) + points.append((x1 + x_gap * 2, y1 + y_gap)) + points.append((x1 + x_gap, y1 + y_gap * 2)) + points.append((x1 + x_gap * 2, y1 + y_gap * 2)) + plabs = [1, 1, 1, 1] + + elif detection_hint == "mask-point-bbox": + center = utils.center_of_bbox(seg.bbox) + points.append(center) + plabs = [1] + + elif detection_hint == "mask-area": + points, plabs = gen_detection_hints_from_mask_area(seg.crop_region[0], seg.crop_region[1], + seg.cropped_mask, + mask_hint_threshold, use_small_negative) + + if mask_hint_use_negative == "Outter": + npoints, nplabs = gen_negative_hints(image.shape[0], image.shape[1], + seg.crop_region[0], seg.crop_region[1], + seg.crop_region[2], seg.crop_region[3]) + + points += npoints + plabs += nplabs + + return points, plabs + + +def convert_and_stack_masks(masks): + if len(masks) == 0: + return None + + mask_tensors = [] + for mask in masks: + mask_array = np.array(mask, dtype=np.uint8) + mask_tensor = torch.from_numpy(mask_array) + mask_tensors.append(mask_tensor) + + stacked_masks = torch.stack(mask_tensors, dim=0) + stacked_masks = stacked_masks.unsqueeze(1) + + return stacked_masks + + +def merge_and_stack_masks(stacked_masks, group_size): + if stacked_masks is None: + return None + + num_masks = stacked_masks.size(0) + merged_masks = [] + + for i in range(0, num_masks, group_size): + subset_masks = stacked_masks[i:i + group_size] + merged_mask = torch.any(subset_masks, dim=0) + merged_masks.append(merged_mask) + + if len(merged_masks) > 0: + merged_masks = torch.stack(merged_masks, dim=0) + + return merged_masks + + +def segs_scale_match(segs, target_shape): + h = segs[0][0] + w = segs[0][1] + + th = target_shape[1] + tw = target_shape[2] + + if (h == th and w == tw) or h == 0 or w == 0: + return segs + + rh = th / h + rw = tw / w + + new_segs = [] + for seg in segs[1]: + cropped_image = seg.cropped_image + cropped_mask = seg.cropped_mask + x1, y1, x2, y2 = seg.crop_region + bx1, by1, bx2, by2 = seg.bbox + + crop_region = int(x1*rw), int(y1*rw), int(x2*rh), int(y2*rh) + bbox = int(bx1*rw), int(by1*rw), int(bx2*rh), int(by2*rh) + new_w = crop_region[2] - crop_region[0] + new_h = crop_region[3] - crop_region[1] + + if isinstance(cropped_mask, np.ndarray): + cropped_mask = torch.from_numpy(cropped_mask) + + if isinstance(cropped_mask, torch.Tensor) and len(cropped_mask.shape) == 3: + cropped_mask = torch.nn.functional.interpolate(cropped_mask.unsqueeze(0), size=(new_h, new_w), mode='bilinear', align_corners=False) + cropped_mask = cropped_mask.squeeze(0) + else: + cropped_mask = torch.nn.functional.interpolate(cropped_mask.unsqueeze(0).unsqueeze(0), size=(new_h, new_w), mode='bilinear', align_corners=False) + cropped_mask = cropped_mask.squeeze(0).squeeze(0).numpy() + + if cropped_image is not None: + cropped_image = utils.tensor_resize(cropped_image if isinstance(cropped_image, torch.Tensor) else torch.from_numpy(cropped_image), new_w, new_h) + cropped_image = cropped_image.numpy() + + new_seg = SEG(cropped_image, cropped_mask, seg.confidence, crop_region, bbox, seg.label, seg.control_net_wrapper) + new_segs.append(new_seg) + + return (th, tw), new_segs + + +# Used Python's slicing feature. stacked_masks[2::3] means starting from index 2, selecting every third tensor with a step size of 3. +# This allows for quickly obtaining the last tensor of every three tensors in stacked_masks. +def every_three_pick_last(stacked_masks): + selected_masks = stacked_masks[2::3] + return selected_masks + + +def make_sam_mask_segmented(sam, segs, image, detection_hint, dilation, + threshold, bbox_expansion, mask_hint_threshold, mask_hint_use_negative): + + if not hasattr(sam, 'sam_wrapper'): + raise Exception("[Impact Pack] Invalid SAMLoader is connected. Make sure 'SAMLoader (Impact)'.") + + sam_obj = sam.sam_wrapper + sam_obj.prepare_device() + + try: + image = np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8) + + total_masks = [] + + use_small_negative = mask_hint_use_negative == "Small" + + # seg_shape = segs[0] + segs = segs[1] + if detection_hint == "mask-points": + points = [] + plabs = [] + + for i in range(len(segs)): + bbox = segs[i].bbox + center = utils.center_of_bbox(bbox) + points.append(center) + + # small point is background, big point is foreground + if use_small_negative and bbox[2] - bbox[0] < 10: + plabs.append(0) + else: + plabs.append(1) + + detected_masks = sam_obj.predict(image, points, plabs, None, threshold) + total_masks += detected_masks + + else: + for i in range(len(segs)): + bbox = segs[i].bbox + center = utils.center_of_bbox(bbox) + x1 = max(bbox[0] - bbox_expansion, 0) + y1 = max(bbox[1] - bbox_expansion, 0) + x2 = min(bbox[2] + bbox_expansion, image.shape[1]) + y2 = min(bbox[3] + bbox_expansion, image.shape[0]) + + dilated_bbox = [x1, y1, x2, y2] + + points, plabs = generate_detection_hints(image, segs[i], center, detection_hint, dilated_bbox, + mask_hint_threshold, use_small_negative, + mask_hint_use_negative) + + detected_masks = sam_obj.predict(image, points, plabs, dilated_bbox, threshold) + + total_masks += detected_masks + + # merge every collected masks + mask = utils.combine_masks2(total_masks) + + finally: + sam_obj.release_device() + + mask_working_device = torch.device("cpu") + + if mask is not None: + mask = mask.float() + mask = utils.dilate_mask(mask.cpu().numpy(), dilation) + mask = torch.from_numpy(mask) + mask = mask.to(device=mask_working_device) + else: + # Extracting batch, height and width + height, width, _ = image.shape + mask = torch.zeros( + (height, width), dtype=torch.float32, device=mask_working_device + ) # empty mask + + stacked_masks = convert_and_stack_masks(total_masks) + + return (mask, merge_and_stack_masks(stacked_masks, group_size=3)) + # return every_three_pick_last(stacked_masks) + + +def segs_bitwise_and_mask(segs, mask): + mask = utils.make_2d_mask(mask) + + if mask is None: + logging.warning("[SegsBitwiseAndMask] Cannot operate: MASK is empty.") + return ([],) + + items = [] + + mask = (mask.cpu().numpy() * 255).astype(np.uint8) + + for seg in segs[1]: + cropped_mask = (seg.cropped_mask * 255).astype(np.uint8) + crop_region = seg.crop_region + + cropped_mask2 = mask[crop_region[1]:crop_region[3], crop_region[0]:crop_region[2]] + + new_mask = np.bitwise_and(cropped_mask.astype(np.uint8), cropped_mask2) + new_mask = new_mask.astype(np.float32) / 255.0 + + item = SEG(seg.cropped_image, new_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, None) + items.append(item) + + return segs[0], items + + +def segs_bitwise_subtract_mask(segs, mask): + mask = utils.make_2d_mask(mask) + + if mask is None: + logging.warning("[SegsBitwiseSubtractMask] Cannot operate: MASK is empty.") + return ([],) + + items = [] + + mask = (mask.cpu().numpy() * 255).astype(np.uint8) + + for seg in segs[1]: + cropped_mask = (seg.cropped_mask * 255).astype(np.uint8) + crop_region = seg.crop_region + + cropped_mask2 = mask[crop_region[1]:crop_region[3], crop_region[0]:crop_region[2]] + + new_mask = cv2.subtract(cropped_mask.astype(np.uint8), cropped_mask2) + new_mask = new_mask.astype(np.float32) / 255.0 + + item = SEG(seg.cropped_image, new_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, None) + items.append(item) + + return segs[0], items + + +def apply_mask_to_each_seg(segs, masks): + if masks is None: + logging.warning("[SegsBitwiseAndMask] Cannot operate: MASK is empty.") + return (segs[0], [],) + + items = [] + + masks = masks.squeeze(1) + + for seg, mask in zip(segs[1], masks): + cropped_mask = (seg.cropped_mask * 255).astype(np.uint8) + crop_region = seg.crop_region + + cropped_mask2 = (mask.cpu().numpy() * 255).astype(np.uint8) + cropped_mask2 = cropped_mask2[crop_region[1]:crop_region[3], crop_region[0]:crop_region[2]] + + new_mask = np.bitwise_and(cropped_mask.astype(np.uint8), cropped_mask2) + new_mask = new_mask.astype(np.float32) / 255.0 + + item = SEG(seg.cropped_image, new_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, None) + items.append(item) + + return segs[0], items + + +def dilate_segs(segs, factor): + if factor == 0: + return segs + + new_segs = [] + for seg in segs[1]: + new_mask = utils.dilate_mask(seg.cropped_mask, factor) + new_seg = SEG(seg.cropped_image, new_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper) + new_segs.append(new_seg) + + return (segs[0], new_segs) + + +class ONNXDetector: + onnx_model = None + + def __init__(self, onnx_model): + self.onnx_model = onnx_model + + def detect(self, image, threshold, dilation, crop_factor, drop_size=1, detailer_hook=None): + drop_size = max(drop_size, 1) + try: + import impact.impact_onnx as onnx + + h = image.shape[1] + w = image.shape[2] + + labels, scores, boxes = onnx.onnx_inference(image, self.onnx_model) + + # collect feasible item + result = [] + + for i in range(len(labels)): + if scores[i] > threshold: + item_bbox = boxes[i] + x1, y1, x2, y2 = item_bbox + + if x2 - x1 > drop_size and y2 - y1 > drop_size: # minimum dimension must be (2,2) to avoid squeeze issue + crop_region = utils.make_crop_region(w, h, item_bbox, crop_factor) + + if detailer_hook is not None: + crop_region = item_bbox.post_crop_region(w, h, item_bbox, crop_region) + + crop_x1, crop_y1, crop_x2, crop_y2, = crop_region + + # prepare cropped mask + cropped_mask = np.zeros((crop_y2 - crop_y1, crop_x2 - crop_x1)) + cropped_mask[y1 - crop_y1:y2 - crop_y1, x1 - crop_x1:x2 - crop_x1] = 1 + cropped_mask = utils.dilate_mask(cropped_mask, dilation) + + # make items. just convert the integer label to a string + item = SEG(None, cropped_mask, scores[i], crop_region, item_bbox, str(labels[i]), None) + result.append(item) + + shape = h, w + segs = shape, result + + if detailer_hook is not None and hasattr(detailer_hook, "post_detection"): + segs = detailer_hook.post_detection(segs) + + return segs + except Exception as e: + logging.error(f"ONNXDetector: unable to execute.\n{e}") + + def detect_combined(self, image, threshold, dilation): + return segs_to_combined_mask(self.detect(image, threshold, dilation, 1)) + + def setAux(self, x): + pass + + +def batch_mask_to_segs(mask, combined, crop_factor, bbox_fill, drop_size=1, label='A', crop_min_size=None, detailer_hook=None): + combined_mask = mask.max(dim=0).values + + segs = mask_to_segs(combined_mask, combined, crop_factor, bbox_fill, drop_size, label, crop_min_size, detailer_hook) + + new_segs = [] + for seg in segs[1]: + x1, y1, x2, y2 = seg.crop_region + cropped_mask = mask[:, y1:y2, x1:x2] + item = SEG(None, cropped_mask, 1.0, seg.crop_region, seg.bbox, label, None) + new_segs.append(item) + + return segs[0], new_segs + + +def mask_to_segs(mask, combined, crop_factor, bbox_fill, drop_size=1, label='A', crop_min_size=None, detailer_hook=None, is_contour=True): + drop_size = max(drop_size, 1) + if mask is None: + logging.info("[mask_to_segs] Cannot operate: MASK is empty.") + return ([],) + + if isinstance(mask, np.ndarray): + pass # `mask` is already a NumPy array + else: + try: + mask = mask.numpy() + except AttributeError: + logging.info("[mask_to_segs] Cannot operate: MASK is not a NumPy array or Tensor.") + return ([],) + + if mask is None: + logging.info("[mask_to_segs] Cannot operate: MASK is empty.") + return ([],) + + result = [] + + if len(mask.shape) == 2: + mask = np.expand_dims(mask, axis=0) + + for i in range(mask.shape[0]): + mask_i = mask[i] + + if combined: + indices = np.nonzero(mask_i) + if len(indices[0]) > 0 and len(indices[1]) > 0: + bbox = ( + np.min(indices[1]), + np.min(indices[0]), + np.max(indices[1]), + np.max(indices[0]), + ) + crop_region = utils.make_crop_region( + mask_i.shape[1], mask_i.shape[0], bbox, crop_factor + ) + x1, y1, x2, y2 = crop_region + + if detailer_hook is not None: + crop_region = detailer_hook.post_crop_region(mask_i.shape[1], mask_i.shape[0], bbox, crop_region) + + if x2 - x1 > 0 and y2 - y1 > 0: + cropped_mask = mask_i[y1:y2, x1:x2] + + if bbox_fill: + bx1, by1, bx2, by2 = bbox + cropped_mask = cropped_mask.copy() + cropped_mask[by1:by2, bx1:bx2] = 1.0 + + if cropped_mask is not None: + item = SEG(None, cropped_mask, 1.0, crop_region, bbox, label, None) + result.append(item) + + else: + mask_i_uint8 = (mask_i * 255.0).astype(np.uint8) + contours, ctree = cv2.findContours(mask_i_uint8, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + for j, contour in enumerate(contours): + hierarchy = ctree[0][j] + if hierarchy[3] != -1: + continue + + separated_mask = np.zeros_like(mask_i_uint8) + cv2.drawContours(separated_mask, [contour], 0, 255, -1) + separated_mask = np.array(separated_mask / 255.0).astype(np.float32) + + x, y, w, h = cv2.boundingRect(contour) + bbox = x, y, x + w, y + h + crop_region = utils.make_crop_region( + mask_i.shape[1], mask_i.shape[0], bbox, crop_factor, crop_min_size + ) + + if detailer_hook is not None: + crop_region = detailer_hook.post_crop_region(mask_i.shape[1], mask_i.shape[0], bbox, crop_region) + + if w > drop_size and h > drop_size: + if is_contour: + mask_src = separated_mask + else: + mask_src = mask_i * separated_mask + + cropped_mask = np.array( + mask_src[ + crop_region[1]: crop_region[3], + crop_region[0]: crop_region[2], + ] + ) + + if bbox_fill: + cx1, cy1, _, _ = crop_region + bx1 = x - cx1 + bx2 = x+w - cx1 + by1 = y - cy1 + by2 = y+h - cy1 + cropped_mask[by1:by2, bx1:bx2] = 1.0 + + if cropped_mask is not None: + cropped_mask = torch.clip(torch.from_numpy(cropped_mask), 0, 1.0) + item = SEG(None, cropped_mask.numpy(), 1.0, crop_region, bbox, label, None) + result.append(item) + + if not result: + logging.info("[mask_to_segs] Empty mask.") + + logging.info(f"# of Detected SEGS: {len(result)}") + # for r in result: + # print(f"\tbbox={r.bbox}, crop={r.crop_region}, label={r.label}") + + # shape: (b,h,w) -> (h,w) + return (mask.shape[1], mask.shape[2]), result + + +def mediapipe_facemesh_to_segs(image, crop_factor, bbox_fill, crop_min_size, drop_size, dilation, face, mouth, left_eyebrow, left_eye, left_pupil, right_eyebrow, right_eye, right_pupil): + parts = { + "face": np.array([0x0A, 0xC8, 0x0A]), + "mouth": np.array([0x0A, 0xB4, 0x0A]), + "left_eyebrow": np.array([0xB4, 0xDC, 0x0A]), + "left_eye": np.array([0xB4, 0xC8, 0x0A]), + "left_pupil": np.array([0xFA, 0xC8, 0x0A]), + "right_eyebrow": np.array([0x0A, 0xDC, 0xB4]), + "right_eye": np.array([0x0A, 0xC8, 0xB4]), + "right_pupil": np.array([0x0A, 0xC8, 0xFA]), + } + + def create_segments(image, color): + image = (image * 255).to(torch.uint8) + image = image.squeeze(0).numpy() + mask = cv2.inRange(image, color, color) + + contours, ctree = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + mask_list = [] + for i, contour in enumerate(contours): + hierarchy = ctree[0][i] + if hierarchy[3] == -1: + convex_hull = cv2.convexHull(contour) + convex_segment = np.zeros_like(image) + cv2.fillPoly(convex_segment, [convex_hull], (255, 255, 255)) + + convex_segment = np.expand_dims(convex_segment, axis=0).astype(np.float32) / 255.0 + tensor = torch.from_numpy(convex_segment) + mask_tensor = torch.any(tensor != 0, dim=-1).float() + mask_tensor = mask_tensor.squeeze(0) + mask_tensor = torch.from_numpy(utils.dilate_mask(mask_tensor.numpy(), dilation)) + mask_list.append(mask_tensor.unsqueeze(0)) + + return mask_list + + segs = [] + + def create_seg(label): + mask_list = create_segments(image, parts[label]) + for mask in mask_list: + seg = mask_to_segs(mask, False, crop_factor, bbox_fill, drop_size=drop_size, label=label, crop_min_size=crop_min_size) + if len(seg[1]) > 0: + segs.extend(seg[1]) + + if face: + create_seg('face') + + if mouth: + create_seg('mouth') + + if left_eyebrow: + create_seg('left_eyebrow') + + if left_eye: + create_seg('left_eye') + + if left_pupil: + create_seg('left_pupil') + + if right_eyebrow: + create_seg('right_eyebrow') + + if right_eye: + create_seg('right_eye') + + if right_pupil: + create_seg('right_pupil') + + return (image.shape[1], image.shape[2]), segs + + +def segs_to_combined_mask(segs): + shape = segs[0] + h = shape[0] + w = shape[1] + + mask = np.zeros((h, w), dtype=np.uint8) + + for seg in segs[1]: + cropped_mask = seg.cropped_mask + crop_region = seg.crop_region + mask[crop_region[1]:crop_region[3], crop_region[0]:crop_region[2]] |= (cropped_mask * 255).astype(np.uint8) + + return torch.from_numpy(mask.astype(np.float32) / 255.0) + + +def segs_to_masklist(segs): + shape = segs[0] + h = shape[0] + w = shape[1] + + masks = [] + for seg in segs[1]: + if isinstance(seg.cropped_mask, np.ndarray): + cropped_mask = torch.from_numpy(seg.cropped_mask) + else: + cropped_mask = seg.cropped_mask + + if cropped_mask.ndim == 2: + cropped_mask = cropped_mask.unsqueeze(0) + + n = len(cropped_mask) + + mask = torch.zeros((n, h, w), dtype=torch.uint8) + crop_region = seg.crop_region + mask[:, crop_region[1]:crop_region[3], crop_region[0]:crop_region[2]] |= (cropped_mask * 255).to(torch.uint8) + mask = (mask / 255.0).to(torch.float32) + + for x in mask: + masks.append(x) + + if len(masks) == 0: + empty_mask = torch.zeros((h, w), dtype=torch.float32, device="cpu") + masks = [empty_mask] + + return masks + + +def vae_decode(vae, samples, use_tile, hook, tile_size=512, overlap=64): + if use_tile: + decoder = nodes.VAEDecodeTiled() + if 'overlap' in inspect.signature(decoder.decode).parameters: + pixels = decoder.decode(vae, samples, tile_size, overlap=overlap)[0] + else: + logging.warning("[Impact Pack] Your ComfyUI is outdated.") + pixels = decoder.decode(vae, samples, tile_size)[0] + else: + pixels = nodes.VAEDecode().decode(vae, samples)[0] + + if hook is not None: + pixels = hook.post_decode(pixels) + + return pixels + + +def vae_encode(vae, pixels, use_tile, hook, tile_size=512, overlap=64): + if use_tile: + encoder = nodes.VAEEncodeTiled() + if 'overlap' in inspect.signature(encoder.encode).parameters: + samples = encoder.encode(vae, pixels, tile_size, overlap=overlap)[0] + else: + logging.warning("[Impact Pack] Your ComfyUI is outdated.") + samples = encoder.encode(vae, pixels, tile_size)[0] + else: + samples = nodes.VAEEncode().encode(vae, pixels)[0] + + if hook is not None: + samples = hook.post_encode(samples) + + return samples + + +def latent_upscale_on_pixel_space_shape(samples, scale_method, w, h, vae, use_tile=False, tile_size=512, save_temp_prefix=None, hook=None, overlap=64): + return latent_upscale_on_pixel_space_shape2(samples, scale_method, w, h, vae, use_tile, tile_size, save_temp_prefix, hook, overlap=overlap)[0] + + +def latent_upscale_on_pixel_space_shape2(samples, scale_method, w, h, vae, use_tile=False, tile_size=512, save_temp_prefix=None, hook=None, overlap=64): + pixels = vae_decode(vae, samples, use_tile, hook, tile_size=tile_size, overlap=overlap) + + if save_temp_prefix is not None: + nodes.PreviewImage().save_images(pixels, filename_prefix=save_temp_prefix) + + pixels = nodes.ImageScale().upscale(pixels, scale_method, int(w), int(h), False)[0] + + old_pixels = pixels + if hook is not None: + pixels = hook.post_upscale(pixels) + + return vae_encode(vae, pixels, use_tile, hook, tile_size=tile_size, overlap=overlap), old_pixels + + +def latent_upscale_on_pixel_space(samples, scale_method, scale_factor, vae, use_tile=False, tile_size=512, save_temp_prefix=None, hook=None, overlap=64): + return latent_upscale_on_pixel_space2(samples, scale_method, scale_factor, vae, use_tile, tile_size, save_temp_prefix, hook, overlap=overlap)[0] + + +def latent_upscale_on_pixel_space2(samples, scale_method, scale_factor, vae, use_tile=False, tile_size=512, save_temp_prefix=None, hook=None, overlap=64): + pixels = vae_decode(vae, samples, use_tile, hook, tile_size=tile_size, overlap=overlap) + + if save_temp_prefix is not None: + nodes.PreviewImage().save_images(pixels, filename_prefix=save_temp_prefix) + + w = pixels.shape[2] * scale_factor + h = pixels.shape[1] * scale_factor + pixels = nodes.ImageScale().upscale(pixels, scale_method, int(w), int(h), False)[0] + + old_pixels = pixels + if hook is not None: + pixels = hook.post_upscale(pixels) + + return vae_encode(vae, pixels, use_tile, hook, tile_size=tile_size, overlap=overlap), old_pixels + + +def latent_upscale_on_pixel_space_with_model_shape(samples, scale_method, upscale_model, new_w, new_h, vae, use_tile=False, tile_size=512, save_temp_prefix=None, hook=None, overlap=64): + return latent_upscale_on_pixel_space_with_model_shape2(samples, scale_method, upscale_model, new_w, new_h, vae, use_tile, tile_size, save_temp_prefix, hook, overlap=overlap)[0] + + +def latent_upscale_on_pixel_space_with_model_shape2(samples, scale_method, upscale_model, new_w, new_h, vae, use_tile=False, tile_size=512, save_temp_prefix=None, hook=None, overlap=64): + pixels = vae_decode(vae, samples, use_tile, hook, tile_size=tile_size, overlap=overlap) + + if save_temp_prefix is not None: + nodes.PreviewImage().save_images(pixels, filename_prefix=save_temp_prefix) + + w = pixels.shape[2] + + # upscale by model upscaler + current_w = w + while current_w < new_w: + pixels = model_upscale.ImageUpscaleWithModel().upscale(upscale_model, pixels)[0] + current_w = pixels.shape[2] + if current_w == w: + logging.info("[latent_upscale_on_pixel_space_with_model] x1 upscale model selected") + break + + # downscale to target scale + pixels = nodes.ImageScale().upscale(pixels, scale_method, int(new_w), int(new_h), False)[0] + + old_pixels = pixels + if hook is not None: + pixels = hook.post_upscale(pixels) + + return vae_encode(vae, pixels, use_tile, hook, tile_size=tile_size, overlap=overlap), old_pixels + + +def latent_upscale_on_pixel_space_with_model(samples, scale_method, upscale_model, scale_factor, vae, use_tile=False, + tile_size=512, save_temp_prefix=None, hook=None, overlap=64): + return latent_upscale_on_pixel_space_with_model2(samples, scale_method, upscale_model, scale_factor, vae, use_tile, tile_size, save_temp_prefix, hook, overlap=overlap)[0] + +def latent_upscale_on_pixel_space_with_model2(samples, scale_method, upscale_model, scale_factor, vae, use_tile=False, + tile_size=512, save_temp_prefix=None, hook=None, overlap=64): + pixels = vae_decode(vae, samples, use_tile, hook, tile_size=tile_size, overlap=overlap) + + if save_temp_prefix is not None: + nodes.PreviewImage().save_images(pixels, filename_prefix=save_temp_prefix) + + w = pixels.shape[2] + h = pixels.shape[1] + + new_w = w * scale_factor + new_h = h * scale_factor + + # upscale by model upscaler + current_w = w + while current_w < new_w: + pixels = model_upscale.ImageUpscaleWithModel().upscale(upscale_model, pixels)[0] + current_w = pixels.shape[2] + if current_w == w: + logging.info("[latent_upscale_on_pixel_space_with_model] x1 upscale model selected") + break + + # downscale to target scale + pixels = nodes.ImageScale().upscale(pixels, scale_method, int(new_w), int(new_h), False)[0] + + old_pixels = pixels + if hook is not None: + pixels = hook.post_upscale(pixels) + + return vae_encode(vae, pixels, use_tile, hook, tile_size=tile_size, overlap=overlap), old_pixels + + +class TwoSamplersForMaskUpscaler: + def __init__(self, scale_method, sample_schedule, use_tiled_vae, base_sampler, mask_sampler, mask, vae, + full_sampler_opt=None, upscale_model_opt=None, hook_base_opt=None, hook_mask_opt=None, + hook_full_opt=None, + tile_size=512): + + mask = utils.make_2d_mask(mask) + + mask = mask.reshape((-1, 1, mask.shape[-2], mask.shape[-1])) + + self.params = scale_method, sample_schedule, use_tiled_vae, base_sampler, mask_sampler, mask, vae + self.upscale_model = upscale_model_opt + self.full_sampler = full_sampler_opt + self.hook_base = hook_base_opt + self.hook_mask = hook_mask_opt + self.hook_full = hook_full_opt + self.use_tiled_vae = use_tiled_vae + self.tile_size = tile_size + self.is_tiled = False + self.vae = vae + + def upscale(self, step_info, samples, upscale_factor, save_temp_prefix=None): + scale_method, sample_schedule, use_tiled_vae, base_sampler, mask_sampler, mask, vae = self.params + + mask = utils.make_2d_mask(mask) + + self.prepare_hook(step_info) + + # upscale latent + if self.upscale_model is None: + upscaled_latent = latent_upscale_on_pixel_space(samples, scale_method, upscale_factor, vae, + use_tile=self.use_tiled_vae, + save_temp_prefix=save_temp_prefix, + hook=self.hook_base, tile_size=self.tile_size) + else: + upscaled_latent = latent_upscale_on_pixel_space_with_model(samples, scale_method, self.upscale_model, + upscale_factor, vae, + use_tile=self.use_tiled_vae, + save_temp_prefix=save_temp_prefix, + hook=self.hook_mask, tile_size=self.tile_size) + + return self.do_samples(step_info, base_sampler, mask_sampler, sample_schedule, mask, upscaled_latent) + + def prepare_hook(self, step_info): + if self.hook_base is not None: + self.hook_base.set_steps(step_info) + if self.hook_mask is not None: + self.hook_mask.set_steps(step_info) + if self.hook_full is not None: + self.hook_full.set_steps(step_info) + + def upscale_shape(self, step_info, samples, w, h, save_temp_prefix=None): + scale_method, sample_schedule, use_tiled_vae, base_sampler, mask_sampler, mask, vae = self.params + + mask = utils.make_2d_mask(mask) + + self.prepare_hook(step_info) + + # upscale latent + if self.upscale_model is None: + upscaled_latent = latent_upscale_on_pixel_space_shape(samples, scale_method, w, h, vae, + use_tile=self.use_tiled_vae, + save_temp_prefix=save_temp_prefix, + hook=self.hook_base, + tile_size=self.tile_size) + else: + upscaled_latent = latent_upscale_on_pixel_space_with_model_shape(samples, scale_method, self.upscale_model, + w, h, vae, + use_tile=self.use_tiled_vae, + save_temp_prefix=save_temp_prefix, + hook=self.hook_mask, + tile_size=self.tile_size) + + return self.do_samples(step_info, base_sampler, mask_sampler, sample_schedule, mask, upscaled_latent) + + def is_full_sample_time(self, step_info, sample_schedule): + cur_step, total_step = step_info + + # make start from 1 instead of zero + cur_step += 1 + total_step += 1 + + if sample_schedule == "none": + return False + + elif sample_schedule == "interleave1": + return cur_step % 2 == 0 + + elif sample_schedule == "interleave2": + return cur_step % 3 == 0 + + elif sample_schedule == "interleave3": + return cur_step % 4 == 0 + + elif sample_schedule == "last1": + return cur_step == total_step + + elif sample_schedule == "last2": + return cur_step >= total_step - 1 + + elif sample_schedule == "interleave1+last1": + return cur_step % 2 == 0 or cur_step >= total_step - 1 + + elif sample_schedule == "interleave2+last1": + return cur_step % 2 == 0 or cur_step >= total_step - 1 + + elif sample_schedule == "interleave3+last1": + return cur_step % 2 == 0 or cur_step >= total_step - 1 + + def do_samples(self, step_info, base_sampler, mask_sampler, sample_schedule, mask, upscaled_latent): + mask = utils.make_2d_mask(mask) + + if self.is_full_sample_time(step_info, sample_schedule): + logging.info(f"step_info={step_info} / full time") + + upscaled_latent = base_sampler.sample(upscaled_latent, self.hook_base) + sampler = self.full_sampler if self.full_sampler is not None else base_sampler + return sampler.sample(upscaled_latent, self.hook_full) + + else: + logging.info(f"step_info={step_info} / non-full time") + # upscale mask + if mask.ndim == 2: + mask = mask[None, :, :, None] + upscaled_mask = F.interpolate(mask, size=(upscaled_latent['samples'].shape[2], upscaled_latent['samples'].shape[3]), mode='bilinear', align_corners=True) + upscaled_mask = upscaled_mask[:, :, :upscaled_latent['samples'].shape[2], :upscaled_latent['samples'].shape[3]] + + # base sampler + upscaled_inv_mask = torch.where(upscaled_mask != 1.0, torch.tensor(1.0), torch.tensor(0.0)) + upscaled_latent['noise_mask'] = upscaled_inv_mask + upscaled_latent = base_sampler.sample(upscaled_latent, self.hook_base) + + # mask sampler + upscaled_latent['noise_mask'] = upscaled_mask + upscaled_latent = mask_sampler.sample(upscaled_latent, self.hook_mask) + + # remove mask + del upscaled_latent['noise_mask'] + return upscaled_latent + + +class PixelKSampleUpscaler: + def __init__(self, scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, + use_tiled_vae, upscale_model_opt=None, hook_opt=None, tile_size=512, scheduler_func=None, + tile_cnet_opt=None, tile_cnet_strength=1.0): + self.params = scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise + self.upscale_model = upscale_model_opt + self.hook = hook_opt + self.use_tiled_vae = use_tiled_vae + self.tile_size = tile_size + self.is_tiled = False + self.vae = vae + self.scheduler_func = scheduler_func + self.tile_cnet = tile_cnet_opt + self.tile_cnet_strength = tile_cnet_strength + + def sample(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise, images): + if self.tile_cnet is not None: + image_batch, image_w, image_h, _ = images.shape + if image_batch > 1: + warnings.warn('Multiple latents in batch, Tile ControlNet being ignored') + else: + if 'TilePreprocessor' not in nodes.NODE_CLASS_MAPPINGS: + raise RuntimeError("'TilePreprocessor' node (from comfyui_controlnet_aux) isn't installed.") + preprocessor = nodes.NODE_CLASS_MAPPINGS['TilePreprocessor']() + # might add capacity to set pyrUp_iters later, not needed for now though + preprocessed = preprocessor.execute(images, pyrUp_iters=3, resolution=min(image_w, image_h))[0] + positive, negative = nodes.ControlNetApplyAdvanced().apply_controlnet(positive=positive, + negative=negative, + control_net=self.tile_cnet, + image=preprocessed, + strength=self.tile_cnet_strength, + start_percent=0, + end_percent=1.0, + vae=self.vae) + + refined_latent = impact_sampling.impact_sample(model, seed, steps, cfg, sampler_name, scheduler, + positive, negative, upscaled_latent, denoise, scheduler_func=self.scheduler_func) + + return refined_latent + + def upscale(self, step_info, samples, upscale_factor, save_temp_prefix=None): + scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise = self.params + + if self.hook is not None: + self.hook.set_steps(step_info) + + if self.upscale_model is None: + upscaled_latent, upscaled_images = \ + latent_upscale_on_pixel_space2(samples, scale_method, upscale_factor, vae, + use_tile=self.use_tiled_vae, + save_temp_prefix=save_temp_prefix, hook=self.hook, tile_size=512) + else: + upscaled_latent, upscaled_images = \ + latent_upscale_on_pixel_space_with_model2(samples, scale_method, self.upscale_model, + upscale_factor, vae, + use_tile=self.use_tiled_vae, + save_temp_prefix=save_temp_prefix, + hook=self.hook, + tile_size=self.tile_size) + + if self.hook is not None: + model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise = \ + self.hook.pre_ksample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, + upscaled_latent, denoise) + + if 'noise_mask' in samples: + upscaled_latent['noise_mask'] = samples['noise_mask'] + + refined_latent = self.sample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise, upscaled_images) + return refined_latent + + def upscale_shape(self, step_info, samples, w, h, save_temp_prefix=None): + scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise = self.params + + if self.hook is not None: + self.hook.set_steps(step_info) + + if self.upscale_model is None: + upscaled_latent, upscaled_images = \ + latent_upscale_on_pixel_space_shape2(samples, scale_method, w, h, vae, + use_tile=self.use_tiled_vae, + save_temp_prefix=save_temp_prefix, hook=self.hook, + tile_size=self.tile_size) + else: + upscaled_latent, upscaled_images = \ + latent_upscale_on_pixel_space_with_model_shape2(samples, scale_method, self.upscale_model, + w, h, vae, + use_tile=self.use_tiled_vae, + save_temp_prefix=save_temp_prefix, + hook=self.hook, + tile_size=self.tile_size) + + if self.hook is not None: + model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise = \ + self.hook.pre_ksample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, + upscaled_latent, denoise) + + if 'noise_mask' in samples: + upscaled_latent['noise_mask'] = samples['noise_mask'] + + refined_latent = self.sample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise, upscaled_images) + return refined_latent + + +class IPAdapterWrapper: + def __init__(self, ipadapter_pipe, weight, noise, weight_type, start_at, end_at, unfold_batch, weight_v2, reference_image, neg_image=None, prev_control_net=None, combine_embeds='concat'): + self.reference_image = reference_image + self.ipadapter_pipe = ipadapter_pipe + self.weight = weight + self.weight_type = weight_type + self.noise = noise + self.start_at = start_at + self.end_at = end_at + self.unfold_batch = unfold_batch + self.prev_control_net = prev_control_net + self.weight_v2 = weight_v2 + self.image = reference_image + self.neg_image = neg_image + self.combine_embeds = combine_embeds + + # name 'apply_ipadapter' isn't allowed + def doit_ipadapter(self, model): + cnet_image_list = [self.image] + prev_cnet_images = [] + + if 'IPAdapterAdvanced' not in nodes.NODE_CLASS_MAPPINGS: + if 'IPAdapterApply' in nodes.NODE_CLASS_MAPPINGS: + raise Exception("[ERROR] 'ComfyUI IPAdapter Plus' is outdated.") + + utils.try_install_custom_node('https://github.com/cubiq/ComfyUI_IPAdapter_plus', + "To use 'IPAdapterApplySEGS' node, 'ComfyUI IPAdapter Plus' extension is required.") + raise Exception("[ERROR] To use IPAdapterApplySEGS, you need to install 'ComfyUI IPAdapter Plus'") + + obj = nodes.NODE_CLASS_MAPPINGS['IPAdapterAdvanced'] + + ipadapter, _, clip_vision, insightface, lora_loader = self.ipadapter_pipe + model = lora_loader(model) + + if self.prev_control_net is not None: + model, prev_cnet_images = self.prev_control_net.doit_ipadapter(model) + + model = obj().apply_ipadapter(model=model, ipadapter=ipadapter, weight=self.weight, weight_type=self.weight_type, + start_at=self.start_at, end_at=self.end_at, combine_embeds=self.combine_embeds, + clip_vision=clip_vision, image=self.image, image_negative=self.neg_image, attn_mask=None, + insightface=insightface, weight_faceidv2=self.weight_v2)[0] + + cnet_image_list.extend(prev_cnet_images) + + return model, cnet_image_list + + def apply(self, positive, negative, image, mask=None, use_acn=False): + if self.prev_control_net is not None: + return self.prev_control_net.apply(positive, negative, image, mask, use_acn=use_acn) + else: + return positive, negative, [] + + +class ControlNetWrapper: + def __init__(self, control_net, strength, preprocessor, prev_control_net=None, original_size=None, crop_region=None, control_image=None): + self.control_net = control_net + self.strength = strength + self.preprocessor = preprocessor + self.prev_control_net = prev_control_net + + if original_size is not None and crop_region is not None and control_image is not None: + self.control_image = utils.tensor_resize(control_image, original_size[1], original_size[0]) + self.control_image = torch.tensor(utils.tensor_crop(self.control_image, crop_region)) + else: + self.control_image = None + + def apply(self, positive, negative, image, mask=None, use_acn=False): + cnet_image_list = [] + prev_cnet_images = [] + + if self.prev_control_net is not None: + positive, negative, prev_cnet_images = self.prev_control_net.apply(positive, negative, image, mask, use_acn=use_acn) + + if self.control_image is not None: + cnet_image = self.control_image + elif self.preprocessor is not None: + cnet_image = self.preprocessor.apply(image, mask) + else: + cnet_image = image + + cnet_image_list.extend(prev_cnet_images) + cnet_image_list.append(cnet_image) + + if use_acn: + if "ACN_AdvancedControlNetApply" in nodes.NODE_CLASS_MAPPINGS: + acn = nodes.NODE_CLASS_MAPPINGS['ACN_AdvancedControlNetApply']() + positive, negative, _ = acn.apply_controlnet(positive=positive, negative=negative, control_net=self.control_net, image=cnet_image, + strength=self.strength, start_percent=0.0, end_percent=1.0) + else: + utils.try_install_custom_node('https://github.com/BlenderNeko/ComfyUI_TiledKSampler', + "To use 'ControlNetWrapper' for AnimateDiff, 'ComfyUI-Advanced-ControlNet' extension is required.") + raise Exception("'ACN_AdvancedControlNetApply' node isn't installed.") + else: + positive = nodes.ControlNetApply().apply_controlnet(positive, self.control_net, cnet_image, self.strength)[0] + + return positive, negative, cnet_image_list + + def doit_ipadapter(self, model): + if self.prev_control_net is not None: + return self.prev_control_net.doit_ipadapter(model) + else: + return model, [] + + +class ControlNetAdvancedWrapper: + def __init__(self, control_net, strength, start_percent, end_percent, preprocessor, prev_control_net=None, + original_size=None, crop_region=None, control_image=None, vae=None): + self.control_net = control_net + self.strength = strength + self.preprocessor = preprocessor + self.prev_control_net = prev_control_net + self.start_percent = start_percent + self.end_percent = end_percent + self.vae = vae + + if original_size is not None and crop_region is not None and control_image is not None: + self.control_image = utils.tensor_resize(control_image, original_size[1], original_size[0]) + self.control_image = torch.tensor(utils.tensor_crop(self.control_image, crop_region)) + else: + self.control_image = None + + def doit_ipadapter(self, model): + if self.prev_control_net is not None: + return self.prev_control_net.doit_ipadapter(model) + else: + return model, [] + + def apply(self, positive, negative, image, mask=None, use_acn=False): + cnet_image_list = [] + prev_cnet_images = [] + + if self.prev_control_net is not None: + positive, negative, prev_cnet_images = self.prev_control_net.apply(positive, negative, image, mask) + + if self.control_image is not None: + cnet_image = self.control_image + elif self.preprocessor is not None: + cnet_image = self.preprocessor.apply(image, mask) + else: + cnet_image = image + + cnet_image_list.extend(prev_cnet_images) + cnet_image_list.append(cnet_image) + + if use_acn: + if "ACN_AdvancedControlNetApply" in nodes.NODE_CLASS_MAPPINGS: + acn = nodes.NODE_CLASS_MAPPINGS['ACN_AdvancedControlNetApply']() + positive, negative, _ = acn.apply_controlnet(positive=positive, negative=negative, control_net=self.control_net, image=cnet_image, + strength=self.strength, start_percent=self.start_percent, end_percent=self.end_percent) + else: + utils.try_install_custom_node('https://github.com/BlenderNeko/ComfyUI_TiledKSampler', + "To use 'ControlNetAdvancedWrapper' for AnimateDiff, 'ComfyUI-Advanced-ControlNet' extension is required.") + raise Exception("'ACN_AdvancedControlNetApply' node isn't installed.") + else: + if self.vae is not None: + apply_controlnet = nodes.ControlNetApplyAdvanced().apply_controlnet + signature = inspect.signature(apply_controlnet) + + if 'vae' in signature.parameters: + positive, negative = nodes.ControlNetApplyAdvanced().apply_controlnet(positive, negative, self.control_net, cnet_image, self.strength, self.start_percent, self.end_percent, vae=self.vae) + else: + logging.error("[Impact Pack] ERROR: The ComfyUI version is outdated. VAE cannot be used in ApplyControlNet.") + raise Exception("[Impact Pack] ERROR: The ComfyUI version is outdated. VAE cannot be used in ApplyControlNet.") + else: + positive, negative = nodes.ControlNetApplyAdvanced().apply_controlnet(positive, negative, self.control_net, cnet_image, self.strength, self.start_percent, self.end_percent) + + return positive, negative, cnet_image_list + + +# REQUIREMENTS: BlenderNeko/ComfyUI_TiledKSampler +class TiledKSamplerWrapper: + params = None + + def __init__(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, + tile_width, tile_height, tiling_strategy): + self.params = model, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, tile_width, tile_height, tiling_strategy + + def sample(self, latent_image, hook=None): + if "BNK_TiledKSampler" in nodes.NODE_CLASS_MAPPINGS: + TiledKSampler = nodes.NODE_CLASS_MAPPINGS['BNK_TiledKSampler'] + else: + utils.try_install_custom_node('https://github.com/BlenderNeko/ComfyUI_TiledKSampler', + "To use 'TiledKSamplerProvider', 'Tiled sampling for ComfyUI' extension is required.") + raise Exception("'BNK_TiledKSampler' node isn't installed.") + + model, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, tile_width, tile_height, tiling_strategy = self.params + + if hook is not None: + model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise = \ + hook.pre_ksample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, + denoise) + + return TiledKSampler().sample(model, seed, tile_width, tile_height, tiling_strategy, steps, cfg, sampler_name, + scheduler, positive, negative, latent_image, denoise)[0] + + +class PixelTiledKSampleUpscaler: + def __init__(self, scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, + denoise, + tile_width, tile_height, tiling_strategy, + upscale_model_opt=None, hook_opt=None, tile_cnet_opt=None, tile_size=512, tile_cnet_strength=1.0, overlap=64): + self.params = scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise + self.vae = vae + self.tile_params = tile_width, tile_height, tiling_strategy + self.upscale_model = upscale_model_opt + self.hook = hook_opt + self.tile_cnet = tile_cnet_opt + self.tile_size = tile_size + self.is_tiled = True + self.tile_cnet_strength = tile_cnet_strength + self.overlap = overlap + + def tiled_ksample(self, latent, images): + if "BNK_TiledKSampler" in nodes.NODE_CLASS_MAPPINGS: + TiledKSampler = nodes.NODE_CLASS_MAPPINGS['BNK_TiledKSampler'] + else: + utils.try_install_custom_node('https://github.com/BlenderNeko/ComfyUI_TiledKSampler', + "To use 'PixelTiledKSampleUpscalerProvider', 'Tiled sampling for ComfyUI' extension is required.") + raise RuntimeError("'BNK_TiledKSampler' node isn't installed.") + + scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise = self.params + tile_width, tile_height, tiling_strategy = self.tile_params + + if self.tile_cnet is not None: + image_batch, image_w, image_h, _ = images.shape + if image_batch > 1: + warnings.warn('Multiple latents in batch, Tile ControlNet being ignored') + else: + if 'TilePreprocessor' not in nodes.NODE_CLASS_MAPPINGS: + raise RuntimeError("'TilePreprocessor' node (from comfyui_controlnet_aux) isn't installed.") + preprocessor = nodes.NODE_CLASS_MAPPINGS['TilePreprocessor']() + # might add capacity to set pyrUp_iters later, not needed for now though + preprocessed = preprocessor.execute(images, pyrUp_iters=3, resolution=min(image_w, image_h))[0] + + positive, negative = nodes.ControlNetApplyAdvanced().apply_controlnet(positive=positive, + negative=negative, + control_net=self.tile_cnet, + image=preprocessed, + strength=self.tile_cnet_strength, + start_percent=0, end_percent=1.0, + vae=self.vae) + + return TiledKSampler().sample(model, seed, tile_width, tile_height, tiling_strategy, steps, cfg, sampler_name, + scheduler, positive, negative, latent, denoise)[0] + + def upscale(self, step_info, samples, upscale_factor, save_temp_prefix=None): + scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise = self.params + + if self.hook is not None: + self.hook.set_steps(step_info) + + if self.upscale_model is None: + upscaled_latent, upscaled_images = \ + latent_upscale_on_pixel_space2(samples, scale_method, upscale_factor, vae, + use_tile=True, save_temp_prefix=save_temp_prefix, + hook=self.hook, tile_size=self.tile_size) + else: + upscaled_latent, upscaled_images = \ + latent_upscale_on_pixel_space_with_model2(samples, scale_method, self.upscale_model, + upscale_factor, vae, use_tile=True, + save_temp_prefix=save_temp_prefix, + hook=self.hook, tile_size=self.tile_size) + + refined_latent = self.tiled_ksample(upscaled_latent, upscaled_images) + + return refined_latent + + def upscale_shape(self, step_info, samples, w, h, save_temp_prefix=None): + scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise = self.params + + if self.hook is not None: + self.hook.set_steps(step_info) + + if self.upscale_model is None: + upscaled_latent, upscaled_images = \ + latent_upscale_on_pixel_space_shape2(samples, scale_method, w, h, vae, + use_tile=True, save_temp_prefix=save_temp_prefix, + hook=self.hook, tile_size=self.tile_size) + else: + upscaled_latent, upscaled_images = \ + latent_upscale_on_pixel_space_with_model_shape2(samples, scale_method, + self.upscale_model, w, h, vae, + use_tile=True, + save_temp_prefix=save_temp_prefix, + hook=self.hook, + tile_size=self.tile_size) + + refined_latent = self.tiled_ksample(upscaled_latent, upscaled_images) + + return refined_latent + + +# REQUIREMENTS: biegert/ComfyUI-CLIPSeg +class BBoxDetectorBasedOnCLIPSeg: + prompt = None + blur = None + threshold = None + dilation_factor = None + aux = None + + def __init__(self, prompt, blur, threshold, dilation_factor): + self.prompt = prompt + self.blur = blur + self.threshold = threshold + self.dilation_factor = dilation_factor + + def detect(self, image, bbox_threshold, bbox_dilation, bbox_crop_factor, drop_size=1, detailer_hook=None): + mask = self.detect_combined(image, bbox_threshold, bbox_dilation) + + mask = utils.make_2d_mask(mask) + + segs = mask_to_segs(mask, False, bbox_crop_factor, True, drop_size, detailer_hook=detailer_hook) + + if detailer_hook is not None and hasattr(detailer_hook, "post_detection"): + segs = detailer_hook.post_detection(segs) + + return segs + + def detect_combined(self, image, bbox_threshold, bbox_dilation): + if "CLIPSeg" in nodes.NODE_CLASS_MAPPINGS: + CLIPSeg = nodes.NODE_CLASS_MAPPINGS['CLIPSeg'] + else: + utils.try_install_custom_node('https://github.com/biegert/ComfyUI-CLIPSeg/raw/main/custom_nodes/clipseg.py', + "To use 'CLIPSegDetectorProvider', 'CLIPSeg' extension is required.") + raise Exception("'CLIPSeg' node isn't installed.") + + if self.threshold is None: + threshold = bbox_threshold + else: + threshold = self.threshold + + if self.dilation_factor is None: + dilation_factor = bbox_dilation + else: + dilation_factor = self.dilation_factor + + prompt = self.aux if self.prompt == '' and self.aux is not None else self.prompt + + mask, _, _ = CLIPSeg().segment_image(image, prompt, self.blur, threshold, dilation_factor) + mask = utils.to_binary_mask(mask) + return mask + + def setAux(self, x): + self.aux = x + + +def update_node_status(node, text, progress=None): + if PromptServer.instance.client_id is None: + return + + PromptServer.instance.send_sync("impact/update_status", { + "node": node, + "progress": progress, + "text": text + }, PromptServer.instance.client_id) + + +def random_mask_raw(mask, bbox, factor): + x1, y1, x2, y2 = bbox + w = x2 - x1 + h = y2 - y1 + + factor = max(6, int(min(w, h) * factor / 4)) + + def draw_random_circle(center, radius): + i, j = center + for x in range(int(i - radius), int(i + radius)): + for y in range(int(j - radius), int(j + radius)): + if np.linalg.norm(np.array([x, y]) - np.array([i, j])) <= radius: + mask[x, y] = 1 + + def draw_irregular_line(start, end, pivot, is_vertical): + i = start + while i < end: + base_radius = np.random.randint(5, factor) + radius = int(base_radius) + + if is_vertical: + draw_random_circle((i, pivot), radius) + else: + draw_random_circle((pivot, i), radius) + + i += radius + + def draw_irregular_line_parallel(start, end, pivot, is_vertical): + with ThreadPoolExecutor(max_workers=16) as executor: + futures = [] + step = (end - start) // 16 + for i in range(start, end, step): + future = executor.submit(draw_irregular_line, i, min(i + step, end), pivot, is_vertical) + futures.append(future) + + for future in futures: + future.result() + + draw_irregular_line_parallel(y1 + factor, y2 - factor, x1 + factor, True) + draw_irregular_line_parallel(y1 + factor, y2 - factor, x2 - factor, True) + draw_irregular_line_parallel(x1 + factor, x2 - factor, y1 + factor, False) + draw_irregular_line_parallel(x1 + factor, x2 - factor, y2 - factor, False) + + mask[y1 + factor:y2 - factor, x1 + factor:x2 - factor] = 1.0 + + +def random_mask(mask, bbox, factor, size=128): + small_mask = np.zeros((size, size)).astype(np.float32) + random_mask_raw(small_mask, (0, 0, size, size), factor) + + x1, y1, x2, y2 = bbox + small_mask = torch.tensor(small_mask).unsqueeze(0).unsqueeze(0) + bbox_mask = torch.nn.functional.interpolate(small_mask, size=(y2 - y1, x2 - x1), mode='bilinear', align_corners=False) + bbox_mask = bbox_mask.squeeze(0).squeeze(0) + mask[y1:y2, x1:x2] = bbox_mask + + +def adaptive_mask_paste(dest_mask, src_mask, bbox): + x1, y1, x2, y2 = bbox + small_mask = torch.tensor(src_mask).unsqueeze(0).unsqueeze(0) + bbox_mask = torch.nn.functional.interpolate(small_mask, size=(y2 - y1, x2 - x1), mode='bilinear', align_corners=False) + bbox_mask = bbox_mask.squeeze(0).squeeze(0) + dest_mask[y1:y2, x1:x2] = bbox_mask + + +def crop_condition_mask(mask, image, crop_region): + cond_scale = (mask.shape[1] / image.shape[1], mask.shape[2] / image.shape[2]) + mask_region = [round(v * cond_scale[i % 2]) for i, v in enumerate(crop_region)] + return utils.crop_ndarray3(mask, mask_region) + + +class SafeToGPU: + def __init__(self, size): + self.size = size + + def to_device(self, obj, device): + if utils.is_same_device(device, 'cpu'): + obj.to(device) + else: + if utils.is_same_device(obj.device, 'cpu'): # cpu to gpu + model_management.free_memory(self.size * 1.3, device) + if model_management.get_free_memory(device) > self.size * 1.3: + try: + obj.to(device) + except Exception: + logging.warning(f"[Impact Pack] The model is not moved to the '{device}' due to insufficient memory. [1]") + else: + logging.warning(f"[Impact Pack] The model is not moved to the '{device}' due to insufficient memory. [2]") + + +class SafeToGPU_stub(): + def to_device(self, obj, device): + pass + + +from comfy.cli_args import args, LatentPreviewMethod +import folder_paths +from latent_preview import TAESD, TAESDPreviewerImpl, Latent2RGBPreviewer + +try: + import comfy.latent_formats as latent_formats + + + def get_previewer(device, latent_format=latent_formats.SD15(), force=False, method=None): + previewer = None + + if method is None: + method = args.preview_method + + if method != LatentPreviewMethod.NoPreviews or force: + # TODO previewer methods + taesd_decoder_path = None + + if hasattr(latent_format, "taesd_decoder_path"): + taesd_decoder_path = folder_paths.get_full_path("vae_approx", latent_format.taesd_decoder_name) + + if method == LatentPreviewMethod.Auto: + method = LatentPreviewMethod.Latent2RGB + if taesd_decoder_path: + method = LatentPreviewMethod.TAESD + + if method == LatentPreviewMethod.TAESD: + if taesd_decoder_path: + taesd = TAESD(None, taesd_decoder_path, latent_channels=latent_format.latent_channels).to(device) + previewer = TAESDPreviewerImpl(taesd) + else: + logging.warning("[Impact Pack] TAESD previews enabled, but could not find models/vae_approx/{}".format( + latent_format.taesd_decoder_name)) + + if previewer is None: + previewer = Latent2RGBPreviewer(latent_format.latent_rgb_factors) + return previewer + +except Exception: + logging.error("#########################################################################") + logging.error("[ERROR] ComfyUI-Impact-Pack: Please update ComfyUI to the latest version.") + logging.error("#########################################################################") diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/defs.py b/custom_nodes/comfyui-impact-pack/modules/impact/defs.py new file mode 100644 index 0000000000000000000000000000000000000000..39b099cfedc047438777aa1f5ca5379d478362d1 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/defs.py @@ -0,0 +1,17 @@ +detection_labels = [ + 'hand', 'face', 'mouth', 'eyes', 'eyebrows', 'pupils', + 'left_eyebrow', 'left_eye', 'left_pupil', 'right_eyebrow', 'right_eye', 'right_pupil', + 'short_sleeved_shirt', 'long_sleeved_shirt', 'short_sleeved_outwear', 'long_sleeved_outwear', + 'vest', 'sling', 'shorts', 'trousers', 'skirt', 'short_sleeved_dress', 'long_sleeved_dress', 'vest_dress', 'sling_dress', + "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", + "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", + "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", + "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", + "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", + "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", + "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", + "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", + "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", + "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", + "hair drier", "toothbrush" + ] diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/detectors.py b/custom_nodes/comfyui-impact-pack/modules/impact/detectors.py new file mode 100644 index 0000000000000000000000000000000000000000..eeb320b8bd542d84e778994b26b43f1467351662 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/detectors.py @@ -0,0 +1,529 @@ +import logging + +import impact.core as core +from nodes import MAX_RESOLUTION +import impact.segs_nodes as segs_nodes +import impact.utils as utils +import torch +from impact.core import SEG + +SAM_MODEL_TOOLTIP = {"tooltip": "Segment Anything Model for Silhouette Detection.\nBe sure to use the SAM_MODEL loaded through the SAMLoader (Impact) node as input."} +SAM_MODEL_TOOLTIP_OPTIONAL = {"tooltip": "[OPTIONAL]\nSegment Anything Model for Silhouette Detection.\nBe sure to use the SAM_MODEL loaded through the SAMLoader (Impact) node as input.\nGiven this input, it refines the rectangular areas detected by BBOX_DETECTOR into silhouette shapes through SAM.\nsam_model_opt takes priority over segm_detector_opt."} + +MASK_HINT_THRESHOLD_TOOLTIP = "When detection_hint is mask-area, the mask of SEGS is used as a point hint for SAM (Segment Anything).\nIn this case, only the areas of the mask with brightness values equal to or greater than mask_hint_threshold are used as hints." +MASK_HINT_USE_NEGATIVE_TOOLTIP = "When detecting with SAM (Segment Anything), negative hints are applied as follows:\nSmall: When the SEGS is smaller than 10 pixels in size\nOuter: Sampling the image area outside the SEGS region at regular intervals" + +DILATION_TOOLTIP = "Set the value to dilate the result mask. If the value is negative, it erodes the mask." +DETECTION_HINT_TOOLTIP = {"tooltip": "It is recommended to use only center-1.\nWhen refining the mask of SEGS with the SAM (Segment Anything) model, center-1 uses only the rectangular area of SEGS and a single point at the exact center as hints.\nOther options were added during the experimental stage and do not work well."} + +BBOX_EXPANSION_TOOLTIP = "When performing SAM (Segment Anything) detection within the SEGS area, the rectangular area of SEGS is expanded and used as a hint." + +class SAMDetectorCombined: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "sam_model": ("SAM_MODEL", SAM_MODEL_TOOLTIP), + "segs": ("SEGS", {"tooltip": "This is the segment information detected by the detector.\nIt refines the Mask through the SAM (Segment Anything) detector for all areas pointed to by SEGS, and combines all Masks to return as a single Mask."}), + "image": ("IMAGE", {"tooltip": "It is assumed that segs contains only the information about the detected areas, and does not include the image. SAM (Segment Anything) operates by referencing this image."}), + "detection_hint": (["center-1", "horizontal-2", "vertical-2", "rect-4", "diamond-4", "mask-area", + "mask-points", "mask-point-bbox", "none"], DETECTION_HINT_TOOLTIP), + "dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1, "tooltip": DILATION_TOOLTIP}), + "threshold": ("FLOAT", {"default": 0.93, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "Set the sensitivity threshold for the mask detected by SAM (Segment Anything). A higher value generates a more specific mask with a narrower range. For example, when pointing to a person's area, it might detect clothes, which is a narrower range, instead of the entire person."}), + "bbox_expansion": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1, "tooltip": BBOX_EXPANSION_TOOLTIP}), + "mask_hint_threshold": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": MASK_HINT_THRESHOLD_TOOLTIP}), + "mask_hint_use_negative": (["False", "Small", "Outter"], {"tooltip": MASK_HINT_USE_NEGATIVE_TOOLTIP}) + } + } + + RETURN_TYPES = ("MASK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detector" + + def doit(self, sam_model, segs, image, detection_hint, dilation, + threshold, bbox_expansion, mask_hint_threshold, mask_hint_use_negative): + return (core.make_sam_mask(sam_model, segs, image, detection_hint, dilation, + threshold, bbox_expansion, mask_hint_threshold, mask_hint_use_negative), ) + + +class SAMDetectorSegmented: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "sam_model": ("SAM_MODEL", SAM_MODEL_TOOLTIP), + "segs": ("SEGS", {"tooltip": "This is the segment information detected by the detector.\nFor the SEGS region, the masks detected by SAM (Segment Anything) are created as a unified mask and a batch of individual masks."}), + "image": ("IMAGE", {"tooltip": "It is assumed that segs contains only the information about the detected areas, and does not include the image. SAM (Segment Anything) operates by referencing this image."}), + "detection_hint": (["center-1", "horizontal-2", "vertical-2", "rect-4", "diamond-4", "mask-area", + "mask-points", "mask-point-bbox", "none"], DETECTION_HINT_TOOLTIP), + "dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1, "tooltip": DILATION_TOOLTIP}), + "threshold": ("FLOAT", {"default": 0.93, "min": 0.0, "max": 1.0, "step": 0.01}), + "bbox_expansion": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1, "tooltip": BBOX_EXPANSION_TOOLTIP}), + "mask_hint_threshold": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": MASK_HINT_THRESHOLD_TOOLTIP}), + "mask_hint_use_negative": (["False", "Small", "Outter"], {"tooltip": MASK_HINT_USE_NEGATIVE_TOOLTIP}) + } + } + + RETURN_TYPES = ("MASK", "MASK") + RETURN_NAMES = ("combined_mask", "batch_masks") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detector" + + def doit(self, sam_model, segs, image, detection_hint, dilation, + threshold, bbox_expansion, mask_hint_threshold, mask_hint_use_negative): + combined_mask, batch_masks = core.make_sam_mask_segmented(sam_model, segs, image, detection_hint, dilation, + threshold, bbox_expansion, mask_hint_threshold, + mask_hint_use_negative) + return (combined_mask, batch_masks, ) + + +class BboxDetectorForEach: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "bbox_detector": ("BBOX_DETECTOR", ), + "image": ("IMAGE", ), + "threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "dilation": ("INT", {"default": 10, "min": -512, "max": 512, "step": 1}), + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + "labels": ("STRING", {"multiline": True, "default": "all", "placeholder": "List the types of segments to be allowed, separated by commas"}), + }, + "optional": {"detailer_hook": ("DETAILER_HOOK",), } + } + + RETURN_TYPES = ("SEGS", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detector" + + def doit(self, bbox_detector, image, threshold, dilation, crop_factor, drop_size, labels=None, detailer_hook=None): + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: BboxDetectorForEach does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + segs = bbox_detector.detect(image, threshold, dilation, crop_factor, drop_size, detailer_hook) + + if labels is not None and labels != '': + labels = labels.split(',') + if len(labels) > 0: + segs, _ = segs_nodes.SEGSLabelFilter.filter(segs, labels) + + return (segs, ) + + +class SegmDetectorForEach: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segm_detector": ("SEGM_DETECTOR", ), + "image": ("IMAGE", ), + "threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "dilation": ("INT", {"default": 10, "min": -512, "max": 512, "step": 1}), + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + "labels": ("STRING", {"multiline": True, "default": "all", "placeholder": "List the types of segments to be allowed, separated by commas"}), + }, + "optional": {"detailer_hook": ("DETAILER_HOOK",), } + } + + RETURN_TYPES = ("SEGS", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detector" + + def doit(self, segm_detector, image, threshold, dilation, crop_factor, drop_size, labels=None, detailer_hook=None): + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: SegmDetectorForEach does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + segs = segm_detector.detect(image, threshold, dilation, crop_factor, drop_size, detailer_hook) + + if labels is not None and labels != '': + labels = labels.split(',') + if len(labels) > 0: + segs, _ = segs_nodes.SEGSLabelFilter.filter(segs, labels) + + return (segs, ) + + +class SegmDetectorCombined: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segm_detector": ("SEGM_DETECTOR", ), + "image": ("IMAGE", ), + "threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}), + } + } + + RETURN_TYPES = ("MASK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detector" + + def doit(self, segm_detector, image, threshold, dilation): + mask = segm_detector.detect_combined(image, threshold, dilation) + + if mask is None: + mask = torch.zeros((image.shape[1], image.shape[2]), dtype=torch.float32, device="cpu") + + return (mask.unsqueeze(0),) + + +class BboxDetectorCombined(SegmDetectorCombined): + @classmethod + def INPUT_TYPES(s): + return {"required": { + "bbox_detector": ("BBOX_DETECTOR", ), + "image": ("IMAGE", ), + "threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "dilation": ("INT", {"default": 4, "min": -512, "max": 512, "step": 1}), + } + } + + def doit(self, bbox_detector, image, threshold, dilation): + mask = bbox_detector.detect_combined(image, threshold, dilation) + + if mask is None: + mask = torch.zeros((image.shape[1], image.shape[2]), dtype=torch.float32, device="cpu") + + return (mask.unsqueeze(0),) + + +class SimpleDetectorForEach: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "bbox_detector": ("BBOX_DETECTOR", ), + "image": ("IMAGE", ), + + "bbox_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "bbox_dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}), + + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + + "sub_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "sub_dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}), + "sub_bbox_expansion": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1}), + + "sam_mask_hint_threshold": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}), + }, + "optional": { + "post_dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}), + "sam_model_opt": ("SAM_MODEL", SAM_MODEL_TOOLTIP_OPTIONAL), + "segm_detector_opt": ("SEGM_DETECTOR", ), + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detector" + + @staticmethod + def detect(bbox_detector, image, bbox_threshold, bbox_dilation, crop_factor, drop_size, + sub_threshold, sub_dilation, sub_bbox_expansion, + sam_mask_hint_threshold, post_dilation=0, sam_model_opt=None, segm_detector_opt=None, + detailer_hook=None): + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: SimpleDetectorForEach does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + if segm_detector_opt is not None and hasattr(segm_detector_opt, 'bbox_detector') and segm_detector_opt.bbox_detector == bbox_detector: + # Better segm support for YOLO-World detector + segs = segm_detector_opt.detect(image, sub_threshold, sub_dilation, crop_factor, drop_size, detailer_hook=detailer_hook) + else: + segs = bbox_detector.detect(image, bbox_threshold, bbox_dilation, crop_factor, drop_size, detailer_hook=detailer_hook) + + if sam_model_opt is not None: + mask = core.make_sam_mask(sam_model_opt, segs, image, "center-1", sub_dilation, + sub_threshold, sub_bbox_expansion, sam_mask_hint_threshold, False) + segs = core.segs_bitwise_and_mask(segs, mask) + elif segm_detector_opt is not None: + segm_segs = segm_detector_opt.detect(image, sub_threshold, sub_dilation, crop_factor, drop_size, detailer_hook=detailer_hook) + mask = core.segs_to_combined_mask(segm_segs) + segs = core.segs_bitwise_and_mask(segs, mask) + + segs = core.dilate_segs(segs, post_dilation) + + return (segs,) + + def doit(self, bbox_detector, image, bbox_threshold, bbox_dilation, crop_factor, drop_size, + sub_threshold, sub_dilation, sub_bbox_expansion, + sam_mask_hint_threshold, post_dilation=0, sam_model_opt=None, segm_detector_opt=None): + + return SimpleDetectorForEach.detect(bbox_detector, image, bbox_threshold, bbox_dilation, crop_factor, drop_size, + sub_threshold, sub_dilation, sub_bbox_expansion, + sam_mask_hint_threshold, post_dilation=post_dilation, + sam_model_opt=sam_model_opt, segm_detector_opt=segm_detector_opt) + + +class SimpleDetectorForEachPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "detailer_pipe": ("DETAILER_PIPE", ), + "image": ("IMAGE", ), + + "bbox_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "bbox_dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}), + + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + + "sub_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "sub_dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}), + "sub_bbox_expansion": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1}), + + "sam_mask_hint_threshold": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}), + }, + "optional": { + "post_dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}), + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detector" + + def doit(self, detailer_pipe, image, bbox_threshold, bbox_dilation, crop_factor, drop_size, + sub_threshold, sub_dilation, sub_bbox_expansion, sam_mask_hint_threshold, post_dilation=0): + + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: SimpleDetectorForEach does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + model, clip, vae, positive, negative, wildcard, bbox_detector, segm_detector_opt, sam_model_opt, detailer_hook, refiner_model, refiner_clip, refiner_positive, refiner_negative = detailer_pipe + + return SimpleDetectorForEach.detect(bbox_detector, image, bbox_threshold, bbox_dilation, crop_factor, drop_size, + sub_threshold, sub_dilation, sub_bbox_expansion, + sam_mask_hint_threshold, post_dilation=post_dilation, sam_model_opt=sam_model_opt, segm_detector_opt=segm_detector_opt, + detailer_hook=detailer_hook) + +class SAM2VideoDetectorSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "image_frames": ("IMAGE", ), + + "bbox_detector": ("BBOX_DETECTOR", ), + "sam2_model": ("SAM_MODEL", ), + + "bbox_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "sam2_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + } + } + + RETURN_TYPES = ("SEGS", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detector" + + @staticmethod + def doit(bbox_detector, sam2_model, image_frames, bbox_threshold, sam2_threshold, crop_factor, drop_size): + if not isinstance(sam2_model, core.SAM2Wrapper): + logging.error("[Impact Pack] To use the SAM2VideoDetectorSEGS node, a SAM2 model must be provided as input to `sam2_model`.") + raise Exception("To use the SAM2VideoDetectorSEGS node, a SAM2 model must be provided as input to `sam2_model`.") + + segs = bbox_detector.detect(image_frames[0].unsqueeze(0), bbox_threshold, 0, 0, drop_size) + segs_masks = sam2_model.predict_video_segs(image_frames, segs) + + def get_whole_merged_mask(all_masks): + merged_mask = (all_masks[0] * 255).to(torch.uint8) + for mask in all_masks[1:]: + merged_mask |= (mask * 255).to(torch.uint8) + + merged_mask = (merged_mask / 255.0).to(torch.float32) + merged_mask = utils.to_binary_mask(merged_mask, 0.1)[0] + return merged_mask + + new_segs = [] + for k, v in segs_masks.items(): + v = v.squeeze(3) + m = get_whole_merged_mask(v) + seg = segs_nodes.MaskToSEGS.doit(m, False, crop_factor, False, drop_size, contour_fill=True)[0][1] + + if len(seg) == 0: + continue + + seg = seg[0] + + x1, y1, x2, y2 = seg.crop_region + masks = [] + for mask in v: + masks.append(mask[y1:y2, x1:x2]) + cropped_mask = torch.stack(masks) + cropped_mask = (cropped_mask >= (sam2_threshold*100-50)).to(torch.uint8).cpu() + new_seg = SEG(seg.cropped_image, cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper) + new_segs.append(new_seg) + + return ((segs[0], new_segs), ) + + +class SimpleDetectorForAnimateDiff: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "bbox_detector": ("BBOX_DETECTOR", ), + "image_frames": ("IMAGE", ), + + "bbox_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "bbox_dilation": ("INT", {"default": 0, "min": -255, "max": 255, "step": 1}), + + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + + "sub_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "sub_dilation": ("INT", {"default": 0, "min": -255, "max": 255, "step": 1}), + "sub_bbox_expansion": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1}), + + "sam_mask_hint_threshold": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}), + }, + "optional": { + "masking_mode": (["Pivot SEGS", "Combine neighboring frames", "Don't combine"],), + "segs_pivot": (["Combined mask", "1st frame mask"],), + "sam_model_opt": ("SAM_MODEL", SAM_MODEL_TOOLTIP_OPTIONAL), + "segm_detector_opt": ("SEGM_DETECTOR", ), + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detector" + + @staticmethod + def detect(bbox_detector, image_frames, bbox_threshold, bbox_dilation, crop_factor, drop_size, + sub_threshold, sub_dilation, sub_bbox_expansion, sam_mask_hint_threshold, + masking_mode="Pivot SEGS", segs_pivot="Combined mask", sam_model_opt=None, segm_detector_opt=None): + + h = image_frames.shape[1] + w = image_frames.shape[2] + + # gather segs for all frames + segs_by_frames = [] + for image in image_frames: + image = image.unsqueeze(0) + segs = bbox_detector.detect(image, bbox_threshold, bbox_dilation, crop_factor, drop_size) + + if sam_model_opt is not None: + mask = core.make_sam_mask(sam_model_opt, segs, image, "center-1", sub_dilation, + sub_threshold, sub_bbox_expansion, sam_mask_hint_threshold, False) + segs = core.segs_bitwise_and_mask(segs, mask) + elif segm_detector_opt is not None: + segm_segs = segm_detector_opt.detect(image, sub_threshold, sub_dilation, crop_factor, drop_size) + mask = core.segs_to_combined_mask(segm_segs) + segs = core.segs_bitwise_and_mask(segs, mask) + + segs_by_frames.append(segs) + + def get_masked_frames(): + masks_by_frame = [] + for i, segs in enumerate(segs_by_frames): + masks_in_frame = segs_nodes.SEGSToMaskList().doit(segs)[0] + current_frame_mask = (masks_in_frame[0] * 255).to(torch.uint8) + + for mask in masks_in_frame[1:]: + current_frame_mask |= (mask * 255).to(torch.uint8) + + current_frame_mask = (current_frame_mask/255.0).to(torch.float32) + current_frame_mask = utils.to_binary_mask(current_frame_mask, 0.1)[0] + + masks_by_frame.append(current_frame_mask) + + return masks_by_frame + + def get_empty_mask(): + return torch.zeros((h, w), dtype=torch.float32, device="cpu") + + def get_neighboring_mask_at(i, masks_by_frame): + prv = masks_by_frame[i-1] if i > 1 else get_empty_mask() + cur = masks_by_frame[i] + nxt = masks_by_frame[i-1] if i > 1 else get_empty_mask() + + prv = prv if prv is not None else get_empty_mask() + cur = cur.clone() if cur is not None else get_empty_mask() + nxt = nxt if nxt is not None else get_empty_mask() + + return prv, cur, nxt + + def get_merged_neighboring_mask(masks_by_frame): + if len(masks_by_frame) <= 1: + return masks_by_frame + + result = [] + for i in range(0, len(masks_by_frame)): + prv, cur, nxt = get_neighboring_mask_at(i, masks_by_frame) + cur = (cur * 255).to(torch.uint8) + cur |= (prv * 255).to(torch.uint8) + cur |= (nxt * 255).to(torch.uint8) + cur = (cur / 255.0).to(torch.float32) + cur = utils.to_binary_mask(cur, 0.1)[0] + result.append(cur) + + return result + + def get_whole_merged_mask(): + all_masks = [] + for segs in segs_by_frames: + all_masks += segs_nodes.SEGSToMaskList().doit(segs)[0] + + merged_mask = (all_masks[0] * 255).to(torch.uint8) + for mask in all_masks[1:]: + merged_mask |= (mask * 255).to(torch.uint8) + + merged_mask = (merged_mask / 255.0).to(torch.float32) + merged_mask = utils.to_binary_mask(merged_mask, 0.1)[0] + return merged_mask + + def get_pivot_segs(): + if segs_pivot == "1st frame mask": + return segs_by_frames[0][1] + else: + merged_mask = get_whole_merged_mask() + return segs_nodes.MaskToSEGS.doit(merged_mask, False, crop_factor, False, drop_size, contour_fill=True)[0] + + def get_segs(merged_neighboring=False): + pivot_segs = get_pivot_segs() + + masks_by_frame = get_masked_frames() + if merged_neighboring: + masks_by_frame = get_merged_neighboring_mask(masks_by_frame) + + new_segs = [] + for seg in pivot_segs[1]: + cropped_mask = torch.zeros(seg.cropped_mask.shape, dtype=torch.float32, device="cpu").unsqueeze(0) + pivot_mask = torch.from_numpy(seg.cropped_mask) + x1, y1, x2, y2 = seg.crop_region + for mask in masks_by_frame: + cropped_mask_at_frame = (mask[y1:y2, x1:x2] * pivot_mask).unsqueeze(0) + cropped_mask = torch.cat((cropped_mask, cropped_mask_at_frame), dim=0) + + if len(cropped_mask) > 1: + cropped_mask = cropped_mask[1:] + + new_seg = SEG(seg.cropped_image, cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper) + new_segs.append(new_seg) + + return pivot_segs[0], new_segs + + # create result mask + if masking_mode == "Pivot SEGS": + return (get_pivot_segs(), ) + + elif masking_mode == "Combine neighboring frames": + return (get_segs(merged_neighboring=True), ) + + else: # elif masking_mode == "Don't combine": + return (get_segs(merged_neighboring=False), ) + + def doit(self, bbox_detector, image_frames, bbox_threshold, bbox_dilation, crop_factor, drop_size, + sub_threshold, sub_dilation, sub_bbox_expansion, sam_mask_hint_threshold, + masking_mode="Pivot SEGS", segs_pivot="Combined mask", sam_model_opt=None, segm_detector_opt=None): + + return SimpleDetectorForAnimateDiff.detect(bbox_detector, image_frames, bbox_threshold, bbox_dilation, crop_factor, drop_size, + sub_threshold, sub_dilation, sub_bbox_expansion, sam_mask_hint_threshold, + masking_mode, segs_pivot, sam_model_opt, segm_detector_opt) diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/hf_nodes.py b/custom_nodes/comfyui-impact-pack/modules/impact/hf_nodes.py new file mode 100644 index 0000000000000000000000000000000000000000..9c8e4208732e15870426f11ca22da9b14f9db930 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/hf_nodes.py @@ -0,0 +1,189 @@ +import comfy +import re +from impact import utils + + +hf_transformer_model_urls = [ + "rizvandwiki/gender-classification-2", + "NTQAI/pedestrian_gender_recognition", + "Leilab/gender_class", + "ProjectPersonal/GenderClassifier", + "crangana/trained-gender", + "cledoux42/GenderNew_v002", + "ivensamdh/genderage2" +] + + +class HF_TransformersClassifierProvider: + @classmethod + def INPUT_TYPES(s): + global hf_transformer_model_urls + return {"required": { + "preset_repo_id": (hf_transformer_model_urls + ['Manual repo id'],), + "manual_repo_id": ("STRING", {"multiline": False}), + "device_mode": (["AUTO", "Prefer GPU", "CPU"],), + }, + } + + RETURN_TYPES = ("TRANSFORMERS_CLASSIFIER",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/HuggingFace" + + def doit(self, preset_repo_id, manual_repo_id, device_mode): + from transformers import pipeline + + if preset_repo_id == 'Manual repo id': + url = manual_repo_id + else: + url = preset_repo_id + + if device_mode != 'CPU': + device = comfy.model_management.get_torch_device() + else: + device = "cpu" + + classifier = pipeline('image-classification', model=url, device=device) + + return (classifier,) + + +preset_classify_expr = [ + '#Female > #Male', + '#Female < #Male', + 'female > 0.5', + 'male > 0.5', + 'Age16to25 > 0.1', + 'Age50to69 > 0.1', +] + +symbolic_label_map = { + '#Female': {'female', 'Female', 'Human Female', 'woman', 'women', 'girl'}, + '#Male': {'male', 'Male', 'Human Male', 'man', 'men', 'boy'} +} + +def is_numeric_string(input_str): + return re.match(r'^-?\d+(\.\d+)?$', input_str) is not None + + +classify_expr_pattern = r'([^><= ]+)\s*(>|<|>=|<=|=)\s*([^><= ]+)' + + +class SEGS_Classify: + @classmethod + def INPUT_TYPES(s): + global preset_classify_expr + return {"required": { + "classifier": ("TRANSFORMERS_CLASSIFIER",), + "segs": ("SEGS",), + "preset_expr": (preset_classify_expr + ['Manual expr'],), + "manual_expr": ("STRING", {"multiline": False}), + }, + "optional": { + "ref_image_opt": ("IMAGE", ), + } + } + + RETURN_TYPES = ("SEGS", "SEGS", "STRING") + RETURN_NAMES = ("filtered_SEGS", "remained_SEGS", "detected_labels") + OUTPUT_IS_LIST = (False, False, True) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/HuggingFace" + + @staticmethod + def lookup_classified_label_score(score_infos, label): + global symbolic_label_map + + if label.startswith('#'): + if label not in symbolic_label_map: + return None + else: + label = symbolic_label_map[label] + else: + label = {label} + + for x in score_infos: + if x['label'] in label: + return x['score'] + + return None + + def doit(self, classifier, segs, preset_expr, manual_expr, ref_image_opt=None): + if preset_expr == 'Manual expr': + expr_str = manual_expr + else: + expr_str = preset_expr + + match = re.match(classify_expr_pattern, expr_str) + + if match is None: + return (segs[0], []), segs, [] + + a = match.group(1) + op = match.group(2) + b = match.group(3) + + a_is_lab = not is_numeric_string(a) + b_is_lab = not is_numeric_string(b) + + classified = [] + remained_SEGS = [] + provided_labels = set() + + for seg in segs[1]: + cropped_image = None + + if seg.cropped_image is not None: + cropped_image = seg.cropped_image + elif ref_image_opt is not None: + # take from original image + cropped_image = utils.crop_image(ref_image_opt, seg.crop_region) + + if cropped_image is not None: + cropped_image = utils.to_pil(cropped_image) + res = classifier(cropped_image) + classified.append((seg, res)) + + for x in res: + provided_labels.add(x['label']) + else: + remained_SEGS.append(seg) + + filtered_SEGS = [] + for seg, res in classified: + if a_is_lab: + avalue = SEGS_Classify.lookup_classified_label_score(res, a) + else: + avalue = a + + if b_is_lab: + bvalue = SEGS_Classify.lookup_classified_label_score(res, b) + else: + bvalue = b + + if avalue is None or bvalue is None: + remained_SEGS.append(seg) + continue + + avalue = float(avalue) + bvalue = float(bvalue) + + if op == '>': + cond = avalue > bvalue + elif op == '<': + cond = avalue < bvalue + elif op == '>=': + cond = avalue >= bvalue + elif op == '<=': + cond = avalue <= bvalue + else: + cond = avalue == bvalue + + if cond: + filtered_SEGS.append(seg) + else: + remained_SEGS.append(seg) + + return (segs[0], filtered_SEGS), (segs[0], remained_SEGS), list(provided_labels) diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/hook_nodes.py b/custom_nodes/comfyui-impact-pack/modules/impact/hook_nodes.py new file mode 100644 index 0000000000000000000000000000000000000000..f17166976c1f58e77bb3a57042851c3ca9844b08 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/hook_nodes.py @@ -0,0 +1,128 @@ +import sys +from . import hooks +from . import defs + + +class SEGSOrderedFilterDetailerHookProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "target": (["area(=w*h)", "width", "height", "x1", "y1", "x2", "y2"],), + "order": ("BOOLEAN", {"default": True, "label_on": "descending", "label_off": "ascending"}), + "take_start": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + "take_count": ("INT", {"default": 1, "min": 0, "max": sys.maxsize, "step": 1}), + }, + } + + RETURN_TYPES = ("DETAILER_HOOK", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, target, order, take_start, take_count): + hook = hooks.SEGSOrderedFilterDetailerHook(target, order, take_start, take_count) + return (hook, ) + + +class SEGSRangeFilterDetailerHookProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "target": (["area(=w*h)", "width", "height", "x1", "y1", "x2", "y2", "length_percent"],), + "mode": ("BOOLEAN", {"default": True, "label_on": "inside", "label_off": "outside"}), + "min_value": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + "max_value": ("INT", {"default": 67108864, "min": 0, "max": sys.maxsize, "step": 1}), + }, + } + + RETURN_TYPES = ("DETAILER_HOOK", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, target, mode, min_value, max_value): + hook = hooks.SEGSRangeFilterDetailerHook(target, mode, min_value, max_value) + return (hook, ) + + +class SEGSLabelFilterDetailerHookProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + "preset": (['all'] + defs.detection_labels,), + "labels": ("STRING", {"multiline": True, "placeholder": "List the types of segments to be allowed, separated by commas"}), + }, + } + + RETURN_TYPES = ("DETAILER_HOOK", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, preset, labels): + hook = hooks.SEGSLabelFilterDetailerHook(labels) + return (hook, ) + + +class PreviewDetailerHookProvider: + @classmethod + def INPUT_TYPES(s): + return { + "required": {"quality": ("INT", {"default": 95, "min": 20, "max": 100})}, + "hidden": {"unique_id": "UNIQUE_ID"}, + } + + RETURN_TYPES = ("DETAILER_HOOK", "UPSCALER_HOOK") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + NOT_IDEMPOTENT = True + + def doit(self, quality, unique_id): + hook = hooks.PreviewDetailerHook(unique_id, quality) + return hook, hook + + +class LamaRemoverDetailerHookProvider: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "mask_threshold":("INT", {"default": 250, "min": 0, "max": 255, "step": 1, "display": "slider"}), + "gaussblur_radius": ("INT", {"default": 8, "min": 0, "max": 20, "step": 1, "display": "slider"}), + "skip_sampling": ("BOOLEAN", {"default": True}), + } + } + + RETURN_TYPES = ("DETAILER_HOOK", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, mask_threshold, gaussblur_radius, skip_sampling): + hook = hooks.LamaRemoverDetailerHook(mask_threshold, gaussblur_radius, skip_sampling) + return (hook, ) + + +class BlackPatchRetryHookProvider: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "mean_thresh": ("INT", {"default": 10, "min": 0, "max": 255}), + "var_thresh": ("INT", {"default": 5, "min": 0, "max": 255}) + }, + } + + RETURN_TYPES = ("DETAILER_HOOK", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + NOT_IDEMPOTENT = True + + def doit(self, mean_thresh, var_thresh): + hook = hooks.BlackPatchRetryHook(mean_thresh, var_thresh) + return hook, diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/hooks.py b/custom_nodes/comfyui-impact-pack/modules/impact/hooks.py new file mode 100644 index 0000000000000000000000000000000000000000..2a8c14df58767f515228a38240c4c2e2eab11df4 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/hooks.py @@ -0,0 +1,595 @@ +import copy +import torch +import nodes +from impact import utils +from . import segs_nodes +from thirdparty import noise_nodes +from server import PromptServer +import asyncio +import folder_paths +import os +from comfy_extras import nodes_custom_sampler +import math +import logging + + +class PixelKSampleHook: + cur_step = 0 + total_step = 0 + + def __init__(self): + pass + + def set_steps(self, info): + self.cur_step, self.total_step = info + + def post_decode(self, pixels): + return pixels + + def post_upscale(self, pixels, mask=None): + return pixels + + def post_encode(self, samples): + return samples + + def pre_decode(self, samples): + return samples + + def pre_ksample(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, + denoise): + return model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise + + def post_crop_region(self, w, h, item_bbox, crop_region): + return crop_region + + def touch_scaled_size(self, w, h): + return w, h + + +class PixelKSampleHookCombine(PixelKSampleHook): + hook1 = None + hook2 = None + + def __init__(self, hook1, hook2): + super().__init__() + self.hook1 = hook1 + self.hook2 = hook2 + + def set_steps(self, info): + self.hook1.set_steps(info) + self.hook2.set_steps(info) + + def pre_decode(self, samples): + return self.hook2.pre_decode(self.hook1.pre_decode(samples)) + + def post_decode(self, pixels): + return self.hook2.post_decode(self.hook1.post_decode(pixels)) + + def post_upscale(self, pixels, mask=None): + return self.hook2.post_upscale(self.hook1.post_upscale(pixels, mask), mask) + + def post_encode(self, samples): + return self.hook2.post_encode(self.hook1.post_encode(samples)) + + def post_crop_region(self, w, h, item_bbox, crop_region): + crop_region = self.hook1.post_crop_region(w, h, item_bbox, crop_region) + return self.hook2.post_crop_region(w, h, item_bbox, crop_region) + + def touch_scaled_size(self, w, h): + w, h = self.hook1.touch_scaled_size(w, h) + return self.hook2.touch_scaled_size(w, h) + + def pre_ksample(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, + denoise): + model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise = \ + self.hook1.pre_ksample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, + upscaled_latent, denoise) + + return self.hook2.pre_ksample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, + upscaled_latent, denoise) + + +class DetailerHookCombine(PixelKSampleHookCombine): + def cycle_latent(self, latent): + latent = self.hook1.cycle_latent(latent) + latent = self.hook2.cycle_latent(latent) + return latent + + def post_detection(self, segs): + segs = self.hook1.post_detection(segs) + segs = self.hook2.post_detection(segs) + return segs + + def post_paste(self, image): + image = self.hook1.post_paste(image) + image = self.hook2.post_paste(image) + return image + + def get_custom_noise(self, seed, noise, is_touched): + noise_1st, is_touched = self.hook1.get_custom_noise(seed, noise, is_touched) + noise_2nd, is_touched = self.hook2.get_custom_noise(seed, noise, is_touched) + return noise, is_touched + + def get_custom_sampler(self): + if self.hook1.get_custom_sampler() is not None: + return self.hook1.get_custom_sampler() + else: + return self.hook2.get_custom_sampler() + + def get_skip_sampling(self): + return self.hook1.get_skip_sampling() and self.hook2.get_skip_sampling() + + def should_retry_patch(self, patch): + return self.hook1.should_retry_patch(patch) or self.hook2.should_retry_patch(patch) + + +class SimpleCfgScheduleHook(PixelKSampleHook): + target_cfg = 0 + + def __init__(self, target_cfg): + super().__init__() + self.target_cfg = target_cfg + + def pre_ksample(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise): + if self.total_step > 1: + progress = self.cur_step / (self.total_step - 1) + gap = self.target_cfg - cfg + current_cfg = int(cfg + gap * progress) + else: + current_cfg = self.target_cfg + + return model, seed, steps, current_cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise + + +class SimpleDenoiseScheduleHook(PixelKSampleHook): + def __init__(self, target_denoise): + super().__init__() + self.target_denoise = target_denoise + + def pre_ksample(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise): + if self.total_step > 1: + progress = self.cur_step / (self.total_step - 1) + gap = self.target_denoise - denoise + current_denoise = denoise + gap * progress + else: + current_denoise = self.target_denoise + + return model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, current_denoise + + +class SimpleStepsScheduleHook(PixelKSampleHook): + def __init__(self, target_steps): + super().__init__() + self.target_steps = target_steps + + def pre_ksample(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise): + if self.total_step > 1: + progress = self.cur_step / (self.total_step - 1) + gap = self.target_steps - steps + current_steps = int(steps + gap * progress) + else: + current_steps = self.target_steps + + return model, seed, current_steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise + + +class DetailerHook(PixelKSampleHook): + def cycle_latent(self, latent): + return latent + + def post_detection(self, segs): + return segs + + def post_paste(self, image): + return image + + def get_custom_noise(self, seed, noise, is_touched): + return noise, is_touched + + def get_custom_sampler(self): + return None + + def get_skip_sampling(self): + return False + + def should_retry_patch(self, patch): + return False + + +class CustomSamplerDetailerHookProvider(DetailerHook): + def __init__(self, sampler): + super().__init__() + self.sampler = sampler + + def get_custom_sampler(self): + return self.sampler + + +# class CustomNoiseDetailerHookProvider(DetailerHook): +# def __init__(self, noise): +# super().__init__() +# self.noise = noise +# +# def get_custom_noise(self, seed, noise, is_start): +# return self.noise + + +class VariationNoiseDetailerHookProvider(DetailerHook): + def __init__(self, variation_seed, variation_strength): + super().__init__() + self.variation_seed = variation_seed + self.variation_strength = variation_strength + + def get_custom_noise(self, seed, noise, is_touched): + empty_noise = {'samples': torch.zeros(noise.size())} + if not is_touched: + noise = nodes_custom_sampler.Noise_RandomNoise(seed).generate_noise(empty_noise) + noise_2nd = nodes_custom_sampler.Noise_RandomNoise(self.variation_seed).generate_noise(empty_noise) + + mixed_noise = ((1 - self.variation_strength) * noise + self.variation_strength * noise_2nd) + + # NOTE: Since the variance of the Gaussian noise in mixed_noise has changed, it must be corrected through scaling. + scale_factor = math.sqrt((1 - self.variation_strength) ** 2 + self.variation_strength ** 2) + corrected_noise = mixed_noise / scale_factor # Scale the noise to maintain variance of 1 + + return corrected_noise, True + + +class SimpleDetailerDenoiseSchedulerHook(DetailerHook): + def __init__(self, target_denoise): + super().__init__() + self.target_denoise = target_denoise + + def pre_ksample(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent, denoise): + if self.total_step > 1: + progress = self.cur_step / (self.total_step - 1) + gap = self.target_denoise - denoise + current_denoise = denoise + gap * progress + else: + # ignore hook if total cycle <= 1 + current_denoise = denoise + + return model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent, current_denoise + + +class CoreMLHook(DetailerHook): + def __init__(self, mode): + super().__init__() + resolution = mode.split('x') + + self.w = int(resolution[0]) + self.h = int(resolution[1]) + + self.override_bbox_by_segm = False + + def pre_decode(self, samples): + new_samples = copy.deepcopy(samples) + new_samples['samples'] = samples['samples'][0].unsqueeze(0) + return new_samples + + def post_encode(self, samples): + new_samples = copy.deepcopy(samples) + new_samples['samples'] = samples['samples'].repeat(2, 1, 1, 1) + return new_samples + + def post_crop_region(self, w, h, item_bbox, crop_region): + x1, y1, x2, y2 = crop_region + bx1, by1, bx2, by2 = item_bbox + crop_w = x2-x1 + crop_h = y2-y1 + + crop_ratio = crop_w/crop_h + target_ratio = self.w/self.h + if crop_ratio < target_ratio: + # shrink height + top_gap = by1 - y1 + bottom_gap = y2 - by2 + + gap_ratio = top_gap / bottom_gap + + target_height = 1/target_ratio*crop_w + delta_height = crop_h - target_height + + new_y1 = int(y1 + delta_height*gap_ratio) + new_y2 = int(new_y1 + target_height) + crop_region = x1, new_y1, x2, new_y2 + + elif crop_ratio > target_ratio: + # shrink width + left_gap = bx1 - x1 + right_gap = x2 - bx2 + + gap_ratio = left_gap / right_gap + + target_width = target_ratio*crop_h + delta_width = crop_w - target_width + + new_x1 = int(x1 + delta_width*gap_ratio) + new_x2 = int(new_x1 + target_width) + crop_region = new_x1, y1, new_x2, y2 + + return crop_region + + def touch_scaled_size(self, w, h): + return self.w, self.h + + +# REQUIREMENTS: BlenderNeko/ComfyUI Noise +class InjectNoiseHook(PixelKSampleHook): + def __init__(self, source, seed, start_strength, end_strength): + super().__init__() + self.source = source + self.seed = seed + self.start_strength = start_strength + self.end_strength = end_strength + + def post_encode(self, samples): + cur_step = self.cur_step + + size = samples['samples'].shape + seed = cur_step + self.seed + cur_step + + if "BNK_NoisyLatentImage" in nodes.NODE_CLASS_MAPPINGS and "BNK_InjectNoise" in nodes.NODE_CLASS_MAPPINGS: + NoisyLatentImage = nodes.NODE_CLASS_MAPPINGS["BNK_NoisyLatentImage"] + InjectNoise = nodes.NODE_CLASS_MAPPINGS["BNK_InjectNoise"] + else: + utils.try_install_custom_node('https://github.com/BlenderNeko/ComfyUI_Noise', + "To use 'NoiseInjectionHookProvider', 'ComfyUI Noise' extension is required.") + raise Exception("'BNK_NoisyLatentImage', 'BNK_InjectNoise' nodes are not installed.") + + noise = NoisyLatentImage().create_noisy_latents(self.source, seed, size[3] * 8, size[2] * 8, size[0])[0] + + # inj noise + mask = None + if 'noise_mask' in samples: + mask = samples['noise_mask'] + + strength = self.start_strength + (self.end_strength - self.start_strength) * cur_step / self.total_step + samples = InjectNoise().inject_noise(samples, strength, noise, mask)[0] + logging.info(f"[Impact Pack] InjectNoiseHook: strength = {strength}") + + if mask is not None: + samples['noise_mask'] = mask + + return samples + + +class UnsamplerHook(PixelKSampleHook): + def __init__(self, model, steps, start_end_at_step, end_end_at_step, cfg, sampler_name, + scheduler, normalize, positive, negative): + super().__init__() + self.model = model + self.cfg = cfg + self.sampler_name = sampler_name + self.steps = steps + self.start_end_at_step = start_end_at_step + self.end_end_at_step = end_end_at_step + self.scheduler = scheduler + self.normalize = normalize + self.positive = positive + self.negative = negative + + def post_encode(self, samples): + cur_step = self.cur_step + + Unsampler = noise_nodes.Unsampler + + end_at_step = self.start_end_at_step + (self.end_end_at_step - self.start_end_at_step) * cur_step / self.total_step + end_at_step = int(end_at_step) + + logging.info(f"[Impact Pack] UnsamplerHook: end_at_step = {end_at_step}") + + # inj noise + mask = None + if 'noise_mask' in samples: + mask = samples['noise_mask'] + + samples = Unsampler().unsampler(self.model, self.cfg, self.sampler_name, self.steps, end_at_step, + self.scheduler, self.normalize, self.positive, self.negative, samples)[0] + + if mask is not None: + samples['noise_mask'] = mask + + return samples + + +class InjectNoiseHookForDetailer(DetailerHook): + def __init__(self, source, seed, start_strength, end_strength, from_start=False): + super().__init__() + self.source = source + self.seed = seed + self.start_strength = start_strength + self.end_strength = end_strength + self.from_start = from_start + + def inject_noise(self, samples): + cur_step = self.cur_step if self.from_start else self.cur_step - 1 + total_step = self.total_step if self.from_start else self.total_step - 1 + + size = samples['samples'].shape + seed = cur_step + self.seed + cur_step + + if "BNK_NoisyLatentImage" in nodes.NODE_CLASS_MAPPINGS and "BNK_InjectNoise" in nodes.NODE_CLASS_MAPPINGS: + NoisyLatentImage = nodes.NODE_CLASS_MAPPINGS["BNK_NoisyLatentImage"] + InjectNoise = nodes.NODE_CLASS_MAPPINGS["BNK_InjectNoise"] + else: + utils.try_install_custom_node('https://github.com/BlenderNeko/ComfyUI_Noise', + "To use 'NoiseInjectionDetailerHookProvider', 'ComfyUI Noise' extension is required.") + raise Exception("'BNK_NoisyLatentImage', 'BNK_InjectNoise' nodes are not installed.") + + noise = NoisyLatentImage().create_noisy_latents(self.source, seed, size[3] * 8, size[2] * 8, size[0])[0] + + # inj noise + mask = None + if 'noise_mask' in samples: + mask = samples['noise_mask'] + + strength = self.start_strength + (self.end_strength - self.start_strength) * cur_step / total_step + samples = InjectNoise().inject_noise(samples, strength, noise, mask)[0] + + if mask is not None: + samples['noise_mask'] = mask + + return samples + + def cycle_latent(self, latent): + if self.cur_step == 0 and not self.from_start: + return latent + else: + return self.inject_noise(latent) + + +class UnsamplerDetailerHook(DetailerHook): + def __init__(self, model, steps, start_end_at_step, end_end_at_step, cfg, sampler_name, + scheduler, normalize, positive, negative, from_start=False): + super().__init__() + self.model = model + self.cfg = cfg + self.sampler_name = sampler_name + self.steps = steps + self.start_end_at_step = start_end_at_step + self.end_end_at_step = end_end_at_step + self.scheduler = scheduler + self.normalize = normalize + self.positive = positive + self.negative = negative + self.from_start = from_start + + def unsample(self, samples): + cur_step = self.cur_step if self.from_start else self.cur_step - 1 + total_step = self.total_step if self.from_start else self.total_step - 1 + + Unsampler = noise_nodes.Unsampler + + end_at_step = self.start_end_at_step + (self.end_end_at_step - self.start_end_at_step) * cur_step / total_step + end_at_step = int(end_at_step) + + # inj noise + mask = None + if 'noise_mask' in samples: + mask = samples['noise_mask'] + + samples = Unsampler().unsampler(self.model, self.cfg, self.sampler_name, self.steps, end_at_step, + self.scheduler, self.normalize, self.positive, self.negative, samples)[0] + + if mask is not None: + samples['noise_mask'] = mask + + return samples + + def cycle_latent(self, latent): + if self.cur_step == 0 and not self.from_start: + return latent + else: + return self.unsample(latent) + + +class SEGSOrderedFilterDetailerHook(DetailerHook): + def __init__(self, target, order, take_start, take_count): + super().__init__() + self.target = target + self.order = order + self.take_start = take_start + self.take_count = take_count + + def post_detection(self, segs): + return segs_nodes.SEGSOrderedFilter().doit(segs, self.target, self.order, self.take_start, self.take_count)[0] + + +class SEGSRangeFilterDetailerHook(DetailerHook): + def __init__(self, target, mode, min_value, max_value): + super().__init__() + self.target = target + self.mode = mode + self.min_value = min_value + self.max_value = max_value + + def post_detection(self, segs): + return segs_nodes.SEGSRangeFilter().doit(segs, self.target, self.mode, self.min_value, self.max_value)[0] + + +class SEGSLabelFilterDetailerHook(DetailerHook): + def __init__(self, labels): + super().__init__() + self.labels = labels + + def post_detection(self, segs): + return segs_nodes.SEGSLabelFilter().doit(segs, "", self.labels)[0] + + +class LamaRemoverDetailerHook(DetailerHook): + def __init__(self, mask_threshold, gaussblur_radius, skip_sampling): + super().__init__() + self.mask_threshold = mask_threshold + self.gaussblur_radius = gaussblur_radius + self.skip_sampling = skip_sampling + + def post_upscale(self, img, mask=None): + if "LamaRemover" in nodes.NODE_CLASS_MAPPINGS: + lama_remover_obj = nodes.NODE_CLASS_MAPPINGS['LamaRemover']() + else: + utils.try_install_custom_node('https://github.com/Layer-norm/comfyui-lama-remover', + "To use 'LAMARemoverDetailerHookProvider', 'comfyui-lama-remover' nodepack is required.") + raise Exception("'LamaRemover' node is not installed.") + + return lama_remover_obj.lama_remover(img, masks=mask, mask_threshold=self.mask_threshold, gaussblur_radius=self.gaussblur_radius, invert_mask=False)[0] + + def get_skip_sampling(self): + return self.skip_sampling + + +class PreviewDetailerHook(DetailerHook): + def __init__(self, node_id, quality): + super().__init__() + self.node_id = node_id + self.quality = quality + + async def send(self, image): + if len(image) > 0: + image = image[0].unsqueeze(0) + img = utils.tensor2pil(image) + + temp_path = os.path.join(folder_paths.get_temp_directory(), 'pvhook') + + if not os.path.exists(temp_path): + os.makedirs(temp_path) + + fullpath = os.path.join(temp_path, f"{self.node_id}.webp") + img.save(fullpath, quality=self.quality) + + item = { + "filename": f"{self.node_id}.webp", + "subfolder": 'pvhook', + "type": 'temp' + } + + PromptServer.instance.send_sync("impact-preview", {'node_id': self.node_id, 'item': item}) + + def post_paste(self, image): + loop = asyncio.get_running_loop() + loop.create_task(self.send(image)) + return image + + +class BlackPatchRetryHook(DetailerHook): + def __init__(self, mean_thresh, var_thresh): + super().__init__() + assert 0 <= mean_thresh <= 255 and 0 <= var_thresh <= 255 + self.mean_thresh = mean_thresh + self.var_thresh = var_thresh + + def should_retry_patch(self, cropped_region): + # remove the first dimension (batch_size) + if cropped_region.ndim == 4: + assert cropped_region.shape[0] == 1 + cropped_region = cropped_region.squeeze(0) + + # turn image to grayscape + if cropped_region.ndim == 3: + assert cropped_region.shape[-1] in [1, 3] + cropped_region = cropped_region.mean(axis=-1) # simple average grayscale + + mean = cropped_region.mean() + var = cropped_region.var() + + return (mean <= self.mean_thresh/255) and (var <= self.var_thresh/255) \ No newline at end of file diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/impact_onnx.py b/custom_nodes/comfyui-impact-pack/modules/impact/impact_onnx.py new file mode 100644 index 0000000000000000000000000000000000000000..77c859fdcb88f39261f09032f63f701345abcdd6 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/impact_onnx.py @@ -0,0 +1,39 @@ +import impact.additional_dependencies +import numpy as np +from impact import utils +import logging + +impact.additional_dependencies.ensure_onnx_package() + +try: + import onnxruntime + + def onnx_inference(image, onnx_model): + # prepare image + pil = utils.tensor2pil(image) + image = np.ascontiguousarray(pil) + image = image[:, :, ::-1] # to BGR image + image = image.astype(np.float32) + image -= [103.939, 116.779, 123.68] # 'caffe' mode image preprocessing + + # do detection + onnx_model = onnxruntime.InferenceSession(onnx_model, providers=["CPUExecutionProvider"]) + outputs = onnx_model.run( + [s_i.name for s_i in onnx_model.get_outputs()], + {onnx_model.get_inputs()[0].name: np.expand_dims(image, axis=0)}, + ) + + labels = [op for op in outputs if op.dtype == "int32"][0] + scores = [op for op in outputs if isinstance(op[0][0], np.float32)][0] + boxes = [op for op in outputs if isinstance(op[0][0], np.ndarray)][0] + + # filter-out useless item + idx = np.where(labels[0] == -1)[0][0] + + labels = labels[0][:idx] + scores = scores[0][:idx] + boxes = boxes[0][:idx].astype(np.uint32) + + return labels, scores, boxes +except Exception: + logging.error("[Impact Pack] ComfyUI-Impact-Pack: 'onnxruntime' package doesn't support 'python 3.11', yet.\t{e}") diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/impact_pack.py b/custom_nodes/comfyui-impact-pack/modules/impact/impact_pack.py new file mode 100644 index 0000000000000000000000000000000000000000..cc5854c89a09b85d0d5b28b7677e46972bc04322 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/impact_pack.py @@ -0,0 +1,2714 @@ +import os +import sys + +import comfy.samplers +import comfy.sd +import warnings +from segment_anything import sam_model_registry +from io import BytesIO +import piexif +import zipfile +import re + +import impact.wildcards + +import impact.core as core +from impact.core import SEG +from impact.config import latent_letter_path +from nodes import MAX_RESOLUTION +from PIL import Image, ImageOps +import numpy as np +import hashlib +import json +import safetensors.torch +from PIL.PngImagePlugin import PngInfo +import comfy.model_management +import base64 +import impact.wildcards as wildcards +from . import hooks +from . import utils +import inspect +import folder_paths +import torch +import nodes +import cv2 +import logging + + +try: + from comfy_extras import nodes_differential_diffusion +except Exception: + logging.warning("\n#############################################\n[Impact Pack] ComfyUI is an outdated version.\n#############################################\n") + raise Exception("[Impact Pack] ComfyUI is an outdated version.") + + +warnings.filterwarnings('ignore', category=UserWarning, message='TypedStorage is deprecated') + +model_path = folder_paths.models_dir + + +# folder_paths.supported_pt_extensions +utils.add_folder_path_and_extensions("sams", [os.path.join(model_path, "sams")], folder_paths.supported_pt_extensions) +utils.add_folder_path_and_extensions("onnx", [os.path.join(model_path, "onnx")], {'.onnx'}) + + +# Nodes +class ONNXDetectorProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": {"model_name": (folder_paths.get_filename_list("onnx"), )}} + + RETURN_TYPES = ("BBOX_DETECTOR", ) + FUNCTION = "load_onnx" + + CATEGORY = "ImpactPack" + + def load_onnx(self, model_name): + model = folder_paths.get_full_path("onnx", model_name) + return (core.ONNXDetector(model), ) + + +class CLIPSegDetectorProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "text": ("STRING", {"multiline": False, "tooltip": "Enter the targets to be detected, separated by commas"}), + "blur": ("FLOAT", {"min": 0, "max": 15, "step": 0.1, "default": 7, "tooltip": "Blurs the detected mask"}), + "threshold": ("FLOAT", {"min": 0, "max": 1, "step": 0.05, "default": 0.4, "tooltip": "Detects only areas that are certain above the threshold."}), + "dilation_factor": ("INT", {"min": 0, "max": 10, "step": 1, "default": 4, "tooltip": "Dilates the detected mask."}), + } + } + + RETURN_TYPES = ("BBOX_DETECTOR", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + DESCRIPTION = "Provides a detection function using CLIPSeg, which generates masks based on text prompts.\nTo use this node, the CLIPSeg custom node must be installed." + + def doit(self, text, blur, threshold, dilation_factor): + if "CLIPSeg" in nodes.NODE_CLASS_MAPPINGS: + return (core.BBoxDetectorBasedOnCLIPSeg(text, blur, threshold, dilation_factor), ) + else: + logging.error("[ERROR] CLIPSegToBboxDetector: CLIPSeg custom node isn't installed. You must install biegert/ComfyUI-CLIPSeg extension to use this node.") + raise Exception("[ERROR] CLIPSegToBboxDetector: CLIPSeg custom node isn't installed. You must install biegert/ComfyUI-CLIPSeg extension to use this node.") + + +sam2_config_table = { + 'sam2.1_hiera_base_plus.pt': 'configs/sam2.1/sam2.1_hiera_b+.yaml', + 'sam2.1_hiera_large.pt': 'configs/sam2.1/sam2.1_hiera_l.yaml', + 'sam2.1_hiera_small.pt': 'configs/sam2.1/sam2.1_hiera_s.yaml', + 'sam2.1_hiera_tiny.pt': 'configs/sam2.1/sam2.1_hiera_t.yaml', + 'sam2_hiera_tiny.pt': 'configs/sam2/sam2_hiera_t.yaml', + 'sam2_hiera_small.pt': 'configs/sam2/sam2_hiera_s.yaml', + 'sam2_hiera_base_plus.pt': 'configs/sam2/sam2_hiera_b+.yaml', + 'sam2_hiera_large.pt': 'configs/sam2/sam2_hiera_l.yaml' +} + +class SAMLoader: + @classmethod + def INPUT_TYPES(cls): + models = [x for x in folder_paths.get_filename_list("sams") if 'hq' not in x and (x.endswith('.pt') or x.endswith('.pth') or x.endswith('.safetensors'))] + + if 'ESAM_ModelLoader_Zho' in nodes.NODE_CLASS_MAPPINGS: + models.append('ESAM') + + return { + "required": { + "model_name": (models, {"tooltip": "The detection accuracy varies depending on the SAM model. ESAM can only be used if ComfyUI-YoloWorld-EfficientSAM is installed."}), + "device_mode": (["AUTO", "Prefer GPU", "CPU"], {"tooltip": "AUTO: Only applicable when a GPU is available. It temporarily loads the SAM_MODEL into VRAM only when the detection function is used.\n" + "Prefer GPU: Tries to keep the SAM_MODEL on the GPU whenever possible. This can be used when there is sufficient VRAM available.\n" + "CPU: Always loads only on the CPU."}), + } + } + + RETURN_TYPES = ("SAM_MODEL", ) + FUNCTION = "load_model" + + CATEGORY = "ImpactPack" + + DESCRIPTION = "Load the SAM (Segment Anything) model. This can be used in places that utilize SAM detection functionality, such as SAMDetector or SimpleDetector.\nThe SAM detection functionality in Impact Pack must use the SAM_MODEL loaded through this node." + + def load_model(self, model_name, device_mode="auto"): + if model_name == 'ESAM': + if 'ESAM_ModelLoader_Zho' not in nodes.NODE_CLASS_MAPPINGS: + utils.try_install_custom_node('https://github.com/ZHO-ZHO-ZHO/ComfyUI-YoloWorld-EfficientSAM', + "To use 'ESAM' model, 'ComfyUI-YoloWorld-EfficientSAM' extension is required.") + raise Exception("'ComfyUI-YoloWorld-EfficientSAM' node isn't installed.") + + esam_loader = nodes.NODE_CLASS_MAPPINGS['ESAM_ModelLoader_Zho']() + + if device_mode == 'CPU': + esam = esam_loader.load_esam_model('CPU')[0] + else: + device_mode = 'CUDA' + esam = esam_loader.load_esam_model('CUDA')[0] + + sam_obj = core.ESAMWrapper(esam, device_mode) + esam.sam_wrapper = sam_obj + + logging.info(f"Loads EfficientSAM model: (device:{device_mode})") + return (esam, ) + elif model_name in sam2_config_table: + model_kind = 'sam2' + config = sam2_config_table[model_name] + modelname = folder_paths.get_full_path("sams", model_name) + else: + modelname = folder_paths.get_full_path("sams", model_name) + + if 'vit_h' in model_name: + model_kind = 'vit_h' + elif 'vit_l' in model_name: + model_kind = 'vit_l' + else: + model_kind = 'vit_b' + + sam = sam_model_registry[model_kind](checkpoint=modelname) + + size = os.path.getsize(modelname) + safe_to = core.SafeToGPU(size) + + # Unless user explicitly wants to use CPU, we use GPU + device = comfy.model_management.get_torch_device() if device_mode == "Prefer GPU" else "CPU" + + if device_mode == "Prefer GPU": + safe_to.to_device(sam, device) + + is_auto_mode = device_mode == "AUTO" + + if model_kind == 'sam2': + sam = core.SAM2Wrapper(config=config, modelname=modelname, is_auto_mode=is_auto_mode, safe_to_gpu=safe_to, device_mode=device_mode) + logging.info(f"Loads SAM2 model: {modelname} (device:{device_mode})") + else: + sam_obj = core.SAMWrapper(sam, is_auto_mode=is_auto_mode, safe_to_gpu=safe_to) + sam.sam_wrapper = sam_obj + logging.info(f"Loads SAM model: {modelname} (device:{device_mode})") + + return (sam, ) + + +class ONNXDetectorForEach: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "onnx_detector": ("ONNX_DETECTOR",), + "image": ("IMAGE",), + "threshold": ("FLOAT", {"default": 0.8, "min": 0.0, "max": 1.0, "step": 0.01}), + "dilation": ("INT", {"default": 10, "min": -512, "max": 512, "step": 1}), + "crop_factor": ("FLOAT", {"default": 1.0, "min": 0.5, "max": 100, "step": 0.1}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + } + } + + RETURN_TYPES = ("SEGS", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detector" + + OUTPUT_NODE = True + + def doit(self, onnx_detector, image, threshold, dilation, crop_factor, drop_size): + segs = onnx_detector.detect(image, threshold, dilation, crop_factor, drop_size) + return (segs, ) + + +class DetailerForEach: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "image": ("IMAGE", ), + "segs": ("SEGS", ), + "model": ("MODEL", {"tooltip": "If the `ImpactDummyInput` is connected to the model, the inference stage is skipped."}), + "clip": ("CLIP",), + "vae": ("VAE",), + "guide_size": ("FLOAT", {"default": 512, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "guide_size_for": ("BOOLEAN", {"default": True, "label_on": "bbox", "label_off": "crop_region"}), + "max_size": ("FLOAT", {"default": 1024, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "positive": ("CONDITIONING",), + "negative": ("CONDITIONING",), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + "feather": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}), + "noise_mask": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "force_inpaint": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "wildcard": ("STRING", {"multiline": True, "dynamicPrompts": False}), + + "cycle": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1}), + }, + "optional": { + "detailer_hook": ("DETAILER_HOOK",), + "inpaint_model": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + "tiled_encode": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "tiled_decode": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + } + } + + RETURN_TYPES = ("IMAGE", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + DESCRIPTION = "It enhances details by inpainting each region within the detected area bundle (SEGS) after enlarging them based on the guide size." + + @staticmethod + def get_core_module(): + return core + + @staticmethod + def do_detail(image, segs, model, clip, vae, guide_size, guide_size_for_bbox, max_size, seed, steps, cfg, sampler_name, scheduler, + positive, negative, denoise, feather, noise_mask, force_inpaint, wildcard_opt=None, detailer_hook=None, + refiner_ratio=None, refiner_model=None, refiner_clip=None, refiner_positive=None, refiner_negative=None, + cycle=1, inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None, tiled_encode=False, tiled_decode=False): + + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: DetailerForEach does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + image = image.clone() + enhanced_alpha_list = [] + enhanced_list = [] + cropped_list = [] + cnet_pil_list = [] + + segs = core.segs_scale_match(segs, image.shape) + new_segs = [] + + wildcard_concat_mode = None + if wildcard_opt is not None: + if wildcard_opt.startswith('[CONCAT]'): + wildcard_concat_mode = 'concat' + wildcard_opt = wildcard_opt[8:] + wmode, wildcard_chooser = wildcards.process_wildcard_for_segs(wildcard_opt) + else: + wmode, wildcard_chooser = None, None + + if wmode in ['ASC', 'DSC', 'ASC-SIZE', 'DSC-SIZE']: + if wmode == 'ASC': + ordered_segs = sorted(segs[1], key=lambda x: (x.bbox[0], x.bbox[1])) + elif wmode == 'DSC': + ordered_segs = sorted(segs[1], key=lambda x: (x.bbox[0], x.bbox[1]), reverse=True) + elif wmode == 'ASC-SIZE': + ordered_segs = sorted(segs[1], key=lambda x: (x.bbox[2]-x.bbox[0]) * (x.bbox[3]-x.bbox[1])) + + else: # wmode == 'DSC-SIZE' + ordered_segs = sorted(segs[1], key=lambda x: (x.bbox[2]-x.bbox[0]) * (x.bbox[3]-x.bbox[1]), reverse=True) + else: + ordered_segs = segs[1] + + if not (isinstance(model, str) and model == "DUMMY") and noise_mask_feather > 0 and 'denoise_mask_function' not in model.model_options: + model = nodes_differential_diffusion.DifferentialDiffusion().apply(model)[0] + + for i, seg in enumerate(ordered_segs): + cropped_image = utils.crop_ndarray4(image.cpu().numpy(), seg.crop_region) # Never use seg.cropped_image to handle overlapping area + cropped_image = utils.to_tensor(cropped_image) + mask = utils.to_tensor(seg.cropped_mask) + mask = utils.tensor_gaussian_blur_mask(mask, feather) + + is_mask_all_zeros = (seg.cropped_mask == 0).all().item() + if is_mask_all_zeros: + logging.info("Detailer: segment skip [empty mask]") + continue + + if noise_mask: + cropped_mask = seg.cropped_mask + else: + cropped_mask = None + + if wildcard_chooser is not None and wmode != "LAB": + seg_seed, wildcard_item = wildcard_chooser.get(seg) + elif wildcard_chooser is not None and wmode == "LAB": + seg_seed, wildcard_item = None, wildcard_chooser.get(seg) + else: + seg_seed, wildcard_item = None, None + + seg_seed = seed + i if seg_seed is None else seg_seed + + if not isinstance(positive, str): + cropped_positive = [ + [condition, { + k: core.crop_condition_mask(v, image, seg.crop_region) if k == "mask" else v + for k, v in details.items() + }] + for condition, details in positive + ] + else: + cropped_positive = positive + + if not isinstance(negative, str): + cropped_negative = [ + [condition, { + k: core.crop_condition_mask(v, image, seg.crop_region) if k == "mask" else v + for k, v in details.items() + }] + for condition, details in negative + ] + else: + # Negative Conditioning is placeholder such as FLUX.1 + cropped_negative = negative + + if wildcard_item and wildcard_item.strip() == '[SKIP]': + continue + + if wildcard_item and wildcard_item.strip() == '[STOP]': + break + + orig_cropped_image = cropped_image.clone() + if not (isinstance(model, str) and model == "DUMMY"): + enhanced_image, cnet_pils = core.enhance_detail(cropped_image, model, clip, vae, guide_size, guide_size_for_bbox, max_size, + seg.bbox, seg_seed, steps, cfg, sampler_name, scheduler, + cropped_positive, cropped_negative, denoise, cropped_mask, force_inpaint, + wildcard_opt=wildcard_item, wildcard_opt_concat_mode=wildcard_concat_mode, + detailer_hook=detailer_hook, + refiner_ratio=refiner_ratio, refiner_model=refiner_model, + refiner_clip=refiner_clip, refiner_positive=refiner_positive, + refiner_negative=refiner_negative, control_net_wrapper=seg.control_net_wrapper, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, + scheduler_func=scheduler_func_opt, vae_tiled_encode=tiled_encode, + vae_tiled_decode=tiled_decode) + else: + enhanced_image = cropped_image + cnet_pils = None + + if cnet_pils is not None: + cnet_pil_list.extend(cnet_pils) + + if enhanced_image is not None: + # don't latent composite-> converting to latent caused poor quality + # use image paste + image = image.cpu() + enhanced_image = enhanced_image.cpu() + utils.tensor_paste(image, enhanced_image, (seg.crop_region[0], seg.crop_region[1]), mask) # this code affecting to `cropped_image`. + enhanced_list.append(enhanced_image) + + if detailer_hook is not None: + image = detailer_hook.post_paste(image) + + if enhanced_image is not None: + # Convert enhanced_pil_alpha to RGBA mode + enhanced_image_alpha = utils.tensor_convert_rgba(enhanced_image) + new_seg_image = enhanced_image.numpy() # alpha should not be applied to seg_image + + # Apply the mask + mask = utils.tensor_resize(mask, *utils.tensor_get_size(enhanced_image)) + utils.tensor_putalpha(enhanced_image_alpha, mask) + enhanced_alpha_list.append(enhanced_image_alpha) + else: + new_seg_image = None + + cropped_list.append(orig_cropped_image) # NOTE: Don't use `cropped_image` + + new_seg = SEG(new_seg_image, seg.cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper) + new_segs.append(new_seg) + + image_tensor = utils.tensor_convert_rgb(image) + + cropped_list.sort(key=lambda x: x.shape, reverse=True) + enhanced_list.sort(key=lambda x: x.shape, reverse=True) + enhanced_alpha_list.sort(key=lambda x: x.shape, reverse=True) + + return image_tensor, cropped_list, enhanced_list, enhanced_alpha_list, cnet_pil_list, (segs[0], new_segs) + + def doit(self, image, segs, model, clip, vae, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, + scheduler, positive, negative, denoise, feather, noise_mask, force_inpaint, wildcard, cycle=1, + detailer_hook=None, inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None, + tiled_encode=False, tiled_decode=False): + + enhanced_img, *_ = \ + DetailerForEach.do_detail(image, segs, model, clip, vae, guide_size, guide_size_for, max_size, seed, steps, + cfg, sampler_name, scheduler, positive, negative, denoise, feather, noise_mask, + force_inpaint, wildcard, detailer_hook, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, + scheduler_func_opt=scheduler_func_opt, tiled_encode=tiled_encode, tiled_decode=tiled_decode) + + return (enhanced_img, ) + + +class DetailerForEachAutoRetry: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "image": ("IMAGE", ), + "segs": ("SEGS", ), + "model": ("MODEL", {"tooltip": "If the `ImpactDummyInput` is connected to the model, the inference stage is skipped."}), + "clip": ("CLIP",), + "vae": ("VAE",), + "guide_size": ("FLOAT", {"default": 512, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "guide_size_for": ("BOOLEAN", {"default": True, "label_on": "bbox", "label_off": "crop_region"}), + "max_size": ("FLOAT", {"default": 1024, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "positive": ("CONDITIONING",), + "negative": ("CONDITIONING",), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + "feather": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}), + "noise_mask": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "force_inpaint": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "wildcard": ("STRING", {"multiline": True, "dynamicPrompts": False}), + + "cycle": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1}), + "max_retries": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1}), + }, + "optional": { + "detailer_hook": ("DETAILER_HOOK",), + "inpaint_model": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + "tiled_encode": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "tiled_decode": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + } + } + + RETURN_TYPES = ("IMAGE", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + DESCRIPTION = "It enhances details by inpainting each region within the detected area bundle (SEGS) after enlarging them based on the guide size." + + @staticmethod + def get_core_module(): + return core + + @staticmethod + def do_detail(image, segs, model, clip, vae, guide_size, guide_size_for_bbox, max_size, seed, steps, cfg, sampler_name, scheduler, + positive, negative, denoise, feather, noise_mask, force_inpaint, wildcard_opt=None, detailer_hook=None, + refiner_ratio=None, refiner_model=None, refiner_clip=None, refiner_positive=None, refiner_negative=None, + cycle=1, inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None, tiled_encode=False, tiled_decode=False, max_retries=1): + + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: DetailerForEach does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + image = image.clone() + enhanced_alpha_list = [] + enhanced_list = [] + cropped_list = [] + cnet_pil_list = [] + + segs = core.segs_scale_match(segs, image.shape) + new_segs = [] + + wildcard_concat_mode = None + if wildcard_opt is not None: + if wildcard_opt.startswith('[CONCAT]'): + wildcard_concat_mode = 'concat' + wildcard_opt = wildcard_opt[8:] + wmode, wildcard_chooser = wildcards.process_wildcard_for_segs(wildcard_opt) + else: + wmode, wildcard_chooser = None, None + + if wmode in ['ASC', 'DSC', 'ASC-SIZE', 'DSC-SIZE']: + if wmode == 'ASC': + ordered_segs = sorted(segs[1], key=lambda x: (x.bbox[0], x.bbox[1])) + elif wmode == 'DSC': + ordered_segs = sorted(segs[1], key=lambda x: (x.bbox[0], x.bbox[1]), reverse=True) + elif wmode == 'ASC-SIZE': + ordered_segs = sorted(segs[1], key=lambda x: (x.bbox[2]-x.bbox[0]) * (x.bbox[3]-x.bbox[1])) + + else: # wmode == 'DSC-SIZE' + ordered_segs = sorted(segs[1], key=lambda x: (x.bbox[2]-x.bbox[0]) * (x.bbox[3]-x.bbox[1]), reverse=True) + else: + ordered_segs = segs[1] + + if not (isinstance(model, str) and model == "DUMMY") and noise_mask_feather > 0 and 'denoise_mask_function' not in model.model_options: + model = nodes_differential_diffusion.DifferentialDiffusion().apply(model)[0] + + for i, seg in enumerate(ordered_segs): + cropped_image = utils.crop_ndarray4(image.cpu().numpy(), seg.crop_region) # Never use seg.cropped_image to handle overlapping area + cropped_image = utils.to_tensor(cropped_image) + mask = utils.to_tensor(seg.cropped_mask) + mask = utils.tensor_gaussian_blur_mask(mask, feather) + + is_mask_all_zeros = (seg.cropped_mask == 0).all().item() + if is_mask_all_zeros: + print("Detailer: segment skip [empty mask]") + continue + + if noise_mask: + cropped_mask = seg.cropped_mask + else: + cropped_mask = None + + if wildcard_chooser is not None and wmode != "LAB": + seg_seed, wildcard_item = wildcard_chooser.get(seg) + elif wildcard_chooser is not None and wmode == "LAB": + seg_seed, wildcard_item = None, wildcard_chooser.get(seg) + else: + seg_seed, wildcard_item = None, None + + seg_seed = seed + i if seg_seed is None else seg_seed + + if not isinstance(positive, str): + cropped_positive = [ + [condition, { + k: core.crop_condition_mask(v, image, seg.crop_region) if k == "mask" else v + for k, v in details.items() + }] + for condition, details in positive + ] + else: + cropped_positive = positive + + if not isinstance(negative, str): + cropped_negative = [ + [condition, { + k: core.crop_condition_mask(v, image, seg.crop_region) if k == "mask" else v + for k, v in details.items() + }] + for condition, details in negative + ] + else: + # Negative Conditioning is placeholder such as FLUX.1 + cropped_negative = negative + + if wildcard_item and wildcard_item.strip() == '[SKIP]': + continue + + if wildcard_item and wildcard_item.strip() == '[STOP]': + break + + orig_cropped_image = cropped_image.clone() + + # initialize + enhanced_image = cropped_image + cnet_pils = None + + if not (isinstance(model, str) and model == "DUMMY"): + for retry in range(max_retries): + enhanced_image, cnet_pils = core.enhance_detail(cropped_image, model, clip, vae, guide_size, guide_size_for_bbox, max_size, + seg.bbox, seg_seed + retry, steps, cfg, sampler_name, scheduler, + cropped_positive, cropped_negative, denoise, cropped_mask, force_inpaint, + wildcard_opt=wildcard_item, wildcard_opt_concat_mode=wildcard_concat_mode, + detailer_hook=detailer_hook, + refiner_ratio=refiner_ratio, refiner_model=refiner_model, + refiner_clip=refiner_clip, refiner_positive=refiner_positive, + refiner_negative=refiner_negative, control_net_wrapper=seg.control_net_wrapper, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, + scheduler_func=scheduler_func_opt, vae_tiled_encode=tiled_encode, + vae_tiled_decode=tiled_decode) + + if detailer_hook is None or not detailer_hook.should_retry_patch(enhanced_image): + break + + if retry + 1 == max_retries: + raise Exception("Max retries reached") + else: + print("Detect bad patch, retrying...") + + if cnet_pils is not None: + cnet_pil_list.extend(cnet_pils) + + if enhanced_image is not None: + # don't latent composite-> converting to latent caused poor quality + # use image paste + image = image.cpu() + enhanced_image = enhanced_image.cpu() + utils.tensor_paste(image, enhanced_image, (seg.crop_region[0], seg.crop_region[1]), mask) # this code affecting to `cropped_image`. + enhanced_list.append(enhanced_image) + + if detailer_hook is not None: + image = detailer_hook.post_paste(image) + + if enhanced_image is not None: + # Convert enhanced_pil_alpha to RGBA mode + enhanced_image_alpha = utils.tensor_convert_rgba(enhanced_image) + new_seg_image = enhanced_image.numpy() # alpha should not be applied to seg_image + + # Apply the mask + mask = utils.tensor_resize(mask, *utils.tensor_get_size(enhanced_image)) + utils.tensor_putalpha(enhanced_image_alpha, mask) + enhanced_alpha_list.append(enhanced_image_alpha) + else: + new_seg_image = None + + cropped_list.append(orig_cropped_image) # NOTE: Don't use `cropped_image` + + new_seg = SEG(new_seg_image, seg.cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper) + new_segs.append(new_seg) + + image_tensor = utils.tensor_convert_rgb(image) + + cropped_list.sort(key=lambda x: x.shape, reverse=True) + enhanced_list.sort(key=lambda x: x.shape, reverse=True) + enhanced_alpha_list.sort(key=lambda x: x.shape, reverse=True) + + return image_tensor, cropped_list, enhanced_list, enhanced_alpha_list, cnet_pil_list, (segs[0], new_segs) + + def doit(self, image, segs, model, clip, vae, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, + scheduler, positive, negative, denoise, feather, noise_mask, force_inpaint, wildcard, cycle=1, + detailer_hook=None, inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None, + tiled_encode=False, tiled_decode=False, max_retries=1): + + enhanced_img, *_ = \ + DetailerForEachAutoRetry.do_detail(image, segs, model, clip, vae, guide_size, guide_size_for, max_size, seed, steps, + cfg, sampler_name, scheduler, positive, negative, denoise, feather, noise_mask, + force_inpaint, wildcard, detailer_hook, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, + scheduler_func_opt=scheduler_func_opt, tiled_encode=tiled_encode, tiled_decode=tiled_decode, max_retries=max_retries) + + return (enhanced_img, ) + + +class DetailerForEachPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "image": ("IMAGE", ), + "segs": ("SEGS", ), + "guide_size": ("FLOAT", {"default": 512, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "guide_size_for": ("BOOLEAN", {"default": True, "label_on": "bbox", "label_off": "crop_region"}), + "max_size": ("FLOAT", {"default": 1024, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + "feather": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}), + "noise_mask": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "force_inpaint": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "basic_pipe": ("BASIC_PIPE", {"tooltip": "If the `ImpactDummyInput` is connected to the model in the basic_pipe, the inference stage is skipped."}), + "wildcard": ("STRING", {"multiline": True, "dynamicPrompts": False}), + "refiner_ratio": ("FLOAT", {"default": 0.2, "min": 0.0, "max": 1.0}), + + "cycle": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1}), + }, + "optional": { + "detailer_hook": ("DETAILER_HOOK",), + "refiner_basic_pipe_opt": ("BASIC_PIPE",), + "inpaint_model": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + "tiled_encode": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "tiled_decode": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + } + } + + RETURN_TYPES = ("IMAGE", "SEGS", "BASIC_PIPE", "IMAGE") + RETURN_NAMES = ("image", "segs", "basic_pipe", "cnet_images") + OUTPUT_IS_LIST = (False, False, False, True) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + DESCRIPTION = DetailerForEach.DESCRIPTION + + def doit(self, image, segs, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, scheduler, + denoise, feather, noise_mask, force_inpaint, basic_pipe, wildcard, + refiner_ratio=None, detailer_hook=None, refiner_basic_pipe_opt=None, + cycle=1, inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None, + tiled_encode=False, tiled_decode=False): + + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: DetailerForEach does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + model, clip, vae, positive, negative = basic_pipe + + if refiner_basic_pipe_opt is None: + refiner_model, refiner_clip, refiner_positive, refiner_negative = None, None, None, None + else: + refiner_model, refiner_clip, _, refiner_positive, refiner_negative = refiner_basic_pipe_opt + + enhanced_img, cropped, cropped_enhanced, cropped_enhanced_alpha, cnet_pil_list, new_segs = \ + DetailerForEach.do_detail(image, segs, model, clip, vae, guide_size, guide_size_for, max_size, seed, steps, cfg, + sampler_name, scheduler, positive, negative, denoise, feather, noise_mask, + force_inpaint, wildcard, detailer_hook, + refiner_ratio=refiner_ratio, refiner_model=refiner_model, + refiner_clip=refiner_clip, refiner_positive=refiner_positive, refiner_negative=refiner_negative, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, scheduler_func_opt=scheduler_func_opt, + tiled_encode=tiled_encode, tiled_decode=tiled_decode) + + # set fallback image + if len(cnet_pil_list) == 0: + cnet_pil_list = [utils.empty_pil_tensor()] + + return enhanced_img, new_segs, basic_pipe, cnet_pil_list + + +class FaceDetailer: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "image": ("IMAGE", ), + "model": ("MODEL", {"tooltip": "If the `ImpactDummyInput` is connected to the model, the inference stage is skipped."}), + "clip": ("CLIP",), + "vae": ("VAE",), + "guide_size": ("FLOAT", {"default": 512, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "guide_size_for": ("BOOLEAN", {"default": True, "label_on": "bbox", "label_off": "crop_region"}), + "max_size": ("FLOAT", {"default": 1024, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "positive": ("CONDITIONING",), + "negative": ("CONDITIONING",), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + "feather": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}), + "noise_mask": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "force_inpaint": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + + "bbox_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "bbox_dilation": ("INT", {"default": 10, "min": -512, "max": 512, "step": 1}), + "bbox_crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 10, "step": 0.1}), + + "sam_detection_hint": (["center-1", "horizontal-2", "vertical-2", "rect-4", "diamond-4", "mask-area", "mask-points", "mask-point-bbox", "none"],), + "sam_dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}), + "sam_threshold": ("FLOAT", {"default": 0.93, "min": 0.0, "max": 1.0, "step": 0.01}), + "sam_bbox_expansion": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1}), + "sam_mask_hint_threshold": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}), + "sam_mask_hint_use_negative": (["False", "Small", "Outter"],), + + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + + "bbox_detector": ("BBOX_DETECTOR", ), + "wildcard": ("STRING", {"multiline": True, "dynamicPrompts": False}), + + "cycle": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1}), + }, + "optional": { + "sam_model_opt": ("SAM_MODEL", ), + "segm_detector_opt": ("SEGM_DETECTOR", ), + "detailer_hook": ("DETAILER_HOOK",), + "inpaint_model": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + "tiled_encode": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "tiled_decode": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + }} + + RETURN_TYPES = ("IMAGE", "IMAGE", "IMAGE", "MASK", "DETAILER_PIPE", "IMAGE") + RETURN_NAMES = ("image", "cropped_refined", "cropped_enhanced_alpha", "mask", "detailer_pipe", "cnet_images") + OUTPUT_IS_LIST = (False, True, True, False, False, True) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Simple" + + DESCRIPTION = "This node enhances details by automatically detecting specific objects in the input image using detection models (bbox, segm, sam) and regenerating the image by enlarging the detected area based on the guide size.\nAlthough this node is specialized to simplify the commonly used facial detail enhancement workflow, it can also be used for various automatic inpainting purposes depending on the detection model." + + @staticmethod + def enhance_face(image, model, clip, vae, guide_size, guide_size_for_bbox, max_size, seed, steps, cfg, sampler_name, scheduler, + positive, negative, denoise, feather, noise_mask, force_inpaint, + bbox_threshold, bbox_dilation, bbox_crop_factor, + sam_detection_hint, sam_dilation, sam_threshold, sam_bbox_expansion, sam_mask_hint_threshold, + sam_mask_hint_use_negative, drop_size, + bbox_detector, segm_detector=None, sam_model_opt=None, wildcard_opt=None, detailer_hook=None, + refiner_ratio=None, refiner_model=None, refiner_clip=None, refiner_positive=None, refiner_negative=None, cycle=1, + inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None, tiled_encode=False, tiled_decode=False): + + # make default prompt as 'face' if empty prompt for CLIPSeg + bbox_detector.setAux('face') + segs = bbox_detector.detect(image, bbox_threshold, bbox_dilation, bbox_crop_factor, drop_size, detailer_hook=detailer_hook) + bbox_detector.setAux(None) + + # bbox + sam combination + if sam_model_opt is not None: + sam_mask = core.make_sam_mask(sam_model_opt, segs, image, sam_detection_hint, sam_dilation, + sam_threshold, sam_bbox_expansion, sam_mask_hint_threshold, + sam_mask_hint_use_negative, ) + segs = core.segs_bitwise_and_mask(segs, sam_mask) + + elif segm_detector is not None: + segm_segs = segm_detector.detect(image, bbox_threshold, bbox_dilation, bbox_crop_factor, drop_size) + + if (hasattr(segm_detector, 'override_bbox_by_segm') and segm_detector.override_bbox_by_segm and + not (detailer_hook is not None and not hasattr(detailer_hook, 'override_bbox_by_segm'))): + segs = segm_segs + else: + segm_mask = core.segs_to_combined_mask(segm_segs) + segs = core.segs_bitwise_and_mask(segs, segm_mask) + + if len(segs[1]) > 0: + enhanced_img, _, cropped_enhanced, cropped_enhanced_alpha, cnet_pil_list, new_segs = \ + DetailerForEach.do_detail(image, segs, model, clip, vae, guide_size, guide_size_for_bbox, max_size, seed, steps, cfg, + sampler_name, scheduler, positive, negative, denoise, feather, noise_mask, + force_inpaint, wildcard_opt, detailer_hook, + refiner_ratio=refiner_ratio, refiner_model=refiner_model, + refiner_clip=refiner_clip, refiner_positive=refiner_positive, + refiner_negative=refiner_negative, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, + scheduler_func_opt=scheduler_func_opt, tiled_encode=tiled_encode, tiled_decode=tiled_decode) + else: + enhanced_img = image + cropped_enhanced = [] + cropped_enhanced_alpha = [] + cnet_pil_list = [] + + # Mask Generator + mask = core.segs_to_combined_mask(segs) + + if len(cropped_enhanced) == 0: + cropped_enhanced = [utils.empty_pil_tensor()] + + if len(cropped_enhanced_alpha) == 0: + cropped_enhanced_alpha = [utils.empty_pil_tensor()] + + if len(cnet_pil_list) == 0: + cnet_pil_list = [utils.empty_pil_tensor()] + + return enhanced_img, cropped_enhanced, cropped_enhanced_alpha, mask, cnet_pil_list + + def doit(self, image, model, clip, vae, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, scheduler, + positive, negative, denoise, feather, noise_mask, force_inpaint, + bbox_threshold, bbox_dilation, bbox_crop_factor, + sam_detection_hint, sam_dilation, sam_threshold, sam_bbox_expansion, sam_mask_hint_threshold, + sam_mask_hint_use_negative, drop_size, bbox_detector, wildcard, cycle=1, + sam_model_opt=None, segm_detector_opt=None, detailer_hook=None, inpaint_model=False, noise_mask_feather=0, + scheduler_func_opt=None, tiled_encode=False, tiled_decode=False): + + result_img = None + result_mask = None + result_cropped_enhanced = [] + result_cropped_enhanced_alpha = [] + result_cnet_images = [] + + if len(image) > 1: + logging.warning("[Impact Pack] WARN: FaceDetailer is not a node designed for video detailing. If you intend to perform video detailing, please use Detailer For AnimateDiff.") + + for i, single_image in enumerate(image): + enhanced_img, cropped_enhanced, cropped_enhanced_alpha, mask, cnet_pil_list = FaceDetailer.enhance_face( + single_image.unsqueeze(0), model, clip, vae, guide_size, guide_size_for, max_size, seed + i, steps, cfg, sampler_name, scheduler, + positive, negative, denoise, feather, noise_mask, force_inpaint, + bbox_threshold, bbox_dilation, bbox_crop_factor, + sam_detection_hint, sam_dilation, sam_threshold, sam_bbox_expansion, sam_mask_hint_threshold, + sam_mask_hint_use_negative, drop_size, bbox_detector, segm_detector_opt, sam_model_opt, wildcard, detailer_hook, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, scheduler_func_opt=scheduler_func_opt, + tiled_encode=tiled_encode, tiled_decode=tiled_decode) + + result_img = torch.cat((result_img, enhanced_img), dim=0) if result_img is not None else enhanced_img + result_mask = torch.cat((result_mask, mask), dim=0) if result_mask is not None else mask + result_cropped_enhanced.extend(cropped_enhanced) + result_cropped_enhanced_alpha.extend(cropped_enhanced_alpha) + result_cnet_images.extend(cnet_pil_list) + + pipe = (model, clip, vae, positive, negative, wildcard, bbox_detector, segm_detector_opt, sam_model_opt, detailer_hook, None, None, None, None) + return result_img, result_cropped_enhanced, result_cropped_enhanced_alpha, result_mask, pipe, result_cnet_images + + +class LatentPixelScale: + upscale_methods = ["nearest-exact", "bilinear", "lanczos", "area"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "samples": ("LATENT", ), + "scale_method": (s.upscale_methods,), + "scale_factor": ("FLOAT", {"default": 1.5, "min": 0.1, "max": 10000, "step": 0.05}), + "vae": ("VAE", ), + "use_tiled_vae": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + }, + "optional": { + "upscale_model_opt": ("UPSCALE_MODEL", ), + } + } + + RETURN_TYPES = ("LATENT", "IMAGE") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, samples, scale_method, scale_factor, vae, use_tiled_vae, upscale_model_opt=None): + if upscale_model_opt is None: + latimg = core.latent_upscale_on_pixel_space2(samples, scale_method, scale_factor, vae, use_tile=use_tiled_vae) + else: + latimg = core.latent_upscale_on_pixel_space_with_model2(samples, scale_method, upscale_model_opt, scale_factor, vae, use_tile=use_tiled_vae) + return latimg + + +class NoiseInjectionDetailerHookProvider: + schedules = ["skip_start", "from_start"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "schedule_for_cycle": (s.schedules,), + "source": (["CPU", "GPU"],), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "start_strength": ("FLOAT", {"default": 2.0, "min": 0.0, "max": 200.0, "step": 0.01}), + "end_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 200.0, "step": 0.01}), + }, + } + + RETURN_TYPES = ("DETAILER_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + def doit(self, schedule_for_cycle, source, seed, start_strength, end_strength): + try: + hook = hooks.InjectNoiseHookForDetailer(source, seed, start_strength, end_strength, + from_start=('from_start' in schedule_for_cycle)) + return (hook, ) + except Exception as e: + logging.error(f"[Impact Pack] NoiseInjectionDetailerHookProvider: 'ComfyUI Noise' custom node isn't installed. You must install 'BlenderNeko/ComfyUI Noise' extension to use this node.\t{e}") + + +# class CustomNoiseDetailerHookProvider: +# @classmethod +# def INPUT_TYPES(s): +# return {"required": { +# "noise": ("NOISE",)}, +# } +# +# RETURN_TYPES = ("DETAILER_HOOK",) +# FUNCTION = "doit" +# +# CATEGORY = "ImpactPack/Detailer" +# +# def doit(self, noise): +# hook = hooks.CustomNoiseDetailerHookProvider(noise) +# return (hook, ) + + +class VariationNoiseDetailerHookProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "strength": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01})} + } + + RETURN_TYPES = ("DETAILER_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + def doit(self, seed, strength): + hook = hooks.VariationNoiseDetailerHookProvider(seed, strength) + return (hook, ) + + +class UnsamplerDetailerHookProvider: + schedules = ["skip_start", "from_start"] + + @classmethod + def INPUT_TYPES(s): + return {"required": + {"model": ("MODEL",), + "steps": ("INT", {"default": 25, "min": 1, "max": 10000}), + "start_end_at_step": ("INT", {"default": 21, "min": 0, "max": 10000}), + "end_end_at_step": ("INT", {"default": 24, "min": 0, "max": 10000}), + "cfg": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, ), + "scheduler": (comfy.samplers.KSampler.SCHEDULERS, ), + "normalize": (["disable", "enable"], ), + "positive": ("CONDITIONING", ), + "negative": ("CONDITIONING", ), + "schedule_for_cycle": (s.schedules,), + }} + + RETURN_TYPES = ("DETAILER_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + def doit(self, model, steps, start_end_at_step, end_end_at_step, cfg, sampler_name, + scheduler, normalize, positive, negative, schedule_for_cycle): + try: + hook = hooks.UnsamplerDetailerHook(model, steps, start_end_at_step, end_end_at_step, cfg, sampler_name, + scheduler, normalize, positive, negative, + from_start=('from_start' in schedule_for_cycle)) + + return (hook, ) + except Exception as e: + logging.error(f"[Impact Pack] UnsamplerDetailerHookProvider: 'ComfyUI Noise' custom node isn't installed. You must install 'BlenderNeko/ComfyUI Noise' extension to use this node.\t{e}") + pass + + +class DenoiseSchedulerDetailerHookProvider: + schedules = ["simple"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "schedule_for_cycle": (s.schedules,), + "target_denoise": ("FLOAT", {"default": 0.3, "min": 0.0, "max": 1.0, "step": 0.01}), + }, + } + + RETURN_TYPES = ("DETAILER_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + def doit(self, schedule_for_cycle, target_denoise): + hook = hooks.SimpleDetailerDenoiseSchedulerHook(target_denoise) + return (hook, ) + + +class CoreMLDetailerHookProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": {"mode": (["512x512", "768x768", "512x768", "768x512"], )}, } + + RETURN_TYPES = ("DETAILER_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + def doit(self, mode): + hook = hooks.CoreMLHook(mode) + return (hook, ) + + +class CustomSamplerDetailerHookProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "sampler": ("SAMPLER", ), + }, + } + + RETURN_TYPES = ("DETAILER_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + DESCRIPTION = "Apply a hook that allows you to use a custom sampler in the Detailer nodes. When using `DetailerHookCombine`, the sampler from the first hook is applied." + + def doit(self, sampler): + hook = hooks.CustomSamplerDetailerHookProvider(sampler) + return (hook, ) + + +class CfgScheduleHookProvider: + schedules = ["simple"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "schedule_for_iteration": (s.schedules,), + "target_cfg": ("FLOAT", {"default": 3.0, "min": 0.0, "max": 100.0}), + }, + } + + RETURN_TYPES = ("PK_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, schedule_for_iteration, target_cfg): + hook = None + if schedule_for_iteration == "simple": + hook = hooks.SimpleCfgScheduleHook(target_cfg) + + return (hook, ) + + +class UnsamplerHookProvider: + schedules = ["simple"] + + @classmethod + def INPUT_TYPES(s): + return {"required": + {"model": ("MODEL",), + "steps": ("INT", {"default": 25, "min": 1, "max": 10000}), + "start_end_at_step": ("INT", {"default": 21, "min": 0, "max": 10000}), + "end_end_at_step": ("INT", {"default": 24, "min": 0, "max": 10000}), + "cfg": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, ), + "scheduler": (comfy.samplers.KSampler.SCHEDULERS, ), + "normalize": (["disable", "enable"], ), + "positive": ("CONDITIONING", ), + "negative": ("CONDITIONING", ), + "schedule_for_iteration": (s.schedules,), + }} + + RETURN_TYPES = ("PK_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, model, steps, start_end_at_step, end_end_at_step, cfg, sampler_name, + scheduler, normalize, positive, negative, schedule_for_iteration): + try: + hook = None + if schedule_for_iteration == "simple": + hook = hooks.UnsamplerHook(model, steps, start_end_at_step, end_end_at_step, cfg, sampler_name, + scheduler, normalize, positive, negative) + + return (hook, ) + except Exception as e: + logging.error(f"[Impact Pack] UnsamplerHookProvider: 'ComfyUI Noise' custom node isn't installed. You must install 'BlenderNeko/ComfyUI Noise' extension to use this node.\t{e}") + + +class NoiseInjectionHookProvider: + schedules = ["simple"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "schedule_for_iteration": (s.schedules,), + "source": (["CPU", "GPU"],), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "start_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 200.0, "step": 0.01}), + "end_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 200.0, "step": 0.01}), + }, + } + + RETURN_TYPES = ("PK_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, schedule_for_iteration, source, seed, start_strength, end_strength): + try: + hook = None + if schedule_for_iteration == "simple": + hook = hooks.InjectNoiseHook(source, seed, start_strength, end_strength) + + return (hook, ) + except Exception as e: + logging.error(f"[Impact Pack] NoiseInjectionHookProvider: 'ComfyUI Noise' custom node isn't installed. You must install 'BlenderNeko/ComfyUI Noise' extension to use this node.\t{e}") + + +class DenoiseScheduleHookProvider: + schedules = ["simple"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "schedule_for_iteration": (s.schedules,), + "target_denoise": ("FLOAT", {"default": 0.2, "min": 0.0, "max": 1.0, "step": 0.01}), + }, + } + + RETURN_TYPES = ("PK_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, schedule_for_iteration, target_denoise): + hook = None + if schedule_for_iteration == "simple": + hook = hooks.SimpleDenoiseScheduleHook(target_denoise) + + return (hook, ) + + +class StepsScheduleHookProvider: + schedules = ["simple"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "schedule_for_iteration": (s.schedules,), + "target_steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + }, + } + + RETURN_TYPES = ("PK_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, schedule_for_iteration, target_steps): + hook = None + if schedule_for_iteration == "simple": + hook = hooks.SimpleStepsScheduleHook(target_steps) + + return (hook, ) + + +class DetailerHookCombine: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "hook1": ("DETAILER_HOOK",), + "hook2": ("DETAILER_HOOK",), + }, + } + + RETURN_TYPES = ("DETAILER_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, hook1, hook2): + hook = hooks.DetailerHookCombine(hook1, hook2) + return (hook, ) + + +class PixelKSampleHookCombine: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "hook1": ("PK_HOOK",), + "hook2": ("PK_HOOK",), + }, + } + + RETURN_TYPES = ("PK_HOOK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, hook1, hook2): + hook = hooks.PixelKSampleHookCombine(hook1, hook2) + return (hook, ) + + +class PixelTiledKSampleUpscalerProvider: + upscale_methods = ["nearest-exact", "bilinear", "lanczos", "area"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "scale_method": (s.upscale_methods,), + "model": ("MODEL",), + "vae": ("VAE",), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, ), + "scheduler": (comfy.samplers.KSampler.SCHEDULERS, ), + "positive": ("CONDITIONING", ), + "negative": ("CONDITIONING", ), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), + "tile_width": ("INT", {"default": 512, "min": 320, "max": MAX_RESOLUTION, "step": 64}), + "tile_height": ("INT", {"default": 512, "min": 320, "max": MAX_RESOLUTION, "step": 64}), + "tiling_strategy": (["random", "padded", 'simple'], ), + }, + "optional": { + "upscale_model_opt": ("UPSCALE_MODEL", ), + "pk_hook_opt": ("PK_HOOK", ), + "tile_cnet_opt": ("CONTROL_NET", ), + "tile_cnet_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), + "overlap": ("INT", {"default": 64, "min": 0, "max": 4096, "step": 32}), + } + } + + RETURN_TYPES = ("UPSCALER",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, tile_width, tile_height, tiling_strategy, upscale_model_opt=None, + pk_hook_opt=None, tile_cnet_opt=None, tile_cnet_strength=1.0, overlap=64): + if "BNK_TiledKSampler" in nodes.NODE_CLASS_MAPPINGS: + upscaler = core.PixelTiledKSampleUpscaler(scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, + tile_width, tile_height, tiling_strategy, upscale_model_opt, pk_hook_opt, tile_cnet_opt, + tile_size=max(tile_width, tile_height), tile_cnet_strength=tile_cnet_strength, overlap=overlap) + return (upscaler, ) + else: + utils.try_install_custom_node('https://github.com/BlenderNeko/ComfyUI_TiledKSampler', + "To use 'PixelTiledKSampleUpscalerProvider' node, 'BlenderNeko/ComfyUI_TiledKSampler' extension is required.") + + raise Exception("[ERROR] PixelTiledKSampleUpscalerProvider: ComfyUI_TiledKSampler custom node isn't installed. You must install BlenderNeko/ComfyUI_TiledKSampler extension to use this node.") + + +class PixelTiledKSampleUpscalerProviderPipe: + upscale_methods = ["nearest-exact", "bilinear", "lanczos", "area"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "scale_method": (s.upscale_methods,), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, ), + "scheduler": (comfy.samplers.KSampler.SCHEDULERS, ), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), + "tile_width": ("INT", {"default": 512, "min": 320, "max": MAX_RESOLUTION, "step": 64}), + "tile_height": ("INT", {"default": 512, "min": 320, "max": MAX_RESOLUTION, "step": 64}), + "tiling_strategy": (["random", "padded", 'simple'], ), + "basic_pipe": ("BASIC_PIPE",) + }, + "optional": { + "upscale_model_opt": ("UPSCALE_MODEL", ), + "pk_hook_opt": ("PK_HOOK", ), + "tile_cnet_opt": ("CONTROL_NET", ), + "tile_cnet_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), + } + } + + RETURN_TYPES = ("UPSCALER",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, scale_method, seed, steps, cfg, sampler_name, scheduler, denoise, tile_width, tile_height, tiling_strategy, basic_pipe, upscale_model_opt=None, pk_hook_opt=None, + tile_cnet_opt=None, tile_cnet_strength=1.0): + if "BNK_TiledKSampler" in nodes.NODE_CLASS_MAPPINGS: + model, _, vae, positive, negative = basic_pipe + upscaler = core.PixelTiledKSampleUpscaler(scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, + tile_width, tile_height, tiling_strategy, upscale_model_opt, pk_hook_opt, tile_cnet_opt, + tile_size=max(tile_width, tile_height), tile_cnet_strength=tile_cnet_strength) + return (upscaler, ) + else: + logging.error("[Impact Pack] PixelTiledKSampleUpscalerProviderPipe: ComfyUI_TiledKSampler custom node isn't installed. You must install BlenderNeko/ComfyUI_TiledKSampler extension to use this node.") + raise Exception("[Impact Pack] PixelTiledKSampleUpscalerProviderPipe: ComfyUI_TiledKSampler custom node isn't installed. You must install BlenderNeko/ComfyUI_TiledKSampler extension to use this node.") + + +class PixelKSampleUpscalerProvider: + upscale_methods = ["nearest-exact", "bilinear", "lanczos", "area"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "scale_method": (s.upscale_methods,), + "model": ("MODEL",), + "vae": ("VAE",), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, ), + "scheduler": (core.SCHEDULERS, ), + "positive": ("CONDITIONING", ), + "negative": ("CONDITIONING", ), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), + "use_tiled_vae": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "tile_size": ("INT", {"default": 512, "min": 320, "max": 4096, "step": 64}), + }, + "optional": { + "upscale_model_opt": ("UPSCALE_MODEL", ), + "pk_hook_opt": ("PK_HOOK", ), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + } + } + + RETURN_TYPES = ("UPSCALER",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, + use_tiled_vae, upscale_model_opt=None, pk_hook_opt=None, tile_size=512, scheduler_func_opt=None): + upscaler = core.PixelKSampleUpscaler(scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, + positive, negative, denoise, use_tiled_vae, upscale_model_opt, pk_hook_opt, + tile_size=tile_size, scheduler_func=scheduler_func_opt) + return (upscaler, ) + + +class PixelKSampleUpscalerProviderPipe(PixelKSampleUpscalerProvider): + upscale_methods = ["nearest-exact", "bilinear", "lanczos", "area"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "scale_method": (s.upscale_methods,), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, ), + "scheduler": (core.SCHEDULERS, ), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), + "use_tiled_vae": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "basic_pipe": ("BASIC_PIPE",), + "tile_size": ("INT", {"default": 512, "min": 320, "max": 4096, "step": 64}), + }, + "optional": { + "upscale_model_opt": ("UPSCALE_MODEL", ), + "pk_hook_opt": ("PK_HOOK", ), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + "tile_cnet_opt": ("CONTROL_NET", ), + "tile_cnet_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), + } + } + + RETURN_TYPES = ("UPSCALER",) + FUNCTION = "doit_pipe" + + CATEGORY = "ImpactPack/Upscale" + + def doit_pipe(self, scale_method, seed, steps, cfg, sampler_name, scheduler, denoise, + use_tiled_vae, basic_pipe, upscale_model_opt=None, pk_hook_opt=None, + tile_size=512, scheduler_func_opt=None, tile_cnet_opt=None, tile_cnet_strength=1.0): + model, _, vae, positive, negative = basic_pipe + upscaler = core.PixelKSampleUpscaler(scale_method, model, vae, seed, steps, cfg, sampler_name, scheduler, + positive, negative, denoise, use_tiled_vae, upscale_model_opt, pk_hook_opt, + tile_size=tile_size, scheduler_func=scheduler_func_opt, + tile_cnet_opt=tile_cnet_opt, tile_cnet_strength=tile_cnet_strength) + return (upscaler, ) + + +class TwoSamplersForMaskUpscalerProvider: + upscale_methods = ["nearest-exact", "bilinear", "lanczos", "area"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "scale_method": (s.upscale_methods,), + "full_sample_schedule": ( + ["none", "interleave1", "interleave2", "interleave3", + "last1", "last2", + "interleave1+last1", "interleave2+last1", "interleave3+last1", + ],), + "use_tiled_vae": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "base_sampler": ("KSAMPLER", ), + "mask_sampler": ("KSAMPLER", ), + "mask": ("MASK", ), + "vae": ("VAE",), + "tile_size": ("INT", {"default": 512, "min": 320, "max": 4096, "step": 64}), + }, + "optional": { + "full_sampler_opt": ("KSAMPLER",), + "upscale_model_opt": ("UPSCALE_MODEL", ), + "pk_hook_base_opt": ("PK_HOOK", ), + "pk_hook_mask_opt": ("PK_HOOK", ), + "pk_hook_full_opt": ("PK_HOOK", ), + } + } + + RETURN_TYPES = ("UPSCALER", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, scale_method, full_sample_schedule, use_tiled_vae, base_sampler, mask_sampler, mask, vae, + full_sampler_opt=None, upscale_model_opt=None, + pk_hook_base_opt=None, pk_hook_mask_opt=None, pk_hook_full_opt=None, tile_size=512): + upscaler = core.TwoSamplersForMaskUpscaler(scale_method, full_sample_schedule, use_tiled_vae, + base_sampler, mask_sampler, mask, vae, full_sampler_opt, upscale_model_opt, + pk_hook_base_opt, pk_hook_mask_opt, pk_hook_full_opt, tile_size=tile_size) + return (upscaler, ) + + +class TwoSamplersForMaskUpscalerProviderPipe: + upscale_methods = ["nearest-exact", "bilinear", "lanczos", "area"] + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "scale_method": (s.upscale_methods,), + "full_sample_schedule": ( + ["none", "interleave1", "interleave2", "interleave3", + "last1", "last2", + "interleave1+last1", "interleave2+last1", "interleave3+last1", + ],), + "use_tiled_vae": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "base_sampler": ("KSAMPLER", ), + "mask_sampler": ("KSAMPLER", ), + "mask": ("MASK", ), + "basic_pipe": ("BASIC_PIPE",), + "tile_size": ("INT", {"default": 512, "min": 320, "max": 4096, "step": 64}), + }, + "optional": { + "full_sampler_opt": ("KSAMPLER",), + "upscale_model_opt": ("UPSCALE_MODEL", ), + "pk_hook_base_opt": ("PK_HOOK", ), + "pk_hook_mask_opt": ("PK_HOOK", ), + "pk_hook_full_opt": ("PK_HOOK", ), + } + } + + RETURN_TYPES = ("UPSCALER", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, scale_method, full_sample_schedule, use_tiled_vae, base_sampler, mask_sampler, mask, basic_pipe, + full_sampler_opt=None, upscale_model_opt=None, + pk_hook_base_opt=None, pk_hook_mask_opt=None, pk_hook_full_opt=None, tile_size=512): + + mask = utils.make_2d_mask(mask) + + _, _, vae, _, _ = basic_pipe + upscaler = core.TwoSamplersForMaskUpscaler(scale_method, full_sample_schedule, use_tiled_vae, + base_sampler, mask_sampler, mask, vae, full_sampler_opt, upscale_model_opt, + pk_hook_base_opt, pk_hook_mask_opt, pk_hook_full_opt, tile_size=tile_size) + return (upscaler, ) + + +class IterativeLatentUpscale: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "samples": ("LATENT", ), + "upscale_factor": ("FLOAT", {"default": 1.5, "min": 1, "max": 10000, "step": 0.1}), + "steps": ("INT", {"default": 3, "min": 1, "max": 10000, "step": 1}), + "temp_prefix": ("STRING", {"default": ""}), + "upscaler": ("UPSCALER",), + "step_mode": (["simple", "geometric"], {"default": "simple"}) + }, + "hidden": {"unique_id": "UNIQUE_ID"}, + } + + RETURN_TYPES = ("LATENT", "VAE") + RETURN_NAMES = ("latent", "vae") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, samples, upscale_factor, steps, temp_prefix, upscaler, step_mode="simple", unique_id=None): + w = samples['samples'].shape[3]*8 # image width + h = samples['samples'].shape[2]*8 # image height + + if temp_prefix == "": + temp_prefix = None + + if step_mode == "geometric": + upscale_factor_unit = pow(upscale_factor, 1.0/steps) + else: # simple + upscale_factor_unit = max(0, (upscale_factor - 1.0) / steps) + + current_latent = samples + noise_mask = current_latent.get('noise_mask') + scale = 1 + + for i in range(steps-1): + if step_mode == "geometric": + scale *= upscale_factor_unit + else: # simple + scale += upscale_factor_unit + + new_w = w*scale + new_h = h*scale + core.update_node_status(unique_id, f"{i+1}/{steps} steps | x{scale:.2f}", (i+1)/steps) + logging.info(f"IterativeLatentUpscale[{i+1}/{steps}]: {new_w:.1f}x{new_h:.1f} (scale:{scale:.2f}) ") + step_info = i, steps + current_latent = upscaler.upscale_shape(step_info, current_latent, new_w, new_h, temp_prefix) + if noise_mask is not None: + current_latent['noise_mask'] = noise_mask + + if scale < upscale_factor: + new_w = w*upscale_factor + new_h = h*upscale_factor + core.update_node_status(unique_id, f"Final step | x{upscale_factor:.2f}", 1.0) + logging.info(f"IterativeLatentUpscale[Final]: {new_w:.1f}x{new_h:.1f} (scale:{upscale_factor:.2f}) ") + step_info = steps-1, steps + current_latent = upscaler.upscale_shape(step_info, current_latent, new_w, new_h, temp_prefix) + + core.update_node_status(unique_id, "", None) + + return current_latent, upscaler.vae + + +class IterativeImageUpscale: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "pixels": ("IMAGE", ), + "upscale_factor": ("FLOAT", {"default": 1.5, "min": 1, "max": 10000, "step": 0.1}), + "steps": ("INT", {"default": 3, "min": 1, "max": 10000, "step": 1}), + "temp_prefix": ("STRING", {"default": ""}), + "upscaler": ("UPSCALER",), + "vae": ("VAE",), + "step_mode": (["simple", "geometric"], {"default": "simple"}) + }, + "hidden": {"unique_id": "UNIQUE_ID"} + } + + RETURN_TYPES = ("IMAGE",) + RETURN_NAMES = ("image",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + def doit(self, pixels, upscale_factor, steps, temp_prefix, upscaler, vae, step_mode="simple", unique_id=None): + if temp_prefix == "": + temp_prefix = None + + core.update_node_status(unique_id, "VAEEncode (first)", 0) + if upscaler.is_tiled: + encoder = nodes.VAEEncodeTiled() + if 'overlap' in inspect.signature(encoder.encode).parameters: + latent = encoder.encode(vae, pixels, upscaler.tile_size, overlap=upscaler.overlap)[0] + else: + latent = encoder.encode(vae, pixels, upscaler.tile_size)[0] + else: + latent = nodes.VAEEncode().encode(vae, pixels)[0] + + refined_latent = IterativeLatentUpscale().doit(latent, upscale_factor, steps, temp_prefix, upscaler, step_mode, unique_id) + + core.update_node_status(unique_id, "VAEDecode (final)", 1.0) + if upscaler.is_tiled: + pixels = nodes.VAEDecodeTiled().decode(vae, refined_latent[0], upscaler.tile_size)[0] + else: + pixels = nodes.VAEDecode().decode(vae, refined_latent[0])[0] + + core.update_node_status(unique_id, "", None) + + return (pixels, ) + + +class FaceDetailerPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "image": ("IMAGE", ), + "detailer_pipe": ("DETAILER_PIPE", {"tooltip": "If the `ImpactDummyInput` is connected to the model in the detailer_pipe, the inference stage is skipped."}), + "guide_size": ("FLOAT", {"default": 512, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "guide_size_for": ("BOOLEAN", {"default": True, "label_on": "bbox", "label_off": "crop_region"}), + "max_size": ("FLOAT", {"default": 1024, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + "feather": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}), + "noise_mask": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "force_inpaint": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + + "bbox_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + "bbox_dilation": ("INT", {"default": 10, "min": -512, "max": 512, "step": 1}), + "bbox_crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 10, "step": 0.1}), + + "sam_detection_hint": (["center-1", "horizontal-2", "vertical-2", "rect-4", "diamond-4", "mask-area", "mask-points", "mask-point-bbox", "none"],), + "sam_dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}), + "sam_threshold": ("FLOAT", {"default": 0.93, "min": 0.0, "max": 1.0, "step": 0.01}), + "sam_bbox_expansion": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1}), + "sam_mask_hint_threshold": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}), + "sam_mask_hint_use_negative": (["False", "Small", "Outter"],), + + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + "refiner_ratio": ("FLOAT", {"default": 0.2, "min": 0.0, "max": 1.0}), + + "cycle": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1}), + }, + "optional": { + "inpaint_model": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + "tiled_encode": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "tiled_decode": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + } + } + + RETURN_TYPES = ("IMAGE", "IMAGE", "IMAGE", "MASK", "DETAILER_PIPE", "IMAGE") + RETURN_NAMES = ("image", "cropped_refined", "cropped_enhanced_alpha", "mask", "detailer_pipe", "cnet_images") + OUTPUT_IS_LIST = (False, True, True, False, False, True) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Simple" + + DESCRIPTION = FaceDetailer.DESCRIPTION + + def doit(self, image, detailer_pipe, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, scheduler, + denoise, feather, noise_mask, force_inpaint, bbox_threshold, bbox_dilation, bbox_crop_factor, + sam_detection_hint, sam_dilation, sam_threshold, sam_bbox_expansion, + sam_mask_hint_threshold, sam_mask_hint_use_negative, drop_size, refiner_ratio=None, + cycle=1, inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None, + tiled_encode=False, tiled_decode=False): + + result_img = None + result_mask = None + result_cropped_enhanced = [] + result_cropped_enhanced_alpha = [] + result_cnet_images = [] + + if len(image) > 1: + logging.warning("[Impact Pack] WARN: FaceDetailer is not a node designed for video detailing. If you intend to perform video detailing, please use Detailer For AnimateDiff.") + + model, clip, vae, positive, negative, wildcard, bbox_detector, segm_detector, sam_model_opt, detailer_hook, \ + refiner_model, refiner_clip, refiner_positive, refiner_negative = detailer_pipe + + for i, single_image in enumerate(image): + enhanced_img, cropped_enhanced, cropped_enhanced_alpha, mask, cnet_pil_list = FaceDetailer.enhance_face( + single_image.unsqueeze(0), model, clip, vae, guide_size, guide_size_for, max_size, seed + i, steps, cfg, sampler_name, scheduler, + positive, negative, denoise, feather, noise_mask, force_inpaint, + bbox_threshold, bbox_dilation, bbox_crop_factor, + sam_detection_hint, sam_dilation, sam_threshold, sam_bbox_expansion, sam_mask_hint_threshold, + sam_mask_hint_use_negative, drop_size, bbox_detector, segm_detector, sam_model_opt, wildcard, detailer_hook, + refiner_ratio=refiner_ratio, refiner_model=refiner_model, + refiner_clip=refiner_clip, refiner_positive=refiner_positive, refiner_negative=refiner_negative, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, scheduler_func_opt=scheduler_func_opt, + tiled_encode=tiled_encode, tiled_decode=tiled_decode) + + result_img = torch.cat((result_img, enhanced_img), dim=0) if result_img is not None else enhanced_img + result_mask = torch.cat((result_mask, mask), dim=0) if result_mask is not None else mask + result_cropped_enhanced.extend(cropped_enhanced) + result_cropped_enhanced_alpha.extend(cropped_enhanced_alpha) + result_cnet_images.extend(cnet_pil_list) + + if len(result_cropped_enhanced) == 0: + result_cropped_enhanced = [utils.empty_pil_tensor()] + + if len(result_cropped_enhanced_alpha) == 0: + result_cropped_enhanced_alpha = [utils.empty_pil_tensor()] + + if len(result_cnet_images) == 0: + result_cnet_images = [utils.empty_pil_tensor()] + + return result_img, result_cropped_enhanced, result_cropped_enhanced_alpha, result_mask, detailer_pipe, result_cnet_images + + +class MaskDetailerPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "image": ("IMAGE", ), + "mask": ("MASK", ), + "basic_pipe": ("BASIC_PIPE",), + + "guide_size": ("FLOAT", {"default": 512, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "guide_size_for": ("BOOLEAN", {"default": True, "label_on": "mask bbox", "label_off": "crop region"}), + "max_size": ("FLOAT", {"default": 1024, "min": 64, "max": nodes.MAX_RESOLUTION, "step": 8}), + "mask_mode": ("BOOLEAN", {"default": True, "label_on": "masked only", "label_off": "whole"}), + + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + + "feather": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}), + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 10, "step": 0.1}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + "refiner_ratio": ("FLOAT", {"default": 0.2, "min": 0.0, "max": 1.0}), + "batch_size": ("INT", {"default": 1, "min": 1, "max": 100}), + + "cycle": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1}), + }, + "optional": { + "refiner_basic_pipe_opt": ("BASIC_PIPE", ), + "detailer_hook": ("DETAILER_HOOK",), + "inpaint_model": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + "bbox_fill": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "contour_fill": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + } + } + + RETURN_TYPES = ("IMAGE", "IMAGE", "IMAGE", "BASIC_PIPE", "BASIC_PIPE") + RETURN_NAMES = ("image", "cropped_refined", "cropped_enhanced_alpha", "basic_pipe", "refiner_basic_pipe_opt") + OUTPUT_IS_LIST = (False, True, True, False, False) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + DESCRIPTION = "" + + def doit(self, image, mask, basic_pipe, guide_size, guide_size_for, max_size, mask_mode, + seed, steps, cfg, sampler_name, scheduler, denoise, + feather, crop_factor, drop_size, refiner_ratio, batch_size, cycle=1, + refiner_basic_pipe_opt=None, detailer_hook=None, inpaint_model=False, noise_mask_feather=0, + bbox_fill=False, contour_fill=True, scheduler_func_opt=None): + + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: MaskDetailer does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + model, clip, vae, positive, negative = basic_pipe + + if refiner_basic_pipe_opt is None: + refiner_model, refiner_clip, refiner_positive, refiner_negative = None, None, None, None + else: + refiner_model, refiner_clip, _, refiner_positive, refiner_negative = refiner_basic_pipe_opt + + # create segs + if mask is not None: + mask = utils.make_2d_mask(mask) + segs = core.mask_to_segs(mask, False, crop_factor, bbox_fill, drop_size, is_contour=contour_fill) + else: + segs = ((image.shape[1], image.shape[2]), []) + + enhanced_img_batch = None + cropped_enhanced_list = [] + cropped_enhanced_alpha_list = [] + + for i in range(batch_size): + if mask is not None: + enhanced_img, _, cropped_enhanced, cropped_enhanced_alpha, _, _ = \ + DetailerForEach.do_detail(image, segs, model, clip, vae, guide_size, guide_size_for, max_size, seed+i, steps, + cfg, sampler_name, scheduler, positive, negative, denoise, feather, mask_mode, + force_inpaint=True, wildcard_opt=None, detailer_hook=detailer_hook, + refiner_ratio=refiner_ratio, refiner_model=refiner_model, refiner_clip=refiner_clip, + refiner_positive=refiner_positive, refiner_negative=refiner_negative, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, scheduler_func_opt=scheduler_func_opt) + else: + enhanced_img, cropped_enhanced, cropped_enhanced_alpha = image, [], [] + + if enhanced_img_batch is None: + enhanced_img_batch = enhanced_img + else: + enhanced_img_batch = torch.cat((enhanced_img_batch, enhanced_img), dim=0) + + cropped_enhanced_list += cropped_enhanced + cropped_enhanced_alpha_list += cropped_enhanced_alpha + + # set fallback image + if len(cropped_enhanced_list) == 0: + cropped_enhanced_list = [utils.empty_pil_tensor()] + + if len(cropped_enhanced_alpha_list) == 0: + cropped_enhanced_alpha_list = [utils.empty_pil_tensor()] + + return enhanced_img_batch, cropped_enhanced_list, cropped_enhanced_alpha_list, basic_pipe, refiner_basic_pipe_opt + + +class DetailerForEachTest(DetailerForEach): + RETURN_TYPES = ("IMAGE", "IMAGE", "IMAGE", "IMAGE", "IMAGE") + RETURN_NAMES = ("image", "cropped", "cropped_refined", "cropped_refined_alpha", "cnet_images") + OUTPUT_IS_LIST = (False, True, True, True, True) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + def doit(self, image, segs, model, clip, vae, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, + scheduler, positive, negative, denoise, feather, noise_mask, force_inpaint, wildcard, detailer_hook=None, + cycle=1, inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None, tiled_encode=False, tiled_decode=False): + + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: DetailerForEach does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + enhanced_img, cropped, cropped_enhanced, cropped_enhanced_alpha, cnet_pil_list, new_segs = \ + DetailerForEach.do_detail(image, segs, model, clip, vae, guide_size, guide_size_for, max_size, seed, steps, + cfg, sampler_name, scheduler, positive, negative, denoise, feather, noise_mask, + force_inpaint, wildcard, detailer_hook, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, + scheduler_func_opt=scheduler_func_opt, tiled_encode=tiled_encode, tiled_decode=tiled_decode) + + # set fallback image + if len(cropped) == 0: + cropped = [utils.empty_pil_tensor()] + + if len(cropped_enhanced) == 0: + cropped_enhanced = [utils.empty_pil_tensor()] + + if len(cropped_enhanced_alpha) == 0: + cropped_enhanced_alpha = [utils.empty_pil_tensor()] + + if len(cnet_pil_list) == 0: + cnet_pil_list = [utils.empty_pil_tensor()] + + return enhanced_img, cropped, cropped_enhanced, cropped_enhanced_alpha, cnet_pil_list + + +class DetailerForEachTestPipe(DetailerForEachPipe): + RETURN_TYPES = ("IMAGE", "SEGS", "BASIC_PIPE", "IMAGE", "IMAGE", "IMAGE", "IMAGE", ) + RETURN_NAMES = ("image", "segs", "basic_pipe", "cropped", "cropped_refined", "cropped_refined_alpha", 'cnet_images') + OUTPUT_IS_LIST = (False, False, False, True, True, True, True) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + DESCRIPTION = DetailerForEach.DESCRIPTION + + def doit(self, image, segs, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, scheduler, + denoise, feather, noise_mask, force_inpaint, basic_pipe, wildcard, cycle=1, + refiner_ratio=None, detailer_hook=None, refiner_basic_pipe_opt=None, inpaint_model=False, noise_mask_feather=0, + scheduler_func_opt=None, tiled_encode=False, tiled_decode=False): + + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: DetailerForEach does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + model, clip, vae, positive, negative = basic_pipe + + if refiner_basic_pipe_opt is None: + refiner_model, refiner_clip, refiner_positive, refiner_negative = None, None, None, None + else: + refiner_model, refiner_clip, _, refiner_positive, refiner_negative = refiner_basic_pipe_opt + + enhanced_img, cropped, cropped_enhanced, cropped_enhanced_alpha, cnet_pil_list, new_segs = \ + DetailerForEach.do_detail(image, segs, model, clip, vae, guide_size, guide_size_for, max_size, seed, steps, cfg, + sampler_name, scheduler, positive, negative, denoise, feather, noise_mask, + force_inpaint, wildcard, detailer_hook, + refiner_ratio=refiner_ratio, refiner_model=refiner_model, + refiner_clip=refiner_clip, refiner_positive=refiner_positive, + refiner_negative=refiner_negative, + cycle=cycle, inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, + scheduler_func_opt=scheduler_func_opt, tiled_encode=tiled_encode, tiled_decode=tiled_decode) + + # set fallback image + if len(cropped) == 0: + cropped = [utils.empty_pil_tensor()] + + if len(cropped_enhanced) == 0: + cropped_enhanced = [utils.empty_pil_tensor()] + + if len(cropped_enhanced_alpha) == 0: + cropped_enhanced_alpha = [utils.empty_pil_tensor()] + + if len(cnet_pil_list) == 0: + cnet_pil_list = [utils.empty_pil_tensor()] + + return enhanced_img, new_segs, basic_pipe, cropped, cropped_enhanced, cropped_enhanced_alpha, cnet_pil_list + + +class SegsBitwiseAndMask: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS",), + "mask": ("MASK",), + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, segs, mask): + return (core.segs_bitwise_and_mask(segs, mask), ) + + +class SegsBitwiseAndMaskForEach: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS",), + "masks": ("MASK",), + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, segs, masks): + return (core.apply_mask_to_each_seg(segs, masks), ) + + +class BitwiseAndMaskForEach: + @classmethod + def INPUT_TYPES(s): + return {"required": + { + "base_segs": ("SEGS",), + "mask_segs": ("SEGS",), + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + DESCRIPTION = "Retains only the overlapping areas between the masks included in base_segs and the mask regions of mask_segs. SEGS with no overlapping mask areas are filtered out." + + def doit(self, base_segs, mask_segs): + mask = core.segs_to_combined_mask(mask_segs) + mask = utils.make_3d_mask(mask) + + return SegsBitwiseAndMask().doit(base_segs, mask) + + +class SubtractMaskForEach: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "base_segs": ("SEGS",), + "mask_segs": ("SEGS",), + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + DESCRIPTION = "Removes only the overlapping areas between the masks included in base_segs and the mask regions of mask_segs. SEGS with no overlapping mask areas are filtered out." + + def doit(self, base_segs, mask_segs): + mask = core.segs_to_combined_mask(mask_segs) + mask = utils.make_3d_mask(mask) + return (core.segs_bitwise_subtract_mask(base_segs, mask), ) + + +class ToBinaryMask: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "mask": ("MASK",), + "threshold": ("INT", {"default": 20, "min": 1, "max": 255}), + } + } + + RETURN_TYPES = ("MASK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, mask, threshold): + mask = utils.to_binary_mask(mask, threshold/255.0) + return (mask,) + + +class FlattenMask: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "masks": ("MASK",), + } + } + + RETURN_TYPES = ("MASK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, masks): + masks = utils.make_3d_mask(masks) + masks = utils.flatten_mask(masks) + return (masks,) + + +class BitwiseAndMask: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "mask1": ("MASK",), + "mask2": ("MASK",), + } + } + + RETURN_TYPES = ("MASK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, mask1, mask2): + mask = utils.bitwise_and_masks(mask1, mask2) + return (mask,) + + +class SubtractMask: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "mask1": ("MASK", ), + "mask2": ("MASK", ), + } + } + + RETURN_TYPES = ("MASK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, mask1, mask2): + mask = utils.subtract_masks(mask1, mask2) + return (mask,) + + +class AddMask: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "mask1": ("MASK",), + "mask2": ("MASK",), + } + } + + RETURN_TYPES = ("MASK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, mask1, mask2): + mask = utils.add_masks(mask1, mask2) + return (mask,) + + +def get_image_hash(arr): + split_index1 = arr.shape[0] // 2 + split_index2 = arr.shape[1] // 2 + part1 = arr[:split_index1, :split_index2] + part2 = arr[:split_index1, split_index2:] + part3 = arr[split_index1:, :split_index2] + part4 = arr[split_index1:, split_index2:] + + # 각 부분을 합산 + sum1 = np.sum(part1) + sum2 = np.sum(part2) + sum3 = np.sum(part3) + sum4 = np.sum(part4) + + return hash((sum1, sum2, sum3, sum4)) + + +def get_file_item(base_type, path): + path_type = base_type + + if path == "[output]": + path_type = "output" + path = path[:-9] + elif path == "[input]": + path_type = "input" + path = path[:-8] + elif path == "[temp]": + path_type = "temp" + path = path[:-7] + + subfolder = os.path.dirname(path) + filename = os.path.basename(path) + + return { + "filename": filename, + "subfolder": subfolder, + "type": path_type + } + + +class MaskRectArea: + # Creates a rectangle mask using percentage. + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + }, + "hidden": {"extra_pnginfo": "EXTRA_PNGINFO", "unique_id": "UNIQUE_ID"} + } + + RETURN_TYPES = ("MASK",) + + CATEGORY = "ImpactPack/Operation" + FUNCTION = "create_mask" + + def create_mask(self, extra_pnginfo, unique_id, **kwargs): + # search for node + node_found = False + for node in extra_pnginfo["workflow"]["nodes"]: + if str(node["id"]) == unique_id: + min_x = node["properties"].get("x", 0) / 100 + min_y = node["properties"].get("y", 0) / 100 + width = node["properties"].get("w", 0) / 100 + height = node["properties"].get("h", 0) / 100 + blur_radius = node["properties"].get("blur_radius", 0) + node_found = True + break + + if not node_found: + raise ValueError(f"No node found with unique_id {unique_id}.") + + # Create a mask with standard resolution (e.g., 512x512) + resolution = 512 + mask = torch.zeros((resolution, resolution)) + + # Calculate pixel coordinates + min_x_px = int(min_x * resolution) + min_y_px = int(min_y * resolution) + max_x_px = int((min_x + width) * resolution) + max_y_px = int((min_y + height) * resolution) + + # Draw the rectangle on the mask + mask[min_y_px:max_y_px, min_x_px:max_x_px] = 1 + + # Apply blur if the radii are greater than 0 + if blur_radius > 0: + dx = blur_radius * 2 + 1 + dy = blur_radius * 2 + 1 + + # Convert the mask to a format compatible with OpenCV (numpy array) + mask_np = mask.cpu().numpy().astype("float32") + + # Apply Gaussian Blur + blurred_mask = cv2.GaussianBlur(mask_np, (dx, dy), 0) + + # Convert back to tensor + mask = torch.from_numpy(blurred_mask) + + # Return the mask as a tensor with an additional channel + return (mask.unsqueeze(0),) + + +class MaskRectAreaAdvanced: + # Creates a rectangle mask using pixels relative to image size. + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + }, + "hidden": {"extra_pnginfo": "EXTRA_PNGINFO", "unique_id": "UNIQUE_ID"} + } + + RETURN_TYPES = ("MASK",) + + CATEGORY = "ImpactPack/Operation" + FUNCTION = "create_mask_advanced" + + def create_mask_advanced(self, extra_pnginfo, unique_id, **kwargs): + # search for node + node_found = False + for node in extra_pnginfo["workflow"]["nodes"]: + if node["id"] == int(unique_id): + min_x = node["properties"]["x"] + min_y = node["properties"]["y"] + width = node["properties"]["w"] + height = node["properties"]["h"] + image_width = node["properties"]["width"] + image_height = node["properties"]["height"] + blur_radius = node["properties"]["blur_radius"] + node_found = True + break + + if not node_found: + raise ValueError(f"No node found with unique_id {unique_id}.") + + # Calculate maximum coordinates + max_x = min_x + width + max_y = min_y + height + + # Create a mask with the image dimensions + mask = torch.zeros((image_height, image_width)) + + # Draw the rectangle on the mask + mask[int(min_y):int(max_y), int(min_x):int(max_x)] = 1 + + # Apply blur if the radii are greater than 0 + if blur_radius > 0: + dx = blur_radius * 2 + 1 + dy = blur_radius * 2 + 1 + + # Convert the mask to a format compatible with OpenCV (numpy array) + mask_np = mask.cpu().numpy().astype("float32") + + # Apply Gaussian Blur + blurred_mask = cv2.GaussianBlur(mask_np, (dx, dy), 0) + + # Convert back to tensor + mask = torch.from_numpy(blurred_mask) + + # Return the mask as a tensor with an additional channel + return (mask.unsqueeze(0),) + + +class ImageReceiver: + @classmethod + def INPUT_TYPES(s): + input_dir = folder_paths.get_input_directory() + files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))] + return {"required": { + "image": (sorted(files), ), + "link_id": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + "save_to_workflow": ("BOOLEAN", {"default": False}), + "image_data": ("STRING", {"multiline": False}), + "trigger_always": ("BOOLEAN", {"default": False, "label_on": "enable", "label_off": "disable"}), + }, + } + + FUNCTION = "doit" + + RETURN_TYPES = ("IMAGE", "MASK") + + CATEGORY = "ImpactPack/Util" + + def doit(self, image, link_id, save_to_workflow, image_data, trigger_always): + if save_to_workflow: + try: + image_data = base64.b64decode(image_data.split(",")[1]) + i = Image.open(BytesIO(image_data)) + i = ImageOps.exif_transpose(i) + image = i.convert("RGB") + image = np.array(image).astype(np.float32) / 255.0 + image = torch.from_numpy(image)[None,] + if 'A' in i.getbands(): + mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0 + mask = 1. - torch.from_numpy(mask) + else: + mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu") + return image, mask.unsqueeze(0) + except Exception: + logging.warning("[WARN] ComfyUI-Impact-Pack: ImageReceiver - invalid 'image_data'") + mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu") + return utils.empty_pil_tensor(64, 64), mask + else: + return nodes.LoadImage().load_image(image) + + @classmethod + def VALIDATE_INPUTS(s, image, link_id, save_to_workflow, image_data, trigger_always): + if image != '#DATA' and not folder_paths.exists_annotated_filepath(image) or image.startswith("/") or ".." in image: + return "Invalid image file: {}".format(image) + + return True + + @classmethod + def IS_CHANGED(s, image, link_id, save_to_workflow, image_data, trigger_always): + if trigger_always: + return float("NaN") + else: + if save_to_workflow: + return hash(image_data) + else: + return hash(image) + + +from server import PromptServer + +class ImageSender(nodes.PreviewImage): + @classmethod + def INPUT_TYPES(s): + return {"required": { + "images": ("IMAGE", ), + "filename_prefix": ("STRING", {"default": "ImgSender"}), + "link_id": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), }, + "hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"}, + } + + OUTPUT_NODE = True + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, images, filename_prefix="ImgSender", link_id=0, prompt=None, extra_pnginfo=None): + result = nodes.PreviewImage().save_images(images, filename_prefix, prompt, extra_pnginfo) + PromptServer.instance.send_sync("img-send", {"link_id": link_id, "images": result['ui']['images']}) + return result + + +class LatentReceiver: + def __init__(self): + self.input_dir = folder_paths.get_input_directory() + self.type = "input" + + @classmethod + def INPUT_TYPES(s): + def check_file_extension(x): + return x.endswith(".latent") or x.endswith(".latent.png") + + input_dir = folder_paths.get_input_directory() + files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f)) and check_file_extension(f)] + return {"required": { + "latent": (sorted(files), ), + "link_id": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + "trigger_always": ("BOOLEAN", {"default": False, "label_on": "enable", "label_off": "disable"}), + }, + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + RETURN_TYPES = ("LATENT",) + + @staticmethod + def load_preview_latent(image_path): + if not os.path.exists(image_path): + return None + + image = Image.open(image_path) + exif_data = piexif.load(image.info["exif"]) + + if piexif.ExifIFD.UserComment in exif_data["Exif"]: + compressed_data = exif_data["Exif"][piexif.ExifIFD.UserComment] + compressed_data_io = BytesIO(compressed_data) + with zipfile.ZipFile(compressed_data_io, mode='r') as archive: + tensor_bytes = archive.read("latent") + tensor = safetensors.torch.load(tensor_bytes) + return {"samples": tensor['latent_tensor']} + return None + + def parse_filename(self, filename): + pattern = r"^(.*)/(.*?)\[(.*)\]\s*$" + match = re.match(pattern, filename) + if match: + subfolder = match.group(1) + filename = match.group(2).rstrip() + file_type = match.group(3) + else: + subfolder = '' + file_type = self.type + + return {'filename': filename, 'subfolder': subfolder, 'type': file_type} + + def doit(self, **kwargs): + if 'latent' not in kwargs: + return (torch.zeros([1, 4, 8, 8]), ) + + latent = kwargs['latent'] + + latent_name = latent + latent_path = folder_paths.get_annotated_filepath(latent_name) + + if latent.endswith(".latent"): + latent = safetensors.torch.load_file(latent_path, device="cpu") + multiplier = 1.0 + if "latent_format_version_0" not in latent: + multiplier = 1.0 / 0.18215 + samples = {"samples": latent["latent_tensor"].float() * multiplier} + else: + samples = LatentReceiver.load_preview_latent(latent_path) + + if samples is None: + samples = {'samples': torch.zeros([1, 4, 8, 8])} + + preview = self.parse_filename(latent_name) + + return { + 'ui': {"images": [preview]}, + 'result': (samples, ) + } + + @classmethod + def IS_CHANGED(s, latent, link_id, trigger_always): + if trigger_always: + return float("NaN") + else: + image_path = folder_paths.get_annotated_filepath(latent) + m = hashlib.sha256() + with open(image_path, 'rb') as f: + m.update(f.read()) + return m.digest().hex() + + @classmethod + def VALIDATE_INPUTS(s, latent, link_id, trigger_always): + if not folder_paths.exists_annotated_filepath(latent) or latent.startswith("/") or ".." in latent: + return "Invalid latent file: {}".format(latent) + return True + + +class LatentSender(nodes.SaveLatent): + def __init__(self): + super().__init__() + self.output_dir = folder_paths.get_temp_directory() + self.type = "temp" + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "samples": ("LATENT", ), + "filename_prefix": ("STRING", {"default": "latents/LatentSender"}), + "link_id": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + "preview_method": (["Latent2RGB-FLUX.1", + "Latent2RGB-SDXL", "Latent2RGB-SD15", "Latent2RGB-SD3", + "Latent2RGB-SD-X4", "Latent2RGB-Playground-2.5", + "Latent2RGB-SC-Prior", "Latent2RGB-SC-B", + "Latent2RGB-LTXV", + "TAEF1", "TAESDXL", "TAESD15", "TAESD3"],) + }, + "hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"}, + } + + OUTPUT_NODE = True + + RETURN_TYPES = () + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def save_to_file(tensor_bytes, prompt, extra_pnginfo, image, image_path): + compressed_data = BytesIO() + with zipfile.ZipFile(compressed_data, mode='w') as archive: + archive.writestr("latent", tensor_bytes) + image = image.copy() + exif_data = {"Exif": {piexif.ExifIFD.UserComment: compressed_data.getvalue()}} + + metadata = PngInfo() + if prompt is not None: + metadata.add_text("prompt", json.dumps(prompt)) + if extra_pnginfo is not None: + for x in extra_pnginfo: + metadata.add_text(x, json.dumps(extra_pnginfo[x])) + + exif_bytes = piexif.dump(exif_data) + image.save(image_path, format='png', exif=exif_bytes, pnginfo=metadata, optimize=True) + + @staticmethod + def prepare_preview(latent_tensor, preview_method): + from comfy.cli_args import LatentPreviewMethod + import comfy.latent_formats as latent_formats + + lower_bound = 128 + upper_bound = 256 + + if preview_method == "Latent2RGB-SD15": + latent_format = latent_formats.SD15() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-SDXL": + latent_format = latent_formats.SDXL() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-SD3": + latent_format = latent_formats.SD3() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-SD-X4": + latent_format = latent_formats.SD_X4() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-Playground-2.5": + latent_format = latent_formats.SDXL_Playground_2_5() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-SC-Prior": + latent_format = latent_formats.SC_Prior() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-SC-B": + latent_format = latent_formats.SC_B() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-FLUX.1": + latent_format = latent_formats.Flux() + method = LatentPreviewMethod.Latent2RGB + elif preview_method == "Latent2RGB-LTXV": + latent_format = latent_formats.LTXV() + method = LatentPreviewMethod.Latent2RGB + else: + logging.warning(f"[Impact Pack] LatentSender: '{preview_method}' is unsupported preview method.") + latent_format = latent_formats.SD15() + method = LatentPreviewMethod.Latent2RGB + + previewer = core.get_previewer("cpu", latent_format=latent_format, force=True, method=method) + + image = previewer.decode_latent_to_preview(latent_tensor) + min_size = min(image.size[0], image.size[1]) + max_size = max(image.size[0], image.size[1]) + + scale_factor = 1 + if max_size > upper_bound: + scale_factor = upper_bound/max_size + + # prevent too small preview + if min_size*scale_factor < lower_bound: + scale_factor = lower_bound/min_size + + w = int(image.size[0] * scale_factor) + h = int(image.size[1] * scale_factor) + + image = image.resize((w, h), resample=Image.NEAREST) + + return LatentSender.attach_format_text(image) + + @staticmethod + def attach_format_text(image): + width_a, height_a = image.size + + letter_image = Image.open(latent_letter_path) + width_b, height_b = letter_image.size + + new_width = max(width_a, width_b) + new_height = height_a + height_b + + new_image = Image.new('RGB', (new_width, new_height), (0, 0, 0)) + + offset_x = (new_width - width_b) // 2 + offset_y = (height_a + (new_height - height_a - height_b) // 2) + new_image.paste(letter_image, (offset_x, offset_y)) + + new_image.paste(image, (0, 0)) + + return new_image + + def doit(self, samples, filename_prefix="latents/LatentSender", link_id=0, preview_method="Latent2RGB-SDXL", prompt=None, extra_pnginfo=None): + full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir) + + # load preview + preview = LatentSender.prepare_preview(samples['samples'], preview_method) + + # support save metadata for latent sharing + file = f"{filename}_{counter:05}_.latent.png" + fullpath = os.path.join(full_output_folder, file) + + output = {"latent_tensor": samples["samples"]} + + tensor_bytes = safetensors.torch.save(output) + LatentSender.save_to_file(tensor_bytes, prompt, extra_pnginfo, preview, fullpath) + + latent_path = { + 'filename': file, + 'subfolder': subfolder, + 'type': self.type + } + + PromptServer.instance.send_sync("latent-send", {"link_id": link_id, "images": [latent_path]}) + + return {'ui': {'images': [latent_path]}} + + +class ImpactWildcardProcessor: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "wildcard_text": ("STRING", {"multiline": True, "dynamicPrompts": False, "tooltip": "Enter a prompt using wildcard syntax."}), + "populated_text": ("STRING", {"multiline": True, "dynamicPrompts": False, "tooltip": "The actual value passed during the execution of 'ImpactWildcardProcessor' is what is shown here. The behavior varies slightly depending on the mode. Wildcard syntax can also be used in 'populated_text'."}), + "mode": (["populate", "fixed", "reproduce"], {"default": "populate", "tooltip": + "populate: Before running the workflow, it overwrites the existing value of 'populated_text' with the prompt processed from 'wildcard_text'. In this mode, 'populated_text' cannot be edited.\n" + "fixed: Ignores wildcard_text and keeps 'populated_text' as is. You can edit 'populated_text' in this mode.\n" + "reproduce: This mode operates as 'fixed' mode only once for reproduction, and then it switches to 'populate' mode." + }), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Determines the random seed to be used for wildcard processing."}), + "Select to add Wildcard": (["Select the Wildcard to add to the text"],), + }, + } + + CATEGORY = "ImpactPack/Prompt" + + DESCRIPTION = ("The 'ImpactWildcardProcessor' processes text prompts written in wildcard syntax and outputs the processed text prompt.\n\n" + "TIP: Before the workflow is executed, the processing result of 'wildcard_text' is displayed in 'populated_text', and the populated text is saved along with the workflow. If you want to use a seed converted as input, write the prompt directly in 'populated_text' instead of 'wildcard_text', and set the mode to 'fixed'.") + + RETURN_TYPES = ("STRING", ) + RETURN_NAMES = ("processed text",) + FUNCTION = "doit" + + @staticmethod + def process(**kwargs): + return impact.wildcards.process(**kwargs) + + def doit(self, *args, **kwargs): + populated_text = ImpactWildcardProcessor.process(text=kwargs['populated_text'], seed=kwargs['seed']) + return (populated_text, ) + + +class ImpactWildcardEncode: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "model": ("MODEL",), + "clip": ("CLIP",), + "wildcard_text": ("STRING", {"multiline": True, "dynamicPrompts": False, "tooltip": "Enter a prompt using wildcard syntax."}), + "populated_text": ("STRING", {"multiline": True, "dynamicPrompts": False, "tooltip": "The actual value passed during the execution of 'ImpactWildcardEncode' is what is shown here. The behavior varies slightly depending on the mode. Wildcard syntax can also be used in 'populated_text'."}), + "mode": (["populate", "fixed", "reproduce"], {"tooltip": + "populate: Before running the workflow, it overwrites the existing value of 'populated_text' with the prompt processed from 'wildcard_text'. In this mode, 'populated_text' cannot be edited.\n" + "fixed: Ignores wildcard_text and keeps 'populated_text' as is. You can edit 'populated_text' in this mode\n." + "reproduce: This mode operates as 'fixed' mode only once for reproduction, and then it switches to 'populate' mode."}), + "Select to add LoRA": (["Select the LoRA to add to the text"] + folder_paths.get_filename_list("loras"), ), + "Select to add Wildcard": (["Select the Wildcard to add to the text"], ), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Determines the random seed to be used for wildcard processing."}), + }, + } + + CATEGORY = "ImpactPack/Prompt" + + DESCRIPTION = ("The 'ImpactWildcardEncode' node processes text prompts written in wildcard syntax and outputs them as conditioning. It also supports LoRA syntax, with the applied LoRA reflected in the model's output.\n\n" + "TIP1: Before the workflow is executed, the processing result of 'wildcard_text' is displayed in 'populated_text', and the populated text is saved along with the workflow. If you want to use a seed converted as input, write the prompt directly in 'populated_text' instead of 'wildcard_text', and set the mode to 'fixed'.\n" + "TIP2: If the 'Inspire Pack' is installed, LBW(LoRA Block Weight) syntax can also be applied.") + + RETURN_TYPES = ("MODEL", "CLIP", "CONDITIONING", "STRING") + RETURN_NAMES = ("model", "clip", "conditioning", "populated_text") + FUNCTION = "doit" + + @staticmethod + def process_with_loras(**kwargs): + return impact.wildcards.process_with_loras(**kwargs) + + @staticmethod + def get_wildcard_list(): + return impact.wildcards.get_wildcard_list() + + def doit(self, *args, **kwargs): + populated = kwargs['populated_text'] + processed = [] + model, clip, conditioning = impact.wildcards.process_with_loras(wildcard_opt=populated, model=kwargs['model'], clip=kwargs['clip'], seed=kwargs['seed'], processed=processed) + return model, clip, conditioning, processed[0] + + +class ImpactSchedulerAdapter: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "scheduler": (comfy.samplers.KSampler.SCHEDULERS, {"defaultInput": True, }), + "extra_scheduler": (['None', 'AYS SDXL', 'AYS SD1', 'AYS SVD', 'GITS[coeff=1.2]', 'LTXV[default]', 'OSS FLUX', 'OSS Wan', 'OSS Chroma'],), + }} + + CATEGORY = "ImpactPack/Util" + + RETURN_TYPES = (core.SCHEDULERS,) + RETURN_NAMES = ("scheduler",) + + FUNCTION = "doit" + + def doit(self, scheduler, extra_scheduler): + if extra_scheduler != 'None': + return (extra_scheduler,) + + return (scheduler,) + diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/impact_sampling.py b/custom_nodes/comfyui-impact-pack/modules/impact/impact_sampling.py new file mode 100644 index 0000000000000000000000000000000000000000..5bc11cf7b3193316ecdfdf2dc22d73fd3443e857 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/impact_sampling.py @@ -0,0 +1,323 @@ +import logging + +import nodes +from comfy.k_diffusion import sampling as k_diffusion_sampling +from comfy import samplers +from comfy_extras import nodes_custom_sampler +import latent_preview +import comfy +import torch +import math +import comfy.model_management as mm + + +try: + from comfy_extras.nodes_custom_sampler import Noise_EmptyNoise, Noise_RandomNoise + import node_helpers +except Exception: + logging.warning("\n#############################################\n[Impact Pack] ComfyUI is an outdated version.\n#############################################\n") + raise Exception("[Impact Pack] ComfyUI is an outdated version.") + + +def calculate_sigmas(model, sampler, scheduler, steps): + discard_penultimate_sigma = False + if sampler in ['dpm_2', 'dpm_2_ancestral', 'uni_pc', 'uni_pc_bh2']: + steps += 1 + discard_penultimate_sigma = True + + if scheduler.startswith('AYS'): + sigmas = nodes.NODE_CLASS_MAPPINGS['AlignYourStepsScheduler']().get_sigmas(scheduler[4:], steps, denoise=1.0)[0] + elif scheduler.startswith('GITS[coeff='): + sigmas = nodes.NODE_CLASS_MAPPINGS['GITSScheduler']().get_sigmas(float(scheduler[11:-1]), steps, denoise=1.0)[0] + elif scheduler == 'LTXV[default]': + sigmas = nodes.NODE_CLASS_MAPPINGS['LTXVScheduler']().get_sigmas(20, 2.05, 0.95, True, 0.1)[0] + elif scheduler.startswith('OSS'): + sigmas = nodes.NODE_CLASS_MAPPINGS['OptimalStepsScheduler']().get_sigmas(scheduler[4:], steps, denoise=1.0)[0] + else: + sigmas = samplers.calculate_sigmas(model.get_model_object("model_sampling"), scheduler, steps) + + if discard_penultimate_sigma: + sigmas = torch.cat([sigmas[:-2], sigmas[-1:]]) + return sigmas + + +def get_noise_sampler(x, cpu, total_sigmas, **kwargs): + if 'extra_args' in kwargs and 'seed' in kwargs['extra_args']: + sigma_min, sigma_max = total_sigmas[total_sigmas > 0].min(), total_sigmas.max() + seed = kwargs['extra_args'].get("seed", None) + return k_diffusion_sampling.BrownianTreeNoiseSampler(x, sigma_min, sigma_max, seed=seed, cpu=cpu) + return None + + +def ksampler(sampler_name, total_sigmas, extra_options={}, inpaint_options={}): + if sampler_name in ["dpmpp_sde", "dpmpp_sde_gpu", "dpmpp_2m_sde", "dpmpp_2m_sde_gpu", "dpmpp_3m_sde", "dpmpp_3m_sde_gpu"]: + if sampler_name == "dpmpp_sde": + orig_sampler_function = k_diffusion_sampling.sample_dpmpp_sde + elif sampler_name == "dpmpp_sde_gpu": + orig_sampler_function = k_diffusion_sampling.sample_dpmpp_sde_gpu + elif sampler_name == "dpmpp_2m_sde": + orig_sampler_function = k_diffusion_sampling.sample_dpmpp_2m_sde + elif sampler_name == "dpmpp_2m_sde_gpu": + orig_sampler_function = k_diffusion_sampling.sample_dpmpp_2m_sde_gpu + elif sampler_name == "dpmpp_3m_sde": + orig_sampler_function = k_diffusion_sampling.sample_dpmpp_3m_sde + elif sampler_name == "dpmpp_3m_sde_gpu": + orig_sampler_function = k_diffusion_sampling.sample_dpmpp_3m_sde_gpu + + def sampler_function_wrapper(model, x, sigmas, **kwargs): + if 'noise_sampler' not in kwargs: + kwargs['noise_sampler'] = get_noise_sampler(x, 'gpu' not in sampler_name, total_sigmas, **kwargs) + + return orig_sampler_function(model, x, sigmas, **kwargs) + + sampler_function = sampler_function_wrapper + + else: + return comfy.samplers.sampler_object(sampler_name) + + return samplers.KSAMPLER(sampler_function, extra_options, inpaint_options) + + +# modified version of SamplerCustom.sample +def sample_with_custom_noise(model, add_noise, noise_seed, cfg, positive, negative, sampler, sigmas, latent_image, noise=None, callback=None): + latent = latent_image + latent_image = latent["samples"] + + if hasattr(comfy.sample, 'fix_empty_latent_channels'): + latent_image = comfy.sample.fix_empty_latent_channels(model, latent_image) + + out = latent.copy() + out['samples'] = latent_image + + if noise is None: + if not add_noise: + noise = Noise_EmptyNoise().generate_noise(out) + else: + noise = Noise_RandomNoise(noise_seed).generate_noise(out) + + noise_mask = None + if "noise_mask" in latent: + noise_mask = latent["noise_mask"] + + x0_output = {} + preview_callback = latent_preview.prepare_callback(model, sigmas.shape[-1] - 1, x0_output) + + if callback is not None: + def touched_callback(step, x0, x, total_steps): + callback(step, x0, x, total_steps) + preview_callback(step, x0, x, total_steps) + else: + touched_callback = preview_callback + + disable_pbar = not comfy.utils.PROGRESS_BAR_ENABLED + + device = mm.get_torch_device() + + noise = noise.to(device) + latent_image = latent_image.to(device) + if noise_mask is not None: + noise_mask = noise_mask.to(device) + + if negative != 'NegativePlaceholder': + # This way is incompatible with Advanced ControlNet, yet. + # guider = comfy.samplers.CFGGuider(model) + # guider.set_conds(positive, negative) + # guider.set_cfg(cfg) + samples = comfy.sample.sample_custom(model, noise, cfg, sampler, sigmas, positive, negative, latent_image, + noise_mask=noise_mask, callback=touched_callback, + disable_pbar=disable_pbar, seed=noise_seed) + else: + guider = nodes_custom_sampler.Guider_Basic(model) + positive = node_helpers.conditioning_set_values(positive, {"guidance": cfg}) + guider.set_conds(positive) + samples = guider.sample(noise, latent_image, sampler, sigmas, denoise_mask=noise_mask, callback=touched_callback, disable_pbar=disable_pbar, seed=noise_seed) + + samples = samples.to(comfy.model_management.intermediate_device()) + + out["samples"] = samples + if "x0" in x0_output: + out_denoised = latent.copy() + out_denoised["samples"] = model.model.process_latent_out(x0_output["x0"].cpu()) + else: + out_denoised = out + return out, out_denoised + + +# When sampling one step at a time, it mitigates the problem. (especially for _sde series samplers) +def separated_sample(model, add_noise, seed, steps, cfg, sampler_name, scheduler, positive, negative, + latent_image, start_at_step, end_at_step, return_with_leftover_noise, sigma_ratio=1.0, sampler_opt=None, noise=None, callback=None, scheduler_func=None): + + if scheduler_func is not None: + total_sigmas = scheduler_func(model, sampler_name, steps) + else: + if sampler_opt is None: + total_sigmas = calculate_sigmas(model, sampler_name, scheduler, steps) + else: + total_sigmas = calculate_sigmas(model, "", scheduler, steps) + + sigmas = total_sigmas + + if end_at_step is not None and end_at_step < (len(total_sigmas) - 1): + sigmas = total_sigmas[:end_at_step + 1] + if not return_with_leftover_noise: + sigmas[-1] = 0 + + if start_at_step is not None: + if start_at_step < (len(sigmas) - 1): + sigmas = sigmas[start_at_step:] * sigma_ratio + else: + if latent_image is not None: + return latent_image + else: + return {'samples': torch.zeros_like(noise)} + + if sampler_opt is None: + impact_sampler = ksampler(sampler_name, total_sigmas) + else: + impact_sampler = sampler_opt + + if len(sigmas) == 0 or (len(sigmas) == 1 and sigmas[0] == 0): + return latent_image + + res = sample_with_custom_noise(model, add_noise, seed, cfg, positive, negative, impact_sampler, sigmas, latent_image, noise=noise, callback=callback) + + if return_with_leftover_noise: + return res[0] + else: + return res[1] + + +def impact_sample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise=1.0, sigma_ratio=1.0, sampler_opt=None, noise=None, scheduler_func=None): + advanced_steps = math.floor(steps / denoise) + start_at_step = advanced_steps - steps + end_at_step = start_at_step + steps + return separated_sample(model, True, seed, advanced_steps, cfg, sampler_name, scheduler, positive, negative, latent_image, + start_at_step, end_at_step, False, scheduler_func=scheduler_func) + + +def ksampler_wrapper(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise, + refiner_ratio=None, refiner_model=None, refiner_clip=None, refiner_positive=None, refiner_negative=None, sigma_factor=1.0, noise=None, scheduler_func=None, sampler_opt=None): + + if refiner_ratio is None or refiner_model is None or refiner_clip is None or refiner_positive is None or refiner_negative is None: + # Use separated_sample instead of KSampler for `AYS scheduler` + # refined_latent = nodes.KSampler().sample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise * sigma_factor)[0] + + advanced_steps = math.floor(steps / denoise) + start_at_step = advanced_steps - steps + end_at_step = start_at_step + steps + + refined_latent = separated_sample(model, True, seed, advanced_steps, cfg, sampler_name, scheduler, + positive, negative, latent_image, start_at_step, end_at_step, False, + sigma_ratio=sigma_factor, sampler_opt=sampler_opt, noise=noise, scheduler_func=scheduler_func) + else: + advanced_steps = math.floor(steps / denoise) + start_at_step = advanced_steps - steps + end_at_step = start_at_step + math.floor(steps * (1.0 - refiner_ratio)) + + # print(f"pre: {start_at_step} .. {end_at_step} / {advanced_steps}") + temp_latent = separated_sample(model, True, seed, advanced_steps, cfg, sampler_name, scheduler, + positive, negative, latent_image, start_at_step, end_at_step, True, + sigma_ratio=sigma_factor, sampler_opt=sampler_opt, noise=noise, scheduler_func=scheduler_func) + + if 'noise_mask' in latent_image: + # noise_latent = \ + # impact_sampling.separated_sample(refiner_model, "enable", seed, advanced_steps, cfg, sampler_name, + # scheduler, refiner_positive, refiner_negative, latent_image, end_at_step, + # end_at_step, "enable") + + latent_compositor = nodes.NODE_CLASS_MAPPINGS['LatentCompositeMasked']() + temp_latent = latent_compositor.composite(latent_image, temp_latent, 0, 0, False, latent_image['noise_mask'])[0] + + # print(f"post: {end_at_step} .. {advanced_steps + 1} / {advanced_steps}") + refined_latent = separated_sample(refiner_model, False, seed, advanced_steps, cfg, sampler_name, scheduler, + refiner_positive, refiner_negative, temp_latent, end_at_step, advanced_steps + 1, False, + sigma_ratio=sigma_factor, sampler_opt=sampler_opt, scheduler_func=scheduler_func) + + return refined_latent + + +class KSamplerAdvancedWrapper: + params = None + + def __init__(self, model, cfg, sampler_name, scheduler, positive, negative, sampler_opt=None, sigma_factor=1.0, scheduler_func=None): + self.params = model, cfg, sampler_name, scheduler, positive, negative, sigma_factor + self.sampler_opt = sampler_opt + self.scheduler_func = scheduler_func + + def clone_with_conditionings(self, positive, negative): + model, cfg, sampler_name, scheduler, _, _, _ = self.params + return KSamplerAdvancedWrapper(model, cfg, sampler_name, scheduler, positive, negative, self.sampler_opt) + + def sample_advanced(self, add_noise, seed, steps, latent_image, start_at_step, end_at_step, return_with_leftover_noise, hook=None, + recovery_mode="ratio additional", recovery_sampler="AUTO", recovery_sigma_ratio=1.0, noise=None): + + model, cfg, sampler_name, scheduler, positive, negative, sigma_factor = self.params + # steps, start_at_step, end_at_step = self.compensate_denoise(steps, start_at_step, end_at_step) + + if hook is not None: + model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent = hook.pre_ksample_advanced(model, add_noise, seed, steps, cfg, sampler_name, scheduler, + positive, negative, latent_image, start_at_step, end_at_step, + return_with_leftover_noise) + + if recovery_mode != 'DISABLE' and sampler_name in ['uni_pc', 'uni_pc_bh2', 'dpmpp_sde', 'dpmpp_sde_gpu', 'dpmpp_2m_sde', 'dpmpp_2m_sde_gpu', 'dpmpp_3m_sde', 'dpmpp_3m_sde_gpu']: + base_image = latent_image.copy() + if recovery_mode == "ratio between": + sigma_ratio = 1.0 - recovery_sigma_ratio + else: + sigma_ratio = 1.0 + else: + base_image = None + sigma_ratio = 1.0 + + try: + if sigma_ratio > 0: + latent_image = separated_sample(model, add_noise, seed, steps, cfg, sampler_name, scheduler, + positive, negative, latent_image, start_at_step, end_at_step, + return_with_leftover_noise, sigma_ratio=sigma_ratio * sigma_factor, + sampler_opt=self.sampler_opt, noise=noise, scheduler_func=self.scheduler_func) + except ValueError as e: + if str(e) == 'sigma_min and sigma_max must not be 0': + logging.warning("\nWARN: sampling skipped - sigma_min and sigma_max are 0") + return latent_image + + if (recovery_sigma_ratio > 0 and recovery_mode != 'DISABLE' and + sampler_name in ['uni_pc', 'uni_pc_bh2', 'dpmpp_sde', 'dpmpp_sde_gpu', 'dpmpp_2m_sde', 'dpmpp_2m_sde_gpu', 'dpmpp_3m_sde', 'dpmpp_3m_sde_gpu']): + compensate = 0 if sampler_name in ['uni_pc', 'uni_pc_bh2', 'dpmpp_sde', 'dpmpp_sde_gpu', 'dpmpp_2m_sde', 'dpmpp_2m_sde_gpu', 'dpmpp_3m_sde', 'dpmpp_3m_sde_gpu'] else 2 + if recovery_sampler == "AUTO": + recovery_sampler = 'dpm_fast' if sampler_name in ['uni_pc', 'uni_pc_bh2', 'dpmpp_sde', 'dpmpp_sde_gpu'] else 'dpmpp_2m' + + latent_compositor = nodes.NODE_CLASS_MAPPINGS['LatentCompositeMasked']() + + noise_mask = latent_image['noise_mask'] + + if len(noise_mask.shape) == 4: + noise_mask = noise_mask.squeeze(0).squeeze(0) + + latent_image = latent_compositor.composite(base_image, latent_image, 0, 0, False, noise_mask)[0] + + try: + latent_image = separated_sample(model, add_noise, seed, steps, cfg, recovery_sampler, scheduler, + positive, negative, latent_image, start_at_step-compensate, end_at_step, return_with_leftover_noise, + sigma_ratio=recovery_sigma_ratio * sigma_factor, sampler_opt=self.sampler_opt, scheduler_func=self.scheduler_func) + except ValueError as e: + if str(e) == 'sigma_min and sigma_max must not be 0': + logging.warning("\nWARN: sampling skipped - sigma_min and sigma_max are 0") + + return latent_image + + +class KSamplerWrapper: + params = None + + def __init__(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, scheduler_func=None): + self.params = model, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise + self.scheduler_func = scheduler_func + + def sample(self, latent_image, hook=None): + model, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise = self.params + + if hook is not None: + model, seed, steps, cfg, sampler_name, scheduler, positive, negative, upscaled_latent, denoise = \ + hook.pre_ksample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise) + + return impact_sample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise, scheduler_func=self.scheduler_func) diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/impact_server.py b/custom_nodes/comfyui-impact-pack/modules/impact/impact_server.py new file mode 100644 index 0000000000000000000000000000000000000000..d1240f2b873602f51fcfb24fad8a0ef0aff83441 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/impact_server.py @@ -0,0 +1,582 @@ +import os +import threading +import traceback + +from aiohttp import web + +import impact +import folder_paths + +import torchvision + +import impact.core as core +import impact.impact_pack as impact_pack +from impact.utils import to_tensor +import impact.utils as utils +from segment_anything import SamPredictor, sam_model_registry +import numpy as np +import nodes +from PIL import Image +import io +import comfy +from io import BytesIO +import random +from server import PromptServer +import logging + + +sam_predictor = None +default_sam_model_name = os.path.join(impact_pack.model_path, "sams", "sam_vit_b_01ec64.pth") + +sam_lock = threading.Condition() + +last_prepare_data = None + + +def async_prepare_sam(image_dir, model_name, filename): + with sam_lock: + global sam_predictor + + if 'vit_h' in model_name: + model_kind = 'vit_h' + elif 'vit_l' in model_name: + model_kind = 'vit_l' + else: + model_kind = 'vit_b' + + sam_model = sam_model_registry[model_kind](checkpoint=model_name) + sam_predictor = SamPredictor(sam_model) + + image_path = os.path.join(image_dir, filename) + image = nodes.LoadImage().load_image(image_path)[0] + image = np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8) + + if impact.config.get_config()['sam_editor_cpu']: + device = 'cpu' + else: + device = comfy.model_management.get_torch_device() + + sam_predictor.model.to(device=device) + sam_predictor.set_image(image, "RGB") + sam_predictor.model.cpu() + + +@PromptServer.instance.routes.post("/sam/prepare") +async def sam_prepare(request): + global sam_predictor + global last_prepare_data + data = await request.json() + + with sam_lock: + if last_prepare_data is not None and last_prepare_data == data: + # already loaded: skip -- prevent redundant loading + return web.Response(status=200) + + last_prepare_data = data + + model_name = 'sam_vit_b_01ec64.pth' + if data['sam_model_name'] == 'auto': + model_name = impact.config.get_config()['sam_editor_model'] + + model_path = folder_paths.get_full_path("sams", model_name) + + if model_path is None: + logging.error(f"[Impact Pack] The '{model_name}' model file cannot be found in any sams model path.") + return web.Response(status=400) + + logging.info(f"[Impact Pack] Loading SAM model '{model_path}'") + + filename, image_dir = folder_paths.annotated_filepath(data["filename"]) + + if image_dir is None: + typ = data['type'] if data['type'] != '' else 'output' + image_dir = folder_paths.get_directory_by_type(typ) + if data['subfolder'] is not None and data['subfolder'] != '': + image_dir += f"/{data['subfolder']}" + + if image_dir is None: + return web.Response(status=400) + + thread = threading.Thread(target=async_prepare_sam, args=(image_dir, model_path, filename,)) + thread.start() + + logging.info("[Impact Pack] SAM model loaded. ") + return web.Response(status=200) + + +@PromptServer.instance.routes.post("/sam/release") +async def release_sam(request): + global sam_predictor + + with sam_lock: + temp = sam_predictor + del temp + sam_predictor = None + + logging.info("[Impact Pack]: unloading SAM model") + + +@PromptServer.instance.routes.post("/sam/detect") +async def sam_detect(request): + global sam_predictor + with sam_lock: + if sam_predictor is not None: + if impact.config.get_config()['sam_editor_cpu']: + device = 'cpu' + else: + device = comfy.model_management.get_torch_device() + + sam_predictor.model.to(device=device) + try: + data = await request.json() + + positive_points = data['positive_points'] + negative_points = data['negative_points'] + threshold = data['threshold'] + + points = [] + plabs = [] + + for p in positive_points: + points.append(p) + plabs.append(1) + + for p in negative_points: + points.append(p) + plabs.append(0) + + detected_masks = core.sam_predict(sam_predictor, points, plabs, None, threshold) + mask = utils.combine_masks2(detected_masks) + + if mask is None: + return web.Response(status=400) + + image = mask.reshape((-1, 1, mask.shape[-2], mask.shape[-1])).movedim(1, -1).expand(-1, -1, -1, 3) + i = 255. * image.cpu().numpy() + + img = Image.fromarray(np.clip(i[0], 0, 255).astype(np.uint8)) + + img_buffer = io.BytesIO() + img.save(img_buffer, format='png') + + headers = {'Content-Type': 'image/png'} + finally: + sam_predictor.model.to(device="cpu") + + return web.Response(body=img_buffer.getvalue(), headers=headers) + + else: + return web.Response(status=400) + + +@PromptServer.instance.routes.get("/impact/wildcards/refresh") +async def wildcards_refresh(request): + impact.wildcards.wildcard_load() + return web.Response(status=200) + + +@PromptServer.instance.routes.get("/impact/wildcards/list") +async def wildcards_list(request): + data = {'data': impact.wildcards.get_wildcard_list()} + return web.json_response(data) + + +@PromptServer.instance.routes.post("/impact/wildcards") +async def populate_wildcards(request): + data = await request.json() + populated = impact.wildcards.process(data['text'], data.get('seed', None)) + return web.json_response({"text": populated}) + + +segs_picker_map = {} + +@PromptServer.instance.routes.get("/impact/segs/picker/count") +async def segs_picker_count(request): + node_id = request.rel_url.query.get('id', '') + + if node_id in segs_picker_map: + res = len(segs_picker_map[node_id]) + return web.Response(status=200, text=str(res)) + + return web.Response(status=400) + + +@PromptServer.instance.routes.get("/impact/segs/picker/view") +async def segs_picker(request): + node_id = request.rel_url.query.get('id', '') + idx = int(request.rel_url.query.get('idx', '')) + + if node_id in segs_picker_map and idx < len(segs_picker_map[node_id]): + img = to_tensor(segs_picker_map[node_id][idx]).permute(0, 3, 1, 2).squeeze(0) + pil = torchvision.transforms.ToPILImage('RGB')(img) + + image_bytes = BytesIO() + pil.save(image_bytes, format="PNG") + image_bytes.seek(0) + return web.Response(status=200, body=image_bytes, content_type='image/png', headers={"Content-Disposition": f"filename={node_id}{idx}.png"}) + + return web.Response(status=400) + + +@PromptServer.instance.routes.get("/view/validate") +async def view_validate(request): + if "filename" in request.rel_url.query: + filename = request.rel_url.query["filename"] + subfolder = request.rel_url.query["subfolder"] + filename, base_dir = folder_paths.annotated_filepath(filename) + + if filename == '' or filename[0] == '/' or '..' in filename: + return web.Response(status=400) + + if base_dir is None: + base_dir = folder_paths.get_input_directory() + + file = os.path.join(base_dir, subfolder, filename) + + if os.path.isfile(file): + return web.Response(status=200) + + return web.Response(status=400) + + +@PromptServer.instance.routes.get("/impact/validate/pb_id_image") +async def view_pb_id_image(request): + if "id" in request.rel_url.query: + pb_id = request.rel_url.query["id"] + + if pb_id not in core.preview_bridge_image_id_map: + return web.Response(status=400) + + file = core.preview_bridge_image_id_map[pb_id] + if os.path.isfile(file): + return web.Response(status=200) + + return web.Response(status=400) + + +@PromptServer.instance.routes.get("/impact/set/pb_id_image") +async def set_previewbridge_image(request): + try: + if "filename" in request.rel_url.query: + node_id = request.rel_url.query["node_id"] + filename = request.rel_url.query["filename"] + path_type = request.rel_url.query["type"] + subfolder = request.rel_url.query["subfolder"] + filename, output_dir = folder_paths.annotated_filepath(filename) + + if filename == '' or filename[0] == '/' or '..' in filename: + return web.Response(status=400) + + if output_dir is None: + if path_type == 'input': + output_dir = folder_paths.get_input_directory() + elif path_type == 'output': + output_dir = folder_paths.get_output_directory() + else: + output_dir = folder_paths.get_temp_directory() + + file = os.path.join(output_dir, subfolder, filename) + item = { + 'filename': filename, + 'type': path_type, + 'subfolder': subfolder, + } + pb_id = core.set_previewbridge_image(node_id, file, item) + + return web.Response(status=200, text=pb_id) + except Exception: + traceback.print_exc() + + return web.Response(status=400) + + +@PromptServer.instance.routes.get("/impact/get/pb_id_image") +async def get_previewbridge_image(request): + if "id" in request.rel_url.query: + pb_id = request.rel_url.query["id"] + + if pb_id in core.preview_bridge_image_id_map: + _, path_item = core.preview_bridge_image_id_map[pb_id] + return web.json_response(path_item) + + return web.Response(status=400) + + +@PromptServer.instance.routes.get("/impact/view/pb_id_image") +async def view_previewbridge_image(request): + if "id" in request.rel_url.query: + pb_id = request.rel_url.query["id"] + + if pb_id in core.preview_bridge_image_id_map: + file = core.preview_bridge_image_id_map[pb_id] + + with Image.open(file): + filename = os.path.basename(file) + return web.FileResponse(file, headers={"Content-Disposition": f"filename=\"{filename}\""}) + + return web.Response(status=400) + + +def onprompt_for_switch(json_data): + inversed_switch_info = {} + onprompt_switch_info = {} + onprompt_cond_branch_info = {} + disabled_switch = set() + + + for k, v in json_data['prompt'].items(): + if 'class_type' not in v: + continue + + cls = v['class_type'] + if cls == 'ImpactInversedSwitch': + # if 'sel_mode' is 'select_on_prompt' + if 'sel_mode' in v['inputs'] and v['inputs']['sel_mode'] and 'select' in v['inputs']: + select_input = v['inputs']['select'] + # if 'select' is converted input + if isinstance(select_input, list) and len(select_input) == 2: + input_node = json_data['prompt'][select_input[0]] + if input_node['class_type'] == 'ImpactInt' and 'inputs' in input_node and 'value' in input_node['inputs']: + inversed_switch_info[k] = input_node['inputs']['value'] + else: + logging.warning(f"\n##### ##### #####\n[Impact Pack] {cls}: For the 'select' operation, only 'select_index' of the 'ImpactInversedSwitch', which is not an input, or 'ImpactInt' and 'Primitive' are allowed as inputs if 'select_on_prompt' is selected.\n##### ##### #####\n") + else: + inversed_switch_info[k] = select_input + + elif cls in ['ImpactSwitch', 'LatentSwitch', 'SEGSSwitch', 'ImpactMakeImageList']: + # if 'sel_mode' is 'select_on_prompt' + if 'sel_mode' in v['inputs'] and v['inputs']['sel_mode'] and 'select' in v['inputs']: + select_input = v['inputs']['select'] + # if 'select' is converted input + if isinstance(select_input, list) and len(select_input) == 2: + input_node = json_data['prompt'][select_input[0]] + if input_node['class_type'] == 'ImpactInt' and 'inputs' in input_node and 'value' in input_node['inputs']: + onprompt_switch_info[k] = input_node['inputs']['value'] + if input_node['class_type'] == 'ImpactSwitch' and 'inputs' in input_node and 'select' in input_node['inputs']: + if isinstance(input_node['inputs']['select'], int): + onprompt_switch_info[k] = input_node['inputs']['select'] + else: + logging.warning(f"\n##### ##### #####\n[Impact Pack] {cls}: For the 'select' operation, only 'select_index' of the 'ImpactSwitch', which is not an input, or 'ImpactInt' and 'Primitive' are allowed as inputs if 'select_on_prompt' is selected.\n##### ##### #####\n") + else: + onprompt_switch_info[k] = select_input + + if k in onprompt_switch_info and f'input{onprompt_switch_info[k]}' not in v['inputs']: + # disconnect output + disabled_switch.add(k) + + elif cls == 'ImpactConditionalBranchSelMode': + if 'sel_mode' in v['inputs'] and v['inputs']['sel_mode'] and 'cond' in v['inputs']: + cond_input = v['inputs']['cond'] + if isinstance(cond_input, list) and len(cond_input) == 2: + input_node = json_data['prompt'][cond_input[0]] + if (input_node['class_type'] == 'ImpactValueReceiver' and 'inputs' in input_node + and 'value' in input_node['inputs'] and 'typ' in input_node['inputs']): + if 'BOOLEAN' == input_node['inputs']['typ']: + try: + onprompt_cond_branch_info[k] = input_node['inputs']['value'].lower() == "true" + except Exception: + pass + else: + onprompt_cond_branch_info[k] = cond_input + + for k, v in json_data['prompt'].items(): + disable_targets = set() + + for kk, vv in v['inputs'].items(): + if isinstance(vv, list) and len(vv) == 2: + if vv[0] in inversed_switch_info: + if vv[1] + 1 != inversed_switch_info[vv[0]]: + disable_targets.add(kk) + else: + del inversed_switch_info[k] + + if vv[0] in disabled_switch: + disable_targets.add(kk) + + if k in onprompt_switch_info: + selected_slot_name = f"input{onprompt_switch_info[k]}" + for kk, vv in v['inputs'].items(): + if kk != selected_slot_name and kk.startswith('input'): + disable_targets.add(kk) + + if k in onprompt_cond_branch_info: + selected_slot_name = "tt_value" if onprompt_cond_branch_info[k] else "ff_value" + for kk, vv in v['inputs'].items(): + if kk in ['tt_value', 'ff_value'] and kk != selected_slot_name: + disable_targets.add(kk) + + for kk in disable_targets: + del v['inputs'][kk] + + # inversed_switch - select out of range + for target in inversed_switch_info.keys(): + del json_data['prompt'][target]['inputs']['input'] + + +def onprompt_for_pickers(json_data): + detected_pickers = set() + + for k, v in json_data['prompt'].items(): + if 'class_type' not in v: + continue + + cls = v['class_type'] + if cls == 'ImpactSEGSPicker': + detected_pickers.add(k) + + # garbage collection + keys_to_remove = [key for key in segs_picker_map if key not in detected_pickers] + for key in keys_to_remove: + del segs_picker_map[key] + + +def gc_preview_bridge_cache(json_data): + prompt_keys = json_data['prompt'].keys() + + for key in list(core.preview_bridge_cache.keys()): + if key not in prompt_keys: + # print(f"key deleted [PB]: {key}") + del core.preview_bridge_cache[key] + + for key in list(core.preview_bridge_last_mask_cache.keys()): + if key not in prompt_keys: + # print(f"key deleted [PB_last_mask]: {key}") + del core.preview_bridge_last_mask_cache[key] + + +def workflow_imagereceiver_update(json_data): + prompt = json_data['prompt'] + + for v in prompt.values(): + if 'class_type' in v and v['class_type'] == 'ImageReceiver': + if v['inputs']['save_to_workflow']: + v['inputs']['image'] = "#DATA" + + +def regional_sampler_seed_update(json_data): + prompt = json_data['prompt'] + + for k, v in prompt.items(): + if 'class_type' in v and v['class_type'] == 'RegionalSampler': + seed_2nd_mode = v['inputs']['seed_2nd_mode'] + + new_seed = None + if seed_2nd_mode == 'increment': + new_seed = v['inputs']['seed_2nd']+1 + if new_seed > 1125899906842624: + new_seed = 0 + elif seed_2nd_mode == 'decrement': + new_seed = v['inputs']['seed_2nd']-1 + if new_seed < 0: + new_seed = 1125899906842624 + elif seed_2nd_mode == 'randomize': + new_seed = random.randint(0, 1125899906842624) + + if new_seed is not None: + PromptServer.instance.send_sync("impact-node-feedback", {"node_id": k, "widget_name": "seed_2nd", "type": "INT", "value": new_seed}) + + +def onprompt_populate_wildcards(json_data): + prompt = json_data['prompt'] + + updated_widget_values = {} + for k, v in prompt.items(): + if 'class_type' in v and (v['class_type'] == 'ImpactWildcardEncode' or v['class_type'] == 'ImpactWildcardProcessor'): + inputs = v['inputs'] + + # legacy adapter + if isinstance(inputs['mode'], bool): + if inputs['mode']: + new_mode = 'populate' + else: + new_mode = 'fixed' + + inputs['mode'] = new_mode + + if inputs['mode'] == 'populate' and isinstance(inputs['populated_text'], str): + if isinstance(inputs['seed'], list): + try: + input_node = prompt[inputs['seed'][0]] + if input_node['class_type'] == 'ImpactInt': + input_seed = int(input_node['inputs']['value']) + if not isinstance(input_seed, int): + continue + if input_node['class_type'] == 'Seed (rgthree)': + input_seed = int(input_node['inputs']['seed']) + if not isinstance(input_seed, int): + continue + else: + logging.info(f"[Impact Pack] Only `ImpactInt`, `Seed (rgthree)` and `Primitive` Node are allowed as the seed for '{v['class_type']}'. It will be ignored. ") + continue + except Exception: + continue + else: + input_seed = int(inputs['seed']) + + inputs['populated_text'] = impact.wildcards.process(inputs['wildcard_text'], input_seed) + inputs['mode'] = 'reproduce' + + PromptServer.instance.send_sync("impact-node-feedback", {"node_id": k, "widget_name": "populated_text", "type": "STRING", "value": inputs['populated_text']}) + updated_widget_values[k] = inputs['populated_text'] + + if inputs['mode'] == 'reproduce': + PromptServer.instance.send_sync("impact-node-feedback", {"node_id": k, "widget_name": "mode", "type": "STRING", "value": 'populate'}) + + + + if 'extra_data' in json_data and 'extra_pnginfo' in json_data['extra_data']: + for node in json_data['extra_data']['extra_pnginfo']['workflow']['nodes']: + key = str(node['id']) + if key in updated_widget_values: + node['widgets_values'][1] = updated_widget_values[key] + node['widgets_values'][2] = 'reproduce' + + +def onprompt_for_remote(json_data): + prompt = json_data['prompt'] + + for v in prompt.values(): + if 'class_type' in v: + cls = v['class_type'] + if cls == 'ImpactRemoteBoolean' or cls == 'ImpactRemoteInt': + inputs = v['inputs'] + node_id = str(inputs['node_id']) + + if node_id not in prompt: + continue + + target_inputs = prompt[node_id]['inputs'] + + widget_name = inputs['widget_name'] + if widget_name in target_inputs: + widget_type = None + if cls == 'ImpactRemoteBoolean' and isinstance(target_inputs[widget_name], bool): + widget_type = 'BOOLEAN' + + elif cls == 'ImpactRemoteInt' and (isinstance(target_inputs[widget_name], int) or isinstance(target_inputs[widget_name], float)): + widget_type = 'INT' + + if widget_type is None: + break + + target_inputs[widget_name] = inputs['value'] + PromptServer.instance.send_sync("impact-node-feedback", {"node_id": node_id, "widget_name": widget_name, "type": widget_type, "value": inputs['value']}) + + +def onprompt(json_data): + try: + onprompt_for_remote(json_data) # NOTE: top priority + onprompt_for_switch(json_data) + onprompt_for_pickers(json_data) + onprompt_populate_wildcards(json_data) + gc_preview_bridge_cache(json_data) + workflow_imagereceiver_update(json_data) + regional_sampler_seed_update(json_data) + core.current_prompt = json_data + except Exception as e: + logging.warning(f"[Impact Pack] ComfyUI-Impact-Pack: Error on prompt - several features will not work.\n{e}") + + return json_data + + +PromptServer.instance.add_on_prompt_handler(onprompt) diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/logics.py b/custom_nodes/comfyui-impact-pack/modules/impact/logics.py new file mode 100644 index 0000000000000000000000000000000000000000..413ba494e5128be9d68995735730cb3f80ae1b72 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/logics.py @@ -0,0 +1,779 @@ +import sys +import time + +import execution +import impact.impact_server +from server import PromptServer +from impact.utils import any_typ +import impact.core as core +import re +import nodes +import logging + + +class ImpactCompare: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "cmp": (['a = b', 'a <> b', 'a > b', 'a < b', 'a >= b', 'a <= b', 'tt', 'ff'],), + "a": (any_typ, ), + "b": (any_typ, ), + }, + } + + FUNCTION = "doit" + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = ("BOOLEAN", ) + + def doit(self, cmp, a, b): + if cmp == "a = b": + return (a == b, ) + elif cmp == "a <> b": + return (a != b, ) + elif cmp == "a > b": + return (a > b, ) + elif cmp == "a < b": + return (a < b, ) + elif cmp == "a >= b": + return (a >= b, ) + elif cmp == "a <= b": + return (a <= b, ) + elif cmp == 'tt': + return (True, ) + else: + return (False, ) + + +class ImpactNotEmptySEGS: + @classmethod + def INPUT_TYPES(cls): + return {"required": {"segs": ("SEGS",)}} + + FUNCTION = "doit" + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = ("BOOLEAN", ) + + def doit(self, segs): + return (segs[1] != [], ) + + +class ImpactConditionalBranch: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "cond": ("BOOLEAN",), + "tt_value": (any_typ,{"lazy": True}), + "ff_value": (any_typ,{"lazy": True}), + }, + } + + FUNCTION = "doit" + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = (any_typ, ) + + def check_lazy_status(self, cond, tt_value=None, ff_value=None): + if cond and tt_value is None: + return ["tt_value"] + if not cond and ff_value is None: + return ["ff_value"] + + def doit(self, cond, tt_value=None, ff_value=None): + if cond: + return (tt_value,) + else: + return (ff_value,) + + +class ImpactConditionalBranchSelMode: + @classmethod + def INPUT_TYPES(cls): + if not core.is_execution_model_version_supported(): + required_inputs = { + "cond": ("BOOLEAN",), + "sel_mode": ("BOOLEAN", {"default": True, "label_on": "select_on_prompt", "label_off": "select_on_execution"}), + } + else: + required_inputs = { + "cond": ("BOOLEAN",), + } + + return { + "required": required_inputs, + "optional": { + "tt_value": (any_typ,), + "ff_value": (any_typ,), + }, + } + + FUNCTION = "doit" + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = (any_typ, ) + + def doit(self, cond, tt_value=None, ff_value=None, **kwargs): + if cond: + return (tt_value,) + else: + return (ff_value,) + + +class ImpactConvertDataType: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return {"required": {"value": (any_typ,)}} + + RETURN_TYPES = ("STRING", "FLOAT", "INT", "BOOLEAN") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic" + + @staticmethod + def is_number(string): + pattern = re.compile(r'^[-+]?[0-9]*\.?[0-9]+$') + return bool(pattern.match(string)) + + def doit(self, value): + if self.is_number(str(value)): + num = value + else: + if str.lower(str(value)) != "false": + num = 1 + else: + num = 0 + return (str(value), float(num), int(float(num)), bool(float(num)), ) + + +class ImpactIfNone: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": {}, + "optional": {"signal": (any_typ,), "any_input": (any_typ,), } + } + + RETURN_TYPES = (any_typ, "BOOLEAN") + RETURN_NAMES = ("signal_opt", "bool") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic" + + def doit(self, signal=None, any_input=None): + if any_input is None: + return (signal, False, ) + else: + return (signal, True, ) + + +class ImpactLogicalOperators: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "operator": (['and', 'or', 'xor'],), + "bool_a": ("BOOLEAN", {"forceInput": True}), + "bool_b": ("BOOLEAN", {"forceInput": True}), + }, + } + + FUNCTION = "doit" + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = ("BOOLEAN", ) + + def doit(self, operator, bool_a, bool_b): + if operator == "and": + return (bool_a and bool_b, ) + elif operator == "or": + return (bool_a or bool_b, ) + else: + return (bool_a != bool_b, ) + + +class ImpactConditionalStopIteration: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { "cond": ("BOOLEAN", {"forceInput": True}), }, + } + + FUNCTION = "doit" + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = () + + OUTPUT_NODE = True + + def doit(self, cond): + if cond: + PromptServer.instance.send_sync("stop-iteration", {}) + return {} + + +class ImpactNeg: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { "value": ("BOOLEAN", {"forceInput": True}), }, + } + + FUNCTION = "doit" + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = ("BOOLEAN", ) + + def doit(self, value): + return (not value, ) + + +class ImpactInt: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "value": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + }, + } + + FUNCTION = "doit" + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = ("INT", ) + + def doit(self, value): + return (value, ) + + +class ImpactFloat: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "value": ("FLOAT", {"default": 1.0, "min": -3.402823466e+38, "max": 3.402823466e+38}), + }, + } + + FUNCTION = "doit" + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = ("FLOAT", ) + + def doit(self, value): + return (value, ) + + +class ImpactBoolean: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "value": ("BOOLEAN", {"default": False}), + }, + } + + FUNCTION = "doit" + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = ("BOOLEAN", ) + + def doit(self, value): + return (value, ) + + +class ImpactValueSender: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "value": (any_typ, ), + "link_id": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + }, + "optional": { + "signal_opt": (any_typ,), + } + } + + OUTPUT_NODE = True + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = (any_typ, ) + RETURN_NAMES = ("signal", ) + + def doit(self, value, link_id=0, signal_opt=None): + PromptServer.instance.send_sync("value-send", {"link_id": link_id, "value": value}) + return (signal_opt, ) + + +class ImpactIntConstSender: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "signal": (any_typ, ), + "value": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + "link_id": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + }, + } + + OUTPUT_NODE = True + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = () + + def doit(self, signal, value, link_id=0): + PromptServer.instance.send_sync("value-send", {"link_id": link_id, "value": value}) + return {} + + +class ImpactValueReceiver: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "typ": (["STRING", "INT", "FLOAT", "BOOLEAN"], ), + "value": ("STRING", {"default": ""}), + "link_id": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + }, + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic" + + RETURN_TYPES = (any_typ, ) + + def doit(self, typ, value, link_id=0): + if typ == "INT": + return (int(value), ) + elif typ == "FLOAT": + return (float(value), ) + elif typ == "BOOLEAN": + return (value.lower() == "true", ) + else: + return (value, ) + + +class ImpactImageInfo: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "value": ("IMAGE", ), + }, + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic/_for_test" + + RETURN_TYPES = ("INT", "INT", "INT", "INT") + RETURN_NAMES = ("batch", "height", "width", "channel") + + def doit(self, value): + return (value.shape[0], value.shape[1], value.shape[2], value.shape[3]) + + +class ImpactLatentInfo: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "value": ("LATENT", ), + }, + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic/_for_test" + + RETURN_TYPES = ("INT", "INT", "INT", "INT") + RETURN_NAMES = ("batch", "height", "width", "channel") + + def doit(self, value): + shape = value['samples'].shape + return (shape[0], shape[2] * 8, shape[3] * 8, shape[1]) + + +class ImpactMinMax: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "mode": ("BOOLEAN", {"default": True, "label_on": "max", "label_off": "min"}), + "a": (any_typ,), + "b": (any_typ,), + }, + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic/_for_test" + + RETURN_TYPES = ("INT", ) + + def doit(self, mode, a, b): + if mode: + return (max(a, b), ) + else: + return (min(a, b),) + + +class ImpactQueueTrigger: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "signal": (any_typ,), + "mode": ("BOOLEAN", {"default": True, "label_on": "Trigger", "label_off": "Don't trigger"}), + } + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic/_for_test" + RETURN_TYPES = (any_typ,) + RETURN_NAMES = ("signal_opt",) + OUTPUT_NODE = True + + def doit(self, signal, mode): + if(mode): + PromptServer.instance.send_sync("impact-add-queue", {}) + + return (signal,) + + +class ImpactQueueTriggerCountdown: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "count": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "total": ("INT", {"default": 10, "min": 1, "max": 0xffffffffffffffff}), + "mode": ("BOOLEAN", {"default": True, "label_on": "Trigger", "label_off": "Don't trigger"}), + }, + "optional": {"signal": (any_typ,),}, + "hidden": {"unique_id": "UNIQUE_ID"} + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic/_for_test" + RETURN_TYPES = (any_typ, "INT", "INT") + RETURN_NAMES = ("signal_opt", "count", "total") + OUTPUT_NODE = True + + def doit(self, count, total, mode, unique_id, signal=None): + if (mode): + if count < total - 1: + PromptServer.instance.send_sync("impact-node-feedback", + {"node_id": unique_id, "widget_name": "count", "type": "int", "value": count+1}) + PromptServer.instance.send_sync("impact-add-queue", {}) + if count >= total - 1: + PromptServer.instance.send_sync("impact-node-feedback", + {"node_id": unique_id, "widget_name": "count", "type": "int", "value": 0}) + + return (signal, count, total) + + + +class ImpactSetWidgetValue: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "signal": (any_typ,), + "node_id": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "widget_name": ("STRING", {"multiline": False}), + }, + "optional": { + "boolean_value": ("BOOLEAN", {"forceInput": True}), + "int_value": ("INT", {"forceInput": True}), + "float_value": ("FLOAT", {"forceInput": True}), + "string_value": ("STRING", {"forceInput": True}), + } + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic/_for_test" + RETURN_TYPES = (any_typ,) + RETURN_NAMES = ("signal_opt",) + OUTPUT_NODE = True + + def doit(self, signal, node_id, widget_name, boolean_value=None, int_value=None, float_value=None, string_value=None, ): + kind = None + if boolean_value is not None: + value = boolean_value + kind = "BOOLEAN" + elif int_value is not None: + value = int_value + kind = "INT" + elif float_value is not None: + value = float_value + kind = "FLOAT" + elif string_value is not None: + value = string_value + kind = "STRING" + else: + value = None + + if value is not None: + PromptServer.instance.send_sync("impact-node-feedback", + {"node_id": node_id, "widget_name": widget_name, "type": kind, "value": value}) + + return (signal,) + + +class ImpactNodeSetMuteState: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "signal": (any_typ,), + "node_id": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "set_state": ("BOOLEAN", {"default": True, "label_on": "active", "label_off": "mute"}), + } + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic/_for_test" + RETURN_TYPES = (any_typ,) + RETURN_NAMES = ("signal_opt",) + OUTPUT_NODE = True + + def doit(self, signal, node_id, set_state): + PromptServer.instance.send_sync("impact-node-mute-state", {"node_id": node_id, "is_active": set_state}) + return (signal,) + + +class ImpactSleep: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "signal": (any_typ,), + "seconds": ("FLOAT", {"default": 0.5, "min": 0, "max": 3600}), + } + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic/_for_test" + RETURN_TYPES = (any_typ,) + RETURN_NAMES = ("signal_opt",) + OUTPUT_NODE = True + + def doit(self, signal, seconds): + time.sleep(seconds) + return (signal,) + + +def workflow_to_map(workflow): + nodes = {} + links = {} + for link in workflow['links']: + links[link[0]] = link[1:] + for node in workflow['nodes']: + nodes[str(node['id'])] = node + + return nodes, links + + +class ImpactRemoteBoolean: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "node_id": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "widget_name": ("STRING", {"multiline": False}), + "value": ("BOOLEAN", {"default": True, "label_on": "True", "label_off": "False"}), + }} + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic/_for_test" + RETURN_TYPES = () + OUTPUT_NODE = True + + def doit(self, **kwargs): + return {} + + +class ImpactRemoteInt: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "node_id": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "widget_name": ("STRING", {"multiline": False}), + "value": ("INT", {"default": 0, "min": -0xffffffffffffffff, "max": 0xffffffffffffffff}), + }} + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic/_for_test" + RETURN_TYPES = () + OUTPUT_NODE = True + + def doit(self, **kwargs): + return {} + +class ImpactControlBridge: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "value": (any_typ,), + "mode": ("BOOLEAN", {"default": True, "label_on": "Active", "label_off": "Stop/Mute/Bypass"}), + "behavior": (["Stop", "Mute", "Bypass"], ), + }, + "hidden": {"unique_id": "UNIQUE_ID", "prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"} + } + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Logic" + RETURN_TYPES = (any_typ,) + RETURN_NAMES = ("value",) + OUTPUT_NODE = True + + DESCRIPTION = ("When behavior is Stop and mode is active, the input value is passed directly to the output.\n" + "When behavior is Mute/Bypass and mode is active, the node connected to the output is changed to active state.\n" + "When behavior is Stop and mode is Stop/Mute/Bypass, the workflow execution of the current node is halted.\n" + "When behavior is Mute/Bypass and mode is Stop/Mute/Bypass, the node connected to the output is changed to Mute/Bypass state.") + + @classmethod + def IS_CHANGED(self, value, mode, behavior="Stop", unique_id=None, prompt=None, extra_pnginfo=None): + if behavior == "Stop": + return value, mode, behavior + else: + # NOTE: extra_pnginfo is not populated for IS_CHANGED. + # so extra_pnginfo is useless in here + try: + workflow = core.current_prompt['extra_data']['extra_pnginfo']['workflow'] + except Exception: + logging.info("[Impact Pack] core.current_prompt['extra_data']['extra_pnginfo']['workflow']") + return 0 + + nodes, links = workflow_to_map(workflow) + next_nodes = [] + + for link in nodes[unique_id]['outputs'][0]['links']: + node_id = str(links[link][2]) + impact.utils.collect_non_reroute_nodes(nodes, links, next_nodes, node_id) + + return next_nodes + + def doit(self, value, mode, behavior="Stop", unique_id=None, prompt=None, extra_pnginfo=None): + global error_skip_flag + + if core.is_execution_model_version_supported(): + from comfy_execution.graph import ExecutionBlocker + else: + logging.info("[Impact Pack] ImpactControlBridge: ComfyUI is outdated. The 'Stop' behavior cannot function properly.") + + if behavior == "Stop": + if mode: + return (value, ) + else: + return (ExecutionBlocker(None), ) + elif extra_pnginfo is None: + logging.warning(f"[Impact Pack] limitation: '{behavior}' behavior cannot be used in API execution.") + return (value,) + else: + workflow_nodes, links = workflow_to_map(extra_pnginfo['workflow']) + + active_nodes = [] + mute_nodes = [] + bypass_nodes = [] + + for link in workflow_nodes[unique_id]['outputs'][0]['links']: + node_id = str(links[link][2]) + + next_nodes = [] + impact.utils.collect_non_reroute_nodes(workflow_nodes, links, next_nodes, node_id) + + for next_node_id in next_nodes: + node_mode = workflow_nodes[next_node_id]['mode'] + + if node_mode == 0: + active_nodes.append(next_node_id) + elif node_mode == 2: + mute_nodes.append(next_node_id) + elif node_mode == 4: + bypass_nodes.append(next_node_id) + + if mode: + # active + should_be_active_nodes = mute_nodes + bypass_nodes + if len(should_be_active_nodes) > 0: + PromptServer.instance.send_sync("impact-bridge-continue", {"node_id": unique_id, 'actives': list(should_be_active_nodes)}) + nodes.interrupt_processing() + + elif behavior == "Mute" or behavior == True: # noqa: E712 + # mute + should_be_mute_nodes = active_nodes + bypass_nodes + if len(should_be_mute_nodes) > 0: + PromptServer.instance.send_sync("impact-bridge-continue", {"node_id": unique_id, 'mutes': list(should_be_mute_nodes)}) + nodes.interrupt_processing() + + else: + # bypass + should_be_bypass_nodes = active_nodes + mute_nodes + if len(should_be_bypass_nodes) > 0: + PromptServer.instance.send_sync("impact-bridge-continue", {"node_id": unique_id, 'bypasses': list(should_be_bypass_nodes)}) + nodes.interrupt_processing() + + return (value, ) + + +class ImpactExecutionOrderController: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "signal": (any_typ,), + "value": (any_typ,), + }} + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + RETURN_TYPES = (any_typ, any_typ) + RETURN_NAMES = ("signal", "value") + + def doit(self, signal, value): + return signal, value + + +class ImpactListBridge: + @classmethod + def INPUT_TYPES(cls): + return {"required": { + "list_input": (any_typ,), + }} + + FUNCTION = "doit" + + DESCRIPTION = "When passing the list output through this node, it collects and organizes the data before forwarding it, which ensures that the previous stage's sub-workflow has been completed." + + CATEGORY = "ImpactPack/Util" + RETURN_TYPES = (any_typ, ) + RETURN_NAMES = ("list_output", ) + + INPUT_IS_LIST = True + OUTPUT_IS_LIST = (True, ) + + @staticmethod + def doit(list_input): + return (list_input,) + + +original_handle_execution = execution.PromptExecutor.handle_execution_error + + +def handle_execution_error(**kwargs): + execution.PromptExecutor.handle_execution_error(**kwargs) + diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/pipe.py b/custom_nodes/comfyui-impact-pack/modules/impact/pipe.py new file mode 100644 index 0000000000000000000000000000000000000000..4e782f6a88690b3ef6e376746ee0eabb7c8ea08e --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/pipe.py @@ -0,0 +1,440 @@ +import folder_paths +from impact.utils import any_typ + + +class ToDetailerPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "model": ("MODEL",), + "clip": ("CLIP",), + "vae": ("VAE",), + "positive": ("CONDITIONING",), + "negative": ("CONDITIONING",), + "bbox_detector": ("BBOX_DETECTOR", ), + "wildcard": ("STRING", {"multiline": True, "dynamicPrompts": False}), + "Select to add LoRA": (["Select the LoRA to add to the text"] + folder_paths.get_filename_list("loras"),), + "Select to add Wildcard": (["Select the Wildcard to add to the text"], ), + }, + "optional": { + "sam_model_opt": ("SAM_MODEL",), + "segm_detector_opt": ("SEGM_DETECTOR",), + "detailer_hook": ("DETAILER_HOOK",), + }} + + RETURN_TYPES = ("DETAILER_PIPE", ) + RETURN_NAMES = ("detailer_pipe", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, *args, **kwargs): + pipe = (kwargs['model'], kwargs['clip'], kwargs['vae'], kwargs['positive'], kwargs['negative'], kwargs['wildcard'], kwargs['bbox_detector'], + kwargs.get('segm_detector_opt', None), kwargs.get('sam_model_opt', None), kwargs.get('detailer_hook', None), + kwargs.get('refiner_model', None), kwargs.get('refiner_clip', None), + kwargs.get('refiner_positive', None), kwargs.get('refiner_negative', None)) + return (pipe, ) + + +class ToDetailerPipeSDXL(ToDetailerPipe): + @classmethod + def INPUT_TYPES(s): + return {"required": { + "model": ("MODEL",), + "clip": ("CLIP",), + "vae": ("VAE",), + "positive": ("CONDITIONING",), + "negative": ("CONDITIONING",), + "refiner_model": ("MODEL",), + "refiner_clip": ("CLIP",), + "refiner_positive": ("CONDITIONING",), + "refiner_negative": ("CONDITIONING",), + "bbox_detector": ("BBOX_DETECTOR", ), + "wildcard": ("STRING", {"multiline": True, "dynamicPrompts": False}), + "Select to add LoRA": (["Select the LoRA to add to the text"] + folder_paths.get_filename_list("loras"),), + "Select to add Wildcard": (["Select the Wildcard to add to the text"],), + }, + "optional": { + "sam_model_opt": ("SAM_MODEL",), + "segm_detector_opt": ("SEGM_DETECTOR",), + "detailer_hook": ("DETAILER_HOOK",), + }} + + +class FromDetailerPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": {"detailer_pipe": ("DETAILER_PIPE",), }, } + + RETURN_TYPES = ("MODEL", "CLIP", "VAE", "CONDITIONING", "CONDITIONING", "BBOX_DETECTOR", "SAM_MODEL", "SEGM_DETECTOR", "DETAILER_HOOK") + RETURN_NAMES = ("model", "clip", "vae", "positive", "negative", "bbox_detector", "sam_model_opt", "segm_detector_opt", "detailer_hook") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, detailer_pipe): + model, clip, vae, positive, negative, wildcard, bbox_detector, segm_detector_opt, sam_model_opt, detailer_hook, _, _, _, _ = detailer_pipe + return model, clip, vae, positive, negative, bbox_detector, sam_model_opt, segm_detector_opt, detailer_hook + + +class FromDetailerPipe_v2: + @classmethod + def INPUT_TYPES(s): + return {"required": {"detailer_pipe": ("DETAILER_PIPE",), }, } + + RETURN_TYPES = ("DETAILER_PIPE", "MODEL", "CLIP", "VAE", "CONDITIONING", "CONDITIONING", "BBOX_DETECTOR", "SAM_MODEL", "SEGM_DETECTOR", "DETAILER_HOOK") + RETURN_NAMES = ("detailer_pipe", "model", "clip", "vae", "positive", "negative", "bbox_detector", "sam_model_opt", "segm_detector_opt", "detailer_hook") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, detailer_pipe): + model, clip, vae, positive, negative, wildcard, bbox_detector, segm_detector_opt, sam_model_opt, detailer_hook, _, _, _, _ = detailer_pipe + return detailer_pipe, model, clip, vae, positive, negative, bbox_detector, sam_model_opt, segm_detector_opt, detailer_hook + + +class FromDetailerPipe_SDXL: + @classmethod + def INPUT_TYPES(s): + return {"required": {"detailer_pipe": ("DETAILER_PIPE",), }, } + + RETURN_TYPES = ("DETAILER_PIPE", "MODEL", "CLIP", "VAE", "CONDITIONING", "CONDITIONING", "BBOX_DETECTOR", "SAM_MODEL", "SEGM_DETECTOR", "DETAILER_HOOK", "MODEL", "CLIP", "CONDITIONING", "CONDITIONING") + RETURN_NAMES = ("detailer_pipe", "model", "clip", "vae", "positive", "negative", "bbox_detector", "sam_model_opt", "segm_detector_opt", "detailer_hook", "refiner_model", "refiner_clip", "refiner_positive", "refiner_negative") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, detailer_pipe): + model, clip, vae, positive, negative, wildcard, bbox_detector, segm_detector_opt, sam_model_opt, detailer_hook, refiner_model, refiner_clip, refiner_positive, refiner_negative = detailer_pipe + return detailer_pipe, model, clip, vae, positive, negative, bbox_detector, sam_model_opt, segm_detector_opt, detailer_hook, refiner_model, refiner_clip, refiner_positive, refiner_negative + + +class AnyPipeToBasic: + @classmethod + def INPUT_TYPES(s): + return { + "required": {"any_pipe": (any_typ,)}, + } + + RETURN_TYPES = ("BASIC_PIPE", ) + RETURN_NAMES = ("basic_pipe", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, any_pipe): + return (any_pipe[:5], ) + + +class ToBasicPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "model": ("MODEL",), + "clip": ("CLIP",), + "vae": ("VAE",), + "positive": ("CONDITIONING",), + "negative": ("CONDITIONING",), + }, + } + + RETURN_TYPES = ("BASIC_PIPE", ) + RETURN_NAMES = ("basic_pipe", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, model, clip, vae, positive, negative): + pipe = (model, clip, vae, positive, negative) + return (pipe, ) + + +class FromBasicPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": {"basic_pipe": ("BASIC_PIPE",), }, } + + RETURN_TYPES = ("MODEL", "CLIP", "VAE", "CONDITIONING", "CONDITIONING") + RETURN_NAMES = ("model", "clip", "vae", "positive", "negative") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, basic_pipe): + model, clip, vae, positive, negative = basic_pipe + return model, clip, vae, positive, negative + + +class FromBasicPipe_v2: + @classmethod + def INPUT_TYPES(s): + return {"required": {"basic_pipe": ("BASIC_PIPE",), }, } + + RETURN_TYPES = ("BASIC_PIPE", "MODEL", "CLIP", "VAE", "CONDITIONING", "CONDITIONING") + RETURN_NAMES = ("basic_pipe", "model", "clip", "vae", "positive", "negative") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, basic_pipe): + model, clip, vae, positive, negative = basic_pipe + return basic_pipe, model, clip, vae, positive, negative + + +class BasicPipeToDetailerPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": {"basic_pipe": ("BASIC_PIPE",), + "bbox_detector": ("BBOX_DETECTOR", ), + "wildcard": ("STRING", {"multiline": True, "dynamicPrompts": False}), + "Select to add LoRA": (["Select the LoRA to add to the text"] + folder_paths.get_filename_list("loras"),), + "Select to add Wildcard": (["Select the Wildcard to add to the text"],), + }, + "optional": { + "sam_model_opt": ("SAM_MODEL", ), + "segm_detector_opt": ("SEGM_DETECTOR",), + "detailer_hook": ("DETAILER_HOOK",), + }, + } + + RETURN_TYPES = ("DETAILER_PIPE", ) + RETURN_NAMES = ("detailer_pipe", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, *args, **kwargs): + basic_pipe = kwargs['basic_pipe'] + bbox_detector = kwargs['bbox_detector'] + wildcard = kwargs['wildcard'] + sam_model_opt = kwargs.get('sam_model_opt', None) + segm_detector_opt = kwargs.get('segm_detector_opt', None) + detailer_hook = kwargs.get('detailer_hook', None) + + model, clip, vae, positive, negative = basic_pipe + pipe = model, clip, vae, positive, negative, wildcard, bbox_detector, segm_detector_opt, sam_model_opt, detailer_hook, None, None, None, None + return (pipe, ) + + +class BasicPipeToDetailerPipeSDXL: + @classmethod + def INPUT_TYPES(s): + return {"required": {"base_basic_pipe": ("BASIC_PIPE",), + "refiner_basic_pipe": ("BASIC_PIPE",), + "bbox_detector": ("BBOX_DETECTOR", ), + "wildcard": ("STRING", {"multiline": True, "dynamicPrompts": False}), + "Select to add LoRA": (["Select the LoRA to add to the text"] + folder_paths.get_filename_list("loras"),), + "Select to add Wildcard": (["Select the Wildcard to add to the text"],), + }, + "optional": { + "sam_model_opt": ("SAM_MODEL", ), + "segm_detector_opt": ("SEGM_DETECTOR",), + "detailer_hook": ("DETAILER_HOOK",), + }, + } + + RETURN_TYPES = ("DETAILER_PIPE", ) + RETURN_NAMES = ("detailer_pipe", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, *args, **kwargs): + base_basic_pipe = kwargs['base_basic_pipe'] + refiner_basic_pipe = kwargs['refiner_basic_pipe'] + bbox_detector = kwargs['bbox_detector'] + wildcard = kwargs['wildcard'] + sam_model_opt = kwargs.get('sam_model_opt', None) + segm_detector_opt = kwargs.get('segm_detector_opt', None) + detailer_hook = kwargs.get('detailer_hook', None) + + model, clip, vae, positive, negative = base_basic_pipe + refiner_model, refiner_clip, refiner_vae, refiner_positive, refiner_negative = refiner_basic_pipe + pipe = model, clip, vae, positive, negative, wildcard, bbox_detector, segm_detector_opt, sam_model_opt, detailer_hook, refiner_model, refiner_clip, refiner_positive, refiner_negative + return (pipe, ) + + +class DetailerPipeToBasicPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": {"detailer_pipe": ("DETAILER_PIPE",), }} + + RETURN_TYPES = ("BASIC_PIPE", "BASIC_PIPE") + RETURN_NAMES = ("base_basic_pipe", "refiner_basic_pipe") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, detailer_pipe): + model, clip, vae, positive, negative, _, _, _, _, _, refiner_model, refiner_clip, refiner_positive, refiner_negative = detailer_pipe + pipe = model, clip, vae, positive, negative + refiner_pipe = refiner_model, refiner_clip, vae, refiner_positive, refiner_negative + return (pipe, refiner_pipe) + + +class EditBasicPipe: + @classmethod + def INPUT_TYPES(s): + return { + "required": {"basic_pipe": ("BASIC_PIPE",), }, + "optional": { + "model": ("MODEL",), + "clip": ("CLIP",), + "vae": ("VAE",), + "positive": ("CONDITIONING",), + "negative": ("CONDITIONING",), + }, + } + + RETURN_TYPES = ("BASIC_PIPE", ) + RETURN_NAMES = ("basic_pipe", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, basic_pipe, model=None, clip=None, vae=None, positive=None, negative=None): + res_model, res_clip, res_vae, res_positive, res_negative = basic_pipe + + if model is not None: + res_model = model + + if clip is not None: + res_clip = clip + + if vae is not None: + res_vae = vae + + if positive is not None: + res_positive = positive + + if negative is not None: + res_negative = negative + + pipe = res_model, res_clip, res_vae, res_positive, res_negative + + return (pipe, ) + + +class EditDetailerPipe: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "detailer_pipe": ("DETAILER_PIPE",), + "wildcard": ("STRING", {"multiline": True, "dynamicPrompts": False}), + "Select to add LoRA": (["Select the LoRA to add to the text"] + folder_paths.get_filename_list("loras"),), + "Select to add Wildcard": (["Select the Wildcard to add to the text"],), + }, + "optional": { + "model": ("MODEL",), + "clip": ("CLIP",), + "vae": ("VAE",), + "positive": ("CONDITIONING",), + "negative": ("CONDITIONING",), + "bbox_detector": ("BBOX_DETECTOR",), + "sam_model": ("SAM_MODEL",), + "segm_detector": ("SEGM_DETECTOR",), + "detailer_hook": ("DETAILER_HOOK",), + }, + } + + RETURN_TYPES = ("DETAILER_PIPE",) + RETURN_NAMES = ("detailer_pipe",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Pipe" + + def doit(self, *args, **kwargs): + detailer_pipe = kwargs['detailer_pipe'] + wildcard = kwargs['wildcard'] + model = kwargs.get('model', None) + clip = kwargs.get('clip', None) + vae = kwargs.get('vae', None) + positive = kwargs.get('positive', None) + negative = kwargs.get('negative', None) + bbox_detector = kwargs.get('bbox_detector', None) + sam_model = kwargs.get('sam_model', None) + segm_detector = kwargs.get('segm_detector', None) + detailer_hook = kwargs.get('detailer_hook', None) + refiner_model = kwargs.get('refiner_model', None) + refiner_clip = kwargs.get('refiner_clip', None) + refiner_positive = kwargs.get('refiner_positive', None) + refiner_negative = kwargs.get('refiner_negative', None) + + res_model, res_clip, res_vae, res_positive, res_negative, res_wildcard, res_bbox_detector, res_segm_detector, res_sam_model, res_detailer_hook, res_refiner_model, res_refiner_clip, res_refiner_positive, res_refiner_negative = detailer_pipe + + if model is not None: + res_model = model + + if clip is not None: + res_clip = clip + + if vae is not None: + res_vae = vae + + if positive is not None: + res_positive = positive + + if negative is not None: + res_negative = negative + + if bbox_detector is not None: + res_bbox_detector = bbox_detector + + if segm_detector is not None: + res_segm_detector = segm_detector + + if wildcard != "": + res_wildcard = wildcard + + if sam_model is not None: + res_sam_model = sam_model + + if detailer_hook is not None: + res_detailer_hook = detailer_hook + + if refiner_model is not None: + res_refiner_model = refiner_model + + if refiner_clip is not None: + res_refiner_clip = refiner_clip + + if refiner_positive is not None: + res_refiner_positive = refiner_positive + + if refiner_negative is not None: + res_refiner_negative = refiner_negative + + pipe = (res_model, res_clip, res_vae, res_positive, res_negative, res_wildcard, + res_bbox_detector, res_segm_detector, res_sam_model, res_detailer_hook, + res_refiner_model, res_refiner_clip, res_refiner_positive, res_refiner_negative) + + return (pipe, ) + + +class EditDetailerPipeSDXL(EditDetailerPipe): + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "detailer_pipe": ("DETAILER_PIPE",), + "wildcard": ("STRING", {"multiline": True, "dynamicPrompts": False}), + "Select to add LoRA": (["Select the LoRA to add to the text"] + folder_paths.get_filename_list("loras"),), + "Select to add Wildcard": (["Select the Wildcard to add to the text"],), + }, + "optional": { + "model": ("MODEL",), + "clip": ("CLIP",), + "vae": ("VAE",), + "positive": ("CONDITIONING",), + "negative": ("CONDITIONING",), + "refiner_model": ("MODEL",), + "refiner_clip": ("CLIP",), + "refiner_positive": ("CONDITIONING",), + "refiner_negative": ("CONDITIONING",), + "bbox_detector": ("BBOX_DETECTOR",), + "sam_model": ("SAM_MODEL",), + "segm_detector": ("SEGM_DETECTOR",), + "detailer_hook": ("DETAILER_HOOK",), + }, + } diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/segs_nodes.py b/custom_nodes/comfyui-impact-pack/modules/impact/segs_nodes.py new file mode 100644 index 0000000000000000000000000000000000000000..7ea145cd84c3c1c23871a9b314809565feaf1351 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/segs_nodes.py @@ -0,0 +1,2025 @@ +import os +import sys + +import impact.impact_server +from nodes import MAX_RESOLUTION + +from . import core +from .core import SEG +import impact.utils as utils +from . import defs +from . import segs_upscaler +from comfy.cli_args import args +import math +from PIL import Image +import comfy +import numpy as np +import torch +import folder_paths +import logging + + +from typing import Callable, Union + +try: + from comfy_extras import nodes_differential_diffusion +except Exception: + logging.info("\n#############################################\n[Impact Pack] ComfyUI is an outdated version.\n#############################################\n") + raise Exception("[Impact Pack] ComfyUI is an outdated version.") + + +class SEGSDetailer: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "image": ("IMAGE", ), + "segs": ("SEGS", ), + "guide_size": ("FLOAT", {"default": 512, "min": 64, "max": MAX_RESOLUTION, "step": 8}), + "guide_size_for": ("BOOLEAN", {"default": True, "label_on": "bbox", "label_off": "crop_region"}), + "max_size": ("FLOAT", {"default": 768, "min": 64, "max": MAX_RESOLUTION, "step": 8}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + "noise_mask": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "force_inpaint": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), + "basic_pipe": ("BASIC_PIPE", {"tooltip": "If the `ImpactDummyInput` is connected to the model in the basic_pipe, the inference stage is skipped."}), + "refiner_ratio": ("FLOAT", {"default": 0.2, "min": 0.0, "max": 1.0}), + "batch_size": ("INT", {"default": 1, "min": 1, "max": 100}), + + "cycle": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1}), + }, + "optional": { + "refiner_basic_pipe_opt": ("BASIC_PIPE",), + "inpaint_model": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + } + } + + RETURN_TYPES = ("SEGS", "IMAGE") + RETURN_NAMES = ("segs", "cnet_images") + OUTPUT_IS_LIST = (False, True) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + DESCRIPTION = "This node enhances details by inpainting each region within the detected area bundle (SEGS) after enlarging them based on the guide size.\nThis node is applied specifically to SEGS rather than the entire image. To apply it to the entire image, use the 'SEGS Paste' node." + + @staticmethod + def do_detail(image, segs, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, scheduler, + denoise, noise_mask, force_inpaint, basic_pipe, refiner_ratio=None, batch_size=1, cycle=1, + refiner_basic_pipe_opt=None, inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None): + + model, clip, vae, positive, negative = basic_pipe + if refiner_basic_pipe_opt is None: + refiner_model, refiner_clip, refiner_positive, refiner_negative = None, None, None, None + else: + refiner_model, refiner_clip, _, refiner_positive, refiner_negative = refiner_basic_pipe_opt + + segs = core.segs_scale_match(segs, image.shape) + + new_segs = [] + cnet_pil_list = [] + + if not (isinstance(model, str) and model == "DUMMY") and noise_mask_feather > 0 and 'denoise_mask_function' not in model.model_options: + model = nodes_differential_diffusion.DifferentialDiffusion().apply(model)[0] + + for i in range(batch_size): + seed += 1 + for seg in segs[1]: + cropped_image = seg.cropped_image if seg.cropped_image is not None \ + else utils.crop_ndarray4(image.numpy(), seg.crop_region) + cropped_image = utils.to_tensor(cropped_image) + + is_mask_all_zeros = (seg.cropped_mask == 0).all().item() + if is_mask_all_zeros: + logging.info("Detailer: segment skip [empty mask]") + new_segs.append(seg) + continue + + if noise_mask: + cropped_mask = seg.cropped_mask + else: + cropped_mask = None + + cropped_positive = [ + [condition, { + k: core.crop_condition_mask(v, image, seg.crop_region) if k == "mask" else v + for k, v in details.items() + }] + for condition, details in positive + ] + + cropped_negative = [ + [condition, { + k: core.crop_condition_mask(v, image, seg.crop_region) if k == "mask" else v + for k, v in details.items() + }] + for condition, details in negative + ] + + if not (isinstance(model, str) and model == "DUMMY"): + enhanced_image, cnet_pils = core.enhance_detail(cropped_image, model, clip, vae, guide_size, guide_size_for, max_size, + seg.bbox, seed, steps, cfg, sampler_name, scheduler, + cropped_positive, cropped_negative, denoise, cropped_mask, force_inpaint, + refiner_ratio=refiner_ratio, refiner_model=refiner_model, + refiner_clip=refiner_clip, refiner_positive=refiner_positive, refiner_negative=refiner_negative, + control_net_wrapper=seg.control_net_wrapper, cycle=cycle, + inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, scheduler_func=scheduler_func_opt) + else: + enhanced_image = cropped_image + cnet_pils = None + + if cnet_pils is not None: + cnet_pil_list.extend(cnet_pils) + + if enhanced_image is None: + new_cropped_image = cropped_image + else: + new_cropped_image = enhanced_image + + new_seg = SEG(utils.to_numpy(new_cropped_image), seg.cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, None) + new_segs.append(new_seg) + + return (segs[0], new_segs), cnet_pil_list + + def doit(self, image, segs, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, scheduler, + denoise, noise_mask, force_inpaint, basic_pipe, refiner_ratio=None, batch_size=1, cycle=1, + refiner_basic_pipe_opt=None, inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None): + + if len(image) > 1: + raise Exception('[Impact Pack] ERROR: SEGSDetailer does not allow image batches.\nPlease refer to https://github.com/ltdrdata/ComfyUI-extension-tutorials/blob/Main/ComfyUI-Impact-Pack/tutorial/batching-detailer.md for more information.') + + segs, cnet_pil_list = SEGSDetailer.do_detail(image, segs, guide_size, guide_size_for, max_size, seed, steps, cfg, sampler_name, + scheduler, denoise, noise_mask, force_inpaint, basic_pipe, refiner_ratio, batch_size, cycle=cycle, + refiner_basic_pipe_opt=refiner_basic_pipe_opt, + inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, scheduler_func_opt=scheduler_func_opt) + + # set fallback image + if len(cnet_pil_list) == 0: + cnet_pil_list = [utils.empty_pil_tensor()] + + return segs, cnet_pil_list + + +class SEGSPaste: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "image": ("IMAGE", ), + "segs": ("SEGS", ), + "feather": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}), + "alpha": ("INT", {"default": 255, "min": 0, "max": 255, "step": 1}), + }, + "optional": {"ref_image_opt": ("IMAGE", ), } + } + + RETURN_TYPES = ("IMAGE", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Detailer" + + DESCRIPTION = "This node provides a function to paste the enhanced SEGS, improved through the SEGS detailer, back onto the original image." + + @staticmethod + def doit(image, segs, feather, alpha=255, ref_image_opt=None): + + segs = core.segs_scale_match(segs, image.shape) + + result = None + for i, single_image in enumerate(image): + image_i = single_image.unsqueeze(0).clone() + + for seg in segs[1]: + ref_image = None + if ref_image_opt is None and seg.cropped_image is not None: + cropped_image = seg.cropped_image + if isinstance(cropped_image, np.ndarray): + cropped_image = torch.from_numpy(cropped_image) + ref_image = cropped_image[i].unsqueeze(0) + elif ref_image_opt is not None: + ref_tensor = ref_image_opt[i].unsqueeze(0) + ref_image = utils.crop_image(ref_tensor, seg.crop_region) + if ref_image is not None: + if seg.cropped_mask.ndim == 3 and len(seg.cropped_mask) == len(image): + mask = seg.cropped_mask[i] + elif seg.cropped_mask.ndim == 3 and len(seg.cropped_mask) > 1: + logging.warning(f"[Impact Pack] SEGSPaste: The number of the mask batch({len(seg.cropped_mask)}) and the image batch({len(image)}) are different. Combine the mask frames and apply.") + combined_mask = (seg.cropped_mask[0] * 255).to(torch.uint8) + + for frame_mask in seg.cropped_mask[1:]: + combined_mask |= (frame_mask * 255).to(torch.uint8) + + combined_mask = (combined_mask/255.0).to(torch.float32) + mask = utils.to_binary_mask(combined_mask, 0.1) + else: # ndim == 2 + mask = seg.cropped_mask + + mask = utils.tensor_gaussian_blur_mask(mask, feather) * (alpha/255) + x, y, *_ = seg.crop_region + + # ensure same device + mask = mask.to(image_i.device) + ref_image = ref_image.to(image_i.device) + + utils.tensor_paste(image_i, ref_image, (x, y), mask) + + if result is None: + result = image_i + else: + result = torch.concat((result, image_i), dim=0) + + if not args.highvram and not args.gpu_only: + result = result.cpu() + + return (result, ) + + +class SEGSPreviewCNet: + def __init__(self): + self.output_dir = folder_paths.get_temp_directory() + self.type = "temp" + + @classmethod + def INPUT_TYPES(s): + return {"required": {"segs": ("SEGS", ),}, } + + RETURN_TYPES = ("IMAGE", ) + OUTPUT_IS_LIST = (True, ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + OUTPUT_NODE = True + + def doit(self, segs): + full_output_folder, filename, counter, subfolder, filename_prefix = \ + folder_paths.get_save_image_path("impact_seg_preview", self.output_dir, segs[0][1], segs[0][0]) + + results = list() + result_image_list = [] + + for seg in segs[1]: + file = f"{filename}_{counter:05}_.webp" + + if seg.control_net_wrapper is not None and seg.control_net_wrapper.control_image is not None: + cnet_image = seg.control_net_wrapper.control_image + result_image_list.append(cnet_image) + else: + cnet_image = utils.empty_pil_tensor(64, 64) + + cnet_pil = utils.tensor2pil(cnet_image) + cnet_pil.save(os.path.join(full_output_folder, file)) + + results.append({ + "filename": file, + "subfolder": subfolder, + "type": self.type + }) + + counter += 1 + + return {"ui": {"images": results}, "result": (result_image_list,)} + + +class SEGSPreview: + def __init__(self): + self.output_dir = folder_paths.get_temp_directory() + self.type = "temp" + + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + "alpha_mode": ("BOOLEAN", {"default": True, "label_on": "enable", "label_off": "disable"}), + "min_alpha": ("FLOAT", {"default": 0.2, "min": 0.0, "max": 1.0, "step": 0.01}), + }, + "optional": { + "fallback_image_opt": ("IMAGE", ), + } + } + + RETURN_TYPES = ("IMAGE", ) + OUTPUT_IS_LIST = (True, ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + OUTPUT_NODE = True + + def doit(self, segs, alpha_mode=True, min_alpha=0.0, fallback_image_opt=None): + full_output_folder, filename, counter, subfolder, filename_prefix = \ + folder_paths.get_save_image_path("impact_seg_preview", self.output_dir, segs[0][1], segs[0][0]) + + results = list() + result_image_list = [] + + if fallback_image_opt is not None: + segs = core.segs_scale_match(segs, fallback_image_opt.shape) + + if min_alpha != 0: + min_alpha = int(255 * min_alpha) + + if len(segs[1]) > 0: + if segs[1][0].cropped_image is not None: + batch_count = len(segs[1][0].cropped_image) + elif fallback_image_opt is not None: + batch_count = len(fallback_image_opt) + else: + return {"ui": {"images": results}} + + for seg in segs[1]: + result_image_batch = None + cached_mask = None + + def get_combined_mask(): + nonlocal cached_mask + + if cached_mask is not None: + return cached_mask + else: + if isinstance(seg.cropped_mask, np.ndarray): + masks = torch.tensor(seg.cropped_mask) + else: + masks = seg.cropped_mask + + cached_mask = (masks[0] * 255).to(torch.uint8) + for x in masks[1:]: + cached_mask |= (x * 255).to(torch.uint8) + cached_mask = (cached_mask/255.0).to(torch.float32) + cached_mask = utils.to_binary_mask(cached_mask, 0.1) + cached_mask = cached_mask.numpy() + + return cached_mask + + def stack_image(image, mask=None): + nonlocal result_image_batch + + if isinstance(image, np.ndarray): + image = torch.from_numpy(image) + + if mask is not None: + image *= torch.tensor(mask)[None, ..., None] + + if result_image_batch is None: + result_image_batch = image + else: + result_image_batch = torch.concat((result_image_batch, image), dim=0) + + for i in range(batch_count): + cropped_image = None + + if seg.cropped_image is not None: + cropped_image = seg.cropped_image[i, None] + elif fallback_image_opt is not None: + # take from original image + ref_image = fallback_image_opt[i].unsqueeze(0) + cropped_image = utils.crop_image(ref_image, seg.crop_region) + + if cropped_image is not None: + if isinstance(cropped_image, np.ndarray): + cropped_image = torch.from_numpy(cropped_image) + + cropped_image = cropped_image.clone() + cropped_pil = utils.to_pil(cropped_image) + + if alpha_mode: + if isinstance(seg.cropped_mask, np.ndarray): + cropped_mask = seg.cropped_mask + else: + if seg.cropped_image is not None and len(seg.cropped_image) != len(seg.cropped_mask): + cropped_mask = get_combined_mask() + else: + cropped_mask = seg.cropped_mask[i].numpy() + + mask_array = (cropped_mask * 255).astype(np.uint8) + + if min_alpha != 0: + mask_array[mask_array < min_alpha] = min_alpha + + mask_pil = Image.fromarray(mask_array, mode='L').resize(cropped_pil.size) + cropped_pil.putalpha(mask_pil) + stack_image(cropped_image, cropped_mask) + else: + stack_image(cropped_image) + + file = f"{filename}_{counter:05}_.webp" + cropped_pil.save(os.path.join(full_output_folder, file)) + results.append({ + "filename": file, + "subfolder": subfolder, + "type": self.type + }) + + counter += 1 + + if result_image_batch is not None: + result_image_list.append(result_image_batch) + + return {"ui": {"images": results}, "result": (result_image_list,) } + + +class SEGSLabelFilter: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + "preset": (['all'] + defs.detection_labels, ), + "labels": ("STRING", {"multiline": True, "placeholder": "List the types of segments to be allowed, separated by commas"}), + }, + } + + RETURN_TYPES = ("SEGS", "SEGS",) + RETURN_NAMES = ("filtered_SEGS", "remained_SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def filter(segs, labels): + labels = set([label.strip() for label in labels]) + + if 'all' in labels: + return (segs, (segs[0], []), ) + else: + res_segs = [] + remained_segs = [] + + for x in segs[1]: + if x.label in labels: + res_segs.append(x) + elif 'eyes' in labels and x.label in ['left_eye', 'right_eye']: + res_segs.append(x) + elif 'eyebrows' in labels and x.label in ['left_eyebrow', 'right_eyebrow']: + res_segs.append(x) + elif 'pupils' in labels and x.label in ['left_pupil', 'right_pupil']: + res_segs.append(x) + else: + remained_segs.append(x) + + return ((segs[0], res_segs), (segs[0], remained_segs), ) + + def doit(self, segs, preset, labels): + labels = labels.split(',') + return SEGSLabelFilter.filter(segs, labels) + + +class SEGSLabelAssign: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + "labels": ("STRING", {"multiline": True, "placeholder": "List the label to be assigned in order of segs, separated by commas"}), + }, + } + + RETURN_TYPES = ("SEGS",) + RETURN_NAMES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def assign(segs, labels): + labels = [label.strip() for label in labels] + + if len(labels) != len(segs[1]): + logging.warning(f'[Impact Pack] SEGSLabelAssign: length of labels ({len(labels)}) != length of segs ({len(segs[1])})') + + labeled_segs = [] + + idx = 0 + for x in segs[1]: + if len(labels) > idx: + x = x._replace(label=labels[idx]) + labeled_segs.append(x) + idx += 1 + + return ((segs[0], labeled_segs), ) + + def doit(self, segs, labels): + labels = labels.split(',') + return SEGSLabelAssign.assign(segs, labels) + + +class SEGSOrderedFilter: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + "target": (["area(=w*h)", "width", "height", "x1", "y1", "x2", "y2", "confidence", "none"],), + "order": ("BOOLEAN", {"default": True, "label_on": "descending", "label_off": "ascending"}), + "take_start": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + "take_count": ("INT", {"default": 1, "min": 0, "max": sys.maxsize, "step": 1}), + }, + } + + RETURN_TYPES = ("SEGS", "SEGS",) + RETURN_NAMES = ("filtered_SEGS", "remained_SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def get_sort_key_fn(target: str) -> Union[Callable, None]: + if target == "none": + return None + + def sort_key_fn(seg): + x1, y1, x2, y2 = seg.crop_region + if target == "confidence": return seg.confidence + if target == "area(=w*h)": return (x2 - x1) * (y2 - y1) + if target == "width": return x2 - x1 + if target == "height": return y2 - y1 + if target == "x1": return x1 + if target == "y1": return y1 + if target == "x2": return x2 + if target == "y2": return y2 + raise Exception(f"[Impact Pack] SEGSOrderedFilter - Unexpected target '{target}'") + + return sort_key_fn + + def doit(self, segs, target, order, take_start, take_count): + sort_key_fn = SEGSOrderedFilter.get_sort_key_fn(target) + + sorted_list = list(segs[1]) # make a shallow copy, so it does not mutate the original list when sort + if sort_key_fn is not None: + sorted_list.sort(key=sort_key_fn, reverse=order) + + take_stop = take_start + take_count + return (segs[0], sorted_list[take_start:take_stop]), \ + (segs[0], sorted_list[:take_start] + sorted_list[take_stop:]), + + +class SEGSRangeFilter: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + "target": (["area(=w*h)", "width", "height", "x1", "y1", "x2", "y2", "length_percent", "confidence(0-100)"],), + "mode": ("BOOLEAN", {"default": True, "label_on": "inside", "label_off": "outside"}), + "min_value": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1}), + "max_value": ("INT", {"default": 67108864, "min": 0, "max": sys.maxsize, "step": 1}), + }, + } + + RETURN_TYPES = ("SEGS", "SEGS",) + RETURN_NAMES = ("filtered_SEGS", "remained_SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, segs, target, mode, min_value, max_value): + new_segs = [] + remained_segs = [] + + for seg in segs[1]: + x1 = seg.crop_region[0] + y1 = seg.crop_region[1] + x2 = seg.crop_region[2] + y2 = seg.crop_region[3] + + if target == "area(=w*h)": + value = (y2 - y1) * (x2 - x1) + elif target == "length_percent": + h = y2 - y1 + w = x2 - x1 + value = max(h/w, w/h)*100 + elif target == "width": + value = x2 - x1 + elif target == "height": + value = y2 - y1 + elif target == "x1": + value = x1 + elif target == "x2": + value = x2 + elif target == "y1": + value = y1 + elif target == "y2": + value = y2 + elif target == "confidence(0-100)": + value = seg.confidence*100 + else: + raise Exception(f"[Impact Pack] SEGSRangeFilter - Unexpected target '{target}'") + + if mode and min_value <= value <= max_value: + logging.info(f"[in] value={value} / {mode}, {min_value}, {max_value}") + new_segs.append(seg) + elif not mode and (value < min_value or value > max_value): + logging.info(f"[out] value={value} / {mode}, {min_value}, {max_value}") + new_segs.append(seg) + else: + remained_segs.append(seg) + logging.info(f"[filter] value={value} / {mode}, {min_value}, {max_value}") + + return (segs[0], new_segs), (segs[0], remained_segs), + + +class SEGSIntersectionFilter: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs1": ("SEGS", ), + "segs2": ("SEGS", ), + "ioa_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + }, + } + + RETURN_TYPES = ("SEGS",) + RETURN_NAMES = ("filtered_SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def compute_ioa(self, mask1, mask2): + """Compute Intersection over Area (IoA) between two boxes.""" + inter_mask = utils.bitwise_and_masks(mask1, mask2) + + inter_area = (inter_mask > 0).sum() + area1 = (mask1 > 0).sum() + + return inter_area / area1 if area1 > 0 else 0 + + def doit(self, segs1, segs2, ioa_threshold): + """Remove segments from segs1 if their IoA with any segment in segs2 exceeds the threshold.""" + # Extract bounding boxes for all segments in segs1 and segs2 + keep = [] + + # Iterate over all segments in segs1 + for idx1, seg1 in enumerate(segs1[1]): + keep_segment = True # Assume the segment should be kept + mask1 = core.segs_to_combined_mask((segs1[0], [seg1])) + + # Compare with every segment in segs2 + for seg2 in segs2[1]: + mask2 = core.segs_to_combined_mask((segs2[0], [seg2])) + ioa = self.compute_ioa(mask1, mask2) # IoA between segment 1 and segment 2 + + if ioa > ioa_threshold: # If IoA exceeds the threshold, mark the segment for removal + keep_segment = False + break # If one overlap exceeds threshold, break early and mark for removal + + # Keep the segment if it did not exceed the threshold with any other segment + if keep_segment: + keep.append(segs1[1][idx1]) + + return (segs1[0], keep), # Return the updated SEGS + + +class SEGSNMSFilter: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "segs": ("SEGS",), + "iou_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), + }, + } + + RETURN_TYPES = ("SEGS",) + RETURN_NAMES = ("filtered_SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def compute_iou(self, mask1, mask2): + """Compute IoU between two bounding boxes (x1, y1, x2, y2).""" + inter_mask = utils.bitwise_and_masks(mask1, mask2) + union_mask = utils.add_masks(mask1, mask2) + + inter_area = (inter_mask > 0).sum() + union_area = (union_mask > 0).sum() + + return inter_area / union_area if union_area > 0 else 0 + + def doit(self, segs, iou_threshold): + """Perform NMS to filter overlapping segments.""" + confidences = np.ndarray.flatten(np.array([seg.confidence for seg in segs[1]])) + + # Sort boxes by confidence (high to low) + sorted_indices = np.argsort(confidences)[::-1].tolist() + keep = [] + + while len(sorted_indices) > 0: + idx = sorted_indices[0] + mask1 = core.segs_to_combined_mask((segs[0], [segs[1][idx]])) + keep.append(idx) + sorted_indices = sorted_indices[1:] + + # Filter indices only contain the indices where the bbox does not intersect + filtered_indices = [] + for i in sorted_indices: + mask2 = core.segs_to_combined_mask((segs[0], [segs[1][i]])) + iou = self.compute_iou(mask1, mask2) + if iou < iou_threshold: + filtered_indices.append(i) + + sorted_indices = np.array(filtered_indices) + + filtered_segs = [segs[1][i] for i in keep] + return (segs[0], filtered_segs), + + +class SEGSToImageList: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + }, + "optional": { + "fallback_image_opt": ("IMAGE", ), + } + } + + RETURN_TYPES = ("IMAGE",) + OUTPUT_IS_LIST = (True,) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, segs, fallback_image_opt=None): + results = list() + + if fallback_image_opt is not None: + segs = core.segs_scale_match(segs, fallback_image_opt.shape) + + for seg in segs[1]: + if seg.cropped_image is not None: + cropped_image = utils.to_tensor(seg.cropped_image) + elif fallback_image_opt is not None: + # take from original image + cropped_image = utils.to_tensor(utils.crop_image(fallback_image_opt, seg.crop_region)) + else: + cropped_image = utils.empty_pil_tensor() + + results.append(cropped_image) + + if len(results) == 0: + results.append(utils.empty_pil_tensor()) + + return (results,) + + +class SEGSToMaskList: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + }, + } + + RETURN_TYPES = ("MASK",) + OUTPUT_IS_LIST = (True,) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, segs): + masks = core.segs_to_masklist(segs) + if len(masks) == 0: + empty_mask = torch.zeros(segs[0], dtype=torch.float32, device="cpu") + masks = [empty_mask] + masks = [utils.make_3d_mask(mask) for mask in masks] + return (masks,) + + +class SEGSToMaskBatch: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + }, + } + + RETURN_TYPES = ("MASK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, segs): + masks = core.segs_to_masklist(segs) + masks = [utils.make_3d_mask(mask) for mask in masks] + mask_batch = torch.concat(masks) + return (mask_batch,) + + +class SEGSMerge: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + }, + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + DESCRIPTION = "SEGS contains multiple SEGs. SEGS Merge integrates several SEGs into a single merged SEG. The label is changed to `merged` and the confidence becomes the minimum confidence. The applied controlnet and cropped_image are removed." + + def doit(self, segs): + crop_left = sys.maxsize + crop_right = 0 + crop_top = sys.maxsize + crop_bottom = 0 + + bbox_left = sys.maxsize + bbox_right = 0 + bbox_top = sys.maxsize + bbox_bottom = 0 + + min_confidence = 1.0 + + for seg in segs[1]: + cx1 = seg.crop_region[0] + cy1 = seg.crop_region[1] + cx2 = seg.crop_region[2] + cy2 = seg.crop_region[3] + + bx1 = seg.bbox[0] + by1 = seg.bbox[1] + bx2 = seg.bbox[2] + by2 = seg.bbox[3] + + crop_left = min(crop_left, cx1) + crop_top = min(crop_top, cy1) + crop_right = max(crop_right, cx2) + crop_bottom = max(crop_bottom, cy2) + + bbox_left = min(bbox_left, bx1) + bbox_top = min(bbox_top, by1) + bbox_right = max(bbox_right, bx2) + bbox_bottom = max(bbox_bottom, by2) + + min_confidence = min(min_confidence, seg.confidence) + + combined_mask = core.segs_to_combined_mask(segs) + cropped_mask = combined_mask[crop_top:crop_bottom, crop_left:crop_right] + cropped_mask = cropped_mask.unsqueeze(0) + + crop_region = [crop_left, crop_top, crop_right, crop_bottom] + bbox = [bbox_left, bbox_top, bbox_right, bbox_bottom] + + seg = SEG(None, cropped_mask, min_confidence, crop_region, bbox, 'merged', None) + return ((segs[0], [seg]),) + + +class SEGSConcat: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs1": ("SEGS", ), + }, + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, **kwargs): + dim = None + res = None + + for k, v in list(kwargs.items()): + if v[0] == (0, 0) or len(v[1]) == 0: + continue + + if dim is None: + dim = v[0] + res = v[1] + else: + if v[0] == dim: + res = res + v[1] + else: + logging.error(f"[Impact Pack] source shape of 'segs1'{dim} and '{k}'{v[0]} are different. '{k}' will be ignored") + + if dim is None: + empty_segs = ((0, 0), []) + return (empty_segs, ) + else: + return ((dim, res), ) + + +class Count_Elts_in_SEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + }, + } + + RETURN_TYPES = ("INT",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, segs): + return (len(segs[1]), ) + + +class DecomposeSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + }, + } + + RETURN_TYPES = ("SEGS_HEADER", "SEG_ELT",) + OUTPUT_IS_LIST = (False, True, ) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, segs): + return segs + + +class AssembleSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "seg_header": ("SEGS_HEADER", ), + "seg_elt": ("SEG_ELT", ), + }, + } + + INPUT_IS_LIST = True + + RETURN_TYPES = ("SEGS", ) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, seg_header, seg_elt): + return ((seg_header[0], seg_elt), ) + + +class From_SEG_ELT: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "seg_elt": ("SEG_ELT", ), + }, + } + + RETURN_TYPES = ("SEG_ELT", "IMAGE", "MASK", "SEG_ELT_crop_region", "SEG_ELT_bbox", "SEG_ELT_control_net_wrapper", "FLOAT", "STRING") + RETURN_NAMES = ("seg_elt", "cropped_image", "cropped_mask", "crop_region", "bbox", "control_net_wrapper", "confidence", "label") + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, seg_elt): + cropped_image = utils.to_tensor(seg_elt.cropped_image) if seg_elt.cropped_image is not None else None + return (seg_elt, cropped_image, utils.to_tensor(seg_elt.cropped_mask), seg_elt.crop_region, seg_elt.bbox, seg_elt.control_net_wrapper, seg_elt.confidence, seg_elt.label,) + + +class From_SEG_ELT_bbox: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "bbox": ("SEG_ELT_bbox", ), + }, + } + + RETURN_TYPES = ("INT", "INT", "INT", "INT") + RETURN_NAMES = ("left", "top", "right", "bottom") + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, bbox): + return [int(c) for c in bbox] + + +class From_SEG_ELT_crop_region: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "crop_region": ("SEG_ELT_crop_region", ), + }, + } + + RETURN_TYPES = ("INT", "INT", "INT", "INT") + RETURN_NAMES = ("left", "top", "right", "bottom") + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, crop_region): + return crop_region + + +class Edit_SEG_ELT: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "seg_elt": ("SEG_ELT", ), + }, + "optional": { + "cropped_image_opt": ("IMAGE", ), + "cropped_mask_opt": ("MASK", ), + "crop_region_opt": ("SEG_ELT_crop_region", ), + "bbox_opt": ("SEG_ELT_bbox", ), + "control_net_wrapper_opt": ("SEG_ELT_control_net_wrapper", ), + "confidence_opt": ("FLOAT", {"min": 0, "max": 1.0, "step": 0.1, "forceInput": True}), + "label_opt": ("STRING", {"multiline": False, "forceInput": True}), + } + } + + RETURN_TYPES = ("SEG_ELT", ) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, seg_elt, cropped_image_opt=None, cropped_mask_opt=None, confidence_opt=None, crop_region_opt=None, + bbox_opt=None, label_opt=None, control_net_wrapper_opt=None): + + cropped_image = seg_elt.cropped_image if cropped_image_opt is None else cropped_image_opt + cropped_mask = seg_elt.cropped_mask if cropped_mask_opt is None else cropped_mask_opt + confidence = seg_elt.confidence if confidence_opt is None else confidence_opt + crop_region = seg_elt.crop_region if crop_region_opt is None else crop_region_opt + bbox = seg_elt.bbox if bbox_opt is None else bbox_opt + label = seg_elt.label if label_opt is None else label_opt + control_net_wrapper = seg_elt.control_net_wrapper if control_net_wrapper_opt is None else control_net_wrapper_opt + + cropped_image = cropped_image.numpy() if cropped_image is not None else None + + if isinstance(cropped_mask, torch.Tensor): + if len(cropped_mask.shape) == 3: + cropped_mask = cropped_mask.squeeze(0) + + cropped_mask = cropped_mask.numpy() + + seg = SEG(cropped_image, cropped_mask, confidence, crop_region, bbox, label, control_net_wrapper) + + return (seg,) + + +class DilateMask: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "mask": ("MASK", ), + "dilation": ("INT", {"default": 10, "min": -512, "max": 512, "step": 1}), + }} + + RETURN_TYPES = ("MASK", ) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, mask, dilation): + mask = utils.dilate_mask(mask.numpy(), dilation) + mask = torch.from_numpy(mask) + mask = utils.make_3d_mask(mask) + return (mask, ) + + +class GaussianBlurMask: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "mask": ("MASK", ), + "kernel_size": ("INT", {"default": 10, "min": 0, "max": 100, "step": 1}), + "sigma": ("FLOAT", {"default": 10.0, "min": 0.1, "max": 100.0, "step": 0.1}), + }} + + RETURN_TYPES = ("MASK", ) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, mask, kernel_size, sigma): + # Some custom nodes use abnormal 4-dimensional masks in the format of b, c, h, w. In the impact pack, internal 4-dimensional masks are required in the format of b, h, w, c. Therefore, normalization is performed using the normal mask format, which is 3-dimensional, before proceeding with the operation. + mask = utils.make_3d_mask(mask) + mask = torch.unsqueeze(mask, dim=-1) + mask = utils.tensor_gaussian_blur_mask(mask, kernel_size, sigma) + mask = torch.squeeze(mask, dim=-1) + return (mask, ) + + +class DilateMaskInSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + "dilation": ("INT", {"default": 10, "min": -512, "max": 512, "step": 1}), + }} + + RETURN_TYPES = ("SEGS", ) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, segs, dilation): + new_segs = [] + for seg in segs[1]: + mask = utils.dilate_mask(seg.cropped_mask, dilation) + seg = SEG(seg.cropped_image, mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper) + new_segs.append(seg) + + return ((segs[0], new_segs), ) + + +class GaussianBlurMaskInSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + "kernel_size": ("INT", {"default": 10, "min": 0, "max": 100, "step": 1}), + "sigma": ("FLOAT", {"default": 10.0, "min": 0.1, "max": 100.0, "step": 0.1}), + }} + + RETURN_TYPES = ("SEGS", ) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, segs, kernel_size, sigma): + new_segs = [] + for seg in segs[1]: + mask = utils.tensor_gaussian_blur_mask(seg.cropped_mask, kernel_size, sigma) + mask = torch.squeeze(mask, dim=-1).squeeze(0).numpy() + seg = SEG(seg.cropped_image, mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper) + new_segs.append(seg) + + return ((segs[0], new_segs), ) + + +class Dilate_SEG_ELT: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "seg_elt": ("SEG_ELT", ), + "dilation": ("INT", {"default": 10, "min": -512, "max": 512, "step": 1}), + }} + + RETURN_TYPES = ("SEG_ELT", ) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, seg, dilation): + mask = utils.dilate_mask(seg.cropped_mask, dilation) + seg = SEG(seg.cropped_image, mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper) + return (seg,) + + +class SEG_ELT_BBOX_ScaleBy: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "seg": ("SEG_ELT", ), + "scale_by": ("FLOAT", {"default": 1.0, "min": 0.01, "max": 8.0, "step": 0.01}), } + } + + RETURN_TYPES = ("SEG_ELT", ) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def fill_zero_outside_bbox(mask, crop_region, bbox): + cx1, cy1, _, _ = crop_region + x1, y1, x2, y2 = bbox + x1, y1, x2, y2 = x1-cx1, y1-cy1, x2-cx1, y2-cy1 + h, w = mask.shape + + x1 = int(min(w-1, max(0, x1))) + x2 = int(min(w-1, max(0, x2))) + y1 = int(min(h-1, max(0, y1))) + y2 = int(min(h-1, max(0, y2))) + + mask_cropped = mask.copy() + mask_cropped[:, :x1] = 0 # zero fill left side + mask_cropped[:, x2:] = 0 # zero fill right side + mask_cropped[:y1, :] = 0 # zero fill top side + mask_cropped[y2:, :] = 0 # zero fill bottom side + return mask_cropped + + def doit(self, seg, scale_by): + x1, y1, x2, y2 = seg.bbox + w = x2-x1 + h = y2-y1 + + dw = int((w * scale_by - w)/2) + dh = int((h * scale_by - h)/2) + + bbox = (x1-dw, y1-dh, x2+dw, y2+dh) + + cropped_mask = SEG_ELT_BBOX_ScaleBy.fill_zero_outside_bbox(seg.cropped_mask, seg.crop_region, bbox) + seg = SEG(seg.cropped_image, cropped_mask, seg.confidence, seg.crop_region, bbox, seg.label, seg.control_net_wrapper) + return (seg,) + + +class EmptySEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": {}, } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self): + shape = 0, 0 + return ((shape, []),) + + +class SegsToCombinedMask: + @classmethod + def INPUT_TYPES(s): + return {"required": {"segs": ("SEGS",), }} + + RETURN_TYPES = ("MASK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, segs): + mask = core.segs_to_combined_mask(segs) + mask = utils.make_3d_mask(mask) + return (mask,) + + +class MediaPipeFaceMeshToSEGS: + @classmethod + def INPUT_TYPES(s): + bool_true_widget = ("BOOLEAN", {"default": True, "label_on": "Enabled", "label_off": "Disabled"}) + bool_false_widget = ("BOOLEAN", {"default": False, "label_on": "Enabled", "label_off": "Disabled"}) + return {"required": { + "image": ("IMAGE",), + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}), + "bbox_fill": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "crop_min_size": ("INT", {"min": 10, "max": MAX_RESOLUTION, "step": 1, "default": 50}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 1}), + "dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}), + "face": bool_true_widget, + "mouth": bool_false_widget, + "left_eyebrow": bool_false_widget, + "left_eye": bool_false_widget, + "left_pupil": bool_false_widget, + "right_eyebrow": bool_false_widget, + "right_eye": bool_false_widget, + "right_pupil": bool_false_widget, + }, + # "optional": {"reference_image_opt": ("IMAGE", ), } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, image, crop_factor, bbox_fill, crop_min_size, drop_size, dilation, face, mouth, left_eyebrow, left_eye, left_pupil, right_eyebrow, right_eye, right_pupil): + # padding is obsolete now + # https://github.com/Fannovel16/comfyui_controlnet_aux/blob/1ec41fceff1ee99596445a0c73392fd91df407dc/utils.py#L33 + # def calc_pad(h_raw, w_raw): + # resolution = normalize_size_base_64(h_raw, w_raw) + # + # def pad64(x): + # return int(np.ceil(float(x) / 64.0) * 64 - x) + # + # k = float(resolution) / float(min(h_raw, w_raw)) + # h_target = int(np.round(float(h_raw) * k)) + # w_target = int(np.round(float(w_raw) * k)) + # + # return pad64(h_target), pad64(w_target) + + # if reference_image_opt is not None: + # if image.shape[1:] != reference_image_opt.shape[1:]: + # scale_by1 = reference_image_opt.shape[1] / image.shape[1] + # scale_by2 = reference_image_opt.shape[2] / image.shape[2] + # scale_by = min(scale_by1, scale_by2) + # + # # padding is obsolete now + # # h_pad, w_pad = calc_pad(reference_image_opt.shape[1], reference_image_opt.shape[2]) + # # if h_pad != 0: + # # # height padded + # # image = image[:, :-h_pad, :, :] + # # elif w_pad != 0: + # # # width padded + # # image = image[:, :, :-w_pad, :] + # + # image = nodes.ImageScaleBy().upscale(image, "bilinear", scale_by)[0] + + result = core.mediapipe_facemesh_to_segs(image, crop_factor, bbox_fill, crop_min_size, drop_size, dilation, face, mouth, left_eyebrow, left_eye, left_pupil, right_eyebrow, right_eye, right_pupil) + return (result, ) + + +class MaskToSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "mask": ("MASK",), + "combined": ("BOOLEAN", {"default": False, "label_on": "True", "label_off": "False"}), + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}), + "bbox_fill": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + "contour_fill": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + @staticmethod + def doit(mask, combined, crop_factor, bbox_fill, drop_size, contour_fill=False): + mask = utils.make_2d_mask(mask) + result = core.mask_to_segs(mask, combined, crop_factor, bbox_fill, drop_size, is_contour=contour_fill) + + return (result, ) + + +class MaskToSEGS_for_AnimateDiff: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "mask": ("MASK",), + "combined": ("BOOLEAN", {"default": False, "label_on": "True", "label_off": "False"}), + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}), + "bbox_fill": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "drop_size": ("INT", {"min": 1, "max": MAX_RESOLUTION, "step": 1, "default": 10}), + "contour_fill": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + @staticmethod + def doit(mask, combined, crop_factor, bbox_fill, drop_size, contour_fill=False): + if (len(mask.shape) == 4 and mask.shape[1] > 1) or (len(mask.shape) == 3 and mask.shape[0] > 1): + mask = utils.make_3d_mask(mask) + if contour_fill: + logging.info("[Impact Pack] MaskToSEGS_for_AnimateDiff: 'contour_fill' is ignored because batch mask 'contour_fill' is not supported.") + result = core.batch_mask_to_segs(mask, combined, crop_factor, bbox_fill, drop_size) + return (result, ) + + mask = utils.make_2d_mask(mask) + segs = core.mask_to_segs(mask, combined, crop_factor, bbox_fill, drop_size, is_contour=contour_fill) + all_masks = SEGSToMaskList().doit(segs)[0] + + result_mask = (all_masks[0] * 255).to(torch.uint8) + for mask in all_masks[1:]: + result_mask |= (mask * 255).to(torch.uint8) + + result_mask = (result_mask/255.0).to(torch.float32) + result_mask = utils.to_binary_mask(result_mask, 0.1)[0] + + return MaskToSEGS.doit(result_mask, False, crop_factor, False, drop_size, contour_fill) + + +class IPAdapterApplySEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS",), + "ipadapter_pipe": ("IPADAPTER_PIPE",), + "weight": ("FLOAT", {"default": 0.7, "min": -1, "max": 3, "step": 0.05}), + "noise": ("FLOAT", {"default": 0.4, "min": 0.0, "max": 1.0, "step": 0.01}), + "weight_type": (["original", "linear", "channel penalty"], {"default": 'channel penalty'}), + "start_at": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}), + "end_at": ("FLOAT", {"default": 0.9, "min": 0.0, "max": 1.0, "step": 0.001}), + "unfold_batch": ("BOOLEAN", {"default": False}), + "faceid_v2": ("BOOLEAN", {"default": False}), + "weight_v2": ("FLOAT", {"default": 1.0, "min": -1, "max": 3, "step": 0.05}), + "context_crop_factor": ("FLOAT", {"default": 1.2, "min": 1.0, "max": 100, "step": 0.1}), + "reference_image": ("IMAGE",), + }, + "optional": { + "combine_embeds": (["concat", "add", "subtract", "average", "norm average"],), + "neg_image": ("IMAGE",), + }, + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def doit(segs, ipadapter_pipe, weight, noise, weight_type, start_at, end_at, unfold_batch, faceid_v2, weight_v2, context_crop_factor, reference_image, combine_embeds="concat", neg_image=None): + + if len(ipadapter_pipe) == 4: + logging.info("[Impact Pack] IPAdapterApplySEGS: Installed Inspire Pack is outdated.") + raise Exception("Inspire Pack is outdated.") + + new_segs = [] + + h, w = segs[0] + + if reference_image.shape[2] != w or reference_image.shape[1] != h: + reference_image = utils.tensor_resize(reference_image, w, h) + + for seg in segs[1]: + # The context_crop_region sets how much wider the IPAdapter context will reflect compared to the crop_region, not the bbox + context_crop_region = utils.make_crop_region(w, h, seg.crop_region, context_crop_factor) + cropped_image = utils.crop_image(reference_image, context_crop_region) + + control_net_wrapper = core.IPAdapterWrapper(ipadapter_pipe, weight, noise, weight_type, start_at, end_at, unfold_batch, weight_v2, cropped_image, neg_image=neg_image, prev_control_net=seg.control_net_wrapper, combine_embeds=combine_embeds) + new_seg = SEG(seg.cropped_image, seg.cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, control_net_wrapper) + new_segs.append(new_seg) + + return ((segs[0], new_segs), ) + + +class ControlNetApplySEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS",), + "control_net": ("CONTROL_NET",), + "strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.01}), + }, + "optional": { + "segs_preprocessor": ("SEGS_PREPROCESSOR",), + "control_image": ("IMAGE",) + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + DEPRECATED = True + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def doit(segs, control_net, strength, segs_preprocessor=None, control_image=None): + new_segs = [] + + for seg in segs[1]: + control_net_wrapper = core.ControlNetWrapper(control_net, strength, segs_preprocessor, seg.control_net_wrapper, + original_size=segs[0], crop_region=seg.crop_region, control_image=control_image) + new_seg = SEG(seg.cropped_image, seg.cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, control_net_wrapper) + new_segs.append(new_seg) + + return ((segs[0], new_segs), ) + + +class ControlNetApplyAdvancedSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS",), + "control_net": ("CONTROL_NET",), + "strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.01}), + "start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}), + "end_percent": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.001}) + }, + "optional": { + "segs_preprocessor": ("SEGS_PREPROCESSOR",), + "control_image": ("IMAGE",), + "vae": ("VAE",) + } + } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def doit(segs, control_net, strength, start_percent, end_percent, segs_preprocessor=None, control_image=None, vae=None): + new_segs = [] + + for seg in segs[1]: + control_net_wrapper = core.ControlNetAdvancedWrapper(control_net, strength, start_percent, end_percent, segs_preprocessor, + seg.control_net_wrapper, original_size=segs[0], crop_region=seg.crop_region, + control_image=control_image, vae=vae) + new_seg = SEG(seg.cropped_image, seg.cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, control_net_wrapper) + new_segs.append(new_seg) + + return ((segs[0], new_segs), ) + + +class ControlNetClearSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": {"segs": ("SEGS",), }, } + + RETURN_TYPES = ("SEGS",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def doit(segs): + new_segs = [] + + for seg in segs[1]: + new_seg = SEG(seg.cropped_image, seg.cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, None) + new_segs.append(new_seg) + + return ((segs[0], new_segs), ) + + +class SEGSSwitch: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "select": ("INT", {"default": 1, "min": 1, "max": 99999, "step": 1}), + "segs1": ("SEGS",), + }, + } + + RETURN_TYPES = ("SEGS", ) + + OUTPUT_NODE = True + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, *args, **kwargs): + input_name = f"segs{int(kwargs['select'])}" + + if input_name in kwargs: + return (kwargs[input_name],) + else: + logging.info("SEGSSwitch: invalid select index ('segs1' is selected)") + return (kwargs['segs1'],) + + +class SEGSPicker: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "picks": ("STRING", {"multiline": True, "dynamicPrompts": False, "pysssss.autocomplete": False}), + "segs": ("SEGS",), + }, + "optional": { + "fallback_image_opt": ("IMAGE", ), + }, + "hidden": {"unique_id": "UNIQUE_ID"}, + } + + RETURN_TYPES = ("SEGS", ) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + DESCRIPTION = "This node provides a function to select only the chosen SEGS from the input SEGS." + + @staticmethod + def doit(picks, segs, fallback_image_opt=None, unique_id=None): + if fallback_image_opt is not None: + segs = core.segs_scale_match(segs, fallback_image_opt.shape) + + # generate candidates image + cands = [] + for seg in segs[1]: + if seg.cropped_image is not None: + cropped_image = seg.cropped_image + elif fallback_image_opt is not None: + # take from original image + cropped_image = utils.crop_image(fallback_image_opt, seg.crop_region) + else: + cropped_image = utils.empty_pil_tensor() + + mask_array = seg.cropped_mask.copy() + mask_array[mask_array < 0.3] = 0.3 + mask_array = mask_array[None, ..., None] + cropped_image = cropped_image * mask_array + + cands.append(cropped_image) + + impact.impact_server.segs_picker_map[unique_id] = cands + + # pass only selected + pick_ids = set() + + for pick in picks.split(","): + try: + pick_ids.add(int(pick)-1) + except Exception: + pass + + new_segs = [] + for i in pick_ids: + if 0 <= i < len(segs[1]): + new_segs.append(segs[1][i]) + + return ((segs[0], new_segs),) + + +class DefaultImageForSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "segs": ("SEGS", ), + "image": ("IMAGE", ), + "override": ("BOOLEAN", {"default": True}), + }} + + RETURN_TYPES = ("SEGS", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + DESCRIPTION = "If the SEGS have not passed through the detailer, they contain only detection area information without an image. This node sets a default image for the SEGS." + + @staticmethod + def doit(segs, image, override): + results = [] + + segs = core.segs_scale_match(segs, image.shape) + + if len(segs[1]) > 0: + if segs[1][0].cropped_image is not None: + batch_count = len(segs[1][0].cropped_image) + else: + batch_count = len(image) + + for seg in segs[1]: + if seg.cropped_image is not None and not override: + cropped_image = seg.cropped_image + else: + cropped_image = None + for i in range(0, batch_count): + # take from original image + ref_image = image[i].unsqueeze(0) + cropped_image2 = utils.crop_image(ref_image, seg.crop_region) + + if cropped_image is None: + cropped_image = cropped_image2 + else: + cropped_image = torch.cat((cropped_image, cropped_image2), dim=0) + + new_seg = SEG(cropped_image, seg.cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper) + results.append(new_seg) + + return ((segs[0], results), ) + else: + return (segs, ) + + +class RemoveImageFromSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": {"segs": ("SEGS", ), }} + + RETURN_TYPES = ("SEGS", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def doit(segs): + results = [] + + if len(segs[1]) > 0: + for seg in segs[1]: + new_seg = SEG(None, seg.cropped_mask, seg.confidence, seg.crop_region, seg.bbox, seg.label, seg.control_net_wrapper) + results.append(new_seg) + + return ((segs[0], results), ) + else: + return (segs, ) + + +class MakeTileSEGS: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "images": ("IMAGE", ), + "bbox_size": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 8}), + "crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 10, "step": 0.01}), + "min_overlap": ("INT", {"default": 5, "min": 0, "max": 512, "step": 1}), + "filter_segs_dilation": ("INT", {"default": 20, "min": -255, "max": 255, "step": 1}), + "mask_irregularity": ("FLOAT", {"default": 0, "min": 0, "max": 1.0, "step": 0.01}), + "irregular_mask_mode": (["Reuse fast", "Reuse quality", "All random fast", "All random quality"],) + }, + "optional": { + "filter_in_segs_opt": ("SEGS", ), + "filter_out_segs_opt": ("SEGS", ), + } + } + + RETURN_TYPES = ("SEGS",) + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/__for_testing" + + @staticmethod + def doit(images, bbox_size, crop_factor, min_overlap, filter_segs_dilation, mask_irregularity=0, irregular_mask_mode="Reuse fast", filter_in_segs_opt=None, filter_out_segs_opt=None): + if bbox_size <= 2*min_overlap: + new_min_overlap = bbox_size / 2 + logging.info(f"[MakeTileSEGS] min_overlap should be greater than bbox_size. (value changed: {min_overlap} => {new_min_overlap})") + min_overlap = new_min_overlap + + _, ih, iw, _ = images.size() + + mask_cache = None + mask_quality = 512 + if mask_irregularity > 0: + if irregular_mask_mode == "Reuse fast": + mask_quality = 128 + mask_cache = np.zeros((128, 128)).astype(np.float32) + core.random_mask(mask_cache, (0, 0, 128, 128), factor=mask_irregularity, size=mask_quality) + elif irregular_mask_mode == "Reuse quality": + mask_quality = 512 + mask_cache = np.zeros((512, 512)).astype(np.float32) + core.random_mask(mask_cache, (0, 0, 512, 512), factor=mask_irregularity, size=mask_quality) + elif irregular_mask_mode == "All random fast": + mask_quality = 512 + + # compensate overlap/bbox_size for irregular mask + if mask_irregularity > 0: + compensate = max(6, int(mask_quality * mask_irregularity / 4)) + min_overlap += compensate + bbox_size += compensate*2 + + # create exclusion mask + if filter_out_segs_opt is not None: + exclusion_mask = core.segs_to_combined_mask(filter_out_segs_opt) + exclusion_mask = utils.make_3d_mask(exclusion_mask) + exclusion_mask = utils.resize_mask(exclusion_mask, (ih, iw)) + exclusion_mask = utils.dilate_mask(exclusion_mask.cpu().numpy(), filter_segs_dilation) + else: + exclusion_mask = None + + if filter_in_segs_opt is not None: + and_mask = core.segs_to_combined_mask(filter_in_segs_opt) + and_mask = utils.make_3d_mask(and_mask) + and_mask = utils.resize_mask(and_mask, (ih, iw)) + and_mask = utils.dilate_mask(and_mask.cpu().numpy(), filter_segs_dilation) + + a, b = core.mask_to_segs(and_mask, True, 1.0, False, 0) + if len(b) == 0: + return ((a, b),) + + start_x, start_y, c, d = b[0].crop_region + w = c - start_x + h = d - start_y + else: + start_x = 0 + start_y = 0 + h, w = ih, iw + and_mask = None + + # calculate tile factors + if bbox_size > h or bbox_size > w: + new_bbox_size = min(bbox_size, min(w, h)) + logging.info(f"[MaskTileSEGS] bbox_size is greater than resolution (value changed: {bbox_size} => {new_bbox_size}") + bbox_size = new_bbox_size + + n_horizontal = math.ceil(w / (bbox_size - min_overlap)) + n_vertical = math.ceil(h / (bbox_size - min_overlap)) + + w_overlap_sum = (bbox_size * n_horizontal) - w + if w_overlap_sum < 0: + n_horizontal += 1 + w_overlap_sum = (bbox_size * n_horizontal) - w + + w_overlap_size = 0 if n_horizontal == 1 else int(w_overlap_sum/(n_horizontal-1)) + + h_overlap_sum = (bbox_size * n_vertical) - h + if h_overlap_sum < 0: + n_vertical += 1 + h_overlap_sum = (bbox_size * n_vertical) - h + + h_overlap_size = 0 if n_vertical == 1 else int(h_overlap_sum/(n_vertical-1)) + + new_segs = [] + + if w_overlap_size == bbox_size: + n_horizontal = 1 + + if h_overlap_size == bbox_size: + n_vertical = 1 + + y = start_y + for j in range(0, n_vertical): + x = start_x + for i in range(0, n_horizontal): + x1 = x + y1 = y + + if x+bbox_size < iw-1: + x2 = x+bbox_size + else: + x2 = iw + x1 = iw-bbox_size + + if y+bbox_size < ih-1: + y2 = y+bbox_size + else: + y2 = ih + y1 = ih-bbox_size + + bbox = x1, y1, x2, y2 + crop_region = utils.make_crop_region(iw, ih, bbox, crop_factor) + cx1, cy1, cx2, cy2 = crop_region + + mask = np.zeros((cy2 - cy1, cx2 - cx1)).astype(np.float32) + + rel_left = x1 - cx1 + rel_top = y1 - cy1 + rel_right = x2 - cx1 + rel_bot = y2 - cy1 + + if mask_irregularity > 0: + if mask_cache is not None: + core.adaptive_mask_paste(mask, mask_cache, (rel_left, rel_top, rel_right, rel_bot)) + else: + core.random_mask(mask, (rel_left, rel_top, rel_right, rel_bot), factor=mask_irregularity, size=mask_quality) + + # corner filling + if rel_left == 0: + pad = int((x2 - x1) / 8) + mask[rel_top:rel_bot, :pad] = 1.0 + + if rel_top == 0: + pad = int((y2 - y1) / 8) + mask[:pad, rel_left:rel_right] = 1.0 + + if rel_right == mask.shape[1]: + pad = int((x2 - x1) / 8) + mask[rel_top:rel_bot, -pad:] = 1.0 + + if rel_bot == mask.shape[0]: + pad = int((y2 - y1) / 8) + mask[-pad:, rel_left:rel_right] = 1.0 + else: + mask[rel_top:rel_bot, rel_left:rel_right] = 1.0 + + mask = torch.tensor(mask) + + if exclusion_mask is not None: + exclusion_mask_cropped = exclusion_mask[cy1:cy2, cx1:cx2] + mask[exclusion_mask_cropped != 0] = 0.0 + + if and_mask is not None: + and_mask_cropped = and_mask[cy1:cy2, cx1:cx2] + mask[and_mask_cropped == 0] = 0.0 + + is_mask_zero = torch.all(mask == 0.0).item() + + if not is_mask_zero: + item = SEG(None, mask.numpy(), 1.0, crop_region, bbox, "", None) + new_segs.append(item) + + x += bbox_size - w_overlap_size + y += bbox_size - h_overlap_size + + res = (ih, iw), new_segs # segs + return (res,) + + +class SEGSUpscaler: + @classmethod + def INPUT_TYPES(s): + resampling_methods = ["lanczos", "nearest", "bilinear", "bicubic"] + + return {"required": { + "image": ("IMAGE",), + "segs": ("SEGS",), + "model": ("MODEL",), + "clip": ("CLIP",), + "vae": ("VAE",), + "rescale_factor": ("FLOAT", {"default": 2, "min": 0.01, "max": 100.0, "step": 0.01}), + "resampling_method": (resampling_methods,), + "supersample": (["true", "false"],), + "rounding_modulus": ("INT", {"default": 8, "min": 8, "max": 1024, "step": 8}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "positive": ("CONDITIONING",), + "negative": ("CONDITIONING",), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + "feather": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}), + "inpaint_model": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + }, + "optional": { + "upscale_model_opt": ("UPSCALE_MODEL",), + "upscaler_hook_opt": ("UPSCALER_HOOK",), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + } + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + @staticmethod + def doit(image, segs, model, clip, vae, rescale_factor, resampling_method, supersample, rounding_modulus, + seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, feather, inpaint_model, noise_mask_feather, + upscale_model_opt=None, upscaler_hook_opt=None, scheduler_func_opt=None): + + new_image = segs_upscaler.upscaler(image, upscale_model_opt, rescale_factor, resampling_method, supersample, rounding_modulus) + + segs = core.segs_scale_match(segs, new_image.shape) + + ordered_segs = segs[1] + + for i, seg in enumerate(ordered_segs): + cropped_image = utils.crop_ndarray4(new_image.numpy(), seg.crop_region) + cropped_image = utils.to_tensor(cropped_image) + mask = utils.to_tensor(seg.cropped_mask) + mask = utils.tensor_gaussian_blur_mask(mask, feather) + + is_mask_all_zeros = (seg.cropped_mask == 0).all().item() + if is_mask_all_zeros: + logging.info("SEGSUpscaler: segment skip [empty mask]") + continue + + cropped_mask = seg.cropped_mask + + seg_seed = seed + i + + enhanced_image = segs_upscaler.img2img_segs(cropped_image, model, clip, vae, seg_seed, steps, cfg, sampler_name, scheduler, + positive, negative, denoise, + noise_mask=cropped_mask, control_net_wrapper=seg.control_net_wrapper, + inpaint_model=inpaint_model, noise_mask_feather=noise_mask_feather, scheduler_func_opt=scheduler_func_opt) + if enhanced_image is not None: + new_image = new_image.cpu() + enhanced_image = enhanced_image.cpu() + left = seg.crop_region[0] + top = seg.crop_region[1] + utils.tensor_paste(new_image, enhanced_image, (left, top), mask) + + if upscaler_hook_opt is not None: + new_image = upscaler_hook_opt.post_paste(new_image) + + enhanced_img = utils.tensor_convert_rgb(new_image) + + return (enhanced_img,) + + +class SEGSUpscalerPipe: + @classmethod + def INPUT_TYPES(s): + resampling_methods = ["lanczos", "nearest", "bilinear", "bicubic"] + + return {"required": { + "image": ("IMAGE",), + "segs": ("SEGS",), + "basic_pipe": ("BASIC_PIPE",), + "rescale_factor": ("FLOAT", {"default": 2, "min": 0.01, "max": 100.0, "step": 0.01}), + "resampling_method": (resampling_methods,), + "supersample": (["true", "false"],), + "rounding_modulus": ("INT", {"default": 8, "min": 8, "max": 1024, "step": 8}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS,), + "scheduler": (core.SCHEDULERS,), + "denoise": ("FLOAT", {"default": 0.5, "min": 0.0001, "max": 1.0, "step": 0.01}), + "feather": ("INT", {"default": 5, "min": 0, "max": 100, "step": 1}), + "inpaint_model": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "noise_mask_feather": ("INT", {"default": 20, "min": 0, "max": 100, "step": 1}), + }, + "optional": { + "upscale_model_opt": ("UPSCALE_MODEL",), + "upscaler_hook_opt": ("UPSCALER_HOOK",), + "scheduler_func_opt": ("SCHEDULER_FUNC",), + } + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Upscale" + + @staticmethod + def doit(image, segs, basic_pipe, rescale_factor, resampling_method, supersample, rounding_modulus, + seed, steps, cfg, sampler_name, scheduler, denoise, feather, inpaint_model, noise_mask_feather, + upscale_model_opt=None, upscaler_hook_opt=None, scheduler_func_opt=None): + + model, clip, vae, positive, negative = basic_pipe + + return SEGSUpscaler.doit(image, segs, model, clip, vae, rescale_factor, resampling_method, supersample, rounding_modulus, + seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, feather, inpaint_model, noise_mask_feather, + upscale_model_opt=upscale_model_opt, upscaler_hook_opt=upscaler_hook_opt, scheduler_func_opt=scheduler_func_opt) diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/segs_upscaler.py b/custom_nodes/comfyui-impact-pack/modules/impact/segs_upscaler.py new file mode 100644 index 0000000000000000000000000000000000000000..b27a4e14d3ada63cb493211e4dde3dbcdfb85528 --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/segs_upscaler.py @@ -0,0 +1,140 @@ +from impact import impact_sampling +from comfy import model_management +from impact import utils +from PIL import Image +import nodes +import torch +import inspect +import logging +import comfy + +try: + from comfy_extras import nodes_differential_diffusion +except Exception: + logging.info("[Impact Pack] ComfyUI is an outdated version. The DifferentialDiffusion feature will be disabled.") + + +# Implementation based on `https://github.com/lingondricka2/Upscaler-Detailer` + +# code from comfyroll ---> +# https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/blob/main/nodes/functions_upscale.py + +def upscale_with_model(upscale_model, image): + device = model_management.get_torch_device() + upscale_model.to(device) + in_img = image.movedim(-1, -3).to(device) + + tile = 512 + overlap = 32 + + oom = True + while oom: + try: + steps = in_img.shape[0] * comfy.utils.get_tiled_scale_steps(in_img.shape[3], in_img.shape[2], tile_x=tile, tile_y=tile, overlap=overlap) + pbar = comfy.utils.ProgressBar(steps) + s = comfy.utils.tiled_scale(in_img, lambda a: upscale_model(a), tile_x=tile, tile_y=tile, overlap=overlap, upscale_amount=upscale_model.scale, pbar=pbar) + oom = False + except model_management.OOM_EXCEPTION as e: + tile //= 2 + if tile < 128: + raise e + + s = torch.clamp(s.movedim(-3, -1), min=0, max=1.0) + return s + + +def apply_resize_image(image: Image.Image, original_width, original_height, rounding_modulus, mode='scale', supersample='true', factor: int = 2, width: int = 1024, height: int = 1024, + resample='bicubic'): + # Calculate the new width and height based on the given mode and parameters + if mode == 'rescale': + new_width, new_height = int(original_width * factor), int(original_height * factor) + else: + m = rounding_modulus + original_ratio = original_height / original_width + height = int(width * original_ratio) + + new_width = width if width % m == 0 else width + (m - width % m) + new_height = height if height % m == 0 else height + (m - height % m) + + # Define a dictionary of resampling filters + resample_filters = {'nearest': 0, 'bilinear': 2, 'bicubic': 3, 'lanczos': 1} + + # Apply supersample + if supersample == 'true': + image = image.resize((new_width * 8, new_height * 8), resample=Image.Resampling(resample_filters[resample])) + + # Resize the image using the given resampling filter + resized_image = image.resize((new_width, new_height), resample=Image.Resampling(resample_filters[resample])) + + return resized_image + + +def upscaler(image, upscale_model, rescale_factor, resampling_method, supersample, rounding_modulus): + if upscale_model is not None: + up_image = upscale_with_model(upscale_model, image) + else: + up_image = image + + pil_img = utils.tensor2pil(image) + original_width, original_height = pil_img.size + scaled_image = utils.pil2tensor(apply_resize_image(utils.tensor2pil(up_image), original_width, original_height, rounding_modulus, 'rescale', + supersample, rescale_factor, 1024, resampling_method)) + return scaled_image + +# <--- + + +def img2img_segs(image, model, clip, vae, seed, steps, cfg, sampler_name, scheduler, + positive, negative, denoise, noise_mask, control_net_wrapper=None, + inpaint_model=False, noise_mask_feather=0, scheduler_func_opt=None): + + original_image_size = image.shape[1:3] + + # Match to original image size + if original_image_size[0] % 8 > 0 or original_image_size[1] % 8 > 0: + scale = 8/min(original_image_size[0], original_image_size[1]) + 1 + w = int(original_image_size[1] * scale) + h = int(original_image_size[0] * scale) + image = utils.tensor_resize(image, w, h) + + if noise_mask is not None: + noise_mask = utils.tensor_gaussian_blur_mask(noise_mask, noise_mask_feather) + noise_mask = noise_mask.squeeze(3) + + if noise_mask_feather > 0 and 'denoise_mask_function' not in model.model_options: + model = nodes_differential_diffusion.DifferentialDiffusion().apply(model)[0] + + if control_net_wrapper is not None: + positive, negative, _ = control_net_wrapper.apply(positive, negative, image, noise_mask) + + # prepare mask + if noise_mask is not None and inpaint_model: + imc_encode = nodes.InpaintModelConditioning().encode + if 'noise_mask' in inspect.signature(imc_encode).parameters: + positive, negative, latent_image = imc_encode(positive, negative, image, vae, mask=noise_mask, noise_mask=True) + else: + logging.info("[Impact Pack] ComfyUI is an outdated version.") + positive, negative, latent_image = imc_encode(positive, negative, image, vae, noise_mask) + else: + latent_image = utils.to_latent_image(image, vae) + if noise_mask is not None: + latent_image['noise_mask'] = noise_mask + + refined_latent = latent_image + + # ksampler + refined_latent = impact_sampling.ksampler_wrapper(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, refined_latent, denoise, scheduler_func=scheduler_func_opt) + + # non-latent downscale - latent downscale cause bad quality + refined_image = vae.decode(refined_latent['samples']) + + # prevent mixing of device + refined_image = refined_image.cpu() + + # Match to original image size + if refined_image.shape[1:3] != original_image_size: + refined_image = utils.tensor_resize(refined_image, original_image_size[1], original_image_size[0]) + + # don't convert to latent - latent break image + # preserving pil is much better + return refined_image diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/special_samplers.py b/custom_nodes/comfyui-impact-pack/modules/impact/special_samplers.py new file mode 100644 index 0000000000000000000000000000000000000000..16129ec5f0a05475915ee82514d7f77e5cedb14d --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/special_samplers.py @@ -0,0 +1,686 @@ +import math +import impact.core as core +from comfy_extras.nodes_custom_sampler import Noise_RandomNoise +from nodes import MAX_RESOLUTION +import nodes +from impact.impact_sampling import KSamplerWrapper, KSamplerAdvancedWrapper, separated_sample, impact_sample +import comfy +import torch +import numpy as np +import logging + + +class TiledKSamplerProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Random seed to use for generating CPU noise for sampling."}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000, "tooltip": "total sampling steps"}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "tooltip": "classifier free guidance value"}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, {"tooltip": "sampler"}), + "scheduler": (comfy.samplers.KSampler.SCHEDULERS, {"tooltip": "noise schedule"}), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "The amount of noise to remove. This amount is the noise added at the start, and the higher it is, the more the input latent will be modified before being returned."}), + "tile_width": ("INT", {"default": 512, "min": 320, "max": MAX_RESOLUTION, "step": 64, "tooltip": "Sets the width of the tile to be used in TiledKSampler."}), + "tile_height": ("INT", {"default": 512, "min": 320, "max": MAX_RESOLUTION, "step": 64, "tooltip": "Sets the height of the tile to be used in TiledKSampler."}), + "tiling_strategy": (["random", "padded", 'simple'], {"tooltip": "Sets the tiling strategy for TiledKSampler."} ), + "basic_pipe": ("BASIC_PIPE", {"tooltip": "basic_pipe input for sampling"}) + }} + + OUTPUT_TOOLTIPS = ("sampler wrapper. (Can be used when generating a regional_prompt.)", ) + + RETURN_TYPES = ("KSAMPLER",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Sampler" + + @staticmethod + def doit(seed, steps, cfg, sampler_name, scheduler, denoise, + tile_width, tile_height, tiling_strategy, basic_pipe): + model, _, _, positive, negative = basic_pipe + sampler = core.TiledKSamplerWrapper(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, + tile_width, tile_height, tiling_strategy) + return (sampler, ) + + +class KSamplerProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Random seed to use for generating CPU noise for sampling."}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000, "tooltip": "total sampling steps"}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "tooltip": "classifier free guidance value"}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, {"tooltip": "sampler"}), + "scheduler": (core.SCHEDULERS, {"tooltip": "noise schedule"}), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "The amount of noise to remove. This amount is the noise added at the start, and the higher it is, the more the input latent will be modified before being returned."}), + "basic_pipe": ("BASIC_PIPE", {"tooltip": "basic_pipe input for sampling"}) + }, + "optional": { + "scheduler_func_opt": ("SCHEDULER_FUNC", {"tooltip": "[OPTIONAL] Noise schedule generation function. If this is set, the scheduler widget will be ignored."}), + } + } + + OUTPUT_TOOLTIPS = ("sampler wrapper. (Can be used when generating a regional_prompt.)",) + + RETURN_TYPES = ("KSAMPLER",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Sampler" + + @staticmethod + def doit(seed, steps, cfg, sampler_name, scheduler, denoise, basic_pipe, scheduler_func_opt=None): + model, _, _, positive, negative = basic_pipe + sampler = KSamplerWrapper(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, denoise, scheduler_func=scheduler_func_opt) + return (sampler, ) + + +class KSamplerAdvancedProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "toolip": "classifier free guidance value"}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, {"toolip": "sampler"}), + "scheduler": (core.SCHEDULERS, {"toolip": "noise schedule"}), + "sigma_factor": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.01, "toolip": "Multiplier of noise schedule"}), + "basic_pipe": ("BASIC_PIPE", {"toolip": "basic_pipe input for sampling"}) + }, + "optional": { + "sampler_opt": ("SAMPLER", {"toolip": "[OPTIONAL] Uses the passed sampler instead of internal impact_sampler."}), + "scheduler_func_opt": ("SCHEDULER_FUNC", {"toolip": "[OPTIONAL] Noise schedule generation function. If this is set, the scheduler widget will be ignored."}), + } + } + + OUTPUT_TOOLTIPS = ("sampler wrapper. (Can be used when generating a regional_prompt.)", ) + + RETURN_TYPES = ("KSAMPLER_ADVANCED",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Sampler" + + @staticmethod + def doit(cfg, sampler_name, scheduler, basic_pipe, sigma_factor=1.0, sampler_opt=None, scheduler_func_opt=None): + model, _, _, positive, negative = basic_pipe + sampler = KSamplerAdvancedWrapper(model, cfg, sampler_name, scheduler, positive, negative, sampler_opt=sampler_opt, sigma_factor=sigma_factor, scheduler_func=scheduler_func_opt) + return (sampler, ) + + +class TwoSamplersForMask: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "latent_image": ("LATENT", {"tooltip": "input latent image"}), + "base_sampler": ("KSAMPLER", {"tooltip": "Sampler to apply to the region outside the mask."}), + "mask_sampler": ("KSAMPLER", {"tooltip": "Sampler to apply to the masked region."}), + "mask": ("MASK", {"tooltip": "region mask"}) + }, + } + + OUTPUT_TOOLTIPS = ("result latent", ) + + RETURN_TYPES = ("LATENT", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Sampler" + + @staticmethod + def doit(latent_image, base_sampler, mask_sampler, mask): + inv_mask = torch.where(mask != 1.0, torch.tensor(1.0), torch.tensor(0.0)) + + latent_image['noise_mask'] = inv_mask + new_latent_image = base_sampler.sample(latent_image) + + new_latent_image['noise_mask'] = mask + new_latent_image = mask_sampler.sample(new_latent_image) + + del new_latent_image['noise_mask'] + + return (new_latent_image, ) + + +class TwoAdvancedSamplersForMask: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Random seed to use for generating CPU noise for sampling."}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000, "tooltip": "total sampling steps"}), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "The amount of noise to remove. This amount is the noise added at the start, and the higher it is, the more the input latent will be modified before being returned."}), + "samples": ("LATENT", {"tooltip": "input latent image"}), + "base_sampler": ("KSAMPLER_ADVANCED", {"tooltip": "Sampler to apply to the region outside the mask."}), + "mask_sampler": ("KSAMPLER_ADVANCED", {"tooltip": "Sampler to apply to the masked region."}), + "mask": ("MASK", {"tooltip": "region mask"}), + "overlap_factor": ("INT", {"default": 10, "min": 0, "max": 10000, "tooltip": "To smooth the seams of the region boundaries, expand the mask by the overlap_factor amount to overlap with other regions."}) + }, + } + + OUTPUT_TOOLTIPS = ("result latent", ) + + RETURN_TYPES = ("LATENT", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Sampler" + + @staticmethod + def doit(seed, steps, denoise, samples, base_sampler, mask_sampler, mask, overlap_factor): + regional_prompts = RegionalPrompt().doit(mask=mask, advanced_sampler=mask_sampler)[0] + + return RegionalSampler().doit(seed=seed, seed_2nd=0, seed_2nd_mode="ignore", steps=steps, base_only_steps=1, + denoise=denoise, samples=samples, base_sampler=base_sampler, + regional_prompts=regional_prompts, overlap_factor=overlap_factor, + restore_latent=True, additional_mode="ratio between", + additional_sampler="AUTO", additional_sigma_ratio=0.3) + + +class RegionalPrompt: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "mask": ("MASK", {"tooltip": "region mask"}), + "advanced_sampler": ("KSAMPLER_ADVANCED", {"tooltip": "sampler for specified region"}), + }, + "optional": { + "variation_seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Sets the extra seed to be used for noise variation."}), + "variation_strength": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "Sets the strength of the noise variation."}), + "variation_method": (["linear", "slerp"], {"tooltip": "Sets how the original noise and extra noise are blended together."}), + } + } + + OUTPUT_TOOLTIPS = ("regional prompts. (Can be used in the RegionalSampler.)", ) + + RETURN_TYPES = ("REGIONAL_PROMPTS", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Regional" + + @staticmethod + def doit(mask, advanced_sampler, variation_seed=0, variation_strength=0.0, variation_method="linear"): + regional_prompt = core.REGIONAL_PROMPT(mask, advanced_sampler, variation_seed=variation_seed, variation_strength=variation_strength, variation_method=variation_method) + return ([regional_prompt], ) + + +class CombineRegionalPrompts: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "regional_prompts1": ("REGIONAL_PROMPTS", {"tooltip": "input regional_prompts. (Connecting to the input slot increases the number of additional slots.)"}), + }, + } + + OUTPUT_TOOLTIPS = ("Combined REGIONAL_PROMPTS", ) + + RETURN_TYPES = ("REGIONAL_PROMPTS", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Regional" + + @staticmethod + def doit(**kwargs): + res = [] + for k, v in kwargs.items(): + res += v + + return (res, ) + + +class CombineConditionings: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "conditioning1": ("CONDITIONING", { "tooltip": "input conditionings. (Connecting to the input slot increases the number of additional slots.)" }), + }, + } + + OUTPUT_TOOLTIPS = ("Combined conditioning", ) + + RETURN_TYPES = ("CONDITIONING", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def doit(**kwargs): + res = [] + for k, v in kwargs.items(): + res += v + + return (res, ) + + +class ConcatConditionings: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "conditioning1": ("CONDITIONING", { "tooltip": "input conditionings. (Connecting to the input slot increases the number of additional slots.)" }), + }, + } + + OUTPUT_TOOLTIPS = ("Concatenated conditioning", ) + + RETURN_TYPES = ("CONDITIONING", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + @staticmethod + def doit(**kwargs): + conditioning_to = list(kwargs.values())[0] + + for k, conditioning_from in list(kwargs.items())[1:]: + out = [] + if len(conditioning_from) > 1: + logging.warning("Warning: ConcatConditionings {k} contains more than 1 cond, only the first one will actually be applied to conditioning1.") + + cond_from = conditioning_from[0][0] + + for i in range(len(conditioning_to)): + t1 = conditioning_to[i][0] + tw = torch.cat((t1, cond_from), 1) + n = [tw, conditioning_to[i][1].copy()] + out.append(n) + + conditioning_to = out + + return (out, ) + + +class RegionalSampler: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Random seed to use for generating CPU noise for sampling."}), + "seed_2nd": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Additional noise seed. The behavior is determined by seed_2nd_mode."}), + "seed_2nd_mode": (["ignore", "fixed", "seed+seed_2nd", "seed-seed_2nd", "increment", "decrement", "randomize"], {"tooltip": "application method of seed_2nd. 1) ignore: Do not use seed_2nd. In the base only sampling stage, the seed is applied as a noise seed, and in the regional sampling stage, denoising is performed as it is without additional noise. 2) Others: In the base only sampling stage, the seed is applied as a noise seed, and once it is closed so that there is no leftover noise, new noise is added with seed_2nd and the regional samping stage is performed. a) fixed: Use seed_2nd as it is as an additional noise seed. b) seed+seed_2nd: Apply the value of seed+seed_2nd as an additional noise seed. c) seed-seed_2nd: Apply the value of seed-seed_2nd as an additional noise seed. d) increment: Not implemented yet. Same with fixed. e) decrement: Not implemented yet. Same with fixed. f) randomize: Not implemented yet. Same with fixed."}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000, "tooltip": "total sampling steps"}), + "base_only_steps": ("INT", {"default": 2, "min": 0, "max": 10000, "tooltip": "total sampling steps"}), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "The amount of noise to remove. This amount is the noise added at the start, and the higher it is, the more the input latent will be modified before being returned."}), + "samples": ("LATENT", {"tooltip": "input latent image"}), + "base_sampler": ("KSAMPLER_ADVANCED", {"tooltip": "The sampler applied outside the area set by the regional_prompt."}), + "regional_prompts": ("REGIONAL_PROMPTS", {"tooltip": "The prompt applied to each region"}), + "overlap_factor": ("INT", {"default": 10, "min": 0, "max": 10000, "tooltip": "To smooth the seams of the region boundaries, expand the mask set in regional_prompts by the overlap_factor amount to overlap with other regions."}), + "restore_latent": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled", "tooltip": "At each step, restore the noise outside the mask area to its original state, as per the principle of inpainting. This option is provided for backward compatibility, and it is recommended to always set it to true."}), + "additional_mode": (["DISABLE", "ratio additional", "ratio between"], {"default": "ratio between", "tooltip": "..._sde or uni_pc and other special samplers are used, the region is not properly denoised, and it causes a phenomenon that destroys the overall harmony. To compensate for this, a recovery operation is performed using another sampler. This requires a longer time for sampling because a second sampling is performed at each step in each region using a special sampler. 1) DISABLE: Disable this feature. 2) ratio additional: After performing the denoise amount to be performed in the step with the sampler set in the region, the recovery sampler is additionally applied by the additional_sigma_ratio. If you use this option, the total denoise amount increases by additional_sigma_ratio. 3) ratio between: The denoise amount to be performed in the step with the sampler set in the region and the denoise amount to be applied to the recovery sampler are divided by additional_sigma_ratio, and denoise is performed for each denoise amount. If you use this option, the total denoise amount does not change."}), + "additional_sampler": (["AUTO", "euler", "heun", "heunpp2", "dpm_2", "dpm_fast", "dpmpp_2m", "ddpm"], {"tooltip": "1) AUTO: Automatically set the recovery sampler. If the sampler is uni_pc, uni_pc_bh2, dpmpp_sde, dpmpp_sde_gpu, the dpm_fast sampler is selected If the sampler is dpmpp_2m_sde, dpmpp_2m_sde_gpu, dpmpp_3m_sde, dpmpp_3m_sde_gpu, the dpmpp_2m sampler is selected. 2) Others: Manually set the recovery sampler."}), + "additional_sigma_ratio": ("FLOAT", {"default": 0.3, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "Multiplier of noise schedule to be applied according to additional_mode."}), + }, + "hidden": {"unique_id": "UNIQUE_ID"}, + } + + OUTPUT_TOOLTIPS = ("result latent", ) + + RETURN_TYPES = ("LATENT", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Regional" + + @staticmethod + def separated_sample(*args, **kwargs): + return separated_sample(*args, **kwargs) + + @staticmethod + def mask_erosion(samples, mask, grow_mask_by): + mask = mask.clone() + + w = samples['samples'].shape[3] + h = samples['samples'].shape[2] + + mask2 = torch.nn.functional.interpolate(mask.reshape((-1, 1, mask.shape[-2], mask.shape[-1])), size=(w, h), mode="bilinear") + if grow_mask_by == 0: + mask_erosion = mask2 + else: + kernel_tensor = torch.ones((1, 1, grow_mask_by, grow_mask_by)) + padding = math.ceil((grow_mask_by - 1) / 2) + + mask_erosion = torch.clamp(torch.nn.functional.conv2d(mask2.round(), kernel_tensor, padding=padding), 0, 1) + + return mask_erosion[:, :, :w, :h].round() + + @staticmethod + def doit(seed, seed_2nd, seed_2nd_mode, steps, base_only_steps, denoise, samples, base_sampler, regional_prompts, overlap_factor, restore_latent, + additional_mode, additional_sampler, additional_sigma_ratio, unique_id=None): + + samples = samples.copy() + samples['samples'] = comfy.sample.fix_empty_latent_channels(base_sampler.params[0], samples['samples']) + + if restore_latent: + latent_compositor = nodes.NODE_CLASS_MAPPINGS['LatentCompositeMasked']() + else: + latent_compositor = None + + masks = [regional_prompt.mask.numpy() for regional_prompt in regional_prompts] + masks = [np.ceil(mask).astype(np.int32) for mask in masks] + combined_mask = torch.from_numpy(np.bitwise_or.reduce(masks)) + + inv_mask = torch.where(combined_mask == 0, torch.tensor(1.0), torch.tensor(0.0)) + + adv_steps = int(steps / denoise) + start_at_step = adv_steps - steps + + region_len = len(regional_prompts) + total = steps*region_len + + leftover_noise = False + if base_only_steps > 0: + if seed_2nd_mode == 'ignore': + leftover_noise = True + + noise = Noise_RandomNoise(seed).generate_noise(samples) + + for rp in regional_prompts: + noise = rp.touch_noise(noise) + + samples = base_sampler.sample_advanced(True, seed, adv_steps, samples, start_at_step, start_at_step + base_only_steps, leftover_noise, recovery_mode="DISABLE", noise=noise) + + if seed_2nd_mode == "seed+seed_2nd": + seed += seed_2nd + if seed > 1125899906842624: + seed = seed - 1125899906842624 + elif seed_2nd_mode == "seed-seed_2nd": + seed -= seed_2nd + if seed < 0: + seed += 1125899906842624 + elif seed_2nd_mode != 'ignore': + seed = seed_2nd + + new_latent_image = samples.copy() + base_latent_image = None + + if not leftover_noise: + add_noise = True + noise = Noise_RandomNoise(seed).generate_noise(samples) + + for rp in regional_prompts: + noise = rp.touch_noise(noise) + else: + add_noise = False + noise = None + + for i in range(start_at_step+base_only_steps, adv_steps): + core.update_node_status(unique_id, f"{i}/{steps} steps | ", ((i-start_at_step)*region_len)/total) + + new_latent_image['noise_mask'] = inv_mask + new_latent_image = base_sampler.sample_advanced(add_noise, seed, adv_steps, new_latent_image, + start_at_step=i, end_at_step=i + 1, return_with_leftover_noise=True, + recovery_mode=additional_mode, recovery_sampler=additional_sampler, recovery_sigma_ratio=additional_sigma_ratio, noise=noise) + + if restore_latent: + if 'noise_mask' in new_latent_image: + del new_latent_image['noise_mask'] + base_latent_image = new_latent_image.copy() + + j = 1 + for regional_prompt in regional_prompts: + if restore_latent: + new_latent_image = base_latent_image.copy() + + core.update_node_status(unique_id, f"{i}/{steps} steps | {j}/{region_len}", ((i-start_at_step)*region_len + j)/total) + + region_mask = regional_prompt.get_mask_erosion(overlap_factor).squeeze(0).squeeze(0) + + new_latent_image['noise_mask'] = region_mask + new_latent_image = regional_prompt.sampler.sample_advanced(False, seed, adv_steps, new_latent_image, i, i + 1, True, + recovery_mode=additional_mode, recovery_sampler=additional_sampler, recovery_sigma_ratio=additional_sigma_ratio) + + if restore_latent: + del new_latent_image['noise_mask'] + base_latent_image = latent_compositor.composite(base_latent_image, new_latent_image, 0, 0, False, region_mask)[0] + new_latent_image = base_latent_image + + j += 1 + + add_noise = False + + # finalize + core.update_node_status(unique_id, "finalize") + if base_latent_image is not None: + new_latent_image = base_latent_image + else: + base_latent_image = new_latent_image + + new_latent_image['noise_mask'] = inv_mask + new_latent_image = base_sampler.sample_advanced(False, seed, adv_steps, new_latent_image, adv_steps, adv_steps+1, False, + recovery_mode=additional_mode, recovery_sampler=additional_sampler, recovery_sigma_ratio=additional_sigma_ratio) + + core.update_node_status(unique_id, f"{steps}/{steps} steps", total) + core.update_node_status(unique_id, "", None) + + if restore_latent: + new_latent_image = base_latent_image + + if 'noise_mask' in new_latent_image: + del new_latent_image['noise_mask'] + + return (new_latent_image, ) + + +class RegionalSamplerAdvanced: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "add_noise": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled", "tooltip": "Whether to add noise"}), + "noise_seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Random seed to use for generating CPU noise for sampling."}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000, "tooltip": "total sampling steps"}), + "start_at_step": ("INT", {"default": 0, "min": 0, "max": 10000, "tooltip": "The starting step of the sampling to be applied at this node within the range of 'steps'."}), + "end_at_step": ("INT", {"default": 10000, "min": 0, "max": 10000, "tooltip": "The step at which sampling applied at this node will stop within the range of steps (if greater than steps, sampling will continue only up to steps)."}), + "overlap_factor": ("INT", {"default": 10, "min": 0, "max": 10000, "tooltip": "To smooth the seams of the region boundaries, expand the mask set in regional_prompts by the overlap_factor amount to overlap with other regions."}), + "restore_latent": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled", "tooltip": "At each step, restore the noise outside the mask area to its original state, as per the principle of inpainting. This option is provided for backward compatibility, and it is recommended to always set it to true."}), + "return_with_leftover_noise": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled", "tooltip": "Whether to return the latent with noise remaining if the noise has not been completely removed according to the noise schedule, or to completely remove the noise before returning it."}), + "latent_image": ("LATENT", {"tooltip": "input latent image"}), + "base_sampler": ("KSAMPLER_ADVANCED", {"tooltip": "The sampler applied outside the area set by the regional_prompt."}), + "regional_prompts": ("REGIONAL_PROMPTS", {"tooltip": "The prompt applied to each region"}), + "additional_mode": (["DISABLE", "ratio additional", "ratio between"], {"default": "ratio between", "tooltip": "..._sde or uni_pc and other special samplers are used, the region is not properly denoised, and it causes a phenomenon that destroys the overall harmony. To compensate for this, a recovery operation is performed using another sampler. This requires a longer time for sampling because a second sampling is performed at each step in each region using a special sampler. 1) DISABLE: Disable this feature. 2) ratio additional: After performing the denoise amount to be performed in the step with the sampler set in the region, the recovery sampler is additionally applied by the additional_sigma_ratio. If you use this option, the total denoise amount increases by additional_sigma_ratio. 3) ratio between: The denoise amount to be performed in the step with the sampler set in the region and the denoise amount to be applied to the recovery sampler are divided by additional_sigma_ratio, and denoise is performed for each denoise amount. If you use this option, the total denoise amount does not change."}), + "additional_sampler": (["AUTO", "euler", "heun", "heunpp2", "dpm_2", "dpm_fast", "dpmpp_2m", "ddpm"], {"tooltip": "1) AUTO: Automatically set the recovery sampler. If the sampler is uni_pc, uni_pc_bh2, dpmpp_sde, dpmpp_sde_gpu, the dpm_fast sampler is selected If the sampler is dpmpp_2m_sde, dpmpp_2m_sde_gpu, dpmpp_3m_sde, dpmpp_3m_sde_gpu, the dpmpp_2m sampler is selected. 2) Others: Manually set the recovery sampler."}), + "additional_sigma_ratio": ("FLOAT", {"default": 0.3, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "Multiplier of noise schedule to be applied according to additional_mode."}), + }, + "hidden": {"unique_id": "UNIQUE_ID"}, + } + + OUTPUT_TOOLTIPS = ("result latent", ) + + RETURN_TYPES = ("LATENT", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Regional" + + @staticmethod + def doit(add_noise, noise_seed, steps, start_at_step, end_at_step, overlap_factor, restore_latent, return_with_leftover_noise, latent_image, base_sampler, regional_prompts, + additional_mode, additional_sampler, additional_sigma_ratio, unique_id): + + new_latent_image = latent_image.copy() + new_latent_image['samples'] = comfy.sample.fix_empty_latent_channels(base_sampler.params[0], new_latent_image['samples']) + + if restore_latent: + latent_compositor = nodes.NODE_CLASS_MAPPINGS['LatentCompositeMasked']() + else: + latent_compositor = None + + masks = [regional_prompt.mask.numpy() for regional_prompt in regional_prompts] + masks = [np.ceil(mask).astype(np.int32) for mask in masks] + combined_mask = torch.from_numpy(np.bitwise_or.reduce(masks)) + + inv_mask = torch.where(combined_mask == 0, torch.tensor(1.0), torch.tensor(0.0)) + + region_len = len(regional_prompts) + end_at_step = min(steps, end_at_step) + total = (end_at_step - start_at_step) * region_len + + base_latent_image = None + region_masks = {} + + for i in range(start_at_step, end_at_step-1): + core.update_node_status(unique_id, f"{start_at_step+i}/{end_at_step} steps | ", ((i-start_at_step)*region_len)/total) + + cur_add_noise = True if i == start_at_step and add_noise else False + + if cur_add_noise: + noise = Noise_RandomNoise(noise_seed).generate_noise(new_latent_image) + for rp in regional_prompts: + noise = rp.touch_noise(noise) + else: + noise = None + + new_latent_image['noise_mask'] = inv_mask + new_latent_image = base_sampler.sample_advanced(cur_add_noise, noise_seed, steps, new_latent_image, i, i + 1, True, + recovery_mode=additional_mode, recovery_sampler=additional_sampler, recovery_sigma_ratio=additional_sigma_ratio, noise=noise) + + if restore_latent: + del new_latent_image['noise_mask'] + base_latent_image = new_latent_image.copy() + + j = 1 + for regional_prompt in regional_prompts: + if restore_latent: + new_latent_image = base_latent_image.copy() + + core.update_node_status(unique_id, f"{start_at_step+i}/{end_at_step} steps | {j}/{region_len}", ((i-start_at_step)*region_len + j)/total) + + if j not in region_masks: + region_mask = regional_prompt.get_mask_erosion(overlap_factor).squeeze(0).squeeze(0) + region_masks[j] = region_mask + else: + region_mask = region_masks[j] + + new_latent_image['noise_mask'] = region_mask + new_latent_image = regional_prompt.sampler.sample_advanced(False, noise_seed, steps, new_latent_image, i, i + 1, True, + recovery_mode=additional_mode, recovery_sampler=additional_sampler, recovery_sigma_ratio=additional_sigma_ratio) + + if restore_latent: + del new_latent_image['noise_mask'] + base_latent_image = latent_compositor.composite(base_latent_image, new_latent_image, 0, 0, False, region_mask)[0] + new_latent_image = base_latent_image + + j += 1 + + # finalize + core.update_node_status(unique_id, "finalize") + if base_latent_image is not None: + new_latent_image = base_latent_image + else: + base_latent_image = new_latent_image + + new_latent_image['noise_mask'] = inv_mask + new_latent_image = base_sampler.sample_advanced(False, noise_seed, steps, new_latent_image, end_at_step-1, end_at_step, return_with_leftover_noise, + recovery_mode=additional_mode, recovery_sampler=additional_sampler, recovery_sigma_ratio=additional_sigma_ratio) + + core.update_node_status(unique_id, f"{end_at_step}/{end_at_step} steps", total) + core.update_node_status(unique_id, "", None) + + if restore_latent: + new_latent_image = base_latent_image + + if 'noise_mask' in new_latent_image: + del new_latent_image['noise_mask'] + + return (new_latent_image, ) + + +class KSamplerBasicPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": + {"basic_pipe": ("BASIC_PIPE", {"tooltip": "basic_pipe input for sampling"}), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Random seed to use for generating CPU noise for sampling."}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000, "tooltip": "total sampling steps"}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "tooltip": "classifier free guidance value"}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, {"tooltip": "sampler"}), + "scheduler": (core.SCHEDULERS, {"tooltip": "noise schedule"}), + "latent_image": ("LATENT", {"tooltip": "input latent image"}), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "The amount of noise to remove. This amount is the noise added at the start, and the higher it is, the more the input latent will be modified before being returned."}), + }, + "optional": + { + "scheduler_func_opt": ("SCHEDULER_FUNC", {"tooltip": "[OPTIONAL] Noise schedule generation function. If this is set, the scheduler widget will be ignored."}), + } + } + + OUTPUT_TOOLTIPS = ("passthrough input basic_pipe", "result latent", "VAE in basic_pipe") + + RETURN_TYPES = ("BASIC_PIPE", "LATENT", "VAE") + FUNCTION = "sample" + + CATEGORY = "ImpactPack/sampling" + + @staticmethod + def sample(basic_pipe, seed, steps, cfg, sampler_name, scheduler, latent_image, denoise=1.0, scheduler_func_opt=None): + model, clip, vae, positive, negative = basic_pipe + latent = impact_sample(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise, scheduler_func=scheduler_func_opt) + return basic_pipe, latent, vae + + +class KSamplerAdvancedBasicPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": + {"basic_pipe": ("BASIC_PIPE", {"tooltip": "basic_pipe input for sampling"}), + "add_noise": ("BOOLEAN", {"default": True, "label_on": "enable", "label_off": "disable", "tooltip": "Whether to add noise"}), + "noise_seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "tooltip": "Random seed to use for generating CPU noise for sampling."}), + "steps": ("INT", {"default": 20, "min": 1, "max": 10000, "tooltip": "total sampling steps"}), + "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "tooltip": "classifier free guidance value"}), + "sampler_name": (comfy.samplers.KSampler.SAMPLERS, {"tooltip": "sampler"}), + "scheduler": (core.SCHEDULERS, {"tooltip": "noise schedule"}), + "latent_image": ("LATENT", {"tooltip": "input latent image"}), + "start_at_step": ("INT", {"default": 0, "min": 0, "max": 10000, "tooltip": "The starting step of the sampling to be applied at this node within the range of 'steps'."}), + "end_at_step": ("INT", {"default": 10000, "min": 0, "max": 10000, "tooltip": "The step at which sampling applied at this node will stop within the range of steps (if greater than steps, sampling will continue only up to steps)."}), + "return_with_leftover_noise": ("BOOLEAN", {"default": False, "label_on": "enable", "label_off": "disable", "tooltip": "Whether to return the latent with noise remaining if the noise has not been completely removed according to the noise schedule, or to completely remove the noise before returning it."}), + }, + "optional": + { + "scheduler_func_opt": ("SCHEDULER_FUNC", {"tooltip": "[OPTIONAL] Noise schedule generation function. If this is set, the scheduler widget will be ignored."}), + } + } + + OUTPUT_TOOLTIPS = ("passthrough input basic_pipe", "result latent", "VAE in basic_pipe") + + RETURN_TYPES = ("BASIC_PIPE", "LATENT", "VAE") + FUNCTION = "sample" + + CATEGORY = "ImpactPack/sampling" + + @staticmethod + def sample(basic_pipe, add_noise, noise_seed, steps, cfg, sampler_name, scheduler, latent_image, start_at_step, end_at_step, return_with_leftover_noise, denoise=1.0, scheduler_func_opt=None): + model, clip, vae, positive, negative = basic_pipe + + latent = separated_sample(model, add_noise, noise_seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, start_at_step, end_at_step, return_with_leftover_noise, scheduler_func=scheduler_func_opt) + return basic_pipe, latent, vae + + +class GITSSchedulerFuncProvider: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "coeff": ("FLOAT", {"default": 1.20, "min": 0.80, "max": 1.50, "step": 0.05, "tooltip": "coeff factor of GITS Scheduler"}), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "denoise amount for noise schedule"}), + } + } + + OUTPUT_TOOLTIPS = ("Returns a function that generates a noise schedule using GITSScheduler. This can be used in place of a predetermined noise schedule to dynamically generate a noise schedule based on the steps.",) + + RETURN_TYPES = ("SCHEDULER_FUNC",) + CATEGORY = "ImpactPack/sampling" + + FUNCTION = "doit" + + @staticmethod + def doit(coeff, denoise): + def f(model, sampler, steps): + if 'GITSScheduler' not in nodes.NODE_CLASS_MAPPINGS: + raise Exception("[Impact Pack] ComfyUI is an outdated version. Cannot use GITSScheduler.") + + scheduler = nodes.NODE_CLASS_MAPPINGS['GITSScheduler']() + return scheduler.get_sigmas(coeff, steps, denoise)[0] + + return (f, ) + + +class NegativeConditioningPlaceholder: + @classmethod + def INPUT_TYPES(s): + return {"required": {}} + + OUTPUT_TOOLTIPS = ("This is a Placeholder for the FLUX model that does not use Negative Conditioning.",) + + RETURN_TYPES = ("CONDITIONING",) + CATEGORY = "ImpactPack/sampling" + + FUNCTION = "doit" + + @staticmethod + def doit(): + return ("NegativePlaceholder", ) diff --git a/custom_nodes/comfyui-impact-pack/modules/impact/util_nodes.py b/custom_nodes/comfyui-impact-pack/modules/impact/util_nodes.py new file mode 100644 index 0000000000000000000000000000000000000000..a6c5905cedc25b2c7325028176500d76546ca71f --- /dev/null +++ b/custom_nodes/comfyui-impact-pack/modules/impact/util_nodes.py @@ -0,0 +1,738 @@ +from impact.utils import any_typ, ByPassTypeTuple, make_3d_mask +import comfy_extras.nodes_mask +from nodes import MAX_RESOLUTION +import torch +import comfy +import sys +import nodes +import re +import impact.core as core +from server import PromptServer +import inspect +import logging + + +class GeneralSwitch: + @classmethod + def INPUT_TYPES(s): + dyn_inputs = {"input1": (any_typ, {"lazy": True, "tooltip": "Any input. When connected, one more input slot is added."}), } + if core.is_execution_model_version_supported(): + stack = inspect.stack() + if stack[2].function == 'get_input_info': + # bypass validation + class AllContainer: + def __contains__(self, item): + return True + + def __getitem__(self, key): + return any_typ, {"lazy": True} + + dyn_inputs = AllContainer() + + inputs = {"required": { + "select": ("INT", {"default": 1, "min": 1, "max": 999999, "step": 1, "tooltip": "The input number you want to output among the inputs"}), + "sel_mode": ("BOOLEAN", {"default": False, "label_on": "select_on_prompt", "label_off": "select_on_execution", "forceInput": False, + "tooltip": "In the case of 'select_on_execution', the selection is dynamically determined at the time of workflow execution. 'select_on_prompt' is an option that exists for older versions of ComfyUI, and it makes the decision before the workflow execution."}), + }, + "optional": dyn_inputs, + "hidden": {"unique_id": "UNIQUE_ID", "extra_pnginfo": "EXTRA_PNGINFO"} + } + + return inputs + + RETURN_TYPES = (any_typ, "STRING", "INT") + RETURN_NAMES = ("selected_value", "selected_label", "selected_index") + OUTPUT_TOOLTIPS = ("Output is generated only from the input chosen by the 'select' value.", "Slot label of the selected input slot", "Outputs the select value as is") + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def check_lazy_status(self, *args, **kwargs): + selected_index = int(kwargs['select']) + input_name = f"input{selected_index}" + + logging.info(f"SELECTED: {input_name}") + + if input_name in kwargs: + return [input_name] + else: + return [] + + @staticmethod + def doit(*args, **kwargs): + selected_index = int(kwargs['select']) + input_name = f"input{selected_index}" + + selected_label = input_name + node_id = kwargs['unique_id'] + + if 'extra_pnginfo' in kwargs and kwargs['extra_pnginfo'] is not None: + nodelist = kwargs['extra_pnginfo']['workflow']['nodes'] + for node in nodelist: + if str(node['id']) == node_id: + inputs = node['inputs'] + + for slot in inputs: + if slot['name'] == input_name and 'label' in slot: + selected_label = slot['label'] + + break + else: + logging.info("[Impact-Pack] The switch node does not guarantee proper functioning in API mode.") + + if input_name in kwargs: + return kwargs[input_name], selected_label, selected_index + else: + logging.info("ImpactSwitch: invalid select index (ignored)") + return None, "", selected_index + +class LatentSwitch: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "select": ("INT", {"default": 1, "min": 1, "max": 99999, "step": 1}), + "latent1": ("LATENT",), + }, + } + + RETURN_TYPES = ("LATENT", ) + + OUTPUT_NODE = True + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, *args, **kwargs): + input_name = f"latent{int(kwargs['select'])}" + + if input_name in kwargs: + return (kwargs[input_name],) + else: + logging.info("LatentSwitch: invalid select index ('latent1' is selected)") + return (kwargs['latent1'],) + + +class ImageMaskSwitch: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "select": ("INT", {"default": 1, "min": 1, "max": 4, "step": 1}), + "images1": ("IMAGE",), + }, + + "optional": { + "mask1_opt": ("MASK",), + "images2_opt": ("IMAGE",), + "mask2_opt": ("MASK",), + "images3_opt": ("IMAGE",), + "mask3_opt": ("MASK",), + "images4_opt": ("IMAGE",), + "mask4_opt": ("MASK",), + }, + } + + RETURN_TYPES = ("IMAGE", "MASK",) + + OUTPUT_NODE = True + + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, select, images1, mask1_opt=None, images2_opt=None, mask2_opt=None, images3_opt=None, mask3_opt=None, + images4_opt=None, mask4_opt=None): + if select == 1: + return images1, mask1_opt, + elif select == 2: + return images2_opt, mask2_opt, + elif select == 3: + return images3_opt, mask3_opt, + else: + return images4_opt, mask4_opt, + + +class GeneralInversedSwitch: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "select": ("INT", {"default": 1, "min": 1, "max": 999999, "step": 1, "tooltip": "The output number you want to send from the input"}), + "input": (any_typ, {"tooltip": "Any input. When connected, one more input slot is added."}), + + }, + "optional": { + "sel_mode": ("BOOLEAN", {"default": False, "label_on": "select_on_prompt", "label_off": "select_on_execution", "forceInput": False, + "tooltip": "In the case of 'select_on_execution', the selection is dynamically determined at the time of workflow execution. 'select_on_prompt' is an option that exists for older versions of ComfyUI, and it makes the decision before the workflow execution."}), + }, + "hidden": {"prompt": "PROMPT", "unique_id": "UNIQUE_ID"}, + } + + RETURN_TYPES = ByPassTypeTuple((any_typ, )) + OUTPUT_TOOLTIPS = ("Output occurs only from the output selected by the 'select' value.\nWhen slots are connected, additional slots are created.", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, select, prompt, unique_id, input, **kwargs): + if core.is_execution_model_version_supported(): + from comfy_execution.graph import ExecutionBlocker + else: + logging.warning("[Impact Pack] InversedSwitch: ComfyUI is outdated. The 'select_on_execution' mode cannot function properly.") + + res = [] + + # search max output count in prompt + cnt = 0 + for x in prompt.values(): + for y in x.get('inputs', {}).values(): + if isinstance(y, list) and len(y) == 2: + if y[0] == unique_id: + cnt = max(cnt, y[1]) + + for i in range(0, cnt + 1): + if select == i+1: + res.append(input) + elif core.is_execution_model_version_supported(): + res.append(ExecutionBlocker(None)) + else: + res.append(None) + + return res + + +class RemoveNoiseMask: + @classmethod + def INPUT_TYPES(s): + return {"required": {"samples": ("LATENT",)}} + + RETURN_TYPES = ("LATENT",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, samples): + res = {key: value for key, value in samples.items() if key != 'noise_mask'} + return (res, ) + + +class ImagePasteMasked: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "destination": ("IMAGE",), + "source": ("IMAGE",), + "x": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}), + "y": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}), + "resize_source": ("BOOLEAN", {"default": False}), + }, + "optional": { + "mask": ("MASK",), + } + } + RETURN_TYPES = ("IMAGE",) + FUNCTION = "composite" + + CATEGORY = "image" + + def composite(self, destination, source, x, y, resize_source, mask = None): + destination = destination.clone().movedim(-1, 1) + output = comfy_extras.nodes_mask.composite(destination, source.movedim(-1, 1), x, y, mask, 1, resize_source).movedim(1, -1) + return (output,) + + +from impact.utils import any_typ + +class ImpactLogger: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "data": (any_typ,), + "text": ("STRING", {"multiline": True}), + }, + "hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO", "unique_id": "UNIQUE_ID"}, + } + + CATEGORY = "ImpactPack/Debug" + + OUTPUT_NODE = True + + RETURN_TYPES = () + FUNCTION = "doit" + + def doit(self, data, text, prompt, extra_pnginfo, unique_id): + shape = "" + if hasattr(data, "shape"): + shape = f"{data.shape} / " + + logging.info(f"[IMPACT LOGGER]: {shape}{data}") + + logging.info(f" PROMPT: {prompt}") + + # for x in prompt: + # if 'inputs' in x and 'populated_text' in x['inputs']: + # print(f"PROMPT: {x['10']['inputs']['populated_text']}") + # + # for x in extra_pnginfo['workflow']['nodes']: + # if x['type'] == 'ImpactWildcardProcessor': + # print(f" WV : {x['widgets_values'][1]}\n") + + PromptServer.instance.send_sync("impact-node-feedback", {"node_id": unique_id, "widget_name": "text", "type": "TEXT", "value": f"{data}"}) + return {} + + +class ImpactDummyInput: + @classmethod + def INPUT_TYPES(s): + return {"required": {}} + + CATEGORY = "ImpactPack/Debug" + + RETURN_TYPES = (any_typ,) + FUNCTION = "doit" + + def doit(self): + return ("DUMMY",) + + +class MasksToMaskList: + @classmethod + def INPUT_TYPES(s): + return {"optional": { + "masks": ("MASK", ), + } + } + + RETURN_TYPES = ("MASK", ) + OUTPUT_IS_LIST = (True, ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, masks): + if masks is None: + empty_mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu") + return ([empty_mask], ) + + res = [] + + for mask in masks: + res.append(mask) + + res = [make_3d_mask(x) for x in res] + + return (res, ) + + +class MaskListToMaskBatch: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "mask": ("MASK", ), + } + } + + INPUT_IS_LIST = True + + RETURN_TYPES = ("MASK", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, mask): + if len(mask) == 1: + mask = make_3d_mask(mask[0]) + return (mask,) + elif len(mask) > 1: + mask1 = make_3d_mask(mask[0]) + + for mask2 in mask[1:]: + mask2 = make_3d_mask(mask2) + if mask1.shape[1:] != mask2.shape[1:]: + mask2 = comfy.utils.common_upscale(mask2.movedim(-1, 1), mask1.shape[2], mask1.shape[1], "lanczos", "center").movedim(1, -1) + mask1 = torch.cat((mask1, mask2), dim=0) + + return (mask1,) + else: + empty_mask = torch.zeros((1, 64, 64), dtype=torch.float32, device="cpu").unsqueeze(0) + return (empty_mask,) + + +class ImageListToImageBatch: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "images": ("IMAGE", ), + } + } + + INPUT_IS_LIST = True + + RETURN_TYPES = ("IMAGE", ) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Operation" + + def doit(self, images): + if len(images) <= 1: + return (images[0],) + else: + image1 = images[0] + for image2 in images[1:]: + if image1.shape[1:] != image2.shape[1:]: + image2 = comfy.utils.common_upscale(image2.movedim(-1, 1), image1.shape[2], image1.shape[1], "lanczos", "center").movedim(1, -1) + image1 = torch.cat((image1, image2), dim=0) + return (image1,) + + +class ImageBatchToImageList: + @classmethod + def INPUT_TYPES(s): + return {"required": {"image": ("IMAGE",), }} + + RETURN_TYPES = ("IMAGE",) + OUTPUT_IS_LIST = (True,) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, image): + images = [image[i:i + 1, ...] for i in range(image.shape[0])] + return (images, ) + + +class MakeAnyList: + @classmethod + def INPUT_TYPES(s): + return { + "required": {}, + "optional": {"value1": (any_typ,), } + } + + RETURN_TYPES = (any_typ,) + OUTPUT_IS_LIST = (True,) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, **kwargs): + values = [] + + for k, v in kwargs.items(): + if v is not None: + values.append(v) + + return (values, ) + + +class MakeMaskList: + @classmethod + def INPUT_TYPES(s): + return {"required": {"mask1": ("MASK",), }} + + RETURN_TYPES = ("MASK",) + OUTPUT_IS_LIST = (True,) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, **kwargs): + masks = [] + + for k, v in kwargs.items(): + masks.append(v) + + return (masks, ) + + +class NthItemOfAnyList: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "any_list": (any_typ,), + "index": ("INT", {"default": 0, "min": 0, "max": sys.maxsize, "step": 1, "tooltip": "The index of the item you want to select from the list."}), + } + } + + RETURN_TYPES = (any_typ,) + INPUT_IS_LIST = True + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + DESCRIPTION = "Selects the Nth item from a list. If the index is out of range, it returns the last item in the list." + + def doit(self, any_list, index): + i = index[0] + if i >= len(any_list): + return (any_list[-1],) + else: + return (any_list[i],) + + +class MakeImageList: + @classmethod + def INPUT_TYPES(s): + return {"optional": {"image1": ("IMAGE",), }} + + RETURN_TYPES = ("IMAGE",) + OUTPUT_IS_LIST = (True,) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, **kwargs): + images = [] + + for k, v in kwargs.items(): + images.append(v) + + return (images, ) + + +class MakeImageBatch: + @classmethod + def INPUT_TYPES(s): + return {"optional": {"image1": ("IMAGE",), }} + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, **kwargs): + images = [value for value in kwargs.values()] + + if len(images) == 1: + return (images[0],) + else: + image1 = images[0] + for image2 in images[1:]: + if image1.shape[1:] != image2.shape[1:]: + image2 = comfy.utils.common_upscale(image2.movedim(-1, 1), image1.shape[2], image1.shape[1], "lanczos", "center").movedim(1, -1) + image1 = torch.cat((image1, image2), dim=0) + return (image1,) + + +class MakeMaskBatch: + @classmethod + def INPUT_TYPES(s): + return {"optional": {"mask1": ("MASK",), }} + + RETURN_TYPES = ("MASK",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, **kwargs): + masks = [make_3d_mask(value) for value in kwargs.values()] + + if len(masks) == 1: + return (masks[0],) + else: + mask1 = masks[0] + for mask2 in masks[1:]: + if mask1.shape[1:] != mask2.shape[1:]: + mask2 = comfy.utils.common_upscale(mask2.movedim(-1, 1), mask1.shape[2], mask1.shape[1], "lanczos", "center").movedim(1, -1) + mask1 = torch.cat((mask1, mask2), dim=0) + return (mask1,) + + +class ReencodeLatent: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "samples": ("LATENT", ), + "tile_mode": (["None", "Both", "Decode(input) only", "Encode(output) only"],), + "input_vae": ("VAE", ), + "output_vae": ("VAE", ), + "tile_size": ("INT", {"default": 512, "min": 320, "max": 4096, "step": 64}), + }, + "optional": { + "overlap": ("INT", {"default": 64, "min": 0, "max": 4096, "step": 32, "tooltip": "This setting applies when 'tile_mode' is enabled."}), + } + } + + CATEGORY = "ImpactPack/Util" + + RETURN_TYPES = ("LATENT", ) + FUNCTION = "doit" + + def doit(self, samples, tile_mode, input_vae, output_vae, tile_size=512, overlap=64): + if tile_mode in ["Both", "Decode(input) only"]: + decoder = nodes.VAEDecodeTiled() + if 'overlap' in inspect.signature(decoder.decode).parameters: + pixels = decoder.decode(input_vae, samples, tile_size, overlap=overlap)[0] + else: + pixels = decoder.decode(input_vae, samples, tile_size, overlap=overlap)[0] + else: + pixels = nodes.VAEDecode().decode(input_vae, samples)[0] + + if tile_mode in ["Both", "Encode(output) only"]: + encoder = nodes.VAEEncodeTiled() + if 'overlap' in inspect.signature(encoder.encode).parameters: + return encoder.encode(output_vae, pixels, tile_size, overlap=overlap) + else: + return encoder.encode(output_vae, pixels, tile_size) + else: + return nodes.VAEEncode().encode(output_vae, pixels) + + +class ReencodeLatentPipe: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "samples": ("LATENT", ), + "tile_mode": (["None", "Both", "Decode(input) only", "Encode(output) only"],), + "input_basic_pipe": ("BASIC_PIPE", ), + "output_basic_pipe": ("BASIC_PIPE", ), + }, + } + + CATEGORY = "ImpactPack/Util" + + RETURN_TYPES = ("LATENT", ) + FUNCTION = "doit" + + def doit(self, samples, tile_mode, input_basic_pipe, output_basic_pipe): + _, _, input_vae, _, _ = input_basic_pipe + _, _, output_vae, _, _ = output_basic_pipe + return ReencodeLatent().doit(samples, tile_mode, input_vae, output_vae) + + +class StringSelector: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "strings": ("STRING", {"multiline": True}), + "multiline": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), + "select": ("INT", {"min": 0, "max": sys.maxsize, "step": 1, "default": 0}), + }} + + RETURN_TYPES = ("STRING",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, strings, multiline, select): + lines = strings.split('\n') + + if multiline: + result = [] + current_string = "" + + for line in lines: + if line.startswith("#"): + if current_string: + result.append(current_string.strip()) + current_string = "" + current_string += line + "\n" + + if current_string: + result.append(current_string.strip()) + + if len(result) == 0: + selected = strings + else: + selected = result[select % len(result)] + + if selected.startswith('#'): + selected = selected[1:] + else: + if len(lines) == 0: + selected = strings + else: + selected = lines[select % len(lines)] + + return (selected, ) + + +class StringListToString: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "join_with": ("STRING", {"default": "\\n"}), + "string_list": ("STRING", {"forceInput": True}), + } + } + + INPUT_IS_LIST = True + RETURN_TYPES = ("STRING",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, join_with, string_list): + # convert \\n to newline character + if join_with[0] == "\\n": + join_with[0] = "\n" + + joined_text = join_with[0].join(string_list) + + return (joined_text,) + + +class WildcardPromptFromString: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "string": ("STRING", {"forceInput": True}), + "delimiter": ("STRING", {"multiline": False, "default": "\\n" }), + "prefix_all": ("STRING", {"multiline": False}), + "postfix_all": ("STRING", {"multiline": False}), + "restrict_to_tags": ("STRING", {"multiline": False}), + "exclude_tags": ("STRING", {"multiline": False}) + }, + } + + RETURN_TYPES = ("STRING", "STRING",) + RETURN_NAMES = ("wildcard", "segs_labels",) + FUNCTION = "doit" + + CATEGORY = "ImpactPack/Util" + + def doit(self, string, delimiter, prefix_all, postfix_all, restrict_to_tags, exclude_tags): + # convert \\n to newline character + if delimiter == "\\n": + delimiter = "\n" + + # some sanity checks and normalization for later processing + if prefix_all is None: + prefix_all = "" + if postfix_all is None: + postfix_all = "" + if restrict_to_tags is None: + restrict_to_tags = "" + if exclude_tags is None: + exclude_tags = "" + + restrict_to_tags = restrict_to_tags.split(", ") + exclude_tags = exclude_tags.split(", ") + + # build the wildcard prompt per list entry + output = ["[LAB]"] + labels = [] + for x in string.split(delimiter): + label = str(len(labels) + 1) + labels.append(label) + x = x.split(", ") + # restrict to tags + if restrict_to_tags != [""]: + x = list(set(x) & set(restrict_to_tags)) + # remove tags + if exclude_tags != [""]: + x = list(set(x) - set(exclude_tags)) + # next row: