crystantine commited on
Commit
56fa58d
1 Parent(s): 7570721

Upload 3686 files

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. Prompt_Generator/.github/ISSUE_TEMPLATE/bug_report.md +30 -0
  2. Prompt_Generator/.github/ISSUE_TEMPLATE/custom.md +10 -0
  3. Prompt_Generator/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  4. Prompt_Generator/.gitignore +1 -0
  5. Prompt_Generator/README.md +60 -0
  6. Prompt_Generator/blacklist.txt +0 -0
  7. Prompt_Generator/install.py +5 -0
  8. Prompt_Generator/javascript/prompt_generator_hints.js +42 -0
  9. Prompt_Generator/licence +21 -0
  10. Prompt_Generator/models.json +12 -0
  11. Prompt_Generator/scripts/prompt_generator.py +270 -0
  12. adetailer/.github/ISSUE_TEMPLATE/bug_report.yaml +53 -0
  13. adetailer/.github/ISSUE_TEMPLATE/feature_request.yaml +24 -0
  14. adetailer/.github/ISSUE_TEMPLATE/question.yaml +10 -0
  15. adetailer/.github/workflows/stale.yml +13 -0
  16. adetailer/.gitignore +196 -0
  17. adetailer/.pre-commit-config.yaml +19 -0
  18. adetailer/CHANGELOG.md +275 -0
  19. adetailer/LICENSE.md +662 -0
  20. adetailer/README.md +94 -0
  21. adetailer/Taskfile.yml +25 -0
  22. adetailer/adetailer/__init__.py +20 -0
  23. adetailer/adetailer/__version__.py +1 -0
  24. adetailer/adetailer/args.py +232 -0
  25. adetailer/adetailer/common.py +127 -0
  26. adetailer/adetailer/mask.py +245 -0
  27. adetailer/adetailer/mediapipe.py +179 -0
  28. adetailer/adetailer/traceback.py +161 -0
  29. adetailer/adetailer/ui.py +558 -0
  30. adetailer/adetailer/ultralytics.py +50 -0
  31. adetailer/controlnet_ext/__init__.py +7 -0
  32. adetailer/controlnet_ext/controlnet_ext.py +140 -0
  33. adetailer/controlnet_ext/restore.py +43 -0
  34. adetailer/install.py +78 -0
  35. adetailer/preload.py +9 -0
  36. adetailer/pyproject.toml +26 -0
  37. adetailer/scripts/!adetailer.py +808 -0
  38. adetailer/sd_webui/__init__.py +0 -0
  39. adetailer/sd_webui/devices.py +11 -0
  40. adetailer/sd_webui/images.py +62 -0
  41. adetailer/sd_webui/paths.py +14 -0
  42. adetailer/sd_webui/processing.py +179 -0
  43. adetailer/sd_webui/safe.py +10 -0
  44. adetailer/sd_webui/script_callbacks.py +26 -0
  45. adetailer/sd_webui/scripts.py +94 -0
  46. adetailer/sd_webui/sd_samplers.py +17 -0
  47. adetailer/sd_webui/shared.py +66 -0
  48. artists-to-study/.github/FUNDING.yml +13 -0
  49. artists-to-study/LICENSE +24 -0
  50. artists-to-study/README.md +15 -0
