From 05d23535e05dd6362ac50edb156da335ce451a34 Mon Sep 17 00:00:00 2001 From: wzm2256 Date: Mon, 5 Feb 2024 12:47:34 +0100 Subject: [PATCH] remove labelme and cv2 dependency Simply copy and paste 3 related function in labelme into this file. Now support all labelme versions, thus it closes https://github.com/rooneysh/Labelme2YOLO/issues/12 --- labelme2yolo.py | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/labelme2yolo.py b/labelme2yolo.py index 55238d7..fb842d0 100644 --- a/labelme2yolo.py +++ b/labelme2yolo.py @@ -1,8 +1,10 @@ ''' Created on Aug 18, 2021 -@author: xiaosonh +@ author: xiaosonh +@ modify: wzm2256 ''' + import os import sys import argparse @@ -11,12 +13,33 @@ from collections import OrderedDict import json -import cv2 +# import cv2 import PIL.Image - +import pdb + from sklearn.model_selection import train_test_split -from labelme import utils +# from labelme import utils +import base64 +import io +import numpy as np + +def img_data_to_pil(img_data): + f = io.BytesIO() + f.write(img_data) + img_pil = PIL.Image.open(f) + return img_pil + +def img_data_to_arr(img_data): + img_pil = img_data_to_pil(img_data) + img_arr = np.array(img_pil) + return img_arr + + +def img_b64_to_arr(img_b64): + img_data = base64.b64decode(img_b64) + img_arr = img_data_to_arr(img_data) + return img_arr class Labelme2YOLO(object): @@ -127,7 +150,9 @@ def convert_one(self, json_name): def _get_yolo_object_list(self, json_data, img_path): yolo_obj_list = [] - img_h, img_w, _ = cv2.imread(img_path).shape + # img_h, img_w, _ = cv2.imread(img_path).shape + # pdb.set_trace() + img_w, img_h = PIL.Image.open(img_path).size for shape in json_data['shapes']: # labelme circle shape is different from others # it only has 2 points, 1st is circle center, 2nd is drag end point @@ -235,8 +260,14 @@ def _save_yolo_image(self, json_data, json_name, image_dir_path, target_dir): img_path = os.path.join(image_dir_path, target_dir,img_name) if not os.path.exists(img_path): - img = utils.img_b64_to_arr(json_data['imageData']) - PIL.Image.fromarray(img).save(img_path) + if json_data['imageData']: + # print("Using json['imageData']...") + img = img_b64_to_arr(json_data['imageData']) + PIL.Image.fromarray(img).save(img_path) + else: + raw_path = os.path.join(self._json_dir, json_data['imagePath']) + # print("Using raw imaging...") + PIL.Image.open(raw_path).save(img_path) return img_path