Skip to content

Update function #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ ENV/
MANIFEST

\.vscode/

#pycharm

.idea/
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions examples/shapes/shapes_to_coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def main():
image_id = 1
segmentation_id = 1

# filter for jpeg images
# filter for jpeg images
for root, _, files in os.walk(IMAGE_DIR):
image_files = filter_for_jpeg(root, files)
Expand All @@ -97,20 +98,18 @@ def main():

# go through each associated annotation
for annotation_filename in annotation_files:

print(annotation_filename)
class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename][0]

category_info = {'id': class_id, 'is_crowd': 'crowd' in image_filename}
binary_mask = np.asarray(Image.open(annotation_filename)
.convert('1')).astype(np.uint8)

annotation_info = pycococreatortools.create_annotation_info(
binary_mask = np.asarray(Image.open(annotation_filename)).astype(np.uint8)

annotation_info, segmentation_id = pycococreatortools.create_annotation_info(
segmentation_id, image_id, category_info, binary_mask,
image.size, tolerance=2)

if annotation_info is not None:
coco_output["annotations"].append(annotation_info)
coco_output["annotations"] += annotation_info

segmentation_id = segmentation_id + 1

Expand Down
74 changes: 60 additions & 14 deletions pycococreatortools/pycococreatortools.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ def create_image_info(image_id, file_name, image_size,

return image_info

def create_bounding_box(segmentation,bounding_box):

if len(segmentation)<2:
return bounding_box

x=[]
y=[]
bounding_box = []
for seg in segmentation:
for i in range(0,len(seg)-1,2):
x.append(seg[i])
y.append(seg[i+1])

x_min = np.array(x).min()
x_max = np.array(x).max()
y_min = np.array(y).min()
y_max = np.array(y).max()

w = x_max - x_min
h = y_max - y_min
x=[]
y=[]
bounding_box.append(np.array([x_min,y_min,w,h]))

return np.array(bounding_box)

def create_annotation_info(annotation_id, image_id, category_info, binary_mask,
image_size=None, tolerance=2, bounding_box=None):

Expand All @@ -99,17 +125,37 @@ def create_annotation_info(annotation_id, image_id, category_info, binary_mask,
segmentation = binary_mask_to_polygon(binary_mask, tolerance)
if not segmentation:
return None

annotation_info = {
"id": annotation_id,
"image_id": image_id,
"category_id": category_info["id"],
"iscrowd": is_crowd,
"area": area.tolist(),
"bbox": bounding_box.tolist(),
"segmentation": segmentation,
"width": binary_mask.shape[1],
"height": binary_mask.shape[0],
}

return annotation_info
# Sometimes 1 mask contain several same class items. This modification can help generate valid annotation info for muliple mask and bbox
bounding_box = create_bounding_box(segmentation,bounding_box)
annotations = []
if len(segmentation) > 1:
for i in range(len(segmentation)):
annotation_info = {
"id": annotation_id+i,
"image_id": image_id,
"category_id": category_info["id"],
"iscrowd": is_crowd,
"area": area.tolist(),
"bbox": bounding_box[i].tolist(),
"segmentation": [segmentation[i]],
"width": binary_mask.shape[1],
"height": binary_mask.shape[0],
}
annotations.append(annotation_info)
annotation_id+=(len(segmentation)-1)
else:
annotation_info = {
"id": annotation_id,
"image_id": image_id,
"category_id": category_info["id"],
"iscrowd": is_crowd,
"area": area.tolist(),
"bbox": bounding_box.tolist(),
"segmentation": segmentation,
"width": binary_mask.shape[1],
"height": binary_mask.shape[0],
}
annotations.append(annotation_info)


return annotations,annotation_id