Spaces:
Running
on
Zero
Running
on
Zero
Martin Tomov
commited on
yellow bg output1
Browse files
app.py
CHANGED
@@ -141,14 +141,23 @@ def extract_and_paste_insect(original_image: np.ndarray, detection: DetectionRes
|
|
141 |
xmin, ymin, xmax, ymax = mask_to_min_max(mask)
|
142 |
insect_crop = original_image[ymin:ymax, xmin:xmax]
|
143 |
mask_crop = mask[ymin:ymax, xmin:xmax]
|
|
|
|
|
|
|
|
|
|
|
144 |
insect = cv2.bitwise_and(insect_crop, insect_crop, mask=mask_crop)
|
145 |
-
|
146 |
-
|
|
|
147 |
inverse_mask = cv2.bitwise_not(mask_crop)
|
148 |
-
bg_region = background[y_offset:y_end, x_offset:x_end]
|
149 |
bg_ready = cv2.bitwise_and(bg_region, bg_region, mask=inverse_mask)
|
|
|
|
|
150 |
combined = cv2.add(insect, bg_ready)
|
151 |
-
|
|
|
|
|
152 |
|
153 |
def create_yellow_background_with_insects(image: np.ndarray, detections: List[DetectionResult]) -> np.ndarray:
|
154 |
yellow_background = np.full((image.shape[0], image.shape[1], 3), (0, 255, 255), dtype=np.uint8)
|
|
|
141 |
xmin, ymin, xmax, ymax = mask_to_min_max(mask)
|
142 |
insect_crop = original_image[ymin:ymax, xmin:xmax]
|
143 |
mask_crop = mask[ymin:ymax, xmin:xmax]
|
144 |
+
|
145 |
+
# Create an all-yellow background
|
146 |
+
yellow_background = np.full_like(insect_crop, (0, 255, 255), dtype=np.uint8)
|
147 |
+
|
148 |
+
# Extract insect using the mask
|
149 |
insect = cv2.bitwise_and(insect_crop, insect_crop, mask=mask_crop)
|
150 |
+
|
151 |
+
# Prepare the area on the background to accept the insect
|
152 |
+
bg_region = yellow_background.copy()
|
153 |
inverse_mask = cv2.bitwise_not(mask_crop)
|
|
|
154 |
bg_ready = cv2.bitwise_and(bg_region, bg_region, mask=inverse_mask)
|
155 |
+
|
156 |
+
# Combine the insect with the prepared area
|
157 |
combined = cv2.add(insect, bg_ready)
|
158 |
+
|
159 |
+
# Paste the insect onto the yellow background
|
160 |
+
background[ymin:ymax, xmin:xmax] = combined
|
161 |
|
162 |
def create_yellow_background_with_insects(image: np.ndarray, detections: List[DetectionResult]) -> np.ndarray:
|
163 |
yellow_background = np.full((image.shape[0], image.shape[1], 3), (0, 255, 255), dtype=np.uint8)
|