use multiprocessing to boost converting speed

This commit is contained in:
Wang Xin 2022-07-27 12:19:14 +08:00
parent 3cb919641a
commit fe3af9b104
1 changed files with 16 additions and 9 deletions

View File

@ -10,6 +10,7 @@ import argparse
import shutil import shutil
import math import math
from collections import OrderedDict from collections import OrderedDict
from multiprocessing import Pool
import json import json
import cv2 import cv2
@ -101,7 +102,16 @@ class Labelme2YOLO(object):
# also get image from labelme json file and save them under images folder # also get image from labelme json file and save them under images folder
for target_dir, json_names in zip(('train/', 'val/', 'test/'), for target_dir, json_names in zip(('train/', 'val/', 'test/'),
(train_json_names, val_json_names, test_json_names)): (train_json_names, val_json_names, test_json_names)):
pool = Pool(os.cpu_count() - 1)
for json_name in json_names: for json_name in json_names:
pool.apply_async(self.covert_json_to_text, args=(target_dir, json_name))
pool.close()
pool.join()
print('Generating dataset.yaml file ...')
self._save_dataset_yaml()
def covert_json_to_text(self, target_dir, json_name):
json_path = os.path.join(self._json_dir, json_name) json_path = os.path.join(self._json_dir, json_name)
json_data = json.load(open(json_path)) json_data = json.load(open(json_path))
@ -118,9 +128,6 @@ class Labelme2YOLO(object):
target_dir, target_dir,
yolo_obj_list) yolo_obj_list)
print('Generating dataset.yaml file ...')
self._save_dataset_yaml()
def convert_one(self, json_name): def convert_one(self, json_name):
json_path = os.path.join(self._json_dir, json_name) json_path = os.path.join(self._json_dir, json_name)
json_data = json.load(open(json_path)) json_data = json.load(open(json_path))