-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
118 additions
and
126 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,23 +1 @@ | ||
<div align="center"> | ||
<img src="https://github.com/RapidAI/LabelConvert/releases/download/v0/LabelConvertv3.png" width="55%" height="55%"/> | ||
</div> | ||
|
||
|
||
## Label Convert | ||
|
||
<p align="left"> | ||
<a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.12-aff.svg"></a> | ||
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a> | ||
<a href="https://github.com/RapidAI/LabelConvert/graphs/contributors"><img src="https://img.shields.io/github/contributors/RapidAI/LabelConvert?color=9ea"></a> | ||
<a href="https://github.com/RapidAI/LabelConvert/stargazers"><img src="https://img.shields.io/github/stars/RapidAI/LabelConvert?color=ccf" ></a> | ||
<a href="https://pepy.tech/project/label_convert"><img src="https://static.pepy.tech/badge/label_convert?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a> | ||
<a href="https://pypi.org/project/label_convert/"><img alt="PyPI" src="https://img.shields.io/pypi/v/label_convert"></a> | ||
<a href="https://choosealicense.com/licenses/apache-2.0/"><img src="https://img.shields.io/badge/License-Apache%202-dfd.svg"></a> | ||
<a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a> | ||
<a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a> | ||
</p> | ||
|
||
A dataset format conversion tool for object detection and image segmentation, which supports mutual conversion between **labelme, labelImg tools and YOLO, VOC, and COCO** dataset formats. | ||
|
||
|
||
### See details for [LabelConvert](https://github.com/RapidAI/LabelConvert) |
This file was deleted.
Oops, something went wrong.
This file contains 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,116 @@ | ||
# -*- encoding: utf-8 -*- | ||
# @Author: SWHL | ||
# @Contact: liekkaskono@163.com | ||
import argparse | ||
import json | ||
import platform | ||
from pathlib import Path | ||
import random | ||
|
||
import cv2 | ||
|
||
|
||
class VisCOCO: | ||
def __init__( | ||
self, | ||
): | ||
self.font_size = 0.7 | ||
|
||
def __call__(self, img_id: int, json_path, img_path): | ||
with open(json_path, "r", encoding="utf-8") as annos: | ||
anno_dict = json.load(annos) | ||
|
||
anno_imgs = anno_dict.get("images", None) | ||
if anno_imgs is None: | ||
raise ValueError(f"The images of {json_path} cannot be empty.") | ||
|
||
print("The anno_dict num_key is:", len(anno_dict)) | ||
print("The anno_dict key is:", anno_dict.keys()) | ||
print("The anno_dict num_images is:", len(anno_imgs)) | ||
|
||
categories = anno_dict["categories"] | ||
categories_dict = {c["id"]: c["name"] for c in categories} | ||
|
||
class_nums = len(categories_dict.keys()) | ||
color = [ | ||
(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) | ||
for _ in range(class_nums) | ||
] | ||
|
||
img_info = anno_dict["images"][img_id - 1] | ||
img_name = img_info.get("file_name") | ||
|
||
img_full_path = Path(img_path) / img_name | ||
image = cv2.imread(str(img_full_path)) | ||
|
||
annotations = anno_dict["annotations"] | ||
num_bbox = 0 | ||
img_id = img_info.get("id") | ||
for anno in annotations: | ||
if anno["image_id"] != img_id: | ||
continue | ||
|
||
num_bbox += 1 | ||
|
||
class_id = anno["category_id"] | ||
class_name = categories_dict[class_id] | ||
class_color = color[class_id - 1] | ||
|
||
x, y, w, h = list(map(int, anno["bbox"])) | ||
cv2.rectangle( | ||
image, (int(x), int(y)), (int(x + w), int(y + h)), class_color, 2 | ||
) | ||
|
||
txt_size = cv2.getTextSize( | ||
class_name, cv2.FONT_HERSHEY_SIMPLEX, self.font_size, 1 | ||
)[0] | ||
cv2.rectangle( | ||
image, | ||
(x, y + 1), | ||
(x + txt_size[0] + 5, y - int(1.5 * txt_size[1])), | ||
class_color, | ||
-1, | ||
) | ||
cv2.putText( | ||
image, | ||
class_name, | ||
(x + 5, y - 5), | ||
cv2.FONT_HERSHEY_SIMPLEX, | ||
self.font_size, | ||
(255, 255, 255), | ||
1, | ||
) | ||
|
||
print("The unm_bbox of the display image is:", num_bbox) | ||
|
||
cur_os = platform.system() | ||
if cur_os == "Windows": | ||
cv2.namedWindow(img_name, 0) | ||
cv2.resizeWindow(img_name, 1000, 1000) | ||
cv2.imshow(img_name, image) | ||
cv2.waitKey(0) | ||
else: | ||
save_path = f"vis_{Path(img_name).stem}.jpg" | ||
cv2.imwrite(save_path, image) | ||
print(f"The {save_path} has been saved the current director.") | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--img_id", type=int, default=1, help="visual which one") | ||
parser.add_argument( | ||
"--json_path", | ||
type=str, | ||
default="tests/test_files/COCO_dataset/annotations/instances_train2017.json", | ||
) | ||
parser.add_argument( | ||
"--img_dir", type=str, default="tests/test_files/COCO_dataset/train2017" | ||
) | ||
args = parser.parse_args() | ||
|
||
viser = VisCOCO() | ||
viser(args.img_id, args.json_path, args.img_dir) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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