RyanHangZhou commited on
Commit
d4c5a1e
·
verified ·
1 Parent(s): 4d307ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py CHANGED
@@ -40,6 +40,101 @@ def get_unconditional_conditioning(N, obj_thr, device):
40
  uc = single_uc.unsqueeze(-1).repeat(1, 1, 1, obj_thr)
41
  return {"pch_code": uc}
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  # 4. 核心推理函数 (完全照抄你的逻辑)
44
  @spaces.GPU(duration=120)
45
  def pics_pairwise_inference(background, img_a, mask_a, img_b, mask_b):
 
40
  uc = single_uc.unsqueeze(-1).repeat(1, 1, 1, obj_thr)
41
  return {"pch_code": uc}
42
 
43
+ def process_pairs_multiple(mask, tar_image, patch_dir, counter=0, max_ratio=0.8):
44
+ # 1. Process Reference Object (View)
45
+ view = cv2.imread(patch_dir)
46
+ view = cv2.cvtColor(view, cv2.COLOR_BGR2RGB)
47
+ view = pad_to_square(view, pad_value=255, random=False)
48
+ view = cv2.resize(view.astype(np.uint8), (224, 224))
49
+ view = view.astype(np.float32) / 255.0
50
+
51
+ # 2. BBox and Mask Logic
52
+ box_yyxx = get_bbox_from_mask(mask)
53
+
54
+ # Define crop area (using full image here)
55
+ H1, W1 = tar_image.shape[0], tar_image.shape[1]
56
+ box_yyxx_crop = [0, H1, 0, W1]
57
+
58
+ # Handle box within crop
59
+ y1, y2, x1, x2 = box_in_box(box_yyxx, box_yyxx_crop)
60
+
61
+ # 3. Create Collage (Input Hint)
62
+ # Background with hole (zeroed out at object position)
63
+ collage = tar_image.copy()
64
+ source_collage = collage.copy()
65
+ collage[y1:y2, x1:x2, :] = 0
66
+
67
+ # Binary mask for the current object hole
68
+ collage_mask = np.zeros_like(tar_image, dtype=np.float32)
69
+ collage_mask[y1:y2, x1:x2, :] = 1.0
70
+
71
+ # 4. Square Padding & Resizing
72
+ # Pad all to square (pad_value 2 for mask indicates padding area)
73
+ tar_square = pad_to_square(tar_image, pad_value=0, random=False)
74
+ collage_square = pad_to_square(collage, pad_value=0, random=False)
75
+ mask_square = pad_to_square(collage_mask, pad_value=2, random=False)
76
+
77
+ H2, W2 = collage_square.shape[0], collage_square.shape[1]
78
+
79
+ # Resize to model input size
80
+ tar_res = cv2.resize(tar_square, (512, 512)).astype(np.float32)
81
+ col_res = cv2.resize(collage_square, (512, 512)).astype(np.float32)
82
+ mask_res = cv2.resize(mask_square, (512, 512), interpolation=cv2.INTER_NEAREST).astype(np.float32)
83
+
84
+ # 5. Mask Value Normalization
85
+ # Original logic: mask=1 for object, 0 for background, -1 for padding
86
+ mask_res[mask_res == 2] = -1
87
+
88
+ # For conditioning: keep a 0/1 version for cross-attn mask
89
+ c_mask = np.where(mask_res[..., 0:1] == 1, 1.0, 0.0).astype(np.float32)
90
+
91
+ # 6. Final Item Assembly
92
+ # Normalize images to [-1, 1]
93
+ tar_res = tar_res / 127.5 - 1.0
94
+ col_res = col_res / 127.5 - 1.0
95
+
96
+ # Hint: Concatenate background with the (-1, 0, 1) mask
97
+ hint_final = np.concatenate([col_res, mask_res[..., :1]], axis=-1)
98
+
99
+ item = {
100
+ f'view{counter}': view,
101
+ f'hint{counter}': hint_final,
102
+ f'mask{counter}': c_mask,
103
+ f'hint_sizes{counter}': np.array([y1, x1, y2, x2]),
104
+ 'jpg': tar_res, # Targets are same for all counters in a pair
105
+ 'collage': source_collage,
106
+ 'extra_sizes': np.array([H1, W1, H2, W2])
107
+ }
108
+
109
+ return item
110
+
111
+
112
+ def process_composition(item, obj_thr):
113
+ collage = item['collage'].copy()
114
+ collage_mask = np.zeros((collage.shape[0], collage.shape[1], 1), dtype=np.float32)
115
+
116
+ for i in reversed(range(obj_thr)):
117
+ y1, x1, y2, x2 = item['hint_sizes'+str(i)]
118
+ collage[y1:y2, x1:x2, :] = 0
119
+ collage_mask[y1:y2,x1:x2,:] = 1.0
120
+
121
+ collage = pad_to_square(collage, pad_value = 0, random = False).astype(np.uint8)
122
+
123
+ collage_mask = pad_to_square(collage_mask, pad_value = 2, random = False).astype(np.float32)
124
+
125
+ collage = cv2.resize(collage.astype(np.uint8), (512, 512)).astype(np.float32) / 127.5 - 1.0
126
+ collage_mask = cv2.resize(collage_mask, (512, 512), interpolation=cv2.INTER_NEAREST).astype(np.float32)
127
+
128
+ if len(collage_mask.shape) == 2:
129
+ collage_mask = collage_mask[..., None]
130
+
131
+ collage_mask[collage_mask == 2] = -1.0
132
+
133
+ collage_final = np.concatenate([collage, collage_mask[:,:,:1]] , -1)
134
+
135
+ item.update({'hint': collage_final.copy()})
136
+ return item
137
+
138
  # 4. 核心推理函数 (完全照抄你的逻辑)
139
  @spaces.GPU(duration=120)
140
  def pics_pairwise_inference(background, img_a, mask_a, img_b, mask_b):