Skip to content
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

Make package #26

Open
wants to merge 6 commits into
base: main
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
3 changes: 3 additions & 0 deletions json2yolo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from json2yolo.general_json2yolo import convert_coco_json, convert_ath_json, convert_vott_json, convert_infolks_json

__all__ = ["convert_coco_json", "convert_ath_json", "convert_vott_json", "convert_infolks_json"]
68 changes: 1 addition & 67 deletions general_json2yolo.py → json2yolo/general_json2yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas as pd
from PIL import Image

from utils import *
from json2yolo.utils import *


# Convert INFOLKS JSON file into YOLO-format labels ----------------------------
Expand Down Expand Up @@ -295,72 +295,6 @@ def convert_coco_json(json_dir='../coco/annotations/', use_segments=False, cls91
with open((fn / f).with_suffix('.txt'), 'a') as file:
file.write(('%g ' * len(line)).rstrip() % line + '\n')

def min_index(arr1, arr2):
"""Find a pair of indexes with the shortest distance.
Args:
arr1: (N, 2).
arr2: (M, 2).
Return:
a pair of indexes(tuple).
"""
# (N, M)
dis = ((arr1[:, None, :] - arr2[None, :, :]) ** 2).sum(-1)
index = np.unravel_index(np.argmin(dis, axis=None), dis.shape)
return index


def merge_multi_segment(segments):
"""Merge multi segments to one list.
Find the coordinates with min distance between each segment,
then connect these coordinates with one thin line to merge all
segments into one.

Args:
segments(List(List)): original segmentations in coco's json file.
like [segmentation1, segmentation2,...],
each segmentation is a list of coordinates.
"""
s = []
segments = [np.array(i).reshape(-1, 2) for i in segments]
idx_list = [[] for _ in range(len(segments))]

# record the indexes with min distance between each segment
for i in range(1, len(segments)):
idx1, idx2 = min_index(segments[i - 1], segments[i])
idx_list[i - 1].append(idx1)
idx_list[i].append(idx2)

# use two round to connect all the segments
for k in range(2):
# forward connection
if k == 0:
for i, idx in enumerate(idx_list):
# middle segments have two indexes
# reverse the index of middle segments
if len(idx) == 2 and idx[0] > idx[1]:
idx = idx[::-1]
segments[i] = segments[i][::-1, :]

segments[i] = np.roll(segments[i], -idx[0], axis=0)
segments[i] = np.concatenate([segments[i], segments[i][0:1]])
# deal with the first segment and the last one
if i == 0 or i == (len(idx_list) - 1):
s.append(segments[i])
# deal with the middle ones
else:
idx = [0, idx[1] - idx[0]]
s.append(segments[i][idx[0]:idx[1] + 1])

# backward connection
else:
for i in range(len(idx_list) - 1, -1, -1):
if i != 0 and i != (len(idx_list) - 1):
idx = idx_list[i]
nidx = abs(idx[1] - idx[0])
s.append(segments[i][nidx:])
return s



if __name__ == '__main__':
source = 'COCO'
Expand Down
2 changes: 1 addition & 1 deletion labelbox_json2yolo.py → json2yolo/labelbox_json2yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from PIL import Image
from tqdm import tqdm

from utils import make_dirs
from json2yolo.utils import make_dirs


def convert(file, zip=True):
Expand Down
66 changes: 66 additions & 0 deletions utils.py → json2yolo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,69 @@ def coco91_to_coco80_class(): # converts 80-index (val2014) to 91-index (paper)
51, 52, 53, 54, 55, 56, 57, 58, 59, None, 60, None, None, 61, None, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
None, 73, 74, 75, 76, 77, 78, 79, None]
return x


def min_index(arr1, arr2):
"""Find a pair of indexes with the shortest distance.
Args:
arr1: (N, 2).
arr2: (M, 2).
Return:
a pair of indexes(tuple).
"""
# (N, M)
dis = ((arr1[:, None, :] - arr2[None, :, :]) ** 2).sum(-1)
index = np.unravel_index(np.argmin(dis, axis=None), dis.shape)
return index


def merge_multi_segment(segments):
"""Merge multi segments to one list.
Find the coordinates with min distance between each segment,
then connect these coordinates with one thin line to merge all
segments into one.

Args:
segments(List(List)): original segmentations in coco's json file.
like [segmentation1, segmentation2,...],
each segmentation is a list of coordinates.
"""
s = []
segments = [np.array(i).reshape(-1, 2) for i in segments]
idx_list = [[] for _ in range(len(segments))]

# record the indexes with min distance between each segment
for i in range(1, len(segments)):
idx1, idx2 = min_index(segments[i - 1], segments[i])
idx_list[i - 1].append(idx1)
idx_list[i].append(idx2)

# use two round to connect all the segments
for k in range(2):
# forward connection
if k == 0:
for i, idx in enumerate(idx_list):
# middle segments have two indexes
# reverse the index of middle segments
if len(idx) == 2 and idx[0] > idx[1]:
idx = idx[::-1]
segments[i] = segments[i][::-1, :]

segments[i] = np.roll(segments[i], -idx[0], axis=0)
segments[i] = np.concatenate([segments[i], segments[i][0:1]])
# deal with the first segment and the last one
if i == 0 or i == (len(idx_list) - 1):
s.append(segments[i])
# deal with the middle ones
else:
idx = [0, idx[1] - idx[0]]
s.append(segments[i][idx[0]:idx[1] + 1])

# backward connection
else:
for i in range(len(idx_list) - 1, -1, -1):
if i != 0 and i != (len(idx_list) - 1):
idx = idx_list[i]
nidx = abs(idx[1] - idx[0])
s.append(segments[i][nidx:])
return s
9 changes: 0 additions & 9 deletions requirements.txt

This file was deleted.

18 changes: 18 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from setuptools import setup

setup(name='json2yolo',
version='0.0.1',
author='Ultralytics',
packages=['json2yolo'],
description='Dataset converter for yolo',
license='GPL',
install_requires=[
"numpy",
"opencv-python>=4.1.2",
"pandas",
"Pillow",
"pyYAML",
"requests",
"tqdm",
],
)