Create tools.py
Browse files- utils/tools.py +65 -0
utils/tools.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import clip
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
def convert_box_xywh_to_xyxy(box):
|
| 10 |
+
if len(box) == 4:
|
| 11 |
+
return [box[0],box[1],box[0]+box[2],box[1]+box[3]]
|
| 12 |
+
else:
|
| 13 |
+
result = []
|
| 14 |
+
for b in box:
|
| 15 |
+
b = convert_box_xywh_to_xyxy(b)
|
| 16 |
+
result.append(b)
|
| 17 |
+
return result
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def segment_image(image,bbox):
|
| 21 |
+
image_array = np.array(image)
|
| 22 |
+
segmented_image_array = np.zeros_like(image_array)
|
| 23 |
+
x1,y1,x2,y2 = bbox
|
| 24 |
+
segmented_image_array[y1:y2,x1:x2] = image_array[y1:y2,x1:x2]
|
| 25 |
+
segmented_image = Image.fromarray(segmented_image_array)
|
| 26 |
+
black_image = Image.new("RGB",image.size,(255,255,255))
|
| 27 |
+
transparency_mask = np.zeros((image_array.shape[0],image_array.shape[1]),dtype=np.uint8)
|
| 28 |
+
transparency_mask[y1:y2,x1:x2] = 255
|
| 29 |
+
transparency_mask_image = Image.fromarray(transparency_mask,mode="L")
|
| 30 |
+
black_image.paste(segmented_image,mask=transparency_mask_image)
|
| 31 |
+
return black_image
|
| 32 |
+
|
| 33 |
+
def format_results(result,filter=0):
|
| 34 |
+
annotations = []
|
| 35 |
+
n = len(result.masks.data)
|
| 36 |
+
for i in range(n):
|
| 37 |
+
annotation = []
|
| 38 |
+
mask = result.masks.data[i] == 1.0
|
| 39 |
+
|
| 40 |
+
if torch.sum(mask) < filter:
|
| 41 |
+
continue
|
| 42 |
+
annotation['id'] = i
|
| 43 |
+
annotation['segmentation'] = mask.cpu().numpy()
|
| 44 |
+
annotation['bbox'] = result.boxes.data[i]
|
| 45 |
+
annotation['score'] = result.boxes.conf[i]
|
| 46 |
+
annotation['area'] = annotation['segmentation'].sum()
|
| 47 |
+
annotations.append(annotation)
|
| 48 |
+
return annotations
|
| 49 |
+
|
| 50 |
+
def filter_masks(annotations):
|
| 51 |
+
annotations.sort(key=lambda x: x['area'],reverse=True)
|
| 52 |
+
to_remove = set()
|
| 53 |
+
for i in range(0,len(annotations)):
|
| 54 |
+
a = annotations[i]
|
| 55 |
+
for j in range(i+1,len(annotations)):
|
| 56 |
+
b = annotations[j]
|
| 57 |
+
if i!=j and (j not in to_remove):
|
| 58 |
+
if b['area'] < a['area']:
|
| 59 |
+
if (a['segmentation'] & b['segmentation']).sum()/b['segmentation'].sum()>0.8:
|
| 60 |
+
to.remove.add(j)
|
| 61 |
+
return [a for i,a in enumerate(annotations) if i not in to_remove], to_remove
|
| 62 |
+
|
| 63 |
+
def get_bbox_from_mask(mask):
|
| 64 |
+
mask = mask.astype(np.uint8)
|
| 65 |
+
contours,hierarchy = cv2.findContours(mask,cv2.RETR)
|