Skip to content

Added crop by class about ISD data type #146

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 7 commits into
base: develop
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
35 changes: 31 additions & 4 deletions labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from labelme.config import get_config
from labelme.cli import draw_object_label
from labelme.cli import draw_segment_label
from labelme.cli import crop_label_class
from labelme.label_file import LabelFile
from labelme.label_file import LabelFileError
from labelme.logger import logger
Expand Down Expand Up @@ -573,6 +574,22 @@ def __init__(
enabled=False,
)

crop_classes = action(
self.tr("Crop Classes"),
self.crop_classes,
shortcuts["crop_classes"],
icon="eye",
tip=self.tr("Crop classes"),
enabled=False,
)

delete_label_folder = action(
self.tr("Delete Label Folder"),
self.delete_label_dir,
tip=self.tr("Delete label folder"),
enabled=False,
)

convert_segmentation = action(
self.tr("Convert\nSegmentation"),
self.convert_segments,
Expand Down Expand Up @@ -829,9 +846,8 @@ def __init__(
editMode,
convert_objects,
),
onShapesPresent=(saveAs, hideAll, showAll, #),
hidePolygons, hideRectangles),
onAdministrator=(administrator,),
onShapesPresent=(saveAs, hideAll, showAll, hidePolygons, hideRectangles),
onAdministrator=(administrator, crop_classes, delete_label_folder),
)

self.canvas.vertexSelected.connect(self.actions.removePoint.setEnabled)
Expand Down Expand Up @@ -872,7 +888,9 @@ def __init__(
administrator,
None,
convert_segmentation,
convert_objects
convert_objects,
crop_classes,
delete_label_folder
)
)
utils.addActions(
Expand Down Expand Up @@ -1177,6 +1195,15 @@ def check_labels(self):
self.ImagePopup.object_widget_state = True
self.ImagePopup.popUp(self.filename, True)

def crop_classes(self):
folder_path = os.path.split(self.filename)[0]
wait_popup = ConvertLabelPopup()
crop_label_class.crop_labels(folder_path, wait_popup)

def delete_label_dir(self):
folder_path = os.path.split(self.filename)[0]
crop_label_class.delete_class_dir(folder_path)

def convert_segments(self):
folder_path = os.path.split(self.filename)[0]
wait_popup = ConvertLabelPopup()
Expand Down
142 changes: 142 additions & 0 deletions labelme/cli/crop_label_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env python3
# Copyright 2025 ROBOTISAI CO., LTD.
# Authors: Sunghun Jung

import cv2
import os
import sys
import yaml
import glob
import json
import shutil
import argparse

from colorama import Fore, Style


class CropLabelClass:
def __init__(self, config, input_dir, popup=None):
self.config = config
self.allow_class = self.config['ELStateDetection']
_, self.folder_name = os.path.split(input_dir)

self._delete_class_dir(input_dir)
self._created_class_dir(input_dir)

json_list = glob.glob(os.path.join(input_dir, '*.json'))

if popup is not None:
popup.show()

print('===========================================')
print('Crop label class')
print('{0}The folder name : {1}'.format(' ' * 2, self.folder_name))
print('{0}The total number of the files : {1}'.format(' ' * 2, len(json_list)))
print('===========================================')

for index, json_file in enumerate(json_list):
self._crop_image(input_dir, json_file)
if popup is not None:
popup.set_progress(int(index * (100 / len(json_list))))

self._delete_empty_class_dir(input_dir)

print('Completed convert [{0}] folder'.format(self.folder_name))

if popup is not None:
popup.close()


def _created_class_dir(self, input_dir):
for class_name in self.allow_class.keys():
class_dir = os.path.join(input_dir, class_name)
if not os.path.exists(class_dir):
os.makedirs(class_dir)

def _crop_image(self, root_path, json_file):
json_data = self._load_json(json_file)

for index, shape in enumerate(json_data['shapes']):
if shape['shape_type'] == 'rectangle':
label = shape['label']
image_name = json_data['imagePath']
if label not in self.allow_class:
print(Fore.RED + f"{image_name}: '{label}'" + Style.RESET_ALL)
continue

image_path = os.path.join(root_path, image_name)
image = cv2.imread(image_path)
if image is None:
print(f"Error: Could not read image {image_name}")
continue

points = shape['points']
x1, y1 = int(points[0][0]), int(points[0][1])
x2, y2 = int(points[1][0]), int(points[1][1])

height, width = image.shape[:2]
margin_x = int((x2 - x1) * 0.15)
margin_y = int((y2 - y1) * 0.15)

x1 = max(0, x1 - margin_x)
y1 = max(0, y1 - margin_y)
x2 = min(width, x2 + margin_x)
y2 = min(height, y2 + margin_y)

cropped_image = image[y1:y2, x1:x2]

class_dir = os.path.join(root_path, label)
base_name, ext = os.path.splitext(os.path.basename(image_name))
output_path = os.path.join(class_dir, f'{base_name}_{index}{ext}')
cv2.imwrite(output_path, cropped_image)

else:
print(f"Warning: Unsupported shape type '{shape['shape_type']}' in {json_file}. Only 'rectangle' is supported.")

def _delete_class_dir(self, input_dir):
class_dirs = [os.path.join(input_dir, class_name) for class_name in self.allow_class.keys()]
for class_dir in class_dirs:
if os.path.exists(class_dir):
shutil.rmtree(class_dir)

def _delete_empty_class_dir(self, input_dir):
class_dirs = [os.path.join(input_dir, class_name) for class_name in self.allow_class.keys()]
for class_dir in class_dirs:
if os.path.exists(class_dir) and not os.listdir(class_dir):
os.rmdir(class_dir)

def _load_json(self, json_file):
with open(json_file, mode='r', encoding='utf-8') as f:
data = json.load(f)
return data


def crop_labels(input_dir, popup=None):
class_data_yaml = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'class.yaml')

try:
print('Opening data file : {0}'.format(class_data_yaml))
f = open(class_data_yaml, 'r')
CONFIG = yaml.load(f, Loader=yaml.FullLoader)
except Exception as e:
print('Error opening data yaml file! {0}'.format(e))
sys.exit()

CropLabelClass(CONFIG, input_dir, popup)

def delete_class_dir(input_dir):
folder_list = os.listdir(input_dir)
for folder in folder_list:
folder_path = os.path.join(input_dir, folder)
if os.path.isdir(folder_path):
shutil.rmtree(folder_path)

folder_name = os.path.basename(input_dir)
print(f"Deleted all class directories in {folder_name}")

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('input_dir', help='input annotated directory')
args = parser.parse_args()

crop_labels(args.input_dir)
1 change: 1 addition & 0 deletions labelme/config/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,4 @@ shortcuts:

check_labels: Ctrl+/
delete_popup: Ctrl+Shift+Q
crop_classes: Ctrl+.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ hacking==4.1.0
pytest
pytest-qt
twine
opencv-python==4.11.0.86