Spaces:
Running
on
Zero
Running
on
Zero
Martin Tomov
commited on
Place the insect onto the yellow background
Browse files
app.py
CHANGED
@@ -142,22 +142,14 @@ def extract_and_paste_insect(original_image: np.ndarray, detection: DetectionRes
|
|
142 |
insect_crop = original_image[ymin:ymax, xmin:xmax]
|
143 |
mask_crop = mask[ymin:ymax, xmin:xmax]
|
144 |
|
145 |
-
#
|
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 |
-
|
152 |
-
|
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 |
-
#
|
160 |
-
background[
|
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)
|
|
|
142 |
insect_crop = original_image[ymin:ymax, xmin:xmax]
|
143 |
mask_crop = mask[ymin:ymax, xmin:xmax]
|
144 |
|
145 |
+
# Ensure that we keep the original colors of the insect
|
|
|
|
|
|
|
146 |
insect = cv2.bitwise_and(insect_crop, insect_crop, mask=mask_crop)
|
147 |
|
148 |
+
x_offset, y_offset = xmin, ymin
|
149 |
+
x_end, y_end = x_offset + insect.shape[1], y_offset + insect.shape[0]
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
+
# Place the insect onto the yellow background
|
152 |
+
background[y_offset:y_end, x_offset:x_end] = insect
|
153 |
|
154 |
def create_yellow_background_with_insects(image: np.ndarray, detections: List[DetectionResult]) -> np.ndarray:
|
155 |
yellow_background = np.full((image.shape[0], image.shape[1], 3), (0, 255, 255), dtype=np.uint8)
|