1. 类定义和初始化
```python
class easyControlnet:
# 定义一个名为 easyControlnet 的类def __init__(self):
# 初始化函数
pass
```
这段代码定义了一个名为 `easyControlnet` 的类,并包含一个空的初始化函数 `__init__`。
2. 定义 `apply` 方法和初始检查
```python
def apply(self, control_net_name, image, positive, negative, strength, start_percent=0, end_percent=1, control_net=None, scale_soft_weights=1, mask=None, easyCache=None, use_cache=True):
# 定义 apply 方法,处理 ControlNet 应用逻辑,接受多个参数,包括控制网络名称、图像、正向和负向提示、强度等
if strength == 0:
return (positive, negative)
# 如果强度为 0,直接返回正向和负向提示
```
`apply` 方法是 `easyControlnet` 类的核心功能,用于应用 ControlNet。它接受多个参数,包括控制网络名称、图像、正向和负向提示、强度等。如果强度为 0,则直接返回原始的正向和负向提示。
3. 加载控制网络
```python
if control_net is None:
control_net = easyCache.load_controlnet(control_net_name, scale_soft_weights, use_cache)
# 如果控制网络为空,从缓存中加载控制网络
```
如果 `control_net` 参数为空,则使用 `easyCache` 从缓存中加载指定的控制网络。
4. 处理掩码
```python
if mask is not None:
mask = mask.to(self.device)
# 如果提供了掩码,将其转移到设备上if mask is not None and len(mask.shape) < 3:
mask = mask.unsqueeze(0)
# 如果掩码维度小于 3,增加一个维度
```
如果提供了掩码,将其转移到设备上(如 GPU 或 CPU)。如果掩码的维度小于 3,增加一个维度以确保其形状正确。
5. 生成控制提示
```python
control_hint = image.movedim(-1, 1)
# 将图像的最后一个维度移动到第二个维度,生成控制提示
```
这行代码将图像的最后一个维度移动到第二个维度,生成一个控制提示 `control_hint`。
6. 处理正向提示(无负向提示情况)
```python
is_cond = True
if negative is None:
p = []
for t in positive:
n = [t[0], t[1].copy()]
c_net = control_net.copy().set_cond_hint(control_hint, strength, (start_percent, end_percent))
# 如果没有负向提示,处理正向提示
# 复制正向提示,并设置控制网络的提示信息、强度和百分比范围
if 'control' in t[1]:
c_net.set_previous_controlnet(t[1]['control'])
# 如果正向提示中有之前的控制网络,将其设置为前一个控制网络
n[1]['control'] = c_net
# 将当前控制网络设置为提示中的控制网络
n[1]['control_apply_to_uncond'] = True
# 设置控制网络应用到无条件
if mask is not None:
n[1]['mask'] = mask
n[1]['set_area_to_bounds'] = False
# 如果有掩码,将掩码添加到提示中,并设置不限制区域
p.append(n)
positive = p
```
这段代码处理没有负向提示的情况。对于每个正向提示,复制提示并设置控制网络的提示信息、强度和百分比范围。如果提示中已有之前的控制网络,将其设置为前一个控制网络。如果有掩码,将其添加到提示中,并设置不限制区域。最后,将处理后的提示添加到新的正向提示列表 `p` 中。
7. 处理正向和负向提示(有负向提示情况)
```python
else:
cnets = {}
out = []
for conditioning in [positive, negative]:
c = []
for t in conditioning:
d = t[1].copy()
# 如果有负向提示,分别处理正向和负向提示
# 复制每个提示的字典
prev_cnet = d.get('control', None)
if prev_cnet in cnets:
c_net = cnets[prev_cnet]
else:
c_net = control_net.copy().set_cond_hint(control_hint, strength, (start_percent, end_percent))
c_net.set_previous_controlnet(prev_cnet)
cnets[prev_cnet] = c_net
# 如果提示中有之前的控制网络,检查缓存中是否已有该网络,如果有则使用缓存中的网络,否则创建新的控制网络,并设置提示信息、强度和百分比范围
d['control'] = c_net
d['control_apply_to_uncond'] = False
# 设置当前控制网络并应用到无条件
if mask is not None:
d['mask'] = mask
d['set_area_to_bounds'] = False
# 如果有掩码,将掩码添加到提示中,并设置不限制区域
n = [t[0], d]
c.append(n)
out.append(c)
positive = out[0]
negative = out[1]
```这段代码处理有负向提示的情况。对于正向和负向提示,分别处理每个提示字典。如果提示中有之前的控制网络,检查缓存中是否已有该网络,如果有则使用缓存中的网络,否则创建新的控制网络,并设置提示信息、强度和百分比范围。如果有掩码,将其添加到提示中,并设置不限制区域。最后,将处理后的提示添加到新的正向和负向提示列表中。
8. 返回结果
```python
return (positive, negative)
# 返回处理后的正向和负向提示
```
这行代码返回处理后的正向和负向提示。