Prompt_Generator/.github/ISSUE_TEMPLATE/bug_report.md ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Click on '....'
17
+ 3. Scroll down to '....'
18
+ 4. See error
19
+
20
+ **Expected behavior**
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ **Screenshots**
24
+ If applicable, add screenshots to help explain your problem.
25
+
26
+ **What fork of Webui are you using (Eg: Automatic1111, vladmandic):**
27
+
28
+
29
+ **Additional context**
30
+ Add any other context about the problem here.
Prompt_Generator/.github/ISSUE_TEMPLATE/custom.md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ name: Custom issue template
3
+ about: Describe this issue template's purpose here.
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+
Prompt_Generator/.github/ISSUE_TEMPLATE/feature_request.md ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
Prompt_Generator/.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ /style.css
Prompt_Generator/README.md ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Prompt Generator
2
+
3
+ Adds a tab to the webui that allows the user to generate a prompt from a small base prompt. Based on [FredZhang7/distilgpt2-stable-diffusion-v2](https://huggingface.co/FredZhang7/distilgpt2-stable-diffusion-v2) and [Gustavosta/MagicPrompt-Stable-Diffusion](https://huggingface.co/Gustavosta/MagicPrompt-Stable-Diffusion). I did nothing apart from porting it to [AUTOMATIC1111 WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
4
+
5
+
6
+
7
+ ![Screenshot 2023-04-29 000027](https://user-images.githubusercontent.com/8998556/235261664-2c92689d-9915-4543-8d6a-57a8ecd0f484.png)
8
+
9
+
10
+ ## Installation
11
+
12
+ 1. Install [AUTOMATIC1111's Stable Diffusion Webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
13
+ 2. Clone this repository into the `extensions` folder inside the webui
14
+
15
+ ## Usage
16
+
17
+ 1. Write in the prompt in the *Start of the prompt* text box
18
+ 2. Select which model you want to use
19
+ 3. Click Generate and wait
20
+
21
+ The initial use of the model may take longer as it needs to be downloaded to your machine for offline use. The model will be used on your device and will be stored in the default location of `*username*/.cache/huggingface/hub/models`. The entire process of generating results will be done on your local machine and not require internet access.
22
+
23
+ ## Parameters Explanation
24
+
25
+ - **Start of the prompt**: As the name suggests, the start of the prompt that the generator should start with
26
+ - **Temperature**: A higher temperature will produce more diverse results, but with a higher risk of less coherent text
27
+ - **Top K**: Strategy is to sample from a shortlist of the top K tokens. This approach allows the other high-scoring tokens a chance of being picked.
28
+ - **Max Length**: the maximum number of tokens for the output of the model
29
+ - **Repetition Penalty**: The parameter for repetition penalty. 1.0 means no penalty. See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details. Default setting is 1.2
30
+ - **How Many To Generate**: The number of results to generate
31
+ - **Use blacklist?**: Using `.\extensions\stable-diffusion-webui-Prompt_Generator\blacklist.txt`. It will delete any matches to the generated result (case insensitive). Each item to be filtered out should be on a new line. *Be aware that it simply deletes it and doesn't generate more to make up for the lost words*
32
+ - **Use punctuation**: Allows the use of commas in the output
33
+
34
+ ## Models
35
+
36
+ There are two 'default' models provided:
37
+
38
+ ### FredZhang7
39
+
40
+ Made by [FredZhang7](https://huggingface.co/FredZhang7) under creativeml-openrail-m license.
41
+
42
+ Useful to get styles for a prompt. Eg: "A cat sitting" -> "A cat sitting on a chair, digital art. The room is made of clay and metal with the sun shining through in front trending at Artstation 4k uhd..."
43
+
44
+ ### MagicPrompt
45
+
46
+ Made by [Gustavosta](https://huggingface.co/Gustavosta) under the MIT license.
47
+
48
+ Useful to get more natural language prompts. Eg: "A cat sitting" -> "A cat sitting in a chair, wearing pair of sunglasses"
49
+
50
+ *Be aware that sometimes the model fails to produce anything or less than the wanted amount, either try again or use a new prompt in that case*
51
+
52
+ ## Install more models
53
+
54
+ To install more model to use, ensure that the models are hosted on [huggingface.co](https://huggingface.co) and edit the json file at `.\extensions\stable-diffusion-webui-Prompt_Generator\models.json` with the relevant information. Use the models in the file as an example
55
+
56
+ You might need to restart the extension/reload the UI if new items are added onto the list
57
+
58
+ ## Credits
59
+
60
+ Credits to both [FredZhang7](https://huggingface.co/FredZhang7) and [Gustavosta](https://huggingface.co/Gustavosta)
Prompt_Generator/blacklist.txt ADDED
File without changes
Prompt_Generator/install.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ import launch
2
+ if not launch.is_installed("transformers"):
3
+ launch.run_pip("install --upgrade transformers", "Requirement of Prompt-Maker")
4
+ if not launch.is_installed("torch"):
5
+ launch.run_pip("install --upgrade torch", "Requirement of Prompt-Maker")
Prompt_Generator/javascript/prompt_generator_hints.js ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //Basically copied and adapted from AUTOMATIC1111 implementation of the main UI
2
+ // mouseover tooltips for various UI elements in the form of "UI element label"="Tooltip text".
3
+
4
+ prompt_generator_titles = {
5
+ "Temperature": "A higher temperature will produce more diverse results, but with a higher risk of less coherent text",
6
+ "Max Length": "The maximum number of tokens for the output of the model",
7
+ "Top K": "Strategy is to sample from a shortlist of the top K tokens. This approach allows the other high-scoring tokens a chance of being picked.",
8
+ "Repetition Penalty": "The parameter for repetition penalty. 1.0 means no penalty. Default setting is 1.2. Paper explaining it is linked to Github's readme",
9
+ "How Many To Generate":"The number of results to generate. Not guaranteed if models fails to create them",
10
+ "Generate Using Magic Prompt":"Be aware that sometimes the model fails to produce anything or less than the wanted amount, either try again or use a new prompt in that case"
11
+ }
12
+
13
+ onUiUpdate(function(){
14
+ gradioApp().querySelectorAll('span, button, select, p').forEach(function(span){
15
+ tooltip = prompt_generator_titles[span.textContent];
16
+
17
+ if(!tooltip){
18
+ tooltip = prompt_generator_titles[span.value];
19
+ }
20
+
21
+ if(!tooltip){
22
+ for (const c of span.classList) {
23
+ if (c in prompt_generator_titles) {
24
+ tooltip = prompt_generator_titles[c];
25
+ break;
26
+ }
27
+ }
28
+ }
29
+
30
+ if(tooltip){
31
+ span.title = tooltip;
32
+ }
33
+ })
34
+
35
+ gradioApp().querySelectorAll('select').forEach(function(select){
36
+ if (select.onchange != null) return;
37
+
38
+ select.onchange = function(){
39
+ select.title = prompt_generator_titles[select.value] || "";
40
+ }
41
+ })
42
+ })
Prompt_Generator/licence ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 imrayya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
Prompt_Generator/models.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "Title":"Gustavosta",
4
+ "Tokenizer":"gpt2",
5
+ "Model":"Gustavosta/MagicPrompt-Dalle"
6
+ },
7
+ {
8
+ "Title":"FredZhang7",
9
+ "Tokenizer":"distilgpt2",
10
+ "Model":"FredZhang7/distilgpt2-stable-diffusion-v2"
11
+ }
12
+ ]
Prompt_Generator/scripts/prompt_generator.py ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Copyright 2023 Imrayya
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9
+
10
+ """
11
+
12
+
13
+ import json
14
+ import re
15
+
16
+ import gradio as gr
17
+ import modules
18
+ from pathlib import Path
19
+ from modules import script_callbacks
20
+ import modules.scripts as scripts
21
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
22
+
23
+ result_prompt = ""
24
+ models = {}
25
+ max_no_results = 20 # TODO move to setting panel
26
+ base_dir = scripts.basedir()
27
+ model_file = Path(base_dir, "models.json")
28
+
29
+
30
+ class Model:
31
+ '''
32
+ Small strut to hold data for the text generator
33
+ '''
34
+
35
+ def __init__(self, name, model, tokenizer) -> None:
36
+ self.name = name
37
+ self.model = model
38
+ self.tokenizer = tokenizer
39
+ pass
40
+
41
+
42
+ def populate_models():
43
+ """Get the models that this extension can use via models.json
44
+ """
45
+ # TODO add button to refresh and update model list
46
+ path = model_file
47
+ with open(path, 'r') as f:
48
+ data = json.load(f)
49
+ for item in data:
50
+ name = item["Title"]
51
+ model = item["Model"]
52
+ tokenizer = item["Tokenizer"]
53
+ models[name] = Model(name, model, tokenizer)
54
+
55
+
56
+ def add_to_prompt(prompt): # A holder TODO figure out how to get rid of it
57
+ return prompt
58
+
59
+
60
+ def get_list_blacklist():
61
+ # Set the directory you want to start from
62
+ file_path = './extensions/stable-diffusion-webui-Prompt_Generator/blacklist.txt'
63
+ things_to_black_list = []
64
+ with open(file_path, 'r') as f:
65
+ # Read each line in the file and append it to the list
66
+ for line in f:
67
+ things_to_black_list.append(line.rstrip())
68
+
69
+ return things_to_black_list
70
+
71
+
72
+ def on_ui_tabs():
73
+ # Method to create the extended prompt
74
+ def generate_longer_generic(prompt, temperature, top_k,
75
+ max_length, repetition_penalty,
76
+ num_return_sequences, name, use_punctuation=False,
77
+ use_blacklist=False): # TODO make the progress bar work
78
+ """Generates a longer string from the input
79
+
80
+ Args:
81
+ prompt (str): As the name suggests, the start of the prompt that the generator should start with.
82
+
83
+ temperature (float): A higher temperature will produce more diverse results, but with a higher risk of less coherent text
84
+
85
+ top_k (float): Strategy is to sample from a shortlist of the top K tokens. This approach allows the other high-scoring tokens a chance of being picked.
86
+
87
+ max_length (int): the maximum number of tokens for the output of the model
88
+
89
+ repetition_penalty (float): The parameter for repetition penalty. 1.0 means no penalty. Default setting is 1.2
90
+
91
+ num_return_sequences (int): The number of results to generate
92
+
93
+ name (str): Which Model to use
94
+
95
+ use_punctuation (bool): Allows the use of commas in the output. Defaults to False.
96
+
97
+ use_blacklist (bool): It will delete any matches to the generated result (case insensitive). Each item to be filtered out should be on a new line. Defaults to False.
98
+
99
+ Returns:
100
+ Returns only an error otherwise saves it in result_prompt
101
+ """
102
+ try:
103
+ print("[Prompt_Generator]:","Loading Tokenizer")
104
+ tokenizer = GPT2Tokenizer.from_pretrained(models[name].tokenizer)
105
+ tokenizer.add_special_tokens({'pad_token': '[PAD]'})
106
+ print("[Prompt_Generator]:","Loading Model")
107
+ model = GPT2LMHeadModel.from_pretrained(models[name].model)
108
+ except Exception as e:
109
+ print("[Prompt_Generator]:",f"Exception encountered while attempting to install tokenizer")
110
+ return gr.update(), f"Error: {e}"
111
+ try:
112
+ print("[Prompt_Generator]:",f"Generate new prompt from: \"{prompt}\" with {name}")
113
+ input_ids = tokenizer(prompt, return_tensors='pt').input_ids
114
+ if (use_punctuation):
115
+ output = model.generate(input_ids, do_sample=True, temperature=temperature,
116
+ top_k=round(top_k), max_length=max_length,
117
+ num_return_sequences=num_return_sequences,
118
+ repetition_penalty=float(
119
+ repetition_penalty),
120
+ early_stopping=True)
121
+ else:
122
+ output = model.generate(input_ids, do_sample=True, temperature=temperature,
123
+ top_k=round(top_k), max_length=max_length,
124
+ num_return_sequences=num_return_sequences,
125
+ repetition_penalty=float(
126
+ repetition_penalty),
127
+ penalty_alpha=0.6, no_repeat_ngram_size=1,
128
+ early_stopping=True)
129
+ print("[Prompt_Generator]:","Generation complete!")
130
+ tempString = ""
131
+ if (use_blacklist):
132
+ blacklist = get_list_blacklist()
133
+ for i in range(len(output)):
134
+
135
+ tempString += tokenizer.decode(
136
+ output[i], skip_special_tokens=True) + "\n"
137
+
138
+ if (use_blacklist):
139
+ for to_check in blacklist:
140
+ tempString = re.sub(
141
+ to_check, "", tempString, flags=re.IGNORECASE)
142
+ if (i == 0):
143
+ global result_prompt
144
+
145
+ result_prompt = tempString
146
+ # print(result_prompt)
147
+ except Exception as e:
148
+ print("[Prompt_Generator]:",
149
+ f"Exception encountered while attempting to generate prompt: {e}")
150
+ return gr.update(), f"Error: {e}"
151
+
152
+ def ui_dynamic_result_visible(num):
153
+ """Makes the results visible"""
154
+ k = int(num)
155
+ return [gr.Row.update(visible=True)]*k + [gr.Row.update(visible=False)]*(max_no_results-k)
156
+
157
+ def ui_dynamic_result_prompts():
158
+ """Populates the results with the prompts"""
159
+
160
+ lines = result_prompt.splitlines()
161
+ num = len(lines)
162
+ result_list = []
163
+ for i in range(int(max_no_results)):
164
+ if (i < num):
165
+ result_list.append(lines[i])
166
+ else:
167
+ result_list.append("")
168
+ return result_list
169
+
170
+ def ui_dynamic_result_batch():
171
+ return result_prompt
172
+
173
+ def save_prompt_to_file(path, append: bool):
174
+ if len(result_prompt) == 0:
175
+ print("[Prompt_Generator]:","Prompt is empty")
176
+ return
177
+ with open(path, encoding="utf-8", mode="a" if append else "w") as f:
178
+ f.write(result_prompt)
179
+ print("[Prompt_Generator]:","Prompt written to: ", path)
180
+
181
+ # ----------------------------------------------------------------------------
182
+ # UI structure
183
+ txt2img_prompt = modules.ui.txt2img_paste_fields[0][0]
184
+ img2img_prompt = modules.ui.img2img_paste_fields[0][0]
185
+
186
+ with gr.Blocks(analytics_enabled=False) as prompt_generator:
187
+ # Handles UI for prompt creation
188
+ with gr.Column():
189
+ with gr.Row():
190
+ prompt_textbox = gr.Textbox(
191
+ lines=2, elem_id="promptTxt", label="Start of the prompt")
192
+ with gr.Column():
193
+ gr.HTML(
194
+ "Mouse over the labels to access tooltips that provide explanations for the parameters.")
195
+ with gr.Row():
196
+ temp_slider = gr.Slider(
197
+ elem_id="temp_slider", label="Temperature", interactive=True, minimum=0, maximum=1, value=0.9)
198
+ maxLength_slider = gr.Slider(
199
+ elem_id="max_length_slider", label="Max Length", interactive=True, minimum=1, maximum=200, step=1, value=90)
200
+ topK_slider = gr.Slider(
201
+ elem_id="top_k_slider", label="Top K", value=8, minimum=1, maximum=20, step=1, interactive=True)
202
+ with gr.Column():
203
+ with gr.Row():
204
+ repetitionPenalty_slider = gr.Slider(
205
+ elem_id="repetition_penalty_slider", label="Repetition Penalty", value=1.2, minimum=0.1, maximum=10, interactive=True)
206
+ numReturnSequences_slider = gr.Slider(
207
+ elem_id="num_return_sequences_slider", label="How Many To Generate", value=5, minimum=1, maximum=max_no_results, interactive=True, step=1)
208
+ with gr.Column():
209
+ with gr.Row():
210
+ useBlacklist_checkbox = gr.Checkbox(label="Use blacklist?")
211
+ gr.HTML(value="<center>Using <code>\".\extensions\stable-diffusion-webui-Prompt_Generator\\blacklist.txt</code>\".<br>It will delete any matches to the generated result (case insensitive).</center>")
212
+ with gr.Column():
213
+ with gr.Row():
214
+ populate_models()
215
+ generate_dropdown = gr.Dropdown(choices=list(models.keys()), value=list(models.keys())[
216
+ 1 if len(models) > 0 else 0], label="Which model to use?", show_label=True) # TODO Add default to setting page
217
+ use_punctuation_check = gr.Checkbox(label="Use punctuation?")
218
+ generate_button = gr.Button(
219
+ value="Generate", elem_id="generate_button") # TODO Add element to show that it is working in the background so users don't think nothing is happening
220
+
221
+ # Handles UI for results
222
+ results_vis = []
223
+ results_txt_list = []
224
+ with gr.Tab("Results"):
225
+ with gr.Column():
226
+ for i in range(max_no_results):
227
+ with gr.Row(visible=False) as row:
228
+ # Doesn't seem to do anything
229
+ row.style(equal_height=True)
230
+ with gr.Column(scale=3): # Guessing at the scale
231
+ textBox = gr.Textbox(label="", lines=3)
232
+ with gr.Column(scale=1):
233
+ txt2img = gr.Button("send to txt2img")
234
+ img2img = gr.Button("send to img2img")
235
+ # Handles ___2img buttons
236
+ txt2img.click(add_to_prompt, inputs=[
237
+ textBox], outputs=[txt2img_prompt]).then(None, _js='switch_to_txt2img',
238
+ inputs=None, outputs=None)
239
+ img2img.click(add_to_prompt, inputs=[
240
+ textBox], outputs=[img2img_prompt]).then(None, _js='switch_to_img2img',
241
+ inputs=None, outputs=None)
242
+ results_txt_list.append(textBox)
243
+ results_vis.append(row)
244
+ with gr.Tab("Batch"):
245
+ with gr.Column():
246
+ batch_texbox = gr.Textbox("", label="Results")
247
+ with gr.Row():
248
+ with gr.Column(scale=4):
249
+ savePathText = gr.Textbox(
250
+ Path(base_dir, "batch_prompt.txt"), label="Path", interactive=True)
251
+ with gr.Column(scale=1):
252
+ append_checkBox = gr.Checkbox(label="Append")
253
+ save_button = gr.Button("Save To file")
254
+
255
+ # ----------------------------------------------------------------------------------
256
+ # Handle buttons
257
+ save_button.click(fn=save_prompt_to_file, inputs=[
258
+ savePathText, append_checkBox])
259
+ # Please note that we use `.then()` to run other ui elements after the generation is done
260
+ generate_button.click(fn=generate_longer_generic, inputs=[
261
+ prompt_textbox, temp_slider, topK_slider, maxLength_slider,
262
+ repetitionPenalty_slider, numReturnSequences_slider,
263
+ generate_dropdown, use_punctuation_check, useBlacklist_checkbox]).then(
264
+ fn=ui_dynamic_result_visible, inputs=numReturnSequences_slider,
265
+ outputs=results_vis).then(
266
+ fn=ui_dynamic_result_prompts, outputs=results_txt_list).then(fn=ui_dynamic_result_batch, outputs=batch_texbox)
267
+ return (prompt_generator, "Prompt Generator", "Prompt Generator"),
268
+
269
+
270
+ script_callbacks.on_ui_tabs(on_ui_tabs)
adetailer/.github/ISSUE_TEMPLATE/bug_report.yaml ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Bug report
2
+ description: Create a report
3
+ title: "[Bug]: "
4
+ labels:
5
+ - bug
6
+
7
+ body:
8
+ - type: textarea
9
+ attributes:
10
+ label: Describe the bug
11
+ description: A clear and concise description of what the bug is.
12
+ placeholder: |
13
+ Any language accepted
14
+ 아무 언어 사용가능
15
+ すべての言語に対応
16
+ 接受所有语言
17
+ Se aceptan todos los idiomas
18
+ Alle Sprachen werden akzeptiert
19
+ Toutes les langues sont acceptées
20
+ Принимаются все языки
21
+
22
+ - type: textarea
23
+ attributes:
24
+ label: Screenshots
25
+ description: Screenshots related to the issue.
26
+
27
+ - type: textarea
28
+ attributes:
29
+ label: Console logs, from start to end.
30
+ description: |
31
+ The full console log of your terminal.
32
+ placeholder: |
33
+ Python ...
34
+ Version: ...
35
+ Commit hash: ...
36
+ Installing requirements
37
+ ...
38
+
39
+ Launching Web UI with arguments: ...
40
+ [-] ADetailer initialized. version: ...
41
+ ...
42
+ ...
43
+
44
+ Traceback (most recent call last):
45
+ ...
46
+ ...
47
+ render: Shell
48
+ validations:
49
+ required: true
50
+
51
+ - type: textarea
52
+ attributes:
53
+ label: List of installed extensions
adetailer/.github/ISSUE_TEMPLATE/feature_request.yaml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Feature request
2
+ description: Suggest an idea for this project
3
+ title: "[Feature Request]: "
4
+
5
+ body:
6
+ - type: textarea
7
+ attributes:
8
+ label: Is your feature request related to a problem? Please describe.
9
+ description: A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
10
+
11
+ - type: textarea
12
+ attributes:
13
+ label: Describe the solution you'd like
14
+ description: A clear and concise description of what you want to happen.
15
+
16
+ - type: textarea
17
+ attributes:
18
+ label: Describe alternatives you've considered
19
+ description: A clear and concise description of any alternative solutions or features you've considered.
20
+
21
+ - type: textarea
22
+ attributes:
23
+ label: Additional context
24
+ description: Add any other context or screenshots about the feature request here.
adetailer/.github/ISSUE_TEMPLATE/question.yaml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Question
2
+ description: Write a question
3
+ labels:
4
+ - question
5
+
6
+ body:
7
+ - type: textarea
8
+ attributes:
9
+ label: Question
10
+ description: Please do not write bug reports or feature requests here.
adetailer/.github/workflows/stale.yml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: 'Close stale issues and PRs'
2
+ on:
3
+ schedule:
4
+ - cron: '30 1 * * *'
5
+
6
+ jobs:
7
+ stale:
8
+ runs-on: ubuntu-latest
9
+ steps:
10
+ - uses: actions/stale@v8
11
+ with:
12
+ days-before-stale: 23
13
+ days-before-close: 3
adetailer/.gitignore ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Created by https://www.toptal.com/developers/gitignore/api/python,visualstudiocode
2
+ # Edit at https://www.toptal.com/developers/gitignore?templates=python,visualstudiocode
3
+
4
+ ### Python ###
5
+ # Byte-compiled / optimized / DLL files
6
+ __pycache__/
7
+ *.py[cod]
8
+ *$py.class
9
+
10
+ # C extensions
11
+ *.so
12
+
13
+ # Distribution / packaging
14
+ .Python
15
+ build/
16
+ develop-eggs/
17
+ dist/
18
+ downloads/
19
+ eggs/
20
+ .eggs/
21
+ lib/
22
+ lib64/
23
+ parts/
24
+ sdist/
25
+ var/
26
+ wheels/
27
+ share/python-wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+ MANIFEST
32
+
33
+ # PyInstaller
34
+ # Usually these files are written by a python script from a template
35
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
36
+ *.manifest
37
+ *.spec
38
+
39
+ # Installer logs
40
+ pip-log.txt
41
+ pip-delete-this-directory.txt
42
+
43
+ # Unit test / coverage reports
44
+ htmlcov/
45
+ .tox/
46
+ .nox/
47
+ .coverage
48
+ .coverage.*
49
+ .cache
50
+ nosetests.xml
51
+ coverage.xml
52
+ *.cover
53
+ *.py,cover
54
+ .hypothesis/
55
+ .pytest_cache/
56
+ cover/
57
+
58
+ # Translations
59
+ *.mo
60
+ *.pot
61
+
62
+ # Django stuff:
63
+ *.log
64
+ local_settings.py
65
+ db.sqlite3
66
+ db.sqlite3-journal
67
+
68
+ # Flask stuff:
69
+ instance/
70
+ .webassets-cache
71
+
72
+ # Scrapy stuff:
73
+ .scrapy
74
+
75
+ # Sphinx documentation
76
+ docs/_build/
77
+
78
+ # PyBuilder
79
+ .pybuilder/
80
+ target/
81
+
82
+ # Jupyter Notebook
83
+ .ipynb_checkpoints
84
+
85
+ # IPython
86
+ profile_default/
87
+ ipython_config.py
88
+
89
+ # pyenv
90
+ # For a library or package, you might want to ignore these files since the code is
91
+ # intended to run in multiple environments; otherwise, check them in:
92
+ # .python-version
93
+
94
+ # pipenv
95
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
97
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
98
+ # install all needed dependencies.
99
+ #Pipfile.lock
100
+
101
+ # poetry
102
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
103
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
104
+ # commonly ignored for libraries.
105
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
106
+ #poetry.lock
107
+
108
+ # pdm
109
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
110
+ #pdm.lock
111
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
112
+ # in version control.
113
+ # https://pdm.fming.dev/#use-with-ide
114
+ .pdm.toml
115
+
116
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
117
+ __pypackages__/
118
+
119
+ # Celery stuff
120
+ celerybeat-schedule
121
+ celerybeat.pid
122
+
123
+ # SageMath parsed files
124
+ *.sage.py
125
+
126
+ # Environments
127
+ .env
128
+ .venv
129
+ env/
130
+ venv/
131
+ ENV/
132
+ env.bak/
133
+ venv.bak/
134
+
135
+ # Spyder project settings
136
+ .spyderproject
137
+ .spyproject
138
+
139
+ # Rope project settings
140
+ .ropeproject
141
+
142
+ # mkdocs documentation
143
+ /site
144
+
145
+ # mypy
146
+ .mypy_cache/
147
+ .dmypy.json
148
+ dmypy.json
149
+
150
+ # Pyre type checker
151
+ .pyre/
152
+
153
+ # pytype static type analyzer
154
+ .pytype/
155
+
156
+ # Cython debug symbols
157
+ cython_debug/
158
+
159
+ # PyCharm
160
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
161
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
162
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
163
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
164
+ #.idea/
165
+
166
+ ### Python Patch ###
167
+ # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
168
+ poetry.toml
169
+
170
+ # ruff
171
+ .ruff_cache/
172
+
173
+ # LSP config files
174
+ pyrightconfig.json
175
+
176
+ ### VisualStudioCode ###
177
+ .vscode/*
178
+ !.vscode/settings.json
179
+ !.vscode/tasks.json
180
+ !.vscode/launch.json
181
+ !.vscode/extensions.json
182
+ !.vscode/*.code-snippets
183
+
184
+ # Local History for Visual Studio Code
185
+ .history/
186
+
187
+ # Built Visual Studio Code Extensions
188
+ *.vsix
189
+
190
+ ### VisualStudioCode Patch ###
191
+ # Ignore all local history of files
192
+ .history
193
+ .ionide
194
+
195
+ # End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode
196
+ *.ipynb
adetailer/.pre-commit-config.yaml ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.4.0
4
+ hooks:
5
+ - id: trailing-whitespace
6
+ args: [--markdown-linebreak-ext=md]
7
+ - id: end-of-file-fixer
8
+ - id: mixed-line-ending
9
+
10
+ - repo: https://github.com/astral-sh/ruff-pre-commit
11
+ rev: "v0.0.280"
12
+ hooks:
13
+ - id: ruff
14
+ args: [--fix, --exit-non-zero-on-fix]
15
+
16
+ - repo: https://github.com/psf/black
17
+ rev: 23.7.0
18
+ hooks:
19
+ - id: black
adetailer/CHANGELOG.md ADDED
@@ -0,0 +1,275 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Changelog
2
+
3
+ ## 2023-07-31
4
+
5
+ - v23.7.11
6
+ - separate clip skip 옵션 추가
7
+ - install requirements 정리 (ultralytics 새 버전, mediapipe~=3.20)
8
+
9
+ ## 2023-07-28
10
+
11
+ - v23.7.10
12
+ - ultralytics, mediapipe import문 정리
13
+ - traceback에서 컬러를 없앰 (api 때문), 라이브러리 버전도 보여주게 설정.
14
+ - huggingface_hub, pydantic을 install.py에서 없앰
15
+ - 안쓰는 컨트롤넷 관련 코드 삭제
16
+
17
+
18
+ ## 2023-07-23
19
+
20
+ - v23.7.9
21
+ - `ultralytics.utils` ModuleNotFoundError 해결 (https://github.com/ultralytics/ultralytics/issues/3856)
22
+ - `pydantic` 2.0 이상 버전 설치안되도록 함
23
+ - `controlnet_dir` cmd args 문제 수정 (PR #107)
24
+
25
+ ## 2023-07-20
26
+
27
+ - v23.7.8
28
+ - `paste_field_names` 추가했던 것을 되돌림
29
+
30
+ ## 2023-07-19
31
+
32
+ - v23.7.7
33
+ - 인페인팅 단계에서 별도의 샘플러를 선택할 수 있게 옵션을 추가함 (xyz그리드에도 추가)
34
+ - webui 1.0.0-pre 이하 버전에서 batch index 문제 수정
35
+ - 스크립트에 `paste_field_names`을 추가함. 사용되는지는 모르겠음
36
+
37
+ ## 2023-07-16
38
+
39
+ - v23.7.6
40
+ - `ultralytics 8.0.135`에 추가된 cpuinfo 기능을 위해 `py-cpuinfo`를 미리 설치하게 함. (미리 설치 안하면 cpu나 mps사용할 때 재시작해야함)
41
+ - init_image가 RGB 모드가 아닐 때 RGB로 변경.
42
+
43
+ ## 2023-07-07
44
+
45
+ - v23.7.4
46
+ - batch count > 1일때 프롬프트의 인덱스 문제 수정
47
+
48
+ - v23.7.5
49
+ - i2i의 `cached_uc`와 `cached_c`가 p의 `cached_uc`와 `cached_c`가 다른 인스턴스가 되도록 수정
50
+
51
+ ## 2023-07-05
52
+
53
+ - v23.7.3
54
+ - 버그 수정
55
+ - `object()`가 json 직렬화 안되는 문제
56
+ - `process`를 호출함에 따라 배치 카운트가 2이상일 때, all_prompts가 고정되는 문제
57
+ - `ad-before`와 `ad-preview` 이미지 파일명이 실제 파일명과 다른 문제
58
+ - pydantic 2.0 호환성 문제
59
+
60
+ ## 2023-07-04
61
+
62
+ - v23.7.2
63
+ - `mediapipe_face_mesh_eyes_only` 모델 추가: `mediapipe_face_mesh`로 감지한 뒤 눈만 사용함.
64
+ - 매 배치 시작 전에 `scripts.postprocess`를, 후에 `scripts.process`를 호출함.
65
+ - 컨트롤넷을 사용하면 소요 시간이 조금 늘어나지만 몇몇 문제 해결에 도움이 됨.
66
+ - `lora_block_weight`를 스크립트 화이트리스트에 추가함.
67
+ - 한번이라도 ADetailer를 사용한 사람은 수동으로 추가해야함.
68
+
69
+ ## 2023-07-03
70
+
71
+ - v23.7.1
72
+ - `process_images`를 진행한 뒤 `StableDiffusionProcessing` 오브젝트의 close를 호출함
73
+ - api 호출로 사용했는지 확인하는 속성 추가
74
+ - `NansException`이 발생했을 때 중지하지 않고 남은 과정 계속 진행함
75
+
76
+ ## 2023-07-02
77
+
78
+ - v23.7.0
79
+ - `NansException`이 발생하면 로그에 표시하고 원본 이미지를 반환하게 설정
80
+ - `rich`를 사용한 에러 트레이싱
81
+ - install.py에 `rich` 추가
82
+ - 생성 중에 컴포넌트의 값을 변경하면 args의 값도 함께 변경되는 문제 수정 (issue #180)
83
+ - 터미널 로그로 ad_prompt와 ad_negative_prompt에 적용된 실제 프롬프트 확인할 수 있음 (입력과 다를 경우에만)
84
+
85
+ ## 2023-06-28
86
+
87
+ - v23.6.4
88
+ - 최대 모델 수 5 -> 10개
89
+ - ad_prompt와 ad_negative_prompt에 빈칸으로 놔두면 입력 프롬프트가 사용된다는 문구 추가
90
+ - huggingface 모델 다운로드 실패시 로깅
91
+ - 1st 모델이 `None`일 경우 나머지 입력을 무시하던 문제 수정
92
+ - `--use-cpu` 에 `adetailer` 입력 시 cpu로 yolo모델을 사용함
93
+
94
+ ## 2023-06-20
95
+
96
+ - v23.6.3
97
+ - 컨트롤넷 inpaint 모델에 대해, 3가지 모듈을 사용할 수 있도록 함
98
+ - Noise Multiplier 옵션 추가 (PR #149)
99
+ - pydantic 최소 버전 1.10.8로 설정 (Issue #146)
100
+
101
+ ## 2023-06-05
102
+
103
+ - v23.6.2
104
+ - xyz_grid에서 ADetailer를 사용할 수 있게함.
105
+ - 8가지 옵션만 1st 탭에 적용되도록 함.
106
+
107
+ ## 2023-06-01
108
+
109
+ - v23.6.1
110
+ - `inpaint, scribble, lineart, openpose, tile` 5가지 컨트롤넷 모델 지원 (PR #107)
111
+ - controlnet guidance start, end 인자 추가 (PR #107)
112
+ - `modules.extensions`를 사용하여 컨트롤넷 확장을 불러오고 경로를 알아내로록 변경
113
+ - ui에서 컨트롤넷을 별도 함수로 분리
114
+
115
+ ## 2023-05-30
116
+
117
+ - v23.6.0
118
+ - 스크립트의 이름을 `After Detailer`에서 `ADetailer`로 변경
119
+ - API 사용자는 변경 필요함
120
+ - 몇몇 설정 변경
121
+ - `ad_conf` → `ad_confidence`. 0~100 사이의 int → 0.0~1.0 사이의 float
122
+ - `ad_inpaint_full_res` → `ad_inpaint_only_masked`
123
+ - `ad_inpaint_full_res_padding` → `ad_inpaint_only_masked_padding`
124
+ - mediapipe face mesh 모델 추가
125
+ - mediapipe 최소 버전 `0.10.0`
126
+
127
+ - rich traceback 제거함
128
+ - huggingface 다운로드 실패할 때 에러가 나지 않게 하고 해당 모델을 제거함
129
+
130
+ ## 2023-05-26
131
+
132
+ - v23.5.19
133
+ - 1번째 탭에도 `None` 옵션을 추가함
134
+ - api로 ad controlnet model에 inpaint가 아닌 다른 컨트롤넷 모델을 사용하지 못하도록 막음
135
+ - adetailer 진행중에 total tqdm 진행바 업데이트를 멈춤
136
+ - state.inturrupted 상태에서 adetailer 과정을 중지��
137
+ - 컨트롤넷 process를 각 batch가 끝난 순간에만 호출하도록 변경
138
+
139
+ ### 2023-05-25
140
+
141
+ - v23.5.18
142
+ - 컨트롤넷 관련 수정
143
+ - unit의 `input_mode`를 `SIMPLE`로 모두 변경
144
+ - 컨트롤넷 유넷 훅과 하이잭 함수들을 adetailer를 실행할 때에만 되돌리는 기능 추가
145
+ - adetailer 처리가 끝난 뒤 컨트롤넷 스크립트의 process를 다시 진행함. (batch count 2 이상일때의 문제 해결)
146
+ - 기본 활성 스크립트 목록에서 컨트롤넷을 뺌
147
+
148
+ ### 2023-05-22
149
+
150
+ - v23.5.17
151
+ - 컨트롤넷 확장이 있으면 컨트롤넷 스크립트를 활성화함. (컨트롤넷 관련 문제 해결)
152
+ - 모든 컴포넌트에 elem_id 설정
153
+ - ui에 버전을 표시함
154
+
155
+
156
+ ### 2023-05-19
157
+
158
+ - v23.5.16
159
+ - 추가한 옵션
160
+ - Mask min/max ratio
161
+ - Mask merge mode
162
+ - Restore faces after ADetailer
163
+ - 옵션들을 Accordion으로 묶음
164
+
165
+ ### 2023-05-18
166
+
167
+ - v23.5.15
168
+ - 필요한 것만 임포트하도록 변경 (vae 로딩 오류 없어짐. 로딩 속도 빨라짐)
169
+
170
+ ### 2023-05-17
171
+
172
+ - v23.5.14
173
+ - `[SKIP]`으로 ad prompt 일부를 건너뛰는 기능 추가
174
+ - bbox 정렬 옵션 추가
175
+ - sd_webui 타입힌트를 만들어냄
176
+ - enable checker와 관련된 api 오류 수정?
177
+
178
+ ### 2023-05-15
179
+
180
+ - v23.5.13
181
+ - `[SEP]`으로 ad prompt를 분리하여 적용하는 기능 추가
182
+ - enable checker를 다시 pydantic으로 변경함
183
+ - ui 관련 함수를 adetailer.ui 폴더로 분리함
184
+ - controlnet을 사용할 때 모든 controlnet unit 비활성화
185
+ - adetailer 폴더가 없으면 만들게 함
186
+
187
+ ### 2023-05-13
188
+
189
+ - v23.5.12
190
+ - `ad_enable`을 제외한 입력이 dict타입으로 들어오도록 변경
191
+ - web api로 사용할 때에 특히 사용하기 쉬움
192
+ - web api breaking change
193
+ - `mask_preprocess` 인자를 넣지 않았던 오류 수정 (PR #47)
194
+ - huggingface에서 모델을 다운로드하지 않는 옵션 추가 `--ad-no-huggingface`
195
+
196
+ ### 2023-05-12
197
+
198
+ - v23.5.11
199
+ - `ultralytics` 알람 제거
200
+ - 필요없는 exif 인자 더 제거함
201
+ - `use separate steps` 옵션 추가
202
+ - ui 배치를 조정함
203
+
204
+ ### 2023-05-09
205
+
206
+ - v23.5.10
207
+ - 선택한 스크립트만 ADetailer에 적용하는 옵션 추가, 기본값 `True`. 설정 탭에서 지정가능.
208
+ - 기본값: `dynamic_prompting,dynamic_thresholding,wildcards,wildcard_recursive`
209
+ - `person_yolov8s-seg.pt` 모델 추가
210
+ - `ultralytics`의 최소 버전을 `8.0.97`로 설정 (C:\\ 문제 해결된 버전)
211
+
212
+ ### 2023-05-08
213
+
214
+ - v23.5.9
215
+ - 2가지 이상의 모델을 사용할 수 있음. 기본값: 2, 최대: 5
216
+ - segment 모델을 사용할 수 있게 함. `person_yolov8n-seg.pt` 추가
217
+
218
+ ### 2023-05-07
219
+
220
+ - v23.5.8
221
+ - 프롬프트와 네거티브 프롬프트에 방향키 지원 (PR #24)
222
+ - `mask_preprocess`를 추가함. 이전 버전과 시드값이 달라질 가능성 있음!
223
+ - 이미지 처리가 일어났을 때에만 before이미지를 저장함
224
+ - 설정창의 레이블을 ADetailer 대신 더 적절하게 수정함
225
+
226
+ ### 2023-05-06
227
+
228
+ - v23.5.7
229
+ - `ad_use_cfg_scale` 옵션 추가. cfg 스케일을 따로 사용할지 말지 결정함.
230
+ - `ad_enable` 기본값을 `True`에서 `False`로 변경
231
+ - `ad_model`의 기본값을 `None`에서 첫번째 모델로 변경
232
+ - 최소 2개의 입력(ad_enable, ad_model)만 들어오면 작동하게 변경.
233
+
234
+ - v23.5.7.post0
235
+ - `init_controlnet_ext`을 controlnet_exists == True일때에만 실행
236
+ - webui를 C드라이브 바로 밑에 설치한 사람들에게 `ultralytics` 경고 표시
237
+
238
+ ### 2023-05-05 (어린이날)
239
+
240
+ - v23.5.5
241
+ - `Save images before ADetailer` 옵션 추가
242
+ - 입력으로 들어온 인자와 ALL_ARGS의 길이가 다르면 에러메세지
243
+ - README.md에 설치방법 추가
244
+
245
+ - v23.5.6
246
+ - get_args에서 IndexError가 발생하면 자세한 에러메세지를 볼 수 있음
247
+ - AdetailerArgs에 extra_params 내장
248
+ - scripts_args를 딥카피함
249
+ - postprocess_image를 약간 분리함
250
+
251
+ - v23.5.6.post0
252
+ - `init_controlnet_ext`에서 에러메세지를 자세히 볼 수 있음
253
+
254
+ ### 2023-05-04
255
+
256
+ - v23.5.4
257
+ - use pydantic for arguments validation
258
+ - revert: ad_model to `None` as default
259
+ - revert: `__future__` imports
260
+ - lazily import yolo and mediapipe
261
+
262
+ ### 2023-05-03
263
+
264
+ - v23.5.3.post0
265
+ - remove `__future__` imports
266
+ - change to copy scripts and scripts args
267
+
268
+ - v23.5.3.post1
269
+ - change default ad_model from `None`
270
+
271
+ ### 2023-05-02
272
+
273
+ - v23.5.3
274
+ - Remove `None` from model list and add `Enable ADetailer` checkbox.
275
+ - install.py `skip_install` fix.
adetailer/LICENSE.md ADDED
@@ -0,0 +1,662 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ GNU AFFERO GENERAL PUBLIC LICENSE
3
+ Version 3, 19 November 2007
4
+
5
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The GNU Affero General Public License is a free, copyleft license for
12
+ software and other kinds of works, specifically designed to ensure
13
+ cooperation with the community in the case of network server software.
14
+
15
+ The licenses for most software and other practical works are designed
16
+ to take away your freedom to share and change the works. By contrast,
17
+ our General Public Licenses are intended to guarantee your freedom to
18
+ share and change all versions of a program--to make sure it remains free
19
+ software for all its users.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ them if you wish), that you receive source code or can get it if you
25
+ want it, that you can change the software or use pieces of it in new
26
+ free programs, and that you know you can do these things.
27
+
28
+ Developers that use our General Public Licenses protect your rights
29
+ with two steps: (1) assert copyright on the software, and (2) offer
30
+ you this License which gives you legal permission to copy, distribute
31
+ and/or modify the software.
32
+
33
+ A secondary benefit of defending all users' freedom is that
34
+ improvements made in alternate versions of the program, if they
35
+ receive widespread use, become available for other developers to
36
+ incorporate. Many developers of free software are heartened and
37
+ encouraged by the resulting cooperation. However, in the case of
38
+ software used on network servers, this result may fail to come about.
39
+ The GNU General Public License permits making a modified version and
40
+ letting the public access it on a server without ever releasing its
41
+ source code to the public.
42
+
43
+ The GNU Affero General Public License is designed specifically to
44
+ ensure that, in such cases, the modified source code becomes available
45
+ to the community. It requires the operator of a network server to
46
+ provide the source code of the modified version running there to the
47
+ users of that server. Therefore, public use of a modified version, on
48
+ a publicly accessible server, gives the public access to the source
49
+ code of the modified version.
50
+
51
+ An older license, called the Affero General Public License and
52
+ published by Affero, was designed to accomplish similar goals. This is
53
+ a different license, not a version of the Affero GPL, but Affero has
54
+ released a new version of the Affero GPL which permits relicensing under
55
+ this license.
56
+
57
+ The precise terms and conditions for copying, distribution and
58
+ modification follow.
59
+
60
+ TERMS AND CONDITIONS
61
+
62
+ 0. Definitions.
63
+
64
+ "This License" refers to version 3 of the GNU Affero General Public License.
65
+
66
+ "Copyright" also means copyright-like laws that apply to other kinds of
67
+ works, such as semiconductor masks.
68
+
69
+ "The Program" refers to any copyrightable work licensed under this
70
+ License. Each licensee is addressed as "you". "Licensees" and
71
+ "recipients" may be individuals or organizations.
72
+
73
+ To "modify" a work means to copy from or adapt all or part of the work
74
+ in a fashion requiring copyright permission, other than the making of an
75
+ exact copy. The resulting work is called a "modified version" of the
76
+ earlier work or a work "based on" the earlier work.
77
+
78
+ A "covered work" means either the unmodified Program or a work based
79
+ on the Program.
80
+
81
+ To "propagate" a work means to do anything with it that, without
82
+ permission, would make you directly or secondarily liable for
83
+ infringement under applicable copyright law, except executing it on a
84
+ computer or modifying a private copy. Propagation includes copying,
85
+ distribution (with or without modification), making available to the
86
+ public, and in some countries other activities as well.
87
+
88
+ To "convey" a work means any kind of propagation that enables other
89
+ parties to make or receive copies. Mere interaction with a user through
90
+ a computer network, with no transfer of a copy, is not conveying.
91
+
92
+ An interactive user interface displays "Appropriate Legal Notices"
93
+ to the extent that it includes a convenient and prominently visible
94
+ feature that (1) displays an appropriate copyright notice, and (2)
95
+ tells the user that there is no warranty for the work (except to the
96
+ extent that warranties are provided), that licensees may convey the
97
+ work under this License, and how to view a copy of this License. If
98
+ the interface presents a list of user commands or options, such as a
99
+ menu, a prominent item in the list meets this criterion.
100
+
101
+ 1. Source Code.
102
+
103
+ The "source code" for a work means the preferred form of the work
104
+ for making modifications to it. "Object code" means any non-source
105
+ form of a work.
106
+
107
+ A "Standard Interface" means an interface that either is an official
108
+ standard defined by a recognized standards body, or, in the case of
109
+ interfaces specified for a particular programming language, one that
110
+ is widely used among developers working in that language.
111
+
112
+ The "System Libraries" of an executable work include anything, other
113
+ than the work as a whole, that (a) is included in the normal form of
114
+ packaging a Major Component, but which is not part of that Major
115
+ Component, and (b) serves only to enable use of the work with that
116
+ Major Component, or to implement a Standard Interface for which an
117
+ implementation is available to the public in source code form. A
118
+ "Major Component", in this context, means a major essential component
119
+ (kernel, window system, and so on) of the specific operating system
120
+ (if any) on which the executable work runs, or a compiler used to
121
+ produce the work, or an object code interpreter used to run it.
122
+
123
+ The "Corresponding Source" for a work in object code form means all
124
+ the source code needed to generate, install, and (for an executable
125
+ work) run the object code and to modify the work, including scripts to
126
+ control those activities. However, it does not include the work's
127
+ System Libraries, or general-purpose tools or generally available free
128
+ programs which are used unmodified in performing those activities but
129
+ which are not part of the work. For example, Corresponding Source
130
+ includes interface definition files associated with source files for
131
+ the work, and the source code for shared libraries and dynamically
132
+ linked subprograms that the work is specifically designed to require,
133
+ such as by intimate data communication or control flow between those
134
+ subprograms and other parts of the work.
135
+
136
+ The Corresponding Source need not include anything that users
137
+ can regenerate automatically from other parts of the Corresponding
138
+ Source.
139
+
140
+ The Corresponding Source for a work in source code form is that
141
+ same work.
142
+
143
+ 2. Basic Permissions.
144
+
145
+ All rights granted under this License are granted for the term of
146
+ copyright on the Program, and are irrevocable provided the stated
147
+ conditions are met. This License explicitly affirms your unlimited
148
+ permission to run the unmodified Program. The output from running a
149
+ covered work is covered by this License only if the output, given its
150
+ content, constitutes a covered work. This License acknowledges your
151
+ rights of fair use or other equivalent, as provided by copyright law.
152
+
153
+ You may make, run and propagate covered works that you do not
154
+ convey, without conditions so long as your license otherwise remains
155
+ in force. You may convey covered works to others for the sole purpose
156
+ of having them make modifications exclusively for you, or provide you
157
+ with facilities for running those works, provided that you comply with
158
+ the terms of this License in conveying all material for which you do
159
+ not control copyright. Those thus making or running the covered works
160
+ for you must do so exclusively on your behalf, under your direction
161
+ and control, on terms that prohibit them from making any copies of
162
+ your copyrighted material outside their relationship with you.
163
+
164
+ Conveying under any other circumstances is permitted solely under
165
+ the conditions stated below. Sublicensing is not allowed; section 10
166
+ makes it unnecessary.
167
+
168
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
169
+
170
+ No covered work shall be deemed part of an effective technological
171
+ measure under any applicable law fulfilling obligations under article
172
+ 11 of the WIPO copyright treaty adopted on 20 December 1996, or
173
+ similar laws prohibiting or restricting circumvention of such
174
+ measures.
175
+
176
+ When you convey a covered work, you waive any legal power to forbid
177
+ circumvention of technological measures to the extent such circumvention
178
+ is effected by exercising rights under this License with respect to
179
+ the covered work, and you disclaim any intention to limit operation or
180
+ modification of the work as a means of enforcing, against the work's
181
+ users, your or third parties' legal rights to forbid circumvention of
182
+ technological measures.
183
+
184
+ 4. Conveying Verbatim Copies.
185
+
186
+ You may convey verbatim copies of the Program's source code as you
187
+ receive it, in any medium, provided that you conspicuously and
188
+ appropriately publish on each copy an appropriate copyright notice;
189
+ keep intact all notices stating that this License and any
190
+ non-permissive terms added in accord with section 7 apply to the code;
191
+ keep intact all notices of the absence of any warranty; and give all
192
+ recipients a copy of this License along with the Program.
193
+
194
+ You may charge any price or no price for each copy that you convey,
195
+ and you may offer support or warranty protection for a fee.
196
+
197
+ 5. Conveying Modified Source Versions.
198
+
199
+ You may convey a work based on the Program, or the modifications to
200
+ produce it from the Program, in the form of source code under the
201
+ terms of section 4, provided that you also meet all of these conditions:
202
+
203
+ a) The work must carry prominent notices stating that you modified
204
+ it, and giving a relevant date.
205
+
206
+ b) The work must carry prominent notices stating that it is
207
+ released under this License and any conditions added under section
208
+ 7. This requirement modifies the requirement in section 4 to
209
+ "keep intact all notices".
210
+
211
+ c) You must license the entire work, as a whole, under this
212
+ License to anyone who comes into possession of a copy. This
213
+ License will therefore apply, along with any applicable section 7
214
+ additional terms, to the whole of the work, and all its parts,
215
+ regardless of how they are packaged. This License gives no
216
+ permission to license the work in any other way, but it does not
217
+ invalidate such permission if you have separately received it.
218
+
219
+ d) If the work has interactive user interfaces, each must display
220
+ Appropriate Legal Notices; however, if the Program has interactive
221
+ interfaces that do not display Appropriate Legal Notices, your
222
+ work need not make them do so.
223
+
224
+ A compilation of a covered work with other separate and independent
225
+ works, which are not by their nature extensions of the covered work,
226
+ and which are not combined with it such as to form a larger program,
227
+ in or on a volume of a storage or distribution medium, is called an
228
+ "aggregate" if the compilation and its resulting copyright are not
229
+ used to limit the access or legal rights of the compilation's users
230
+ beyond what the individual works permit. Inclusion of a covered work
231
+ in an aggregate does not cause this License to apply to the other
232
+ parts of the aggregate.
233
+
234
+ 6. Conveying Non-Source Forms.
235
+
236
+ You may convey a covered work in object code form under the terms
237
+ of sections 4 and 5, provided that you also convey the
238
+ machine-readable Corresponding Source under the terms of this License,
239
+ in one of these ways:
240
+
241
+ a) Convey the object code in, or embodied in, a physical product
242
+ (including a physical distribution medium), accompanied by the
243
+ Corresponding Source fixed on a durable physical medium
244
+ customarily used for software interchange.
245
+
246
+ b) Convey the object code in, or embodied in, a physical product
247
+ (including a physical distribution medium), accompanied by a
248
+ written offer, valid for at least three years and valid for as
249
+ long as you offer spare parts or customer support for that product
250
+ model, to give anyone who possesses the object code either (1) a
251
+ copy of the Corresponding Source for all the software in the
252
+ product that is covered by this License, on a durable physical
253
+ medium customarily used for software interchange, for a price no
254
+ more than your reasonable cost of physically performing this
255
+ conveying of source, or (2) access to copy the
256
+ Corresponding Source from a network server at no charge.
257
+
258
+ c) Convey individual copies of the object code with a copy of the
259
+ written offer to provide the Corresponding Source. This
260
+ alternative is allowed only occasionally and noncommercially, and
261
+ only if you received the object code with such an offer, in accord
262
+ with subsection 6b.
263
+
264
+ d) Convey the object code by offering access from a designated
265
+ place (gratis or for a charge), and offer equivalent access to the
266
+ Corresponding Source in the same way through the same place at no
267
+ further charge. You need not require recipients to copy the
268
+ Corresponding Source along with the object code. If the place to
269
+ copy the object code is a network server, the Corresponding Source
270
+ may be on a different server (operated by you or a third party)
271
+ that supports equivalent copying facilities, provided you maintain
272
+ clear directions next to the object code saying where to find the
273
+ Corresponding Source. Regardless of what server hosts the
274
+ Corresponding Source, you remain obligated to ensure that it is
275
+ available for as long as needed to satisfy these requirements.
276
+
277
+ e) Convey the object code using peer-to-peer transmission, provided
278
+ you inform other peers where the object code and Corresponding
279
+ Source of the work are being offered to the general public at no
280
+ charge under subsection 6d.
281
+
282
+ A separable portion of the object code, whose source code is excluded
283
+ from the Corresponding Source as a System Library, need not be
284
+ included in conveying the object code work.
285
+
286
+ A "User Product" is either (1) a "consumer product", which means any
287
+ tangible personal property which is normally used for personal, family,
288
+ or household purposes, or (2) anything designed or sold for incorporation
289
+ into a dwelling. In determining whether a product is a consumer product,
290
+ doubtful cases shall be resolved in favor of coverage. For a particular
291
+ product received by a particular user, "normally used" refers to a
292
+ typical or common use of that class of product, regardless of the status
293
+ of the particular user or of the way in which the particular user
294
+ actually uses, or expects or is expected to use, the product. A product
295
+ is a consumer product regardless of whether the product has substantial
296
+ commercial, industrial or non-consumer uses, unless such uses represent
297
+ the only significant mode of use of the product.
298
+
299
+ "Installation Information" for a User Product means any methods,
300
+ procedures, authorization keys, or other information required to install
301
+ and execute modified versions of a covered work in that User Product from
302
+ a modified version of its Corresponding Source. The information must
303
+ suffice to ensure that the continued functioning of the modified object
304
+ code is in no case prevented or interfered with solely because
305
+ modification has been made.
306
+
307
+ If you convey an object code work under this section in, or with, or
308
+ specifically for use in, a User Product, and the conveying occurs as
309
+ part of a transaction in which the right of possession and use of the
310
+ User Product is transferred to the recipient in perpetuity or for a
311
+ fixed term (regardless of how the transaction is characterized), the
312
+ Corresponding Source conveyed under this section must be accompanied
313
+ by the Installation Information. But this requirement does not apply
314
+ if neither you nor any third party retains the ability to install
315
+ modified object code on the User Product (for example, the work has
316
+ been installed in ROM).
317
+
318
+ The requirement to provide Installation Information does not include a
319
+ requirement to continue to provide support service, warranty, or updates
320
+ for a work that has been modified or installed by the recipient, or for
321
+ the User Product in which it has been modified or installed. Access to a
322
+ network may be denied when the modification itself materially and
323
+ adversely affects the operation of the network or violates the rules and
324
+ protocols for communication across the network.
325
+
326
+ Corresponding Source conveyed, and Installation Information provided,
327
+ in accord with this section must be in a format that is publicly
328
+ documented (and with an implementation available to the public in
329
+ source code form), and must require no special password or key for
330
+ unpacking, reading or copying.
331
+
332
+ 7. Additional Terms.
333
+
334
+ "Additional permissions" are terms that supplement the terms of this
335
+ License by making exceptions from one or more of its conditions.
336
+ Additional permissions that are applicable to the entire Program shall
337
+ be treated as though they were included in this License, to the extent
338
+ that they are valid under applicable law. If additional permissions
339
+ apply only to part of the Program, that part may be used separately
340
+ under those permissions, but the entire Program remains governed by
341
+ this License without regard to the additional permissions.
342
+
343
+ When you convey a copy of a covered work, you may at your option
344
+ remove any additional permissions from that copy, or from any part of
345
+ it. (Additional permissions may be written to require their own
346
+ removal in certain cases when you modify the work.) You may place
347
+ additional permissions on material, added by you to a covered work,
348
+ for which you have or can give appropriate copyright permission.
349
+
350
+ Notwithstanding any other provision of this License, for material you
351
+ add to a covered work, you may (if authorized by the copyright holders of
352
+ that material) supplement the terms of this License with terms:
353
+
354
+ a) Disclaiming warranty or limiting liability differently from the
355
+ terms of sections 15 and 16 of this License; or
356
+
357
+ b) Requiring preservation of specified reasonable legal notices or
358
+ author attributions in that material or in the Appropriate Legal
359
+ Notices displayed by works containing it; or
360
+
361
+ c) Prohibiting misrepresentation of the origin of that material, or
362
+ requiring that modified versions of such material be marked in
363
+ reasonable ways as different from the original version; or
364
+
365
+ d) Limiting the use for publicity purposes of names of licensors or
366
+ authors of the material; or
367
+
368
+ e) Declining to grant rights under trademark law for use of some
369
+ trade names, trademarks, or service marks; or
370
+
371
+ f) Requiring indemnification of licensors and authors of that
372
+ material by anyone who conveys the material (or modified versions of
373
+ it) with contractual assumptions of liability to the recipient, for
374
+ any liability that these contractual assumptions directly impose on
375
+ those licensors and authors.
376
+
377
+ All other non-permissive additional terms are considered "further
378
+ restrictions" within the meaning of section 10. If the Program as you
379
+ received it, or any part of it, contains a notice stating that it is
380
+ governed by this License along with a term that is a further
381
+ restriction, you may remove that term. If a license document contains
382
+ a further restriction but permits relicensing or conveying under this
383
+ License, you may add to a covered work material governed by the terms
384
+ of that license document, provided that the further restriction does
385
+ not survive such relicensing or conveying.
386
+
387
+ If you add terms to a covered work in accord with this section, you
388
+ must place, in the relevant source files, a statement of the
389
+ additional terms that apply to those files, or a notice indicating
390
+ where to find the applicable terms.
391
+
392
+ Additional terms, permissive or non-permissive, may be stated in the
393
+ form of a separately written license, or stated as exceptions;
394
+ the above requirements apply either way.
395
+
396
+ 8. Termination.
397
+
398
+ You may not propagate or modify a covered work except as expressly
399
+ provided under this License. Any attempt otherwise to propagate or
400
+ modify it is void, and will automatically terminate your rights under
401
+ this License (including any patent licenses granted under the third
402
+ paragraph of section 11).
403
+
404
+ However, if you cease all violation of this License, then your
405
+ license from a particular copyright holder is reinstated (a)
406
+ provisionally, unless and until the copyright holder explicitly and
407
+ finally terminates your license, and (b) permanently, if the copyright
408
+ holder fails to notify you of the violation by some reasonable means
409
+ prior to 60 days after the cessation.
410
+
411
+ Moreover, your license from a particular copyright holder is
412
+ reinstated permanently if the copyright holder notifies you of the
413
+ violation by some reasonable means, this is the first time you have
414
+ received notice of violation of this License (for any work) from that
415
+ copyright holder, and you cure the violation prior to 30 days after
416
+ your receipt of the notice.
417
+
418
+ Termination of your rights under this section does not terminate the
419
+ licenses of parties who have received copies or rights from you under
420
+ this License. If your rights have been terminated and not permanently
421
+ reinstated, you do not qualify to receive new licenses for the same
422
+ material under section 10.
423
+
424
+ 9. Acceptance Not Required for Having Copies.
425
+
426
+ You are not required to accept this License in order to receive or
427
+ run a copy of the Program. Ancillary propagation of a covered work
428
+ occurring solely as a consequence of using peer-to-peer transmission
429
+ to receive a copy likewise does not require acceptance. However,
430
+ nothing other than this License grants you permission to propagate or
431
+ modify any covered work. These actions infringe copyright if you do
432
+ not accept this License. Therefore, by modifying or propagating a
433
+ covered work, you indicate your acceptance of this License to do so.
434
+
435
+ 10. Automatic Licensing of Downstream Recipients.
436
+
437
+ Each time you convey a covered work, the recipient automatically
438
+ receives a license from the original licensors, to run, modify and
439
+ propagate that work, subject to this License. You are not responsible
440
+ for enforcing compliance by third parties with this License.
441
+
442
+ An "entity transaction" is a transaction transferring control of an
443
+ organization, or substantially all assets of one, or subdividing an
444
+ organization, or merging organizations. If propagation of a covered
445
+ work results from an entity transaction, each party to that
446
+ transaction who receives a copy of the work also receives whatever
447
+ licenses to the work the party's predecessor in interest had or could
448
+ give under the previous paragraph, plus a right to possession of the
449
+ Corresponding Source of the work from the predecessor in interest, if
450
+ the predecessor has it or can get it with reasonable efforts.
451
+
452
+ You may not impose any further restrictions on the exercise of the
453
+ rights granted or affirmed under this License. For example, you may
454
+ not impose a license fee, royalty, or other charge for exercise of
455
+ rights granted under this License, and you may not initiate litigation
456
+ (including a cross-claim or counterclaim in a lawsuit) alleging that
457
+ any patent claim is infringed by making, using, selling, offering for
458
+ sale, or importing the Program or any portion of it.
459
+
460
+ 11. Patents.
461
+
462
+ A "contributor" is a copyright holder who authorizes use under this
463
+ License of the Program or a work on which the Program is based. The
464
+ work thus licensed is called the contributor's "contributor version".
465
+
466
+ A contributor's "essential patent claims" are all patent claims
467
+ owned or controlled by the contributor, whether already acquired or
468
+ hereafter acquired, that would be infringed by some manner, permitted
469
+ by this License, of making, using, or selling its contributor version,
470
+ but do not include claims that would be infringed only as a
471
+ consequence of further modification of the contributor version. For
472
+ purposes of this definition, "control" includes the right to grant
473
+ patent sublicenses in a manner consistent with the requirements of
474
+ this License.
475
+
476
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
477
+ patent license under the contributor's essential patent claims, to
478
+ make, use, sell, offer for sale, import and otherwise run, modify and
479
+ propagate the contents of its contributor version.
480
+
481
+ In the following three paragraphs, a "patent license" is any express
482
+ agreement or commitment, however denominated, not to enforce a patent
483
+ (such as an express permission to practice a patent or covenant not to
484
+ sue for patent infringement). To "grant" such a patent license to a
485
+ party means to make such an agreement or commitment not to enforce a
486
+ patent against the party.
487
+
488
+ If you convey a covered work, knowingly relying on a patent license,
489
+ and the Corresponding Source of the work is not available for anyone
490
+ to copy, free of charge and under the terms of this License, through a
491
+ publicly available network server or other readily accessible means,
492
+ then you must either (1) cause the Corresponding Source to be so
493
+ available, or (2) arrange to deprive yourself of the benefit of the
494
+ patent license for this particular work, or (3) arrange, in a manner
495
+ consistent with the requirements of this License, to extend the patent
496
+ license to downstream recipients. "Knowingly relying" means you have
497
+ actual knowledge that, but for the patent license, your conveying the
498
+ covered work in a country, or your recipient's use of the covered work
499
+ in a country, would infringe one or more identifiable patents in that
500
+ country that you have reason to believe are valid.
501
+
502
+ If, pursuant to or in connection with a single transaction or
503
+ arrangement, you convey, or propagate by procuring conveyance of, a
504
+ covered work, and grant a patent license to some of the parties
505
+ receiving the covered work authorizing them to use, propagate, modify
506
+ or convey a specific copy of the covered work, then the patent license
507
+ you grant is automatically extended to all recipients of the covered
508
+ work and works based on it.
509
+
510
+ A patent license is "discriminatory" if it does not include within
511
+ the scope of its coverage, prohibits the exercise of, or is
512
+ conditioned on the non-exercise of one or more of the rights that are
513
+ specifically granted under this License. You may not convey a covered
514
+ work if you are a party to an arrangement with a third party that is
515
+ in the business of distributing software, under which you make payment
516
+ to the third party based on the extent of your activity of conveying
517
+ the work, and under which the third party grants, to any of the
518
+ parties who would receive the covered work from you, a discriminatory
519
+ patent license (a) in connection with copies of the covered work
520
+ conveyed by you (or copies made from those copies), or (b) primarily
521
+ for and in connection with specific products or compilations that
522
+ contain the covered work, unless you entered into that arrangement,
523
+ or that patent license was granted, prior to 28 March 2007.
524
+
525
+ Nothing in this License shall be construed as excluding or limiting
526
+ any implied license or other defenses to infringement that may
527
+ otherwise be available to you under applicable patent law.
528
+
529
+ 12. No Surrender of Others' Freedom.
530
+
531
+ If conditions are imposed on you (whether by court order, agreement or
532
+ otherwise) that contradict the conditions of this License, they do not
533
+ excuse you from the conditions of this License. If you cannot convey a
534
+ covered work so as to satisfy simultaneously your obligations under this
535
+ License and any other pertinent obligations, then as a consequence you may
536
+ not convey it at all. For example, if you agree to terms that obligate you
537
+ to collect a royalty for further conveying from those to whom you convey
538
+ the Program, the only way you could satisfy both those terms and this
539
+ License would be to refrain entirely from conveying the Program.
540
+
541
+ 13. Remote Network Interaction; Use with the GNU General Public License.
542
+
543
+ Notwithstanding any other provision of this License, if you modify the
544
+ Program, your modified version must prominently offer all users
545
+ interacting with it remotely through a computer network (if your version
546
+ supports such interaction) an opportunity to receive the Corresponding
547
+ Source of your version by providing access to the Corresponding Source
548
+ from a network server at no charge, through some standard or customary
549
+ means of facilitating copying of software. This Corresponding Source
550
+ shall include the Corresponding Source for any work covered by version 3
551
+ of the GNU General Public License that is incorporated pursuant to the
552
+ following paragraph.
553
+
554
+ Notwithstanding any other provision of this License, you have
555
+ permission to link or combine any covered work with a work licensed
556
+ under version 3 of the GNU General Public License into a single
557
+ combined work, and to convey the resulting work. The terms of this
558
+ License will continue to apply to the part which is the covered work,
559
+ but the work with which it is combined will remain governed by version
560
+ 3 of the GNU General Public License.
561
+
562
+ 14. Revised Versions of this License.
563
+
564
+ The Free Software Foundation may publish revised and/or new versions of
565
+ the GNU Affero General Public License from time to time. Such new versions
566
+ will be similar in spirit to the present version, but may differ in detail to
567
+ address new problems or concerns.
568
+
569
+ Each version is given a distinguishing version number. If the
570
+ Program specifies that a certain numbered version of the GNU Affero General
571
+ Public License "or any later version" applies to it, you have the
572
+ option of following the terms and conditions either of that numbered
573
+ version or of any later version published by the Free Software
574
+ Foundation. If the Program does not specify a version number of the
575
+ GNU Affero General Public License, you may choose any version ever published
576
+ by the Free Software Foundation.
577
+
578
+ If the Program specifies that a proxy can decide which future
579
+ versions of the GNU Affero General Public License can be used, that proxy's
580
+ public statement of acceptance of a version permanently authorizes you
581
+ to choose that version for the Program.
582
+
583
+ Later license versions may give you additional or different
584
+ permissions. However, no additional obligations are imposed on any
585
+ author or copyright holder as a result of your choosing to follow a
586
+ later version.
587
+
588
+ 15. Disclaimer of Warranty.
589
+
590
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
591
+ APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
592
+ HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
593
+ OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
594
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
595
+ PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
596
+ IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
597
+ ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
598
+
599
+ 16. Limitation of Liability.
600
+
601
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
602
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
603
+ THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
604
+ GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
605
+ USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
606
+ DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
607
+ PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
608
+ EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
609
+ SUCH DAMAGES.
610
+
611
+ 17. Interpretation of Sections 15 and 16.
612
+
613
+ If the disclaimer of warranty and limitation of liability provided
614
+ above cannot be given local legal effect according to their terms,
615
+ reviewing courts shall apply local law that most closely approximates
616
+ an absolute waiver of all civil liability in connection with the
617
+ Program, unless a warranty or assumption of liability accompanies a
618
+ copy of the Program in return for a fee.
619
+
620
+ END OF TERMS AND CONDITIONS
621
+
622
+ How to Apply These Terms to Your New Programs
623
+
624
+ If you develop a new program, and you want it to be of the greatest
625
+ possible use to the public, the best way to achieve this is to make it
626
+ free software which everyone can redistribute and change under these terms.
627
+
628
+ To do so, attach the following notices to the program. It is safest
629
+ to attach them to the start of each source file to most effectively
630
+ state the exclusion of warranty; and each file should have at least
631
+ the "copyright" line and a pointer to where the full notice is found.
632
+
633
+ <one line to give the program's name and a brief idea of what it does.>
634
+ Copyright (C) <year> <name of author>
635
+
636
+ This program is free software: you can redistribute it and/or modify
637
+ it under the terms of the GNU Affero General Public License as published
638
+ by the Free Software Foundation, either version 3 of the License, or
639
+ (at your option) any later version.
640
+
641
+ This program is distributed in the hope that it will be useful,
642
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
643
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
644
+ GNU Affero General Public License for more details.
645
+
646
+ You should have received a copy of the GNU Affero General Public License
647
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
648
+
649
+ Also add information on how to contact you by electronic and paper mail.
650
+
651
+ If your software can interact with users remotely through a computer
652
+ network, you should also make sure that it provides a way for users to
653
+ get its source. For example, if your program is a web application, its
654
+ interface could display a "Source" link that leads users to an archive
655
+ of the code. There are many ways you could offer source, and different
656
+ solutions will be better for different programs; see section 13 for the
657
+ specific requirements.
658
+
659
+ You should also get your employer (if you work as a programmer) or school,
660
+ if any, to sign a "copyright disclaimer" for the program, if necessary.
661
+ For more information on this, and how to apply and follow the GNU AGPL, see
662
+ <http://www.gnu.org/licenses/>.
adetailer/README.md ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # !After Detailer
2
+
3
+ !After Detailer is a extension for stable diffusion webui, similar to Detection Detailer, except it uses ultralytics instead of the mmdet.
4
+
5
+ ## Install
6
+
7
+ (from Mikubill/sd-webui-controlnet)
8
+
9
+ 1. Open "Extensions" tab.
10
+ 2. Open "Install from URL" tab in the tab.
11
+ 3. Enter `https://github.com/Bing-su/adetailer.git` to "URL for extension's git repository".
12
+ 4. Press "Install" button.
13
+ 5. Wait 5 seconds, and you will see the message "Installed into stable-diffusion-webui\extensions\adetailer. Use Installed tab to restart".
14
+ 6. Go to "Installed" tab, click "Check for updates", and then click "Apply and restart UI". (The next time you can also use this method to update extensions.)
15
+ 7. Completely restart A1111 webui including your terminal. (If you do not know what is a "terminal", you can reboot your computer: turn your computer off and turn it on again.)
16
+
17
+ You can now install it directly from the Extensions tab.
18
+
19
+ ![image](https://i.imgur.com/g6GdRBT.png)
20
+
21
+ You **DON'T** need to download any model from huggingface.
22
+
23
+ ## Options
24
+
25
+ | Model, Prompts | | |
26
+ | --------------------------------- | ------------------------------------- | ------------------------------------------------- |
27
+ | ADetailer model | Determine what to detect. | `None` = disable |
28
+ | ADetailer prompt, negative prompt | Prompts and negative prompts to apply | If left blank, it will use the same as the input. |
29
+
30
+ | Detection | | |
31
+ | ------------------------------------ | -------------------------------------------------------------------------------------------- | --- |
32
+ | Detection model confidence threshold | Only objects with a detection model confidence above this threshold are used for inpainting. | |
33
+ | Mask min/max ratio | Only use masks whose area is between those ratios for the area of the entire image. | |
34
+
35
+ If you want to exclude objects in the background, try setting the min ratio to around `0.01`.
36
+
37
+ | Mask Preprocessing | | |
38
+ | ------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
39
+ | Mask x, y offset | Moves the mask horizontally and vertically by | |
40
+ | Mask erosion (-) / dilation (+) | Enlarge or reduce the detected mask. | [opencv example](https://docs.opencv.org/4.7.0/db/df6/tutorial_erosion_dilatation.html) |
41
+ | Mask merge mode | `None`: Inpaint each mask<br/>`Merge`: Merge all masks and inpaint<br/>`Merge and Invert`: Merge all masks and Invert, then inpaint | |
42
+
43
+ Applied in this order: x, y offset → erosion/dilation → merge/invert.
44
+
45
+ #### Inpainting
46
+
47
+ Each option corresponds to a corresponding option on the inpaint tab. Therefore, please refer to the inpaint tab for usage details on how to use each option.
48
+
49
+ ## ControlNet Inpainting
50
+
51
+ You can use the ControlNet extension if you have ControlNet installed and ControlNet models.
52
+
53
+ Support `inpaint, scribble, lineart, openpose, tile` controlnet models. Once you choose a model, the preprocessor is set automatically. It works separately from the model set by the Controlnet extension.
54
+
55
+ ## Advanced Options
56
+
57
+ API request example: [wiki/API](https://github.com/Bing-su/adetailer/wiki/API)
58
+
59
+ `ui-config.json` entries: [wiki/ui-config.json](https://github.com/Bing-su/adetailer/wiki/ui-config.json)
60
+
61
+ `[SEP], [SKIP]` tokens: [wiki/Advanced](https://github.com/Bing-su/adetailer/wiki/Advanced)
62
+
63
+ ## Media
64
+
65
+ - 🎥 [どこよりも詳しいAfter Detailer (adetailer)の使い方① 【Stable Diffusion】](https://youtu.be/sF3POwPUWCE)
66
+ - 🎥 [どこよりも詳しいAfter Detailer (adetailer)の使い方② 【Stable Diffusion】](https://youtu.be/urNISRdbIEg)
67
+
68
+ ## Model
69
+
70
+ | Model | Target | mAP 50 | mAP 50-95 |
71
+ | --------------------- | --------------------- | ----------------------------- | ----------------------------- |
72
+ | face_yolov8n.pt | 2D / realistic face | 0.660 | 0.366 |
73
+ | face_yolov8s.pt | 2D / realistic face | 0.713 | 0.404 |
74
+ | hand_yolov8n.pt | 2D / realistic hand | 0.767 | 0.505 |
75
+ | person_yolov8n-seg.pt | 2D / realistic person | 0.782 (bbox)<br/>0.761 (mask) | 0.555 (bbox)<br/>0.460 (mask) |
76
+ | person_yolov8s-seg.pt | 2D / realistic person | 0.824 (bbox)<br/>0.809 (mask) | 0.605 (bbox)<br/>0.508 (mask) |
77
+ | mediapipe_face_full | realistic face | - | - |
78
+ | mediapipe_face_short | realistic face | - | - |
79
+ | mediapipe_face_mesh | realistic face | - | - |
80
+
81
+ The yolo models can be found on huggingface [Bingsu/adetailer](https://huggingface.co/Bingsu/adetailer).
82
+
83
+ ### Additional Model
84
+
85
+ Put your [ultralytics](https://github.com/ultralytics/ultralytics) yolo model in `webui/models/adetailer`. The model name should end with `.pt` or `.pth`.
86
+
87
+ It must be a bbox detection or segment model and use all label.
88
+
89
+ ## Example
90
+
91
+ ![image](https://i.imgur.com/38RSxSO.png)
92
+ ![image](https://i.imgur.com/2CYgjLx.png)
93
+
94
+ [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/F1F1L7V2N)
adetailer/Taskfile.yml ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # https://taskfile.dev
2
+
3
+ version: "3"
4
+
5
+ dotenv:
6
+ - .env
7
+
8
+ vars:
9
+ SHELL: '{{if eq .OS "Windows_NT"}}powershell{{end}}'
10
+
11
+ tasks:
12
+ default:
13
+ cmds:
14
+ - echo "$PYTHON"
15
+ - echo "$WEBUI"
16
+ silent: true
17
+
18
+ launch:
19
+ dir: "{{.WEBUI}}"
20
+ cmds:
21
+ - "{{.PYTHON}} launch.py --xformers --api --autolaunch"
22
+
23
+ lint:
24
+ cmds:
25
+ - pre-commit run -a
adetailer/adetailer/__init__.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .__version__ import __version__
2
+ from .args import AD_ENABLE, ALL_ARGS, ADetailerArgs, EnableChecker
3
+ from .common import PredictOutput, get_models
4
+ from .mediapipe import mediapipe_predict
5
+ from .ultralytics import ultralytics_predict
6
+
7
+ AFTER_DETAILER = "ADetailer"
8
+
9
+ __all__ = [
10
+ "__version__",
11
+ "AD_ENABLE",
12
+ "ADetailerArgs",
13
+ "AFTER_DETAILER",
14
+ "ALL_ARGS",
15
+ "EnableChecker",
16
+ "PredictOutput",
17
+ "get_models",
18
+ "mediapipe_predict",
19
+ "ultralytics_predict",
20
+ ]
adetailer/adetailer/__version__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ __version__ = "23.7.11"
adetailer/adetailer/args.py ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from collections import UserList
4
+ from functools import cached_property, partial
5
+ from typing import Any, Literal, NamedTuple, Optional, Union
6
+
7
+ import pydantic
8
+ from pydantic import (
9
+ BaseModel,
10
+ Extra,
11
+ NonNegativeFloat,
12
+ NonNegativeInt,
13
+ PositiveInt,
14
+ confloat,
15
+ conint,
16
+ constr,
17
+ root_validator,
18
+ validator,
19
+ )
20
+
21
+ cn_model_regex = r".*(inpaint|tile|scribble|lineart|openpose).*|^None$"
22
+
23
+
24
+ class Arg(NamedTuple):
25
+ attr: str
26
+ name: str
27
+
28
+
29
+ class ArgsList(UserList):
30
+ @cached_property
31
+ def attrs(self) -> tuple[str]:
32
+ return tuple(attr for attr, _ in self)
33
+
34
+ @cached_property
35
+ def names(self) -> tuple[str]:
36
+ return tuple(name for _, name in self)
37
+
38
+
39
+ class ADetailerArgs(BaseModel, extra=Extra.forbid):
40
+ ad_model: str = "None"
41
+ ad_prompt: str = ""
42
+ ad_negative_prompt: str = ""
43
+ ad_confidence: confloat(ge=0.0, le=1.0) = 0.3
44
+ ad_mask_min_ratio: confloat(ge=0.0, le=1.0) = 0.0
45
+ ad_mask_max_ratio: confloat(ge=0.0, le=1.0) = 1.0
46
+ ad_dilate_erode: int = 4
47
+ ad_x_offset: int = 0
48
+ ad_y_offset: int = 0
49
+ ad_mask_merge_invert: Literal["None", "Merge", "Merge and Invert"] = "None"
50
+ ad_mask_blur: NonNegativeInt = 4
51
+ ad_denoising_strength: confloat(ge=0.0, le=1.0) = 0.4
52
+ ad_inpaint_only_masked: bool = True
53
+ ad_inpaint_only_masked_padding: NonNegativeInt = 32
54
+ ad_use_inpaint_width_height: bool = False
55
+ ad_inpaint_width: PositiveInt = 512
56
+ ad_inpaint_height: PositiveInt = 512
57
+ ad_use_steps: bool = False
58
+ ad_steps: PositiveInt = 28
59
+ ad_use_cfg_scale: bool = False
60
+ ad_cfg_scale: NonNegativeFloat = 7.0
61
+ ad_use_sampler: bool = False
62
+ ad_sampler: str = "DPM++ 2M Karras"
63
+ ad_use_noise_multiplier: bool = False
64
+ ad_noise_multiplier: confloat(ge=0.5, le=1.5) = 1.0
65
+ ad_use_clip_skip: bool = False
66
+ ad_clip_skip: conint(ge=1, le=12) = 1
67
+ ad_restore_face: bool = False
68
+ ad_controlnet_model: constr(regex=cn_model_regex) = "None"
69
+ ad_controlnet_module: Optional[constr(regex=r".*inpaint.*|^None$")] = None
70
+ ad_controlnet_weight: confloat(ge=0.0, le=1.0) = 1.0
71
+ ad_controlnet_guidance_start: confloat(ge=0.0, le=1.0) = 0.0
72
+ ad_controlnet_guidance_end: confloat(ge=0.0, le=1.0) = 1.0
73
+ is_api: bool = True
74
+
75
+ @root_validator(skip_on_failure=True)
76
+ def ad_controlnt_module_validator(cls, values): # noqa: N805
77
+ cn_model = values.get("ad_controlnet_model", "None")
78
+ cn_module = values.get("ad_controlnet_module", None)
79
+ if "inpaint" not in cn_model or cn_module == "None":
80
+ values["ad_controlnet_module"] = None
81
+ return values
82
+
83
+ @validator("is_api", pre=True)
84
+ def is_api_validator(cls, v: Any): # noqa: N805
85
+ "tuple is json serializable but cannot be made with json deserialize."
86
+ return type(v) is not tuple
87
+
88
+ @staticmethod
89
+ def ppop(
90
+ p: dict[str, Any],
91
+ key: str,
92
+ pops: list[str] | None = None,
93
+ cond: Any = None,
94
+ ) -> None:
95
+ if pops is None:
96
+ pops = [key]
97
+ if key not in p:
98
+ return
99
+ value = p[key]
100
+ cond = (not bool(value)) if cond is None else value == cond
101
+
102
+ if cond:
103
+ for k in pops:
104
+ p.pop(k, None)
105
+
106
+ def extra_params(self, suffix: str = "") -> dict[str, Any]:
107
+ if self.ad_model == "None":
108
+ return {}
109
+
110
+ p = {name: getattr(self, attr) for attr, name in ALL_ARGS}
111
+ ppop = partial(self.ppop, p)
112
+
113
+ ppop("ADetailer prompt")
114
+ ppop("ADetailer negative prompt")
115
+ ppop("ADetailer mask min ratio", cond=0.0)
116
+ ppop("ADetailer mask max ratio", cond=1.0)
117
+ ppop("ADetailer x offset", cond=0)
118
+ ppop("ADetailer y offset", cond=0)
119
+ ppop("ADetailer mask merge/invert", cond="None")
120
+ ppop("ADetailer inpaint only masked", ["ADetailer inpaint padding"])
121
+ ppop(
122
+ "ADetailer use inpaint width/height",
123
+ [
124
+ "ADetailer use inpaint width/height",
125
+ "ADetailer inpaint width",
126
+ "ADetailer inpaint height",
127
+ ],
128
+ )
129
+ ppop(
130
+ "ADetailer use separate steps",
131
+ ["ADetailer use separate steps", "ADetailer steps"],
132
+ )
133
+ ppop(
134
+ "ADetailer use separate CFG scale",
135
+ ["ADetailer use separate CFG scale", "ADetailer CFG scale"],
136
+ )
137
+ ppop(
138
+ "ADetailer use separate sampler",
139
+ ["ADetailer use separate sampler", "ADetailer sampler"],
140
+ )
141
+ ppop(
142
+ "ADetailer use separate noise multiplier",
143
+ ["ADetailer use separate noise multiplier", "ADetailer noise multiplier"],
144
+ )
145
+
146
+ ppop(
147
+ "ADetailer use separate CLIP skip",
148
+ ["ADetailer use separate CLIP skip", "ADetailer CLIP skip"],
149
+ )
150
+
151
+ ppop("ADetailer restore face")
152
+ ppop(
153
+ "ADetailer ControlNet model",
154
+ [
155
+ "ADetailer ControlNet model",
156
+ "ADetailer ControlNet module",
157
+ "ADetailer ControlNet weight",
158
+ "ADetailer ControlNet guidance start",
159
+ "ADetailer ControlNet guidance end",
160
+ ],
161
+ cond="None",
162
+ )
163
+ ppop("ADetailer ControlNet module")
164
+ ppop("ADetailer ControlNet weight", cond=1.0)
165
+ ppop("ADetailer ControlNet guidance start", cond=0.0)
166
+ ppop("ADetailer ControlNet guidance end", cond=1.0)
167
+
168
+ if suffix:
169
+ p = {k + suffix: v for k, v in p.items()}
170
+
171
+ return p
172
+
173
+
174
+ class EnableChecker(BaseModel):
175
+ enable: bool
176
+ arg_list: list
177
+
178
+ def is_enabled(self) -> bool:
179
+ ad_model = ALL_ARGS[0].attr
180
+ if not self.enable:
181
+ return False
182
+ return any(arg.get(ad_model, "None") != "None" for arg in self.arg_list)
183
+
184
+
185
+ _all_args = [
186
+ ("ad_enable", "ADetailer enable"),
187
+ ("ad_model", "ADetailer model"),
188
+ ("ad_prompt", "ADetailer prompt"),
189
+ ("ad_negative_prompt", "ADetailer negative prompt"),
190
+ ("ad_confidence", "ADetailer confidence"),
191
+ ("ad_mask_min_ratio", "ADetailer mask min ratio"),
192
+ ("ad_mask_max_ratio", "ADetailer mask max ratio"),
193
+ ("ad_x_offset", "ADetailer x offset"),
194
+ ("ad_y_offset", "ADetailer y offset"),
195
+ ("ad_dilate_erode", "ADetailer dilate/erode"),
196
+ ("ad_mask_merge_invert", "ADetailer mask merge/invert"),
197
+ ("ad_mask_blur", "ADetailer mask blur"),
198
+ ("ad_denoising_strength", "ADetailer denoising strength"),
199
+ ("ad_inpaint_only_masked", "ADetailer inpaint only masked"),
200
+ ("ad_inpaint_only_masked_padding", "ADetailer inpaint padding"),
201
+ ("ad_use_inpaint_width_height", "ADetailer use inpaint width/height"),
202
+ ("ad_inpaint_width", "ADetailer inpaint width"),
203
+ ("ad_inpaint_height", "ADetailer inpaint height"),
204
+ ("ad_use_steps", "ADetailer use separate steps"),
205
+ ("ad_steps", "ADetailer steps"),
206
+ ("ad_use_cfg_scale", "ADetailer use separate CFG scale"),
207
+ ("ad_cfg_scale", "ADetailer CFG scale"),
208
+ ("ad_use_sampler", "ADetailer use separate sampler"),
209
+ ("ad_sampler", "ADetailer sampler"),
210
+ ("ad_use_noise_multiplier", "ADetailer use separate noise multiplier"),
211
+ ("ad_noise_multiplier", "ADetailer noise multiplier"),
212
+ ("ad_use_clip_skip", "ADetailer use separate CLIP skip"),
213
+ ("ad_clip_skip", "ADetailer CLIP skip"),
214
+ ("ad_restore_face", "ADetailer restore face"),
215
+ ("ad_controlnet_model", "ADetailer ControlNet model"),
216
+ ("ad_controlnet_module", "ADetailer ControlNet module"),
217
+ ("ad_controlnet_weight", "ADetailer ControlNet weight"),
218
+ ("ad_controlnet_guidance_start", "ADetailer ControlNet guidance start"),
219
+ ("ad_controlnet_guidance_end", "ADetailer ControlNet guidance end"),
220
+ ]
221
+
222
+ AD_ENABLE = Arg(*_all_args[0])
223
+ _args = [Arg(*args) for args in _all_args[1:]]
224
+ ALL_ARGS = ArgsList(_args)
225
+
226
+ BBOX_SORTBY = [
227
+ "None",
228
+ "Position (left to right)",
229
+ "Position (center to edge)",
230
+ "Area (large to small)",
231
+ ]
232
+ MASK_MERGE_INVERT = ["None", "Merge", "Merge and Invert"]
adetailer/adetailer/common.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from collections import OrderedDict
4
+ from dataclasses import dataclass, field
5
+ from pathlib import Path
6
+ from typing import Optional, Union
7
+
8
+ from huggingface_hub import hf_hub_download
9
+ from PIL import Image, ImageDraw
10
+ from rich import print
11
+
12
+ repo_id = "Bingsu/adetailer"
13
+
14
+
15
+ @dataclass
16
+ class PredictOutput:
17
+ bboxes: list[list[int | float]] = field(default_factory=list)
18
+ masks: list[Image.Image] = field(default_factory=list)
19
+ preview: Optional[Image.Image] = None
20
+
21
+
22
+ def hf_download(file: str):
23
+ try:
24
+ path = hf_hub_download(repo_id, file)
25
+ except Exception:
26
+ msg = f"[-] ADetailer: Failed to load model {file!r} from huggingface"
27
+ print(msg)
28
+ path = "INVALID"
29
+ return path
30
+
31
+
32
+ def get_models(
33
+ model_dir: Union[str, Path], huggingface: bool = True
34
+ ) -> OrderedDict[str, Optional[str]]:
35
+ model_dir = Path(model_dir)
36
+ if model_dir.is_dir():
37
+ model_paths = [
38
+ p
39
+ for p in model_dir.rglob("*")
40
+ if p.is_file() and p.suffix in (".pt", ".pth")
41
+ ]
42
+ else:
43
+ model_paths = []
44
+
45
+ models = OrderedDict()
46
+ if huggingface:
47
+ models.update(
48
+ {
49
+ "face_yolov8n.pt": hf_download("face_yolov8n.pt"),
50
+ "face_yolov8s.pt": hf_download("face_yolov8s.pt"),
51
+ "hand_yolov8n.pt": hf_download("hand_yolov8n.pt"),
52
+ "person_yolov8n-seg.pt": hf_download("person_yolov8n-seg.pt"),
53
+ "person_yolov8s-seg.pt": hf_download("person_yolov8s-seg.pt"),
54
+ }
55
+ )
56
+ models.update(
57
+ {
58
+ "mediapipe_face_full": None,
59
+ "mediapipe_face_short": None,
60
+ "mediapipe_face_mesh": None,
61
+ "mediapipe_face_mesh_eyes_only": None,
62
+ }
63
+ )
64
+
65
+ invalid_keys = [k for k, v in models.items() if v == "INVALID"]
66
+ for key in invalid_keys:
67
+ models.pop(key)
68
+
69
+ for path in model_paths:
70
+ if path.name in models:
71
+ continue
72
+ models[path.name] = str(path)
73
+
74
+ return models
75
+
76
+
77
+ def create_mask_from_bbox(
78
+ bboxes: list[list[float]], shape: tuple[int, int]
79
+ ) -> list[Image.Image]:
80
+ """
81
+ Parameters
82
+ ----------
83
+ bboxes: list[list[float]]
84
+ list of [x1, y1, x2, y2]
85
+ bounding boxes
86
+ shape: tuple[int, int]
87
+ shape of the image (width, height)
88
+
89
+ Returns
90
+ -------
91
+ masks: list[Image.Image]
92
+ A list of masks
93
+
94
+ """
95
+ masks = []
96
+ for bbox in bboxes:
97
+ mask = Image.new("L", shape, 0)
98
+ mask_draw = ImageDraw.Draw(mask)
99
+ mask_draw.rectangle(bbox, fill=255)
100
+ masks.append(mask)
101
+ return masks
102
+
103
+
104
+ def create_bbox_from_mask(
105
+ masks: list[Image.Image], shape: tuple[int, int]
106
+ ) -> list[list[int]]:
107
+ """
108
+ Parameters
109
+ ----------
110
+ masks: list[Image.Image]
111
+ A list of masks
112
+ shape: tuple[int, int]
113
+ shape of the image (width, height)
114
+
115
+ Returns
116
+ -------
117
+ bboxes: list[list[float]]
118
+ A list of bounding boxes
119
+
120
+ """
121
+ bboxes = []
122
+ for mask in masks:
123
+ mask = mask.resize(shape)
124
+ bbox = mask.getbbox()
125
+ if bbox is not None:
126
+ bboxes.append(list(bbox))
127
+ return bboxes
adetailer/adetailer/mask.py ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from enum import IntEnum
4
+ from functools import partial, reduce
5
+ from math import dist
6
+
7
+ import cv2
8
+ import numpy as np
9
+ from PIL import Image, ImageChops
10
+
11
+ from adetailer.args import MASK_MERGE_INVERT
12
+ from adetailer.common import PredictOutput
13
+
14
+
15
+ class SortBy(IntEnum):
16
+ NONE = 0
17
+ LEFT_TO_RIGHT = 1
18
+ CENTER_TO_EDGE = 2
19
+ AREA = 3
20
+
21
+
22
+ class MergeInvert(IntEnum):
23
+ NONE = 0
24
+ MERGE = 1
25
+ MERGE_INVERT = 2
26
+
27
+
28
+ def _dilate(arr: np.ndarray, value: int) -> np.ndarray:
29
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (value, value))
30
+ return cv2.dilate(arr, kernel, iterations=1)
31
+
32
+
33
+ def _erode(arr: np.ndarray, value: int) -> np.ndarray:
34
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (value, value))
35
+ return cv2.erode(arr, kernel, iterations=1)
36
+
37
+
38
+ def dilate_erode(img: Image.Image, value: int) -> Image.Image:
39
+ """
40
+ The dilate_erode function takes an image and a value.
41
+ If the value is positive, it dilates the image by that amount.
42
+ If the value is negative, it erodes the image by that amount.
43
+
44
+ Parameters
45
+ ----------
46
+ img: PIL.Image.Image
47
+ the image to be processed
48
+ value: int
49
+ kernel size of dilation or erosion
50
+
51
+ Returns
52
+ -------
53
+ PIL.Image.Image
54
+ The image that has been dilated or eroded
55
+ """
56
+ if value == 0:
57
+ return img
58
+
59
+ arr = np.array(img)
60
+ arr = _dilate(arr, value) if value > 0 else _erode(arr, -value)
61
+
62
+ return Image.fromarray(arr)
63
+
64
+
65
+ def offset(img: Image.Image, x: int = 0, y: int = 0) -> Image.Image:
66
+ """
67
+ The offset function takes an image and offsets it by a given x(→) and y(↑) value.
68
+
69
+ Parameters
70
+ ----------
71
+ mask: Image.Image
72
+ Pass the mask image to the function
73
+ x: int
74
+
75
+ y: int
76
+
77
+
78
+ Returns
79
+ -------
80
+ PIL.Image.Image
81
+ A new image that is offset by x and y
82
+ """
83
+ return ImageChops.offset(img, x, -y)
84
+
85
+
86
+ def is_all_black(img: Image.Image) -> bool:
87
+ arr = np.array(img)
88
+ return cv2.countNonZero(arr) == 0
89
+
90
+
91
+ def bbox_area(bbox: list[float]):
92
+ return (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
93
+
94
+
95
+ def mask_preprocess(
96
+ masks: list[Image.Image],
97
+ kernel: int = 0,
98
+ x_offset: int = 0,
99
+ y_offset: int = 0,
100
+ merge_invert: int | MergeInvert | str = MergeInvert.NONE,
101
+ ) -> list[Image.Image]:
102
+ """
103
+ The mask_preprocess function takes a list of masks and preprocesses them.
104
+ It dilates and erodes the masks, and offsets them by x_offset and y_offset.
105
+
106
+ Parameters
107
+ ----------
108
+ masks: list[Image.Image]
109
+ A list of masks
110
+ kernel: int
111
+ kernel size of dilation or erosion
112
+ x_offset: int
113
+
114
+ y_offset: int
115
+
116
+
117
+ Returns
118
+ -------
119
+ list[Image.Image]
120
+ A list of processed masks
121
+ """
122
+ if not masks:
123
+ return []
124
+
125
+ if x_offset != 0 or y_offset != 0:
126
+ masks = [offset(m, x_offset, y_offset) for m in masks]
127
+
128
+ if kernel != 0:
129
+ masks = [dilate_erode(m, kernel) for m in masks]
130
+ masks = [m for m in masks if not is_all_black(m)]
131
+
132
+ return mask_merge_invert(masks, mode=merge_invert)
133
+
134
+
135
+ # Bbox sorting
136
+ def _key_left_to_right(bbox: list[float]) -> float:
137
+ """
138
+ Left to right
139
+
140
+ Parameters
141
+ ----------
142
+ bbox: list[float]
143
+ list of [x1, y1, x2, y2]
144
+ """
145
+ return bbox[0]
146
+
147
+
148
+ def _key_center_to_edge(bbox: list[float], *, center: tuple[float, float]) -> float:
149
+ """
150
+ Center to edge
151
+
152
+ Parameters
153
+ ----------
154
+ bbox: list[float]
155
+ list of [x1, y1, x2, y2]
156
+ image: Image.Image
157
+ the image
158
+ """
159
+ bbox_center = ((bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2)
160
+ return dist(center, bbox_center)
161
+
162
+
163
+ def _key_area(bbox: list[float]) -> float:
164
+ """
165
+ Large to small
166
+
167
+ Parameters
168
+ ----------
169
+ bbox: list[float]
170
+ list of [x1, y1, x2, y2]
171
+ """
172
+ return -bbox_area(bbox)
173
+
174
+
175
+ def sort_bboxes(
176
+ pred: PredictOutput, order: int | SortBy = SortBy.NONE
177
+ ) -> PredictOutput:
178
+ if order == SortBy.NONE or len(pred.bboxes) <= 1:
179
+ return pred
180
+
181
+ if order == SortBy.LEFT_TO_RIGHT:
182
+ key = _key_left_to_right
183
+ elif order == SortBy.CENTER_TO_EDGE:
184
+ width, height = pred.preview.size
185
+ center = (width / 2, height / 2)
186
+ key = partial(_key_center_to_edge, center=center)
187
+ elif order == SortBy.AREA:
188
+ key = _key_area
189
+ else:
190
+ raise RuntimeError
191
+
192
+ items = len(pred.bboxes)
193
+ idx = sorted(range(items), key=lambda i: key(pred.bboxes[i]))
194
+ pred.bboxes = [pred.bboxes[i] for i in idx]
195
+ pred.masks = [pred.masks[i] for i in idx]
196
+ return pred
197
+
198
+
199
+ # Filter by ratio
200
+ def is_in_ratio(bbox: list[float], low: float, high: float, orig_area: int) -> bool:
201
+ area = bbox_area(bbox)
202
+ return low <= area / orig_area <= high
203
+
204
+
205
+ def filter_by_ratio(pred: PredictOutput, low: float, high: float) -> PredictOutput:
206
+ if not pred.bboxes:
207
+ return pred
208
+
209
+ w, h = pred.preview.size
210
+ orig_area = w * h
211
+ items = len(pred.bboxes)
212
+ idx = [i for i in range(items) if is_in_ratio(pred.bboxes[i], low, high, orig_area)]
213
+ pred.bboxes = [pred.bboxes[i] for i in idx]
214
+ pred.masks = [pred.masks[i] for i in idx]
215
+ return pred
216
+
217
+
218
+ # Merge / Invert
219
+ def mask_merge(masks: list[Image.Image]) -> list[Image.Image]:
220
+ arrs = [np.array(m) for m in masks]
221
+ arr = reduce(cv2.bitwise_or, arrs)
222
+ return [Image.fromarray(arr)]
223
+
224
+
225
+ def mask_invert(masks: list[Image.Image]) -> list[Image.Image]:
226
+ return [ImageChops.invert(m) for m in masks]
227
+
228
+
229
+ def mask_merge_invert(
230
+ masks: list[Image.Image], mode: int | MergeInvert | str
231
+ ) -> list[Image.Image]:
232
+ if isinstance(mode, str):
233
+ mode = MASK_MERGE_INVERT.index(mode)
234
+
235
+ if mode == MergeInvert.NONE or not masks:
236
+ return masks
237
+
238
+ if mode == MergeInvert.MERGE:
239
+ return mask_merge(masks)
240
+
241
+ if mode == MergeInvert.MERGE_INVERT:
242
+ merged = mask_merge(masks)
243
+ return mask_invert(merged)
244
+
245
+ raise RuntimeError
adetailer/adetailer/mediapipe.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from functools import partial
4
+
5
+ import mediapipe as mp
6
+ import numpy as np
7
+ from PIL import Image, ImageDraw
8
+
9
+ from adetailer import PredictOutput
10
+ from adetailer.common import create_bbox_from_mask, create_mask_from_bbox
11
+
12
+
13
+ def mediapipe_predict(
14
+ model_type: str, image: Image.Image, confidence: float = 0.3
15
+ ) -> PredictOutput:
16
+ mapping = {
17
+ "mediapipe_face_short": partial(mediapipe_face_detection, 0),
18
+ "mediapipe_face_full": partial(mediapipe_face_detection, 1),
19
+ "mediapipe_face_mesh": mediapipe_face_mesh,
20
+ "mediapipe_face_mesh_eyes_only": mediapipe_face_mesh_eyes_only,
21
+ }
22
+ if model_type in mapping:
23
+ func = mapping[model_type]
24
+ return func(image, confidence)
25
+ msg = f"[-] ADetailer: Invalid mediapipe model type: {model_type}, Available: {list(mapping.keys())!r}"
26
+ raise RuntimeError(msg)
27
+
28
+
29
+ def mediapipe_face_detection(
30
+ model_type: int, image: Image.Image, confidence: float = 0.3
31
+ ) -> PredictOutput:
32
+ img_width, img_height = image.size
33
+
34
+ mp_face_detection = mp.solutions.face_detection
35
+ draw_util = mp.solutions.drawing_utils
36
+
37
+ img_array = np.array(image)
38
+
39
+ with mp_face_detection.FaceDetection(
40
+ model_selection=model_type, min_detection_confidence=confidence
41
+ ) as face_detector:
42
+ pred = face_detector.process(img_array)
43
+
44
+ if pred.detections is None:
45
+ return PredictOutput()
46
+
47
+ preview_array = img_array.copy()
48
+
49
+ bboxes = []
50
+ for detection in pred.detections:
51
+ draw_util.draw_detection(preview_array, detection)
52
+
53
+ bbox = detection.location_data.relative_bounding_box
54
+ x1 = bbox.xmin * img_width
55
+ y1 = bbox.ymin * img_height
56
+ w = bbox.width * img_width
57
+ h = bbox.height * img_height
58
+ x2 = x1 + w
59
+ y2 = y1 + h
60
+
61
+ bboxes.append([x1, y1, x2, y2])
62
+
63
+ masks = create_mask_from_bbox(bboxes, image.size)
64
+ preview = Image.fromarray(preview_array)
65
+
66
+ return PredictOutput(bboxes=bboxes, masks=masks, preview=preview)
67
+
68
+
69
+ def get_convexhull(points: np.ndarray) -> list[tuple[int, int]]:
70
+ """
71
+ Parameters
72
+ ----------
73
+ points: An ndarray of shape (n, 2) containing the 2D points.
74
+
75
+ Returns
76
+ -------
77
+ list[tuple[int, int]]: Input for the draw.polygon function
78
+ """
79
+ from scipy.spatial import ConvexHull
80
+
81
+ hull = ConvexHull(points)
82
+ vertices = hull.vertices
83
+ return list(zip(points[vertices, 0], points[vertices, 1]))
84
+
85
+
86
+ def mediapipe_face_mesh(image: Image.Image, confidence: float = 0.3) -> PredictOutput:
87
+ mp_face_mesh = mp.solutions.face_mesh
88
+ draw_util = mp.solutions.drawing_utils
89
+ drawing_styles = mp.solutions.drawing_styles
90
+
91
+ w, h = image.size
92
+
93
+ with mp_face_mesh.FaceMesh(
94
+ static_image_mode=True, max_num_faces=20, min_detection_confidence=confidence
95
+ ) as face_mesh:
96
+ arr = np.array(image)
97
+ pred = face_mesh.process(arr)
98
+
99
+ if pred.multi_face_landmarks is None:
100
+ return PredictOutput()
101
+
102
+ preview = arr.copy()
103
+ masks = []
104
+
105
+ for landmarks in pred.multi_face_landmarks:
106
+ draw_util.draw_landmarks(
107
+ image=preview,
108
+ landmark_list=landmarks,
109
+ connections=mp_face_mesh.FACEMESH_TESSELATION,
110
+ landmark_drawing_spec=None,
111
+ connection_drawing_spec=drawing_styles.get_default_face_mesh_tesselation_style(),
112
+ )
113
+
114
+ points = np.array([(land.x * w, land.y * h) for land in landmarks.landmark])
115
+ outline = get_convexhull(points)
116
+
117
+ mask = Image.new("L", image.size, "black")
118
+ draw = ImageDraw.Draw(mask)
119
+ draw.polygon(outline, fill="white")
120
+ masks.append(mask)
121
+
122
+ bboxes = create_bbox_from_mask(masks, image.size)
123
+ preview = Image.fromarray(preview)
124
+ return PredictOutput(bboxes=bboxes, masks=masks, preview=preview)
125
+
126
+
127
+ def mediapipe_face_mesh_eyes_only(
128
+ image: Image.Image, confidence: float = 0.3
129
+ ) -> PredictOutput:
130
+ mp_face_mesh = mp.solutions.face_mesh
131
+
132
+ left_idx = np.array(list(mp_face_mesh.FACEMESH_LEFT_EYE)).flatten()
133
+ right_idx = np.array(list(mp_face_mesh.FACEMESH_RIGHT_EYE)).flatten()
134
+
135
+ w, h = image.size
136
+
137
+ with mp_face_mesh.FaceMesh(
138
+ static_image_mode=True, max_num_faces=20, min_detection_confidence=confidence
139
+ ) as face_mesh:
140
+ arr = np.array(image)
141
+ pred = face_mesh.process(arr)
142
+
143
+ if pred.multi_face_landmarks is None:
144
+ return PredictOutput()
145
+
146
+ preview = image.copy()
147
+ masks = []
148
+
149
+ for landmarks in pred.multi_face_landmarks:
150
+ points = np.array([(land.x * w, land.y * h) for land in landmarks.landmark])
151
+ left_eyes = points[left_idx]
152
+ right_eyes = points[right_idx]
153
+ left_outline = get_convexhull(left_eyes)
154
+ right_outline = get_convexhull(right_eyes)
155
+
156
+ mask = Image.new("L", image.size, "black")
157
+ draw = ImageDraw.Draw(mask)
158
+ for outline in (left_outline, right_outline):
159
+ draw.polygon(outline, fill="white")
160
+ masks.append(mask)
161
+
162
+ bboxes = create_bbox_from_mask(masks, image.size)
163
+ preview = draw_preview(preview, bboxes, masks)
164
+ return PredictOutput(bboxes=bboxes, masks=masks, preview=preview)
165
+
166
+
167
+ def draw_preview(
168
+ preview: Image.Image, bboxes: list[list[int]], masks: list[Image.Image]
169
+ ) -> Image.Image:
170
+ red = Image.new("RGB", preview.size, "red")
171
+ for mask in masks:
172
+ masked = Image.composite(red, preview, mask)
173
+ preview = Image.blend(preview, masked, 0.25)
174
+
175
+ draw = ImageDraw.Draw(preview)
176
+ for bbox in bboxes:
177
+ draw.rectangle(bbox, outline="red", width=2)
178
+
179
+ return preview
adetailer/adetailer/traceback.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import io
4
+ import platform
5
+ import sys
6
+ from importlib.metadata import version
7
+ from typing import Any, Callable
8
+
9
+ from rich.console import Console, Group
10
+ from rich.panel import Panel
11
+ from rich.table import Table
12
+ from rich.traceback import Traceback
13
+
14
+ from adetailer.__version__ import __version__
15
+
16
+
17
+ def processing(*args: Any) -> dict[str, Any]:
18
+ try:
19
+ from modules.processing import (
20
+ StableDiffusionProcessingImg2Img,
21
+ StableDiffusionProcessingTxt2Img,
22
+ )
23
+ except ImportError:
24
+ return {}
25
+
26
+ p = None
27
+ for arg in args:
28
+ if isinstance(
29
+ arg, (StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img)
30
+ ):
31
+ p = arg
32
+ break
33
+
34
+ if p is None:
35
+ return {}
36
+
37
+ info = {
38
+ "prompt": p.prompt,
39
+ "negative_prompt": p.negative_prompt,
40
+ "n_iter": p.n_iter,
41
+ "batch_size": p.batch_size,
42
+ "width": p.width,
43
+ "height": p.height,
44
+ "sampler_name": p.sampler_name,
45
+ "enable_hr": getattr(p, "enable_hr", False),
46
+ "hr_upscaler": getattr(p, "hr_upscaler", ""),
47
+ }
48
+
49
+ info.update(sd_models())
50
+ return info
51
+
52
+
53
+ def sd_models() -> dict[str, str]:
54
+ try:
55
+ from modules import shared
56
+
57
+ opts = shared.opts
58
+ except Exception:
59
+ return {}
60
+
61
+ return {
62
+ "checkpoint": getattr(opts, "sd_model_checkpoint", "------"),
63
+ "vae": getattr(opts, "sd_vae", "------"),
64
+ "unet": getattr(opts, "sd_unet", "------"),
65
+ }
66
+
67
+
68
+ def ad_args(*args: Any) -> dict[str, Any]:
69
+ ad_args = [
70
+ arg
71
+ for arg in args
72
+ if isinstance(arg, dict) and arg.get("ad_model", "None") != "None"
73
+ ]
74
+ if not ad_args:
75
+ return {}
76
+
77
+ arg0 = ad_args[0]
78
+ is_api = arg0.get("is_api", True)
79
+ return {
80
+ "version": __version__,
81
+ "ad_model": arg0["ad_model"],
82
+ "ad_prompt": arg0.get("ad_prompt", ""),
83
+ "ad_negative_prompt": arg0.get("ad_negative_prompt", ""),
84
+ "ad_controlnet_model": arg0.get("ad_controlnet_model", "None"),
85
+ "is_api": type(is_api) is not tuple,
86
+ }
87
+
88
+
89
+ def library_version():
90
+ libraries = ["torch", "torchvision", "ultralytics", "mediapipe"]
91
+ d = {}
92
+ for lib in libraries:
93
+ try:
94
+ d[lib] = version(lib)
95
+ except Exception:
96
+ d[lib] = "Unknown"
97
+ return d
98
+
99
+
100
+ def sys_info() -> dict[str, Any]:
101
+ try:
102
+ import launch
103
+
104
+ version = launch.git_tag()
105
+ commit = launch.commit_hash()
106
+ except Exception:
107
+ version = "Unknown (too old or vladmandic)"
108
+ commit = "Unknown"
109
+
110
+ return {
111
+ "Platform": platform.platform(),
112
+ "Python": sys.version,
113
+ "Version": version,
114
+ "Commit": commit,
115
+ "Commandline": sys.argv,
116
+ "Libraries": library_version(),
117
+ }
118
+
119
+
120
+ def get_table(title: str, data: dict[str, Any]) -> Table:
121
+ table = Table(title=title, highlight=True)
122
+ table.add_column(" ", justify="right", style="dim")
123
+ table.add_column("Value")
124
+ for key, value in data.items():
125
+ if not isinstance(value, str):
126
+ value = repr(value)
127
+ table.add_row(key, value)
128
+
129
+ return table
130
+
131
+
132
+ def rich_traceback(func: Callable) -> Callable:
133
+ def wrapper(*args, **kwargs):
134
+ string = io.StringIO()
135
+ width = Console().width
136
+ width = width - 4 if width > 4 else None
137
+ console = Console(file=string, width=width)
138
+ try:
139
+ return func(*args, **kwargs)
140
+ except Exception as e:
141
+ tables = [
142
+ get_table(title, data)
143
+ for title, data in [
144
+ ("System info", sys_info()),
145
+ ("Inputs", processing(*args)),
146
+ ("ADetailer", ad_args(*args)),
147
+ ]
148
+ if data
149
+ ]
150
+ tables.append(Traceback())
151
+
152
+ console.print(Panel(Group(*tables)))
153
+ output = "\n" + string.getvalue()
154
+
155
+ try:
156
+ error = e.__class__(output)
157
+ except Exception:
158
+ error = RuntimeError(output)
159
+ raise error from None
160
+
161
+ return wrapper
adetailer/adetailer/ui.py ADDED
@@ -0,0 +1,558 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from functools import partial
4
+ from types import SimpleNamespace
5
+ from typing import Any
6
+
7
+ import gradio as gr
8
+
9
+ from adetailer import AFTER_DETAILER, __version__
10
+ from adetailer.args import AD_ENABLE, ALL_ARGS, MASK_MERGE_INVERT
11
+ from controlnet_ext import controlnet_exists, get_cn_models
12
+
13
+ cn_module_choices = [
14
+ "inpaint_global_harmonious",
15
+ "inpaint_only",
16
+ "inpaint_only+lama",
17
+ ]
18
+
19
+
20
+ class Widgets(SimpleNamespace):
21
+ def tolist(self):
22
+ return [getattr(self, attr) for attr in ALL_ARGS.attrs]
23
+
24
+
25
+ def gr_interactive(value: bool = True):
26
+ return gr.update(interactive=value)
27
+
28
+
29
+ def ordinal(n: int) -> str:
30
+ d = {1: "st", 2: "nd", 3: "rd"}
31
+ return str(n) + ("th" if 11 <= n % 100 <= 13 else d.get(n % 10, "th"))
32
+
33
+
34
+ def suffix(n: int, c: str = " ") -> str:
35
+ return "" if n == 0 else c + ordinal(n + 1)
36
+
37
+
38
+ def on_widget_change(state: dict, value: Any, *, attr: str):
39
+ state[attr] = value
40
+ return state
41
+
42
+
43
+ def on_generate_click(state: dict, *values: Any):
44
+ for attr, value in zip(ALL_ARGS.attrs, values):
45
+ state[attr] = value
46
+ state["is_api"] = ()
47
+ return state
48
+
49
+
50
+ def on_cn_model_update(cn_model: str):
51
+ if "inpaint" in cn_model:
52
+ return gr.update(
53
+ visible=True, choices=cn_module_choices, value=cn_module_choices[0]
54
+ )
55
+ return gr.update(visible=False, choices=["None"], value="None")
56
+
57
+
58
+ def elem_id(item_id: str, n: int, is_img2img: bool) -> str:
59
+ tap = "img2img" if is_img2img else "txt2img"
60
+ suf = suffix(n, "_")
61
+ return f"script_{tap}_adetailer_{item_id}{suf}"
62
+
63
+
64
+ def adui(
65
+ num_models: int,
66
+ is_img2img: bool,
67
+ model_list: list[str],
68
+ samplers: list[str],
69
+ t2i_button: gr.Button,
70
+ i2i_button: gr.Button,
71
+ ):
72
+ states = []
73
+ infotext_fields = []
74
+ eid = partial(elem_id, n=0, is_img2img=is_img2img)
75
+
76
+ with gr.Accordion(AFTER_DETAILER, open=False, elem_id=eid("ad_main_accordion")):
77
+ with gr.Row():
78
+ with gr.Column(scale=6):
79
+ ad_enable = gr.Checkbox(
80
+ label="Enable ADetailer",
81
+ value=False,
82
+ visible=True,
83
+ elem_id=eid("ad_enable"),
84
+ )
85
+
86
+ with gr.Column(scale=1, min_width=180):
87
+ gr.Markdown(
88
+ f"v{__version__}",
89
+ elem_id=eid("ad_version"),
90
+ )
91
+
92
+ infotext_fields.append((ad_enable, AD_ENABLE.name))
93
+
94
+ with gr.Group(), gr.Tabs():
95
+ for n in range(num_models):
96
+ with gr.Tab(ordinal(n + 1)):
97
+ state, infofields = one_ui_group(
98
+ n=n,
99
+ is_img2img=is_img2img,
100
+ model_list=model_list,
101
+ samplers=samplers,
102
+ t2i_button=t2i_button,
103
+ i2i_button=i2i_button,
104
+ )
105
+
106
+ states.append(state)
107
+ infotext_fields.extend(infofields)
108
+
109
+ # components: [bool, dict, dict, ...]
110
+ components = [ad_enable, *states]
111
+ return components, infotext_fields
112
+
113
+
114
+ def one_ui_group(
115
+ n: int,
116
+ is_img2img: bool,
117
+ model_list: list[str],
118
+ samplers: list[str],
119
+ t2i_button: gr.Button,
120
+ i2i_button: gr.Button,
121
+ ):
122
+ w = Widgets()
123
+ state = gr.State({})
124
+ eid = partial(elem_id, n=n, is_img2img=is_img2img)
125
+
126
+ with gr.Row():
127
+ model_choices = [*model_list, "None"] if n == 0 else ["None", *model_list]
128
+
129
+ w.ad_model = gr.Dropdown(
130
+ label="ADetailer model" + suffix(n),
131
+ choices=model_choices,
132
+ value=model_choices[0],
133
+ visible=True,
134
+ type="value",
135
+ elem_id=eid("ad_model"),
136
+ )
137
+
138
+ with gr.Group():
139
+ with gr.Row(elem_id=eid("ad_toprow_prompt")):
140
+ w.ad_prompt = gr.Textbox(
141
+ label="ad_prompt" + suffix(n),
142
+ show_label=False,
143
+ lines=3,
144
+ placeholder="ADetailer prompt"
145
+ + suffix(n)
146
+ + "\nIf blank, the main prompt is used.",
147
+ elem_id=eid("ad_prompt"),
148
+ )
149
+
150
+ with gr.Row(elem_id=eid("ad_toprow_negative_prompt")):
151
+ w.ad_negative_prompt = gr.Textbox(
152
+ label="ad_negative_prompt" + suffix(n),
153
+ show_label=False,
154
+ lines=2,
155
+ placeholder="ADetailer negative prompt"
156
+ + suffix(n)
157
+ + "\nIf blank, the main negative prompt is used.",
158
+ elem_id=eid("ad_negative_prompt"),
159
+ )
160
+
161
+ with gr.Group():
162
+ with gr.Accordion(
163
+ "Detection", open=False, elem_id=eid("ad_detection_accordion")
164
+ ):
165
+ detection(w, n, is_img2img)
166
+
167
+ with gr.Accordion(
168
+ "Mask Preprocessing",
169
+ open=False,
170
+ elem_id=eid("ad_mask_preprocessing_accordion"),
171
+ ):
172
+ mask_preprocessing(w, n, is_img2img)
173
+
174
+ with gr.Accordion(
175
+ "Inpainting", open=False, elem_id=eid("ad_inpainting_accordion")
176
+ ):
177
+ inpainting(w, n, is_img2img, samplers)
178
+
179
+ with gr.Group():
180
+ controlnet(w, n, is_img2img)
181
+
182
+ all_inputs = [state, *w.tolist()]
183
+ target_button = i2i_button if is_img2img else t2i_button
184
+ target_button.click(
185
+ fn=on_generate_click, inputs=all_inputs, outputs=state, queue=False
186
+ )
187
+
188
+ infotext_fields = [(getattr(w, attr), name + suffix(n)) for attr, name in ALL_ARGS]
189
+
190
+ return state, infotext_fields
191
+
192
+
193
+ def detection(w: Widgets, n: int, is_img2img: bool):
194
+ eid = partial(elem_id, n=n, is_img2img=is_img2img)
195
+
196
+ with gr.Row():
197
+ with gr.Column():
198
+ w.ad_confidence = gr.Slider(
199
+ label="Detection model confidence threshold" + suffix(n),
200
+ minimum=0.0,
201
+ maximum=1.0,
202
+ step=0.01,
203
+ value=0.3,
204
+ visible=True,
205
+ elem_id=eid("ad_confidence"),
206
+ )
207
+
208
+ with gr.Column(variant="compact"):
209
+ w.ad_mask_min_ratio = gr.Slider(
210
+ label="Mask min area ratio" + suffix(n),
211
+ minimum=0.0,
212
+ maximum=1.0,
213
+ step=0.001,
214
+ value=0.0,
215
+ visible=True,
216
+ elem_id=eid("ad_mask_min_ratio"),
217
+ )
218
+ w.ad_mask_max_ratio = gr.Slider(
219
+ label="Mask max area ratio" + suffix(n),
220
+ minimum=0.0,
221
+ maximum=1.0,
222
+ step=0.001,
223
+ value=1.0,
224
+ visible=True,
225
+ elem_id=eid("ad_mask_max_ratio"),
226
+ )
227
+
228
+
229
+ def mask_preprocessing(w: Widgets, n: int, is_img2img: bool):
230
+ eid = partial(elem_id, n=n, is_img2img=is_img2img)
231
+
232
+ with gr.Group():
233
+ with gr.Row():
234
+ with gr.Column(variant="compact"):
235
+ w.ad_x_offset = gr.Slider(
236
+ label="Mask x(→) offset" + suffix(n),
237
+ minimum=-200,
238
+ maximum=200,
239
+ step=1,
240
+ value=0,
241
+ visible=True,
242
+ elem_id=eid("ad_x_offset"),
243
+ )
244
+ w.ad_y_offset = gr.Slider(
245
+ label="Mask y(↑) offset" + suffix(n),
246
+ minimum=-200,
247
+ maximum=200,
248
+ step=1,
249
+ value=0,
250
+ visible=True,
251
+ elem_id=eid("ad_y_offset"),
252
+ )
253
+
254
+ with gr.Column(variant="compact"):
255
+ w.ad_dilate_erode = gr.Slider(
256
+ label="Mask erosion (-) / dilation (+)" + suffix(n),
257
+ minimum=-128,
258
+ maximum=128,
259
+ step=4,
260
+ value=4,
261
+ visible=True,
262
+ elem_id=eid("ad_dilate_erode"),
263
+ )
264
+
265
+ with gr.Row():
266
+ w.ad_mask_merge_invert = gr.Radio(
267
+ label="Mask merge mode" + suffix(n),
268
+ choices=MASK_MERGE_INVERT,
269
+ value="None",
270
+ elem_id=eid("ad_mask_merge_invert"),
271
+ )
272
+
273
+
274
+ def inpainting(w: Widgets, n: int, is_img2img: bool, samplers: list[str]):
275
+ eid = partial(elem_id, n=n, is_img2img=is_img2img)
276
+
277
+ with gr.Group():
278
+ with gr.Row():
279
+ w.ad_mask_blur = gr.Slider(
280
+ label="Inpaint mask blur" + suffix(n),
281
+ minimum=0,
282
+ maximum=64,
283
+ step=1,
284
+ value=4,
285
+ visible=True,
286
+ elem_id=eid("ad_mask_blur"),
287
+ )
288
+
289
+ w.ad_denoising_strength = gr.Slider(
290
+ label="Inpaint denoising strength" + suffix(n),
291
+ minimum=0.0,
292
+ maximum=1.0,
293
+ step=0.01,
294
+ value=0.4,
295
+ visible=True,
296
+ elem_id=eid("ad_denoising_strength"),
297
+ )
298
+
299
+ with gr.Row():
300
+ with gr.Column(variant="compact"):
301
+ w.ad_inpaint_only_masked = gr.Checkbox(
302
+ label="Inpaint only masked" + suffix(n),
303
+ value=True,
304
+ visible=True,
305
+ elem_id=eid("ad_inpaint_only_masked"),
306
+ )
307
+ w.ad_inpaint_only_masked_padding = gr.Slider(
308
+ label="Inpaint only masked padding, pixels" + suffix(n),
309
+ minimum=0,
310
+ maximum=256,
311
+ step=4,
312
+ value=32,
313
+ visible=True,
314
+ elem_id=eid("ad_inpaint_only_masked_padding"),
315
+ )
316
+
317
+ w.ad_inpaint_only_masked.change(
318
+ gr_interactive,
319
+ inputs=w.ad_inpaint_only_masked,
320
+ outputs=w.ad_inpaint_only_masked_padding,
321
+ queue=False,
322
+ )
323
+
324
+ with gr.Column(variant="compact"):
325
+ w.ad_use_inpaint_width_height = gr.Checkbox(
326
+ label="Use separate width/height" + suffix(n),
327
+ value=False,
328
+ visible=True,
329
+ elem_id=eid("ad_use_inpaint_width_height"),
330
+ )
331
+
332
+ w.ad_inpaint_width = gr.Slider(
333
+ label="inpaint width" + suffix(n),
334
+ minimum=64,
335
+ maximum=2048,
336
+ step=4,
337
+ value=512,
338
+ visible=True,
339
+ elem_id=eid("ad_inpaint_width"),
340
+ )
341
+
342
+ w.ad_inpaint_height = gr.Slider(
343
+ label="inpaint height" + suffix(n),
344
+ minimum=64,
345
+ maximum=2048,
346
+ step=4,
347
+ value=512,
348
+ visible=True,
349
+ elem_id=eid("ad_inpaint_height"),
350
+ )
351
+
352
+ w.ad_use_inpaint_width_height.change(
353
+ lambda value: (gr_interactive(value), gr_interactive(value)),
354
+ inputs=w.ad_use_inpaint_width_height,
355
+ outputs=[w.ad_inpaint_width, w.ad_inpaint_height],
356
+ queue=False,
357
+ )
358
+
359
+ with gr.Row():
360
+ with gr.Column(variant="compact"):
361
+ w.ad_use_steps = gr.Checkbox(
362
+ label="Use separate steps" + suffix(n),
363
+ value=False,
364
+ visible=True,
365
+ elem_id=eid("ad_use_steps"),
366
+ )
367
+
368
+ w.ad_steps = gr.Slider(
369
+ label="ADetailer steps" + suffix(n),
370
+ minimum=1,
371
+ maximum=150,
372
+ step=1,
373
+ value=28,
374
+ visible=True,
375
+ elem_id=eid("ad_steps"),
376
+ )
377
+
378
+ w.ad_use_steps.change(
379
+ gr_interactive,
380
+ inputs=w.ad_use_steps,
381
+ outputs=w.ad_steps,
382
+ queue=False,
383
+ )
384
+
385
+ with gr.Column(variant="compact"):
386
+ w.ad_use_cfg_scale = gr.Checkbox(
387
+ label="Use separate CFG scale" + suffix(n),
388
+ value=False,
389
+ visible=True,
390
+ elem_id=eid("ad_use_cfg_scale"),
391
+ )
392
+
393
+ w.ad_cfg_scale = gr.Slider(
394
+ label="ADetailer CFG scale" + suffix(n),
395
+ minimum=0.0,
396
+ maximum=30.0,
397
+ step=0.5,
398
+ value=7.0,
399
+ visible=True,
400
+ elem_id=eid("ad_cfg_scale"),
401
+ )
402
+
403
+ w.ad_use_cfg_scale.change(
404
+ gr_interactive,
405
+ inputs=w.ad_use_cfg_scale,
406
+ outputs=w.ad_cfg_scale,
407
+ queue=False,
408
+ )
409
+
410
+ with gr.Row():
411
+ with gr.Column(variant="compact"):
412
+ w.ad_use_sampler = gr.Checkbox(
413
+ label="Use separate sampler" + suffix(n),
414
+ value=False,
415
+ visible=True,
416
+ elem_id=eid("ad_use_sampler"),
417
+ )
418
+
419
+ w.ad_sampler = gr.Dropdown(
420
+ label="ADetailer sampler" + suffix(n),
421
+ choices=samplers,
422
+ value=samplers[0],
423
+ visible=True,
424
+ elem_id=eid("ad_sampler"),
425
+ )
426
+
427
+ w.ad_use_sampler.change(
428
+ gr_interactive,
429
+ inputs=w.ad_use_sampler,
430
+ outputs=w.ad_sampler,
431
+ queue=False,
432
+ )
433
+
434
+ with gr.Column(variant="compact"):
435
+ w.ad_use_noise_multiplier = gr.Checkbox(
436
+ label="Use separate noise multiplier" + suffix(n),
437
+ value=False,
438
+ visible=True,
439
+ elem_id=eid("ad_use_noise_multiplier"),
440
+ )
441
+
442
+ w.ad_noise_multiplier = gr.Slider(
443
+ label="Noise multiplier for img2img" + suffix(n),
444
+ minimum=0.5,
445
+ maximum=1.5,
446
+ step=0.01,
447
+ value=1.0,
448
+ visible=True,
449
+ elem_id=eid("ad_noise_multiplier"),
450
+ )
451
+
452
+ w.ad_use_noise_multiplier.change(
453
+ gr_interactive,
454
+ inputs=w.ad_use_noise_multiplier,
455
+ outputs=w.ad_noise_multiplier,
456
+ queue=False,
457
+ )
458
+
459
+ with gr.Row():
460
+ with gr.Column(variant="compact"):
461
+ w.ad_use_clip_skip = gr.Checkbox(
462
+ label="Use separate CLIP skip" + suffix(n),
463
+ value=False,
464
+ visible=True,
465
+ elem_id=eid("ad_use_clip_skip"),
466
+ )
467
+
468
+ w.ad_clip_skip = gr.Slider(
469
+ label="ADetailer CLIP skip" + suffix(n),
470
+ minimum=1,
471
+ maximum=12,
472
+ step=1,
473
+ value=1,
474
+ visible=True,
475
+ elem_id=eid("ad_clip_skip"),
476
+ )
477
+
478
+ w.ad_use_clip_skip.change(
479
+ gr_interactive,
480
+ inputs=w.ad_use_clip_skip,
481
+ outputs=w.ad_clip_skip,
482
+ queue=False,
483
+ )
484
+
485
+ with gr.Column(variant="compact"):
486
+ w.ad_restore_face = gr.Checkbox(
487
+ label="Restore faces after ADetailer" + suffix(n),
488
+ value=False,
489
+ elem_id=eid("ad_restore_face"),
490
+ )
491
+
492
+
493
+ def controlnet(w: Widgets, n: int, is_img2img: bool):
494
+ eid = partial(elem_id, n=n, is_img2img=is_img2img)
495
+ cn_models = ["None", *get_cn_models()]
496
+
497
+ with gr.Row(variant="panel"):
498
+ with gr.Column(variant="compact"):
499
+ w.ad_controlnet_model = gr.Dropdown(
500
+ label="ControlNet model" + suffix(n),
501
+ choices=cn_models,
502
+ value="None",
503
+ visible=True,
504
+ type="value",
505
+ interactive=controlnet_exists,
506
+ elem_id=eid("ad_controlnet_model"),
507
+ )
508
+
509
+ w.ad_controlnet_module = gr.Dropdown(
510
+ label="ControlNet module" + suffix(n),
511
+ choices=cn_module_choices,
512
+ value="inpaint_global_harmonious",
513
+ visible=False,
514
+ type="value",
515
+ interactive=controlnet_exists,
516
+ elem_id=eid("ad_controlnet_module"),
517
+ )
518
+
519
+ w.ad_controlnet_weight = gr.Slider(
520
+ label="ControlNet weight" + suffix(n),
521
+ minimum=0.0,
522
+ maximum=1.0,
523
+ step=0.01,
524
+ value=1.0,
525
+ visible=True,
526
+ interactive=controlnet_exists,
527
+ elem_id=eid("ad_controlnet_weight"),
528
+ )
529
+
530
+ w.ad_controlnet_model.change(
531
+ on_cn_model_update,
532
+ inputs=w.ad_controlnet_model,
533
+ outputs=w.ad_controlnet_module,
534
+ queue=False,
535
+ )
536
+
537
+ with gr.Column(variant="compact"):
538
+ w.ad_controlnet_guidance_start = gr.Slider(
539
+ label="ControlNet guidance start" + suffix(n),
540
+ minimum=0.0,
541
+ maximum=1.0,
542
+ step=0.01,
543
+ value=0.0,
544
+ visible=True,
545
+ interactive=controlnet_exists,
546
+ elem_id=eid("ad_controlnet_guidance_start"),
547
+ )
548
+
549
+ w.ad_controlnet_guidance_end = gr.Slider(
550
+ label="ControlNet guidance end" + suffix(n),
551
+ minimum=0.0,
552
+ maximum=1.0,
553
+ step=0.01,
554
+ value=1.0,
555
+ visible=True,
556
+ interactive=controlnet_exists,
557
+ elem_id=eid("ad_controlnet_guidance_end"),
558
+ )
adetailer/adetailer/ultralytics.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+
5
+ import cv2
6
+ from PIL import Image
7
+ from torchvision.transforms.functional import to_pil_image
8
+ from ultralytics import YOLO
9
+
10
+ from adetailer import PredictOutput
11
+ from adetailer.common import create_mask_from_bbox
12
+
13
+
14
+ def ultralytics_predict(
15
+ model_path: str | Path,
16
+ image: Image.Image,
17
+ confidence: float = 0.3,
18
+ device: str = "",
19
+ ) -> PredictOutput:
20
+ model = YOLO(model_path)
21
+ pred = model(image, conf=confidence, device=device)
22
+
23
+ bboxes = pred[0].boxes.xyxy.cpu().numpy()
24
+ if bboxes.size == 0:
25
+ return PredictOutput()
26
+ bboxes = bboxes.tolist()
27
+
28
+ if pred[0].masks is None:
29
+ masks = create_mask_from_bbox(bboxes, image.size)
30
+ else:
31
+ masks = mask_to_pil(pred[0].masks.data, image.size)
32
+ preview = pred[0].plot()
33
+ preview = cv2.cvtColor(preview, cv2.COLOR_BGR2RGB)
34
+ preview = Image.fromarray(preview)
35
+
36
+ return PredictOutput(bboxes=bboxes, masks=masks, preview=preview)
37
+
38
+
39
+ def mask_to_pil(masks, shape: tuple[int, int]) -> list[Image.Image]:
40
+ """
41
+ Parameters
42
+ ----------
43
+ masks: torch.Tensor, dtype=torch.float32, shape=(N, H, W).
44
+ The device can be CUDA, but `to_pil_image` takes care of that.
45
+
46
+ shape: tuple[int, int]
47
+ (width, height) of the original image
48
+ """
49
+ n = masks.shape[0]
50
+ return [to_pil_image(masks[i], mode="L").resize(shape) for i in range(n)]
adetailer/controlnet_ext/__init__.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from .controlnet_ext import ControlNetExt, controlnet_exists, get_cn_models
2
+
3
+ __all__ = [
4
+ "ControlNetExt",
5
+ "controlnet_exists",
6
+ "get_cn_models",
7
+ ]
adetailer/controlnet_ext/controlnet_ext.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import importlib
4
+ import re
5
+ from functools import lru_cache
6
+ from pathlib import Path
7
+
8
+ from modules import extensions, sd_models, shared
9
+ from modules.paths import data_path, models_path, script_path
10
+
11
+ ext_path = Path(data_path, "extensions")
12
+ ext_builtin_path = Path(script_path, "extensions-builtin")
13
+ controlnet_exists = False
14
+ controlnet_path = None
15
+ cn_base_path = ""
16
+
17
+ for extension in extensions.active():
18
+ if not extension.enabled:
19
+ continue
20
+ # For cases like sd-webui-controlnet-master
21
+ if "sd-webui-controlnet" in extension.name:
22
+ controlnet_exists = True
23
+ controlnet_path = Path(extension.path)
24
+ cn_base_path = ".".join(controlnet_path.parts[-2:])
25
+ break
26
+
27
+ cn_model_module = {
28
+ "inpaint": "inpaint_global_harmonious",
29
+ "scribble": "t2ia_sketch_pidi",
30
+ "lineart": "lineart_coarse",
31
+ "openpose": "openpose_full",
32
+ "tile": None,
33
+ }
34
+ cn_model_regex = re.compile("|".join(cn_model_module.keys()))
35
+
36
+
37
+ class ControlNetExt:
38
+ def __init__(self):
39
+ self.cn_models = ["None"]
40
+ self.cn_available = False
41
+ self.external_cn = None
42
+
43
+ def init_controlnet(self):
44
+ import_path = cn_base_path + ".scripts.external_code"
45
+
46
+ self.external_cn = importlib.import_module(import_path, "external_code")
47
+ self.cn_available = True
48
+ models = self.external_cn.get_models()
49
+ self.cn_models.extend(m for m in models if cn_model_regex.search(m))
50
+
51
+ def update_scripts_args(
52
+ self,
53
+ p,
54
+ model: str,
55
+ module: str | None,
56
+ weight: float,
57
+ guidance_start: float,
58
+ guidance_end: float,
59
+ ):
60
+ if (not self.cn_available) or model == "None":
61
+ return
62
+
63
+ if module is None:
64
+ for m, v in cn_model_module.items():
65
+ if m in model:
66
+ module = v
67
+ break
68
+
69
+ cn_units = [
70
+ self.external_cn.ControlNetUnit(
71
+ model=model,
72
+ weight=weight,
73
+ control_mode=self.external_cn.ControlMode.BALANCED,
74
+ module=module,
75
+ guidance_start=guidance_start,
76
+ guidance_end=guidance_end,
77
+ pixel_perfect=True,
78
+ )
79
+ ]
80
+
81
+ self.external_cn.update_cn_script_in_processing(p, cn_units)
82
+
83
+
84
+ def get_cn_model_dirs() -> list[Path]:
85
+ cn_model_dir = Path(models_path, "ControlNet")
86
+ if controlnet_path is not None:
87
+ cn_model_dir_old = controlnet_path.joinpath("models")
88
+ else:
89
+ cn_model_dir_old = None
90
+ ext_dir1 = shared.opts.data.get("control_net_models_path", "")
91
+ ext_dir2 = getattr(shared.cmd_opts, "controlnet_dir", "")
92
+
93
+ dirs = [cn_model_dir]
94
+ for ext_dir in [cn_model_dir_old, ext_dir1, ext_dir2]:
95
+ if ext_dir:
96
+ dirs.append(Path(ext_dir))
97
+
98
+ return dirs
99
+
100
+
101
+ @lru_cache
102
+ def _get_cn_models() -> list[str]:
103
+ """
104
+ Since we can't import ControlNet, we use a function that does something like
105
+ controlnet's `list(global_state.cn_models_names.values())`.
106
+ """
107
+ cn_model_exts = (".pt", ".pth", ".ckpt", ".safetensors")
108
+ dirs = get_cn_model_dirs()
109
+ name_filter = shared.opts.data.get("control_net_models_name_filter", "")
110
+ name_filter = name_filter.strip(" ").lower()
111
+
112
+ model_paths = []
113
+
114
+ for base in dirs:
115
+ if not base.exists():
116
+ continue
117
+
118
+ for p in base.rglob("*"):
119
+ if (
120
+ p.is_file()
121
+ and p.suffix in cn_model_exts
122
+ and cn_model_regex.search(p.name)
123
+ ):
124
+ if name_filter and name_filter not in p.name.lower():
125
+ continue
126
+ model_paths.append(p)
127
+ model_paths.sort(key=lambda p: p.name)
128
+
129
+ models = []
130
+ for p in model_paths:
131
+ model_hash = sd_models.model_hash(p)
132
+ name = f"{p.stem} [{model_hash}]"
133
+ models.append(name)
134
+ return models
135
+
136
+
137
+ def get_cn_models() -> list[str]:
138
+ if controlnet_exists:
139
+ return _get_cn_models()
140
+ return []
adetailer/controlnet_ext/restore.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from contextlib import contextmanager
4
+
5
+ from modules import img2img, processing, shared
6
+
7
+
8
+ class CNHijackRestore:
9
+ def __init__(self):
10
+ self.process = hasattr(processing, "__controlnet_original_process_images_inner")
11
+ self.img2img = hasattr(img2img, "__controlnet_original_process_batch")
12
+
13
+ def __enter__(self):
14
+ if self.process:
15
+ self.orig_process = processing.process_images_inner
16
+ processing.process_images_inner = getattr(
17
+ processing, "__controlnet_original_process_images_inner"
18
+ )
19
+ if self.img2img:
20
+ self.orig_img2img = img2img.process_batch
21
+ img2img.process_batch = getattr(
22
+ img2img, "__controlnet_original_process_batch"
23
+ )
24
+
25
+ def __exit__(self, *args, **kwargs):
26
+ if self.process:
27
+ processing.process_images_inner = self.orig_process
28
+ if self.img2img:
29
+ img2img.process_batch = self.orig_img2img
30
+
31
+
32
+ @contextmanager
33
+ def cn_allow_script_control():
34
+ orig = False
35
+ if "control_net_allow_script_control" in shared.opts.data:
36
+ try:
37
+ orig = shared.opts.data["control_net_allow_script_control"]
38
+ shared.opts.data["control_net_allow_script_control"] = True
39
+ yield
40
+ finally:
41
+ shared.opts.data["control_net_allow_script_control"] = orig
42
+ else:
43
+ yield
adetailer/install.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import importlib.util
4
+ import subprocess
5
+ import sys
6
+ from importlib.metadata import version # python >= 3.8
7
+
8
+ from packaging.version import parse
9
+
10
+ import_name = {"py-cpuinfo": "cpuinfo", "protobuf": "google.protobuf"}
11
+
12
+
13
+ def is_installed(
14
+ package: str, min_version: str | None = None, max_version: str | None = None
15
+ ):
16
+ name = import_name.get(package, package)
17
+ try:
18
+ spec = importlib.util.find_spec(name)
19
+ except ModuleNotFoundError:
20
+ return False
21
+
22
+ if spec is None:
23
+ return False
24
+
25
+ if not min_version and not max_version:
26
+ return True
27
+
28
+ if not min_version:
29
+ min_version = "0.0.0"
30
+ if not max_version:
31
+ max_version = "99999999.99999999.99999999"
32
+
33
+ try:
34
+ pkg_version = version(package)
35
+ return parse(min_version) <= parse(pkg_version) <= parse(max_version)
36
+ except Exception:
37
+ return False
38
+
39
+
40
+ def run_pip(*args):
41
+ subprocess.run([sys.executable, "-m", "pip", "install", *args])
42
+
43
+
44
+ def install():
45
+ deps = [
46
+ # requirements
47
+ ("ultralytics", "8.0.145", None),
48
+ ("mediapipe", "0.10.2", None),
49
+ ("rich", "13.4.2", None),
50
+ # ultralytics
51
+ ("py-cpuinfo", None, None),
52
+ # mediapipe
53
+ ("protobuf", "3.20", "3.9999"),
54
+ ]
55
+
56
+ for pkg, low, high in deps:
57
+ if not is_installed(pkg, low, high):
58
+ if low and high:
59
+ cmd = f"{pkg}>={low},<={high}"
60
+ elif low:
61
+ cmd = f"{pkg}>={low}"
62
+ elif high:
63
+ cmd = f"{pkg}<={high}"
64
+ else:
65
+ cmd = pkg
66
+
67
+ run_pip("-U", cmd)
68
+
69
+
70
+ try:
71
+ import launch
72
+
73
+ skip_install = launch.args.skip_install
74
+ except Exception:
75
+ skip_install = False
76
+
77
+ if not skip_install:
78
+ install()
adetailer/preload.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+
3
+
4
+ def preload(parser: argparse.ArgumentParser):
5
+ parser.add_argument(
6
+ "--ad-no-huggingface",
7
+ action="store_true",
8
+ help="Don't use adetailer models from huggingface",
9
+ )
adetailer/pyproject.toml ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "adetailer"
3
+ description = "An object detection and auto-mask extension for stable diffusion webui."
4
+ authors = [
5
+ {name = "dowon", email = "ks2515@naver.com"},
6
+ ]
7
+ requires-python = ">=3.8,<3.12"
8
+ readme = "README.md"
9
+ license = {text = "AGPL-3.0"}
10
+
11
+ [project.urls]
12
+ repository = "https://github.com/Bing-su/adetailer"
13
+
14
+ [tool.isort]
15
+ profile = "black"
16
+ known_first_party = ["launch", "modules"]
17
+
18
+ [tool.ruff]
19
+ select = ["A", "B", "C4", "C90", "E", "EM", "F", "FA", "I001", "ISC", "N", "PIE", "PT", "RET", "RUF", "SIM", "UP", "W"]
20
+ ignore = ["B008", "B905", "E501", "F401", "UP007"]
21
+
22
+ [tool.ruff.isort]
23
+ known-first-party = ["launch", "modules"]
24
+
25
+ [tool.ruff.per-file-ignores]
26
+ "sd_webui/*.py" = ["B027", "F403"]
adetailer/scripts/!adetailer.py ADDED
@@ -0,0 +1,808 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import platform
5
+ import re
6
+ import sys
7
+ import traceback
8
+ from contextlib import contextmanager
9
+ from copy import copy, deepcopy
10
+ from functools import partial
11
+ from pathlib import Path
12
+ from textwrap import dedent
13
+ from typing import Any
14
+
15
+ import gradio as gr
16
+ import torch
17
+ from rich import print
18
+
19
+ import modules
20
+ from adetailer import (
21
+ AFTER_DETAILER,
22
+ __version__,
23
+ get_models,
24
+ mediapipe_predict,
25
+ ultralytics_predict,
26
+ )
27
+ from adetailer.args import ALL_ARGS, BBOX_SORTBY, ADetailerArgs, EnableChecker
28
+ from adetailer.common import PredictOutput
29
+ from adetailer.mask import filter_by_ratio, mask_preprocess, sort_bboxes
30
+ from adetailer.traceback import rich_traceback
31
+ from adetailer.ui import adui, ordinal, suffix
32
+ from controlnet_ext import ControlNetExt, controlnet_exists, get_cn_models
33
+ from controlnet_ext.restore import (
34
+ CNHijackRestore,
35
+ cn_allow_script_control,
36
+ )
37
+ from sd_webui import images, safe, script_callbacks, scripts, shared
38
+ from sd_webui.devices import NansException
39
+ from sd_webui.paths import data_path, models_path
40
+ from sd_webui.processing import (
41
+ Processed,
42
+ StableDiffusionProcessingImg2Img,
43
+ create_infotext,
44
+ process_images,
45
+ )
46
+ from sd_webui.sd_samplers import all_samplers
47
+ from sd_webui.shared import cmd_opts, opts, state
48
+
49
+ no_huggingface = getattr(cmd_opts, "ad_no_huggingface", False)
50
+ adetailer_dir = Path(models_path, "adetailer")
51
+ model_mapping = get_models(adetailer_dir, huggingface=not no_huggingface)
52
+ txt2img_submit_button = img2img_submit_button = None
53
+ SCRIPT_DEFAULT = "dynamic_prompting,dynamic_thresholding,wildcard_recursive,wildcards,lora_block_weight"
54
+
55
+ if (
56
+ not adetailer_dir.exists()
57
+ and adetailer_dir.parent.exists()
58
+ and os.access(adetailer_dir.parent, os.W_OK)
59
+ ):
60
+ adetailer_dir.mkdir()
61
+
62
+ print(
63
+ f"[-] ADetailer initialized. version: {__version__}, num models: {len(model_mapping)}"
64
+ )
65
+
66
+
67
+ @contextmanager
68
+ def change_torch_load():
69
+ orig = torch.load
70
+ try:
71
+ torch.load = safe.unsafe_torch_load
72
+ yield
73
+ finally:
74
+ torch.load = orig
75
+
76
+
77
+ @contextmanager
78
+ def pause_total_tqdm():
79
+ orig = opts.data.get("multiple_tqdm", True)
80
+ try:
81
+ opts.data["multiple_tqdm"] = False
82
+ yield
83
+ finally:
84
+ opts.data["multiple_tqdm"] = orig
85
+
86
+
87
+ @contextmanager
88
+ def preseve_prompts(p):
89
+ all_pt = copy(p.all_prompts)
90
+ all_ng = copy(p.all_negative_prompts)
91
+ try:
92
+ yield
93
+ finally:
94
+ p.all_prompts = all_pt
95
+ p.all_negative_prompts = all_ng
96
+
97
+
98
+ class AfterDetailerScript(scripts.Script):
99
+ def __init__(self):
100
+ super().__init__()
101
+ self.ultralytics_device = self.get_ultralytics_device()
102
+
103
+ self.controlnet_ext = None
104
+
105
+ def __repr__(self):
106
+ return f"{self.__class__.__name__}(version={__version__})"
107
+
108
+ def title(self):
109
+ return AFTER_DETAILER
110
+
111
+ def show(self, is_img2img):
112
+ return scripts.AlwaysVisible
113
+
114
+ def ui(self, is_img2img):
115
+ num_models = opts.data.get("ad_max_models", 2)
116
+ model_list = list(model_mapping.keys())
117
+ samplers = [sampler.name for sampler in all_samplers]
118
+
119
+ components, infotext_fields = adui(
120
+ num_models,
121
+ is_img2img,
122
+ model_list,
123
+ samplers,
124
+ txt2img_submit_button,
125
+ img2img_submit_button,
126
+ )
127
+
128
+ self.infotext_fields = infotext_fields
129
+ return components
130
+
131
+ def init_controlnet_ext(self) -> None:
132
+ if self.controlnet_ext is not None:
133
+ return
134
+ self.controlnet_ext = ControlNetExt()
135
+
136
+ if controlnet_exists:
137
+ try:
138
+ self.controlnet_ext.init_controlnet()
139
+ except ImportError:
140
+ error = traceback.format_exc()
141
+ print(
142
+ f"[-] ADetailer: ControlNetExt init failed:\n{error}",
143
+ file=sys.stderr,
144
+ )
145
+
146
+ def update_controlnet_args(self, p, args: ADetailerArgs) -> None:
147
+ if self.controlnet_ext is None:
148
+ self.init_controlnet_ext()
149
+
150
+ if (
151
+ self.controlnet_ext is not None
152
+ and self.controlnet_ext.cn_available
153
+ and args.ad_controlnet_model != "None"
154
+ ):
155
+ self.controlnet_ext.update_scripts_args(
156
+ p,
157
+ model=args.ad_controlnet_model,
158
+ module=args.ad_controlnet_module,
159
+ weight=args.ad_controlnet_weight,
160
+ guidance_start=args.ad_controlnet_guidance_start,
161
+ guidance_end=args.ad_controlnet_guidance_end,
162
+ )
163
+
164
+ def is_ad_enabled(self, *args_) -> bool:
165
+ arg_list = [arg for arg in args_ if isinstance(arg, dict)]
166
+ if not args_ or not arg_list or not isinstance(args_[0], (bool, dict)):
167
+ message = f"""
168
+ [-] ADetailer: Invalid arguments passed to ADetailer.
169
+ input: {args_!r}
170
+ """
171
+ raise ValueError(dedent(message))
172
+ enable = args_[0] if isinstance(args_[0], bool) else True
173
+ checker = EnableChecker(enable=enable, arg_list=arg_list)
174
+ return checker.is_enabled()
175
+
176
+ def get_args(self, p, *args_) -> list[ADetailerArgs]:
177
+ """
178
+ `args_` is at least 1 in length by `is_ad_enabled` immediately above
179
+ """
180
+ args = [arg for arg in args_ if isinstance(arg, dict)]
181
+
182
+ if not args:
183
+ message = f"[-] ADetailer: Invalid arguments passed to ADetailer: {args_!r}"
184
+ raise ValueError(message)
185
+
186
+ if hasattr(p, "adetailer_xyz"):
187
+ args[0].update(p.adetailer_xyz)
188
+
189
+ all_inputs = []
190
+
191
+ for n, arg_dict in enumerate(args, 1):
192
+ try:
193
+ inp = ADetailerArgs(**arg_dict)
194
+ except ValueError as e:
195
+ msgs = [
196
+ f"[-] ADetailer: ValidationError when validating {ordinal(n)} arguments: {e}\n"
197
+ ]
198
+ for attr in ALL_ARGS.attrs:
199
+ arg = arg_dict.get(attr)
200
+ dtype = type(arg)
201
+ arg = "DEFAULT" if arg is None else repr(arg)
202
+ msgs.append(f" {attr}: {arg} ({dtype})")
203
+ raise ValueError("\n".join(msgs)) from e
204
+
205
+ all_inputs.append(inp)
206
+
207
+ return all_inputs
208
+
209
+ def extra_params(self, arg_list: list[ADetailerArgs]) -> dict:
210
+ params = {}
211
+ for n, args in enumerate(arg_list):
212
+ params.update(args.extra_params(suffix=suffix(n)))
213
+ params["ADetailer version"] = __version__
214
+ return params
215
+
216
+ @staticmethod
217
+ def get_ultralytics_device() -> str:
218
+ if "adetailer" in shared.cmd_opts.use_cpu:
219
+ return "cpu"
220
+
221
+ if platform.system() == "Darwin":
222
+ return ""
223
+
224
+ if any(getattr(cmd_opts, vram, False) for vram in ["lowvram", "medvram"]):
225
+ return "cpu"
226
+
227
+ return ""
228
+
229
+ def prompt_blank_replacement(
230
+ self, all_prompts: list[str], i: int, default: str
231
+ ) -> str:
232
+ if not all_prompts:
233
+ return default
234
+ if i < len(all_prompts):
235
+ return all_prompts[i]
236
+ j = i % len(all_prompts)
237
+ return all_prompts[j]
238
+
239
+ def _get_prompt(
240
+ self, ad_prompt: str, all_prompts: list[str], i: int, default: str
241
+ ) -> list[str]:
242
+ prompts = re.split(r"\s*\[SEP\]\s*", ad_prompt)
243
+ blank_replacement = self.prompt_blank_replacement(all_prompts, i, default)
244
+ for n in range(len(prompts)):
245
+ if not prompts[n]:
246
+ prompts[n] = blank_replacement
247
+ return prompts
248
+
249
+ def get_prompt(self, p, args: ADetailerArgs) -> tuple[list[str], list[str]]:
250
+ i = p._ad_idx
251
+
252
+ prompt = self._get_prompt(args.ad_prompt, p.all_prompts, i, p.prompt)
253
+ negative_prompt = self._get_prompt(
254
+ args.ad_negative_prompt, p.all_negative_prompts, i, p.negative_prompt
255
+ )
256
+
257
+ return prompt, negative_prompt
258
+
259
+ def get_seed(self, p) -> tuple[int, int]:
260
+ i = p._ad_idx
261
+
262
+ if not p.all_seeds:
263
+ seed = p.seed
264
+ elif i < len(p.all_seeds):
265
+ seed = p.all_seeds[i]
266
+ else:
267
+ j = i % len(p.all_seeds)
268
+ seed = p.all_seeds[j]
269
+
270
+ if not p.all_subseeds:
271
+ subseed = p.subseed
272
+ elif i < len(p.all_subseeds):
273
+ subseed = p.all_subseeds[i]
274
+ else:
275
+ j = i % len(p.all_subseeds)
276
+ subseed = p.all_subseeds[j]
277
+
278
+ return seed, subseed
279
+
280
+ def get_width_height(self, p, args: ADetailerArgs) -> tuple[int, int]:
281
+ if args.ad_use_inpaint_width_height:
282
+ width = args.ad_inpaint_width
283
+ height = args.ad_inpaint_height
284
+ else:
285
+ width = p.width
286
+ height = p.height
287
+
288
+ return width, height
289
+
290
+ def get_steps(self, p, args: ADetailerArgs) -> int:
291
+ if args.ad_use_steps:
292
+ return args.ad_steps
293
+ return p.steps
294
+
295
+ def get_cfg_scale(self, p, args: ADetailerArgs) -> float:
296
+ if args.ad_use_cfg_scale:
297
+ return args.ad_cfg_scale
298
+ return p.cfg_scale
299
+
300
+ def get_sampler(self, p, args: ADetailerArgs) -> str:
301
+ sampler_name = args.ad_sampler if args.ad_use_sampler else p.sampler_name
302
+
303
+ if sampler_name in ["PLMS", "UniPC"]:
304
+ sampler_name = "Euler"
305
+ return sampler_name
306
+
307
+ def get_override_settings(self, p, args: ADetailerArgs) -> dict[str, Any]:
308
+ d = {}
309
+ if args.ad_use_clip_skip:
310
+ d["CLIP_stop_at_last_layers"] = args.ad_clip_skip
311
+ return d
312
+
313
+ def get_initial_noise_multiplier(self, p, args: ADetailerArgs) -> float | None:
314
+ if args.ad_use_noise_multiplier:
315
+ return args.ad_noise_multiplier
316
+ return None
317
+
318
+ @staticmethod
319
+ def infotext(p) -> str:
320
+ return create_infotext(
321
+ p, p.all_prompts, p.all_seeds, p.all_subseeds, None, 0, 0
322
+ )
323
+
324
+ def write_params_txt(self, p) -> None:
325
+ infotext = self.infotext(p)
326
+ params_txt = Path(data_path, "params.txt")
327
+ params_txt.write_text(infotext, encoding="utf-8")
328
+
329
+ def script_filter(self, p, args: ADetailerArgs):
330
+ script_runner = copy(p.scripts)
331
+ script_args = deepcopy(p.script_args)
332
+ self.disable_controlnet_units(script_args)
333
+
334
+ ad_only_seleted_scripts = opts.data.get("ad_only_seleted_scripts", True)
335
+ if not ad_only_seleted_scripts:
336
+ return script_runner, script_args
337
+
338
+ ad_script_names = opts.data.get("ad_script_names", SCRIPT_DEFAULT)
339
+ script_names_set = {
340
+ name
341
+ for script_name in ad_script_names.split(",")
342
+ for name in (script_name, script_name.strip())
343
+ }
344
+
345
+ if args.ad_controlnet_model != "None":
346
+ script_names_set.add("controlnet")
347
+
348
+ filtered_alwayson = []
349
+ for script_object in script_runner.alwayson_scripts:
350
+ filepath = script_object.filename
351
+ filename = Path(filepath).stem
352
+ if filename in script_names_set:
353
+ filtered_alwayson.append(script_object)
354
+
355
+ script_runner.alwayson_scripts = filtered_alwayson
356
+ return script_runner, script_args
357
+
358
+ def disable_controlnet_units(self, script_args: list[Any]) -> None:
359
+ for obj in script_args:
360
+ if "controlnet" in obj.__class__.__name__.lower():
361
+ if hasattr(obj, "enabled"):
362
+ obj.enabled = False
363
+ if hasattr(obj, "input_mode"):
364
+ obj.input_mode = getattr(obj.input_mode, "SIMPLE", "simple")
365
+
366
+ elif isinstance(obj, dict) and "module" in obj:
367
+ obj["enabled"] = False
368
+
369
+ def get_i2i_p(self, p, args: ADetailerArgs, image):
370
+ seed, subseed = self.get_seed(p)
371
+ width, height = self.get_width_height(p, args)
372
+ steps = self.get_steps(p, args)
373
+ cfg_scale = self.get_cfg_scale(p, args)
374
+ initial_noise_multiplier = self.get_initial_noise_multiplier(p, args)
375
+ sampler_name = self.get_sampler(p, args)
376
+ override_settings = self.get_override_settings(p, args)
377
+
378
+ i2i = StableDiffusionProcessingImg2Img(
379
+ init_images=[image],
380
+ resize_mode=0,
381
+ denoising_strength=args.ad_denoising_strength,
382
+ mask=None,
383
+ mask_blur=args.ad_mask_blur,
384
+ inpainting_fill=1,
385
+ inpaint_full_res=args.ad_inpaint_only_masked,
386
+ inpaint_full_res_padding=args.ad_inpaint_only_masked_padding,
387
+ inpainting_mask_invert=0,
388
+ initial_noise_multiplier=initial_noise_multiplier,
389
+ sd_model=p.sd_model,
390
+ outpath_samples=p.outpath_samples,
391
+ outpath_grids=p.outpath_grids,
392
+ prompt="", # replace later
393
+ negative_prompt="",
394
+ styles=p.styles,
395
+ seed=seed,
396
+ subseed=subseed,
397
+ subseed_strength=p.subseed_strength,
398
+ seed_resize_from_h=p.seed_resize_from_h,
399
+ seed_resize_from_w=p.seed_resize_from_w,
400
+ sampler_name=sampler_name,
401
+ batch_size=1,
402
+ n_iter=1,
403
+ steps=steps,
404
+ cfg_scale=cfg_scale,
405
+ width=width,
406
+ height=height,
407
+ restore_faces=args.ad_restore_face,
408
+ tiling=p.tiling,
409
+ extra_generation_params=p.extra_generation_params,
410
+ do_not_save_samples=True,
411
+ do_not_save_grid=True,
412
+ override_settings=override_settings,
413
+ )
414
+
415
+ i2i.cached_c = [None, None]
416
+ i2i.cached_uc = [None, None]
417
+ i2i.scripts, i2i.script_args = self.script_filter(p, args)
418
+ i2i._disable_adetailer = True
419
+
420
+ if args.ad_controlnet_model != "None":
421
+ self.update_controlnet_args(i2i, args)
422
+ else:
423
+ i2i.control_net_enabled = False
424
+
425
+ return i2i
426
+
427
+ def save_image(self, p, image, *, condition: str, suffix: str) -> None:
428
+ i = p._ad_idx
429
+ if p.all_prompts:
430
+ i %= len(p.all_prompts)
431
+ save_prompt = p.all_prompts[i]
432
+ else:
433
+ save_prompt = p.prompt
434
+ seed, _ = self.get_seed(p)
435
+
436
+ if opts.data.get(condition, False):
437
+ images.save_image(
438
+ image=image,
439
+ path=p.outpath_samples,
440
+ basename="",
441
+ seed=seed,
442
+ prompt=save_prompt,
443
+ extension=opts.samples_format,
444
+ info=self.infotext(p),
445
+ p=p,
446
+ suffix=suffix,
447
+ )
448
+
449
+ def get_ad_model(self, name: str):
450
+ if name not in model_mapping:
451
+ msg = f"[-] ADetailer: Model {name!r} not found. Available models: {list(model_mapping.keys())}"
452
+ raise ValueError(msg)
453
+ return model_mapping[name]
454
+
455
+ def sort_bboxes(self, pred: PredictOutput) -> PredictOutput:
456
+ sortby = opts.data.get("ad_bbox_sortby", BBOX_SORTBY[0])
457
+ sortby_idx = BBOX_SORTBY.index(sortby)
458
+ return sort_bboxes(pred, sortby_idx)
459
+
460
+ def pred_preprocessing(self, pred: PredictOutput, args: ADetailerArgs):
461
+ pred = filter_by_ratio(
462
+ pred, low=args.ad_mask_min_ratio, high=args.ad_mask_max_ratio
463
+ )
464
+ pred = self.sort_bboxes(pred)
465
+ return mask_preprocess(
466
+ pred.masks,
467
+ kernel=args.ad_dilate_erode,
468
+ x_offset=args.ad_x_offset,
469
+ y_offset=args.ad_y_offset,
470
+ merge_invert=args.ad_mask_merge_invert,
471
+ )
472
+
473
+ @staticmethod
474
+ def ensure_rgb_image(image: Any):
475
+ if hasattr(image, "mode") and image.mode != "RGB":
476
+ image = image.convert("RGB")
477
+ return image
478
+
479
+ @staticmethod
480
+ def i2i_prompts_replace(
481
+ i2i, prompts: list[str], negative_prompts: list[str], j: int
482
+ ) -> None:
483
+ i1 = min(j, len(prompts) - 1)
484
+ i2 = min(j, len(negative_prompts) - 1)
485
+ prompt = prompts[i1]
486
+ negative_prompt = negative_prompts[i2]
487
+ i2i.prompt = prompt
488
+ i2i.negative_prompt = negative_prompt
489
+
490
+ @staticmethod
491
+ def compare_prompt(p, processed, n: int = 0):
492
+ if p.prompt != processed.all_prompts[0]:
493
+ print(
494
+ f"[-] ADetailer: applied {ordinal(n + 1)} ad_prompt: {processed.all_prompts[0]!r}"
495
+ )
496
+
497
+ if p.negative_prompt != processed.all_negative_prompts[0]:
498
+ print(
499
+ f"[-] ADetailer: applied {ordinal(n + 1)} ad_negative_prompt: {processed.all_negative_prompts[0]!r}"
500
+ )
501
+
502
+ @staticmethod
503
+ def need_call_process(p) -> bool:
504
+ i = p._ad_idx
505
+ bs = p.batch_size
506
+ return i % bs == bs - 1
507
+
508
+ @staticmethod
509
+ def need_call_postprocess(p) -> bool:
510
+ i = p._ad_idx
511
+ bs = p.batch_size
512
+ return i % bs == 0
513
+
514
+ @rich_traceback
515
+ def process(self, p, *args_):
516
+ if getattr(p, "_disable_adetailer", False):
517
+ return
518
+
519
+ if self.is_ad_enabled(*args_):
520
+ arg_list = self.get_args(p, *args_)
521
+ extra_params = self.extra_params(arg_list)
522
+ p.extra_generation_params.update(extra_params)
523
+
524
+ def _postprocess_image(self, p, pp, args: ADetailerArgs, *, n: int = 0) -> bool:
525
+ """
526
+ Returns
527
+ -------
528
+ bool
529
+
530
+ `True` if image was processed, `False` otherwise.
531
+ """
532
+ if state.interrupted:
533
+ return False
534
+
535
+ i = p._ad_idx
536
+
537
+ i2i = self.get_i2i_p(p, args, pp.image)
538
+ seed, subseed = self.get_seed(p)
539
+ ad_prompts, ad_negatives = self.get_prompt(p, args)
540
+
541
+ is_mediapipe = args.ad_model.lower().startswith("mediapipe")
542
+
543
+ kwargs = {}
544
+ if is_mediapipe:
545
+ predictor = mediapipe_predict
546
+ ad_model = args.ad_model
547
+ else:
548
+ predictor = ultralytics_predict
549
+ ad_model = self.get_ad_model(args.ad_model)
550
+ kwargs["device"] = self.ultralytics_device
551
+
552
+ with change_torch_load():
553
+ pred = predictor(ad_model, pp.image, args.ad_confidence, **kwargs)
554
+
555
+ masks = self.pred_preprocessing(pred, args)
556
+
557
+ if not masks:
558
+ print(
559
+ f"[-] ADetailer: nothing detected on image {i + 1} with {ordinal(n + 1)} settings."
560
+ )
561
+ return False
562
+
563
+ self.save_image(
564
+ p,
565
+ pred.preview,
566
+ condition="ad_save_previews",
567
+ suffix="-ad-preview" + suffix(n, "-"),
568
+ )
569
+
570
+ steps = len(masks)
571
+ processed = None
572
+ state.job_count += steps
573
+
574
+ if is_mediapipe:
575
+ print(f"mediapipe: {steps} detected.")
576
+
577
+ p2 = copy(i2i)
578
+ for j in range(steps):
579
+ p2.image_mask = masks[j]
580
+ p2.init_images[0] = self.ensure_rgb_image(p2.init_images[0])
581
+ self.i2i_prompts_replace(p2, ad_prompts, ad_negatives, j)
582
+
583
+ if re.match(r"^\s*\[SKIP\]\s*$", p2.prompt):
584
+ continue
585
+
586
+ p2.seed = seed + j
587
+ p2.subseed = subseed + j
588
+
589
+ try:
590
+ processed = process_images(p2)
591
+ except NansException as e:
592
+ msg = f"[-] ADetailer: 'NansException' occurred with {ordinal(n + 1)} settings.\n{e}"
593
+ print(msg, file=sys.stderr)
594
+ continue
595
+ finally:
596
+ p2.close()
597
+
598
+ self.compare_prompt(p2, processed, n=n)
599
+ p2 = copy(i2i)
600
+ p2.init_images = [processed.images[0]]
601
+
602
+ if processed is not None:
603
+ pp.image = processed.images[0]
604
+ return True
605
+
606
+ return False
607
+
608
+ @rich_traceback
609
+ def postprocess_image(self, p, pp, *args_):
610
+ if getattr(p, "_disable_adetailer", False):
611
+ return
612
+
613
+ if not self.is_ad_enabled(*args_):
614
+ return
615
+
616
+ p._ad_idx = getattr(p, "_ad_idx", -1) + 1
617
+ init_image = copy(pp.image)
618
+ arg_list = self.get_args(p, *args_)
619
+
620
+ if p.scripts is not None and self.need_call_postprocess(p):
621
+ dummy = Processed(p, [], p.seed, "")
622
+ with preseve_prompts(p):
623
+ p.scripts.postprocess(copy(p), dummy)
624
+
625
+ is_processed = False
626
+ with CNHijackRestore(), pause_total_tqdm(), cn_allow_script_control():
627
+ for n, args in enumerate(arg_list):
628
+ if args.ad_model == "None":
629
+ continue
630
+ is_processed |= self._postprocess_image(p, pp, args, n=n)
631
+
632
+ if is_processed:
633
+ self.save_image(
634
+ p, init_image, condition="ad_save_images_before", suffix="-ad-before"
635
+ )
636
+
637
+ if p.scripts is not None and self.need_call_process(p):
638
+ with preseve_prompts(p):
639
+ p.scripts.process(copy(p))
640
+
641
+ try:
642
+ ia = p._ad_idx
643
+ lenp = len(p.all_prompts)
644
+ if ia % lenp == lenp - 1:
645
+ self.write_params_txt(p)
646
+ except Exception:
647
+ pass
648
+
649
+
650
+ def on_after_component(component, **_kwargs):
651
+ global txt2img_submit_button, img2img_submit_button
652
+ if getattr(component, "elem_id", None) == "txt2img_generate":
653
+ txt2img_submit_button = component
654
+ return
655
+
656
+ if getattr(component, "elem_id", None) == "img2img_generate":
657
+ img2img_submit_button = component
658
+
659
+
660
+ def on_ui_settings():
661
+ section = ("ADetailer", AFTER_DETAILER)
662
+ shared.opts.add_option(
663
+ "ad_max_models",
664
+ shared.OptionInfo(
665
+ default=2,
666
+ label="Max models",
667
+ component=gr.Slider,
668
+ component_args={"minimum": 1, "maximum": 10, "step": 1},
669
+ section=section,
670
+ ),
671
+ )
672
+
673
+ shared.opts.add_option(
674
+ "ad_save_previews",
675
+ shared.OptionInfo(False, "Save mask previews", section=section),
676
+ )
677
+
678
+ shared.opts.add_option(
679
+ "ad_save_images_before",
680
+ shared.OptionInfo(False, "Save images before ADetailer", section=section),
681
+ )
682
+
683
+ shared.opts.add_option(
684
+ "ad_only_seleted_scripts",
685
+ shared.OptionInfo(
686
+ True, "Apply only selected scripts to ADetailer", section=section
687
+ ),
688
+ )
689
+
690
+ textbox_args = {
691
+ "placeholder": "comma-separated list of script names",
692
+ "interactive": True,
693
+ }
694
+
695
+ shared.opts.add_option(
696
+ "ad_script_names",
697
+ shared.OptionInfo(
698
+ default=SCRIPT_DEFAULT,
699
+ label="Script names to apply to ADetailer (separated by comma)",
700
+ component=gr.Textbox,
701
+ component_args=textbox_args,
702
+ section=section,
703
+ ),
704
+ )
705
+
706
+ shared.opts.add_option(
707
+ "ad_bbox_sortby",
708
+ shared.OptionInfo(
709
+ default="None",
710
+ label="Sort bounding boxes by",
711
+ component=gr.Radio,
712
+ component_args={"choices": BBOX_SORTBY},
713
+ section=section,
714
+ ),
715
+ )
716
+
717
+
718
+ # xyz_grid
719
+
720
+
721
+ def make_axis_on_xyz_grid():
722
+ xyz_grid = None
723
+ for script in scripts.scripts_data:
724
+ if script.script_class.__module__ == "xyz_grid.py":
725
+ xyz_grid = script.module
726
+ break
727
+
728
+ if xyz_grid is None:
729
+ return
730
+
731
+ model_list = ["None", *model_mapping.keys()]
732
+ samplers = [sampler.name for sampler in all_samplers]
733
+
734
+ def set_value(p, x, xs, *, field: str):
735
+ if not hasattr(p, "adetailer_xyz"):
736
+ p.adetailer_xyz = {}
737
+ p.adetailer_xyz[field] = x
738
+
739
+ axis = [
740
+ xyz_grid.AxisOption(
741
+ "[ADetailer] ADetailer model 1st",
742
+ str,
743
+ partial(set_value, field="ad_model"),
744
+ choices=lambda: model_list,
745
+ ),
746
+ xyz_grid.AxisOption(
747
+ "[ADetailer] ADetailer prompt 1st",
748
+ str,
749
+ partial(set_value, field="ad_prompt"),
750
+ ),
751
+ xyz_grid.AxisOption(
752
+ "[ADetailer] ADetailer negative prompt 1st",
753
+ str,
754
+ partial(set_value, field="ad_negative_prompt"),
755
+ ),
756
+ xyz_grid.AxisOption(
757
+ "[ADetailer] Mask erosion / dilation 1st",
758
+ int,
759
+ partial(set_value, field="ad_dilate_erode"),
760
+ ),
761
+ xyz_grid.AxisOption(
762
+ "[ADetailer] Inpaint denoising strength 1st",
763
+ float,
764
+ partial(set_value, field="ad_denoising_strength"),
765
+ ),
766
+ xyz_grid.AxisOption(
767
+ "[ADetailer] Inpaint only masked 1st",
768
+ str,
769
+ partial(set_value, field="ad_inpaint_only_masked"),
770
+ choices=lambda: ["True", "False"],
771
+ ),
772
+ xyz_grid.AxisOption(
773
+ "[ADetailer] Inpaint only masked padding 1st",
774
+ int,
775
+ partial(set_value, field="ad_inpaint_only_masked_padding"),
776
+ ),
777
+ xyz_grid.AxisOption(
778
+ "[ADetailer] ADetailer sampler 1st",
779
+ str,
780
+ partial(set_value, field="ad_sampler"),
781
+ choices=lambda: samplers,
782
+ ),
783
+ xyz_grid.AxisOption(
784
+ "[ADetailer] ControlNet model 1st",
785
+ str,
786
+ partial(set_value, field="ad_controlnet_model"),
787
+ choices=lambda: ["None", *get_cn_models()],
788
+ ),
789
+ ]
790
+
791
+ if not any(x.label.startswith("[ADetailer]") for x in xyz_grid.axis_options):
792
+ xyz_grid.axis_options.extend(axis)
793
+
794
+
795
+ def on_before_ui():
796
+ try:
797
+ make_axis_on_xyz_grid()
798
+ except Exception:
799
+ error = traceback.format_exc()
800
+ print(
801
+ f"[-] ADetailer: xyz_grid error:\n{error}",
802
+ file=sys.stderr,
803
+ )
804
+
805
+
806
+ script_callbacks.on_ui_settings(on_ui_settings)
807
+ script_callbacks.on_after_component(on_after_component)
808
+ script_callbacks.on_before_ui(on_before_ui)
adetailer/sd_webui/__init__.py ADDED
File without changes
adetailer/sd_webui/devices.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+
7
+ class NansException(Exception): # noqa: N818
8
+ pass
9
+
10
+ else:
11
+ from modules.devices import NansException
adetailer/sd_webui/images.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ from PIL import Image, PngImagePlugin
7
+
8
+ from sd_webui.processing import StableDiffusionProcessing
9
+
10
+ def save_image(
11
+ image: Image.Image,
12
+ path: str,
13
+ basename: str,
14
+ seed: int | None = None,
15
+ prompt: str = "",
16
+ extension: str = "png",
17
+ info: str | PngImagePlugin.iTXt = "",
18
+ short_filename: bool = False,
19
+ no_prompt: bool = False,
20
+ grid: bool = False,
21
+ pnginfo_section_name: str = "parameters",
22
+ p: StableDiffusionProcessing | None = None,
23
+ existing_info: dict | None = None,
24
+ forced_filename: str | None = None,
25
+ suffix: str = "",
26
+ save_to_dirs: bool = False,
27
+ ) -> tuple[str, str | None]:
28
+ """Save an image.
29
+
30
+ Args:
31
+ image (`PIL.Image`):
32
+ The image to be saved.
33
+ path (`str`):
34
+ The directory to save the image. Note, the option `save_to_dirs` will make the image to be saved into a sub directory.
35
+ basename (`str`):
36
+ The base filename which will be applied to `filename pattern`.
37
+ seed, prompt, short_filename,
38
+ extension (`str`):
39
+ Image file extension, default is `png`.
40
+ pngsectionname (`str`):
41
+ Specify the name of the section which `info` will be saved in.
42
+ info (`str` or `PngImagePlugin.iTXt`):
43
+ PNG info chunks.
44
+ existing_info (`dict`):
45
+ Additional PNG info. `existing_info == {pngsectionname: info, ...}`
46
+ no_prompt:
47
+ TODO I don't know its meaning.
48
+ p (`StableDiffusionProcessing`)
49
+ forced_filename (`str`):
50
+ If specified, `basename` and filename pattern will be ignored.
51
+ save_to_dirs (bool):
52
+ If true, the image will be saved into a subdirectory of `path`.
53
+
54
+ Returns: (fullfn, txt_fullfn)
55
+ fullfn (`str`):
56
+ The full path of the saved imaged.
57
+ txt_fullfn (`str` or None):
58
+ If a text file is saved for this image, this will be its full path. Otherwise None.
59
+ """
60
+
61
+ else:
62
+ from modules.images import save_image
adetailer/sd_webui/paths.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ import os
7
+
8
+ models_path = os.path.join(os.path.dirname(__file__), "1")
9
+ script_path = os.path.join(os.path.dirname(__file__), "2")
10
+ data_path = os.path.join(os.path.dirname(__file__), "3")
11
+ extensions_dir = os.path.join(os.path.dirname(__file__), "4")
12
+ extensions_builtin_dir = os.path.join(os.path.dirname(__file__), "5")
13
+ else:
14
+ from modules.paths import data_path, models_path, script_path
adetailer/sd_webui/processing.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ from dataclasses import dataclass, field
7
+ from typing import Any, Callable
8
+
9
+ import numpy as np
10
+ import torch
11
+ from PIL import Image
12
+
13
+ def _image():
14
+ return Image.new("L", (512, 512))
15
+
16
+ @dataclass
17
+ class StableDiffusionProcessing:
18
+ sd_model: torch.nn.Module = field(default_factory=lambda: torch.nn.Linear(1, 1))
19
+ outpath_samples: str = ""
20
+ outpath_grids: str = ""
21
+ prompt: str = ""
22
+ prompt_for_display: str = ""
23
+ negative_prompt: str = ""
24
+ styles: list[str] = field(default_factory=list)
25
+ seed: int = -1
26
+ subseed: int = -1
27
+ subseed_strength: float = 0.0
28
+ seed_resize_from_h: int = -1
29
+ seed_resize_from_w: int = -1
30
+ sampler_name: str | None = None
31
+ batch_size: int = 1
32
+ n_iter: int = 1
33
+ steps: int = 50
34
+ cfg_scale: float = 7.0
35
+ width: int = 512
36
+ height: int = 512
37
+ restore_faces: bool = False
38
+ tiling: bool = False
39
+ do_not_save_samples: bool = False
40
+ do_not_save_grid: bool = False
41
+ extra_generation_params: dict[str, Any] = field(default_factory=dict)
42
+ overlay_images: list[Image.Image] = field(default_factory=list)
43
+ eta: float = 0.0
44
+ do_not_reload_embeddings: bool = False
45
+ paste_to: tuple[int | float, ...] = (0, 0, 0, 0)
46
+ color_corrections: list[np.ndarray] = field(default_factory=list)
47
+ denoising_strength: float = 0.0
48
+ sampler_noise_scheduler_override: Callable | None = None
49
+ ddim_discretize: str = ""
50
+ s_min_uncond: float = 0.0
51
+ s_churn: float = 0.0
52
+ s_tmin: float = 0.0
53
+ s_tmax: float = 0.0
54
+ s_noise: float = 0.0
55
+ override_settings: dict[str, Any] = field(default_factory=dict)
56
+ override_settings_restore_afterwards: bool = False
57
+ is_using_inpainting_conditioning: bool = False
58
+ disable_extra_networks: bool = False
59
+ scripts: Any = None
60
+ script_args: list[Any] = field(default_factory=list)
61
+ all_prompts: list[str] = field(default_factory=list)
62
+ all_negative_prompts: list[str] = field(default_factory=list)
63
+ all_seeds: list[int] = field(default_factory=list)
64
+ all_subseeds: list[int] = field(default_factory=list)
65
+ iteration: int = 1
66
+ is_hr_pass: bool = False
67
+
68
+ def close(self) -> None:
69
+ pass
70
+
71
+ @dataclass
72
+ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing):
73
+ sampler: Callable | None = None
74
+ enable_hr: bool = False
75
+ denoising_strength: float = 0.75
76
+ hr_scale: float = 2.0
77
+ hr_upscaler: str = ""
78
+ hr_second_pass_steps: int = 0
79
+ hr_resize_x: int = 0
80
+ hr_resize_y: int = 0
81
+ hr_upscale_to_x: int = 0
82
+ hr_upscale_to_y: int = 0
83
+ width: int = 512
84
+ height: int = 512
85
+ truncate_x: int = 512
86
+ truncate_y: int = 512
87
+ applied_old_hires_behavior_to: tuple[int, int] = (512, 512)
88
+
89
+ @dataclass
90
+ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
91
+ sampler: Callable | None = None
92
+ init_images: list[Image.Image] = field(default_factory=list)
93
+ resize_mode: int = 0
94
+ denoising_strength: float = 0.75
95
+ image_cfg_scale: float | None = None
96
+ init_latent: torch.Tensor | None = None
97
+ image_mask: Image.Image = field(default_factory=_image)
98
+ latent_mask: Image.Image = field(default_factory=_image)
99
+ mask_for_overlay: Image.Image = field(default_factory=_image)
100
+ mask_blur: int = 4
101
+ inpainting_fill: int = 0
102
+ inpaint_full_res: bool = True
103
+ inpaint_full_res_padding: int = 0
104
+ inpainting_mask_invert: int | bool = 0
105
+ initial_noise_multiplier: float = 1.0
106
+ mask: torch.Tensor | None = None
107
+ nmask: torch.Tensor | None = None
108
+ image_conditioning: torch.Tensor | None = None
109
+
110
+ @dataclass
111
+ class Processed:
112
+ images: list[Image.Image] = field(default_factory=list)
113
+ prompt: list[str] = field(default_factory=list)
114
+ negative_prompt: list[str] = field(default_factory=list)
115
+ seed: list[int] = field(default_factory=list)
116
+ subseed: list[int] = field(default_factory=list)
117
+ subseed_strength: float = 0.0
118
+ info: str = ""
119
+ comments: str = ""
120
+ width: int = 512
121
+ height: int = 512
122
+ sampler_name: str = ""
123
+ cfg_scale: float = 7.0
124
+ image_cfg_scale: float | None = None
125
+ steps: int = 50
126
+ batch_size: int = 1
127
+ restore_faces: bool = False
128
+ face_restoration_model: str | None = None
129
+ sd_model_hash: str = ""
130
+ seed_resize_from_w: int = -1
131
+ seed_resize_from_h: int = -1
132
+ denoising_strength: float = 0.0
133
+ extra_generation_params: dict[str, Any] = field(default_factory=dict)
134
+ index_of_first_image: int = 0
135
+ styles: list[str] = field(default_factory=list)
136
+ job_timestamp: str = ""
137
+ clip_skip: int = 1
138
+ eta: float = 0.0
139
+ ddim_discretize: str = ""
140
+ s_churn: float = 0.0
141
+ s_tmin: float = 0.0
142
+ s_tmax: float = 0.0
143
+ s_noise: float = 0.0
144
+ sampler_noise_scheduler_override: Callable | None = None
145
+ is_using_inpainting_conditioning: bool = False
146
+ all_prompts: list[str] = field(default_factory=list)
147
+ all_negative_prompts: list[str] = field(default_factory=list)
148
+ all_seeds: list[int] = field(default_factory=list)
149
+ all_subseeds: list[int] = field(default_factory=list)
150
+ infotexts: list[str] = field(default_factory=list)
151
+
152
+ def create_infotext(
153
+ p: StableDiffusionProcessingTxt2Img | StableDiffusionProcessingImg2Img,
154
+ all_prompts: list[str],
155
+ all_seeds: list[int],
156
+ all_subseeds: list[int],
157
+ comments: Any,
158
+ iteration: int = 0,
159
+ position_in_batch: int = 0,
160
+ use_main_prompt: bool = False,
161
+ index: int | None = None,
162
+ all_negative_prompts: list[str] | None = None,
163
+ ) -> str:
164
+ pass
165
+
166
+ def process_images(
167
+ p: StableDiffusionProcessingTxt2Img | StableDiffusionProcessingImg2Img,
168
+ ) -> Processed:
169
+ pass
170
+
171
+ else:
172
+ from modules.processing import (
173
+ Processed,
174
+ StableDiffusionProcessing,
175
+ StableDiffusionProcessingImg2Img,
176
+ StableDiffusionProcessingTxt2Img,
177
+ create_infotext,
178
+ process_images,
179
+ )
adetailer/sd_webui/safe.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ import torch
7
+
8
+ unsafe_torch_load = torch.load
9
+ else:
10
+ from modules.safe import unsafe_torch_load
adetailer/sd_webui/script_callbacks.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ from typing import Callable
7
+
8
+ def on_app_started(callback: Callable):
9
+ pass
10
+
11
+ def on_ui_settings(callback: Callable):
12
+ pass
13
+
14
+ def on_after_component(callback: Callable):
15
+ pass
16
+
17
+ def on_before_ui(callback: Callable):
18
+ pass
19
+
20
+ else:
21
+ from modules.script_callbacks import (
22
+ on_after_component,
23
+ on_app_started,
24
+ on_before_ui,
25
+ on_ui_settings,
26
+ )
adetailer/sd_webui/scripts.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ from abc import ABC, abstractmethod
7
+ from collections import namedtuple
8
+ from dataclasses import dataclass
9
+ from typing import Any
10
+
11
+ import gradio as gr
12
+ from PIL import Image
13
+
14
+ from sd_webui.processing import (
15
+ Processed,
16
+ StableDiffusionProcessingImg2Img,
17
+ StableDiffusionProcessingTxt2Img,
18
+ )
19
+
20
+ SDPType = StableDiffusionProcessingImg2Img | StableDiffusionProcessingTxt2Img
21
+ AlwaysVisible = object()
22
+
23
+ @dataclass
24
+ class PostprocessImageArgs:
25
+ image: Image.Image
26
+
27
+ class Script(ABC):
28
+ filename: str
29
+ args_from: int
30
+ args_to: int
31
+ alwayson: bool
32
+
33
+ is_txt2img: bool
34
+ is_img2img: bool
35
+
36
+ group: gr.Group
37
+ infotext_fields: list[tuple[str, str]]
38
+ paste_field_names: list[str]
39
+
40
+ @abstractmethod
41
+ def title(self):
42
+ raise NotImplementedError
43
+
44
+ def ui(self, is_img2img: bool):
45
+ pass
46
+
47
+ def show(self, is_img2img: bool):
48
+ return True
49
+
50
+ def run(self, p: SDPType, *args):
51
+ pass
52
+
53
+ def process(self, p: SDPType, *args):
54
+ pass
55
+
56
+ def before_process_batch(self, p: SDPType, *args, **kwargs):
57
+ pass
58
+
59
+ def process_batch(self, p: SDPType, *args, **kwargs):
60
+ pass
61
+
62
+ def postprocess_batch(self, p: SDPType, *args, **kwargs):
63
+ pass
64
+
65
+ def postprocess_image(self, p: SDPType, pp: PostprocessImageArgs, *args):
66
+ pass
67
+
68
+ def postprocess(self, p: SDPType, processed: Processed, *args):
69
+ pass
70
+
71
+ def before_component(self, component, **kwargs):
72
+ pass
73
+
74
+ def after_component(self, component, **kwargs):
75
+ pass
76
+
77
+ def describe(self):
78
+ return ""
79
+
80
+ def elem_id(self, item_id: Any) -> str:
81
+ pass
82
+
83
+ ScriptClassData = namedtuple(
84
+ "ScriptClassData", ["script_class", "path", "basedir", "module"]
85
+ )
86
+ scripts_data: list[ScriptClassData] = []
87
+
88
+ else:
89
+ from modules.scripts import (
90
+ AlwaysVisible,
91
+ PostprocessImageArgs,
92
+ Script,
93
+ scripts_data,
94
+ )
adetailer/sd_webui/sd_samplers.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ from typing import Any, Callable, NamedTuple
7
+
8
+ class SamplerData(NamedTuple):
9
+ name: str
10
+ constructor: Callable
11
+ aliases: list[str]
12
+ options: dict[str, Any]
13
+
14
+ all_samplers: list[SamplerData] = []
15
+
16
+ else:
17
+ from modules.sd_samplers import all_samplers
adetailer/sd_webui/shared.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ import argparse
7
+ from dataclasses import dataclass
8
+ from typing import Any, Callable
9
+
10
+ import torch
11
+ from PIL import Image
12
+
13
+ @dataclass
14
+ class State:
15
+ skipped: bool = False
16
+ interrupted: bool = False
17
+ job: str = ""
18
+ job_no: int = 0
19
+ job_count: int = 0
20
+ processing_has_refined_job_count: bool = False
21
+ job_timestamp: str = "0"
22
+ sampling_step: int = 0
23
+ sampling_steps: int = 0
24
+ current_latent: torch.Tensor | None = None
25
+ current_image: Image.Image | None = None
26
+ current_image_sampling_step: int = 0
27
+ id_live_preview: int = 0
28
+ textinfo: str | None = None
29
+ time_start: float | None = None
30
+ need_restart: bool = False
31
+ server_start: float | None = None
32
+
33
+ @dataclass
34
+ class OptionInfo:
35
+ default: Any = None
36
+ label: str = ""
37
+ component: Any = None
38
+ component_args: Callable[[], dict] | dict[str, Any] | None = None
39
+ onchange: Callable[[], None] | None = None
40
+ section: tuple[str, str] | None = None
41
+ refresh: Callable[[], None] | None = None
42
+
43
+ class Option:
44
+ data_labels: dict[str, OptionInfo]
45
+
46
+ def __init__(self):
47
+ self.data: dict[str, Any] = {}
48
+
49
+ def add_option(self, key: str, info: OptionInfo):
50
+ pass
51
+
52
+ def __getattr__(self, item: str):
53
+ if self.data is not None and item in self.data:
54
+ return self.data[item]
55
+
56
+ if item in self.data_labels:
57
+ return self.data_labels[item].default
58
+
59
+ return super().__getattribute__(item)
60
+
61
+ opts = Option()
62
+ cmd_opts = argparse.Namespace()
63
+ state = State()
64
+
65
+ else:
66
+ from modules.shared import OptionInfo, cmd_opts, opts, state
artists-to-study/.github/FUNDING.yml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # These are supported funding model platforms
2
+
3
+ github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4
+ patreon: camenduru
5
+ open_collective: # Replace with a single Open Collective username
6
+ ko_fi: camenduru
7
+ tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8
+ community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
+ liberapay: # Replace with a single Liberapay username
10
+ issuehunt: # Replace with a single IssueHunt username
11
+ otechie: # Replace with a single Otechie username
12
+ lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
13
+ custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
artists-to-study/LICENSE ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
artists-to-study/README.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 🐣 Please follow me for new updates https://twitter.com/camenduru <br />
2
+ 🔥 Please join our discord server https://discord.gg/k5BwmmvJJU <br />
3
+ 🥳 Please join my patreon community https://patreon.com/camenduru <br />
4
+
5
+ # Artists To Study
6
+
7
+ ![artists-to-study](https://user-images.githubusercontent.com/54370274/197829512-e7d30d44-2697-4ecd-b9a7-3665217918c7.jpg)
8
+
9
+ https://artiststostudy.pages.dev addapted to an extension for [web ui](https://github.com/AUTOMATIC1111/stable-diffusion-webui).
10
+
11
+ To install it, clone the repo into the `extensions` directory and restart the web ui:
12
+
13
+ `git clone https://github.com/camenduru/stable-diffusion-webui-artists-to-study`
14
+
15
+ You can add the artist name to the clipboard by clicking on it. (thanks for the idea @gmaciocci)