MCPcopy Create free account
hub / github.com/ali-vilab/ACE_plus / ACEPlusImageProcessor

Class ACEPlusImageProcessor

inference/utils.py:24–132  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22 return Image.composite(new_edit_image, edit_image, edit_mask)
23
24class ACEPlusImageProcessor():
25 def __init__(self, max_aspect_ratio=4, d=16, max_seq_len=2048):
26 self.max_aspect_ratio = max_aspect_ratio
27 self.d = d
28 self.max_seq_len = max_seq_len
29 self.transforms = T.Compose([
30 T.ToTensor(),
31 T.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
32 ])
33
34 def image_check(self, image):
35 if image is None:
36 return image
37 # preprocess
38 W, H = image.size
39 if H / W > self.max_aspect_ratio:
40 image = T.CenterCrop([int(self.max_aspect_ratio * W), W])(image)
41 elif W / H > self.max_aspect_ratio:
42 image = T.CenterCrop([H, int(self.max_aspect_ratio * H)])(image)
43 return self.transforms(image)
44
45
46 def preprocess(self,
47 reference_image=None,
48 edit_image=None,
49 edit_mask=None,
50 height=1024,
51 width=1024,
52 repainting_scale = 1.0,
53 keep_pixels = False,
54 keep_pixels_rate = 0.8,
55 use_change = False):
56 reference_image = self.image_check(reference_image)
57 edit_image = self.image_check(edit_image)
58 # for reference generation
59 if edit_image is None:
60 edit_image = torch.zeros([3, height, width])
61 edit_mask = torch.ones([1, height, width])
62 else:
63 if edit_mask is None:
64 _, eH, eW = edit_image.shape
65 edit_mask = np.ones((eH, eW))
66 else:
67 edit_mask = np.asarray(edit_mask)
68 edit_mask = np.where(edit_mask > 128, 1, 0)
69 edit_mask = edit_mask.astype(
70 np.float32) if np.any(edit_mask) else np.ones_like(edit_mask).astype(
71 np.float32)
72 edit_mask = torch.tensor(edit_mask).unsqueeze(0)
73
74 edit_image = edit_image * (1 - edit_mask * repainting_scale)
75
76
77 out_h, out_w = edit_image.shape[-2:]
78
79 assert edit_mask is not None
80 if reference_image is not None:
81 _, H, W = reference_image.shape

Callers 2

init_from_cfgMethod · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected