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

Soda10m parser #6

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
code formatted
Nielspace committed Jan 3, 2022
commit 6cbf4fba906ef0027e6dcec6ec57639e03eb09e2
114 changes: 50 additions & 64 deletions parsers/soda/parser.py
Original file line number Diff line number Diff line change
@@ -11,12 +11,10 @@
from glob import glob



class SODAParser:
annotations = {}
filename = []
filename = []
ALLOWED_PATHS = ["/train", "/test", "/val"]


def __init__(
self,
@@ -33,57 +31,60 @@ def __init__(
raise ValueError(f"path should be one of {self.ALLOWED_PATHS}")

self.path = path



def parse_annotations(self,
data_type='train',
save_annotation=True)->ImageAnnotationFile:

files = glob(f'{self.annotation_dir}/*.json')

if data_type=='train':
json_data = f'{self.annotation_dir}/instance_train.json'

def parse_annotations(
self, data_type="train", save_annotation=True
) -> ImageAnnotationFile:

files = glob(f"{self.annotation_dir}/*.json")

if data_type == "train":
json_data = f"{self.annotation_dir}/instance_train.json"
else:
json_data=f'{self.annotation_dir}/instance_val.json'
json_data = f"{self.annotation_dir}/instance_val.json"

f = open(json_data)
data = json.load(f)


for idx, file in tqdm(enumerate(data['images'], 1), total=5000, desc='parsing'):

for idx, file in tqdm(enumerate(data["images"], 1), total=5000, desc="parsing"):
coor = []
self.filename.append(file['file_name'].split('.')[0])
self.filename.append(file["file_name"].split(".")[0])
for seq, an in enumerate(data["annotations"]):
if len(an) != 0:
if an["image_id"] == idx:

coor.append(Annotation(name=data['categories'][an['category_id']-1]['name'])
.add_data(
BoundingBox(
x=int(an['bbox'][0]),
y=int(an['bbox'][1]),
w=int(an['bbox'][2]),
h=int(an['bbox'][3]))))

coor.append(
Annotation(
name=data["categories"][an["category_id"] - 1]["name"]
).add_data(
BoundingBox(
x=int(an["bbox"][0]),
y=int(an["bbox"][1]),
w=int(an["bbox"][2]),
h=int(an["bbox"][3]),
)
)
)
ann = ImageAnnotationFile(
dataset=self.dataset_name,
image=Image(
width=int(file['width']),
height=int(file['height']),
original_filename=file['file_name'],
filename=file['file_name'],
path=self.path),
annotations=coor)
dataset=self.dataset_name,
image=Image(
width=int(file["width"]),
height=int(file["height"]),
original_filename=file["file_name"],
filename=file["file_name"],
path=self.path,
),
annotations=coor,
)
if save_annotation:
self.annotations[file['file_name'].split('.')[0]] = ann


def get_annotations(self, idx:int):
self.annotations[file["file_name"].split(".")[0]] = ann

def get_annotations(self, idx: int):
ann = self.annotations[self.filename[idx]]
return ann
def save_to_json(self, path_to_save='', dir_name='annotationFolder'):
path = f'{path_to_save}/{dir_name}'

def save_to_json(self, path_to_save="", dir_name="annotationFolder"):
path = f"{path_to_save}/{dir_name}"
try:
if not os.path.exists(path):
os.mkdir(path)
@@ -94,16 +95,12 @@ def save_to_json(self, path_to_save='', dir_name='annotationFolder'):
with open(f"{path}/{filename}.json", "w") as outfile:
outfile.write(json_object)
except:
print('path exist')


def upload_to_darwin(self,
api_key:str,
image_dir: Path,
json_dir: Path):

images = glob(f'{image_dir}/*.jpg')
annotations = glob(f'{json_dir}/*.json')
print("path exist")

def upload_to_darwin(self, api_key: str, image_dir: Path, json_dir: Path):

images = glob(f"{image_dir}/*.jpg")
annotations = glob(f"{json_dir}/*.json")
client = Client.from_api_key(api_key)
dataset_identifier = f"{client.default_team}/{self.dataset_name}"
try:
@@ -117,14 +114,3 @@ def upload_to_darwin(self,
annotations,
append=True,
)