forked from wkentaro/labelme
-
Notifications
You must be signed in to change notification settings - Fork 0
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
HunJ93
wants to merge
7
commits into
develop
Choose a base branch
from
feature-crop-by-class-about-ISD
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8e6923f
Added cv module
HunJ93 ac327ca
Added short cut
HunJ93 3027f73
Added crop label function
HunJ93 cacb2da
Created crop label class
HunJ93 d4adb23
Added method that delete class dir
HunJ93 58f7a17
Added logic
HunJ93 b4432c5
Merge remote-tracking branch 'origin/develop' into feature-crop-by-cl…
HunJ93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,3 +269,4 @@ shortcuts: | |
|
||
check_labels: Ctrl+/ | ||
delete_popup: Ctrl+Shift+Q | ||
crop_classes: Ctrl+. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.