(annotations, image_like)
| 340 | |
| 341 | |
| 342 | def crop_image(annotations, image_like): |
| 343 | if isinstance(image_like, str): |
| 344 | image = Image.open(image_like) |
| 345 | else: |
| 346 | image = image_like |
| 347 | ori_w, ori_h = image.size |
| 348 | mask_h, mask_w = annotations[0]["segmentation"].shape |
| 349 | if ori_w != mask_w or ori_h != mask_h: |
| 350 | image = image.resize((mask_w, mask_h)) |
| 351 | cropped_boxes = [] |
| 352 | cropped_images = [] |
| 353 | not_crop = [] |
| 354 | origin_id = [] |
| 355 | for _, mask in enumerate(annotations): |
| 356 | if np.sum(mask["segmentation"]) <= 100: |
| 357 | continue |
| 358 | origin_id.append(_) |
| 359 | bbox = get_bbox_from_mask(mask["segmentation"]) # mask 的 bbox |
| 360 | cropped_boxes.append(segment_image(image, bbox)) # 保存裁剪的图片 |
| 361 | # cropped_boxes.append(segment_image(image,mask["segmentation"])) |
| 362 | cropped_images.append(bbox) # 保存裁剪的图片的bbox |
| 363 | return cropped_boxes, cropped_images, not_crop, origin_id, annotations |
| 364 | |
| 365 | |
| 366 | def box_prompt(masks, bbox, target_height, target_width): |
no test coverage detected