Skip to content

Commit

Permalink
Update vis_coco
Browse files Browse the repository at this point in the history
  • Loading branch information
SWHL committed Mar 30, 2024
1 parent 1156b81 commit 5e8a8a3
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 126 deletions.
22 changes: 0 additions & 22 deletions docs/doc_whl.md
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)
102 changes: 0 additions & 102 deletions label_convert/coco_visual.py

This file was deleted.

116 changes: 116 additions & 0 deletions label_convert/vis_coco.py
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()
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_readme() -> str:
name=MODULE_NAME,
version=VERSION_NUM,
platforms="Any",
description="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.",
description="A tool for object detection and image segmentation dataset format conversion. Supports conversion between labelme tool annotated data, labelImg tool annotated data, YOLO, PubLayNet and COCO data set formats.",
long_description=get_readme(),
long_description_content_type="text/markdown",
author="SWHL",
Expand All @@ -75,7 +75,7 @@ def get_readme() -> str:
entry_points={
"console_scripts": [
f"coco_to_labelImg={MODULE_NAME}.coco_to_labelImg:main",
f"coco_visual={MODULE_NAME}.coco_visual:main",
f"vis_coco={MODULE_NAME}.vis_coco:main",
f"darknet_to_coco={MODULE_NAME}.darknet_to_coco:main",
f"labelImg_to_yolov5={MODULE_NAME}.labelImg_to_yolov5:main",
f"yolov5_to_coco={MODULE_NAME}.yolov5_to_coco:main",
Expand Down

0 comments on commit 5e8a8a3

Please sign in to comment.