Skip to content

add yolov8 to coco#21

Open
Lwen1243 wants to merge 2 commits into
RapidAI:mainfrom
Lwen1243:main
Open

add yolov8 to coco#21
Lwen1243 wants to merge 2 commits into
RapidAI:mainfrom
Lwen1243:main

Conversation

@Lwen1243

Copy link
Copy Markdown

增加了yolov8 格式 转 coco代码

@SWHL

SWHL commented Jun 25, 2026

Copy link
Copy Markdown
Member

感谢,这个等我有时间了整理一下,再merge进去,发新的版本。

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new dataset conversion path to the codebase: converting a YOLOv8-style dataset layout into COCO (images + instances_{train,val}2017.json), along with a basic pytest that verifies the expected output artifacts are generated.

Changes:

  • Added label_convert/yolov8_to_coco.py implementing a YOLOv8-to-COCO converter (CLI + conversion logic).
  • Added tests/test_yolov8_to_coco.py to validate output directories and annotation JSON files are created.
  • Updated .gitignore to ignore test/ and normalized the .DS_Store entry.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 7 comments.

File Description
label_convert/yolov8_to_coco.py New YOLOv8 dataset to COCO conversion implementation and CLI entrypoint.
tests/test_yolov8_to_coco.py New test ensuring conversion output structure is produced.
.gitignore Adds test/ ignore entry and normalizes .DS_Store line.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +37 to +40
if save_dir is None:
save_dir = self.data_dir.parent / f"{Path(self.data_dir).name}_coco"
self.save_dir = save_dir
self.mkdir(self.save_dir)
Comment on lines +68 to +72
if self.yaml_path is not None:
yaml_data = self.read_yaml(self.yaml_path)
class_names = list(yaml_data["names"].values())
else:
class_names = self._get_class_names_from_labels()
Comment on lines +124 to +136
def _get_category(self, class_names):
categories = []
for i, name in enumerate(class_names):
if not name:
continue
categories.append(
{
"supercategory": name,
"id": i,
"name": name,
}
)
return categories
Comment on lines +171 to +178
img_src = cv2.imread(str(img_path))
if img_path.suffix.lower() == ".jpg":
shutil.copyfile(img_path, save_img_path)
else:
cv2.imwrite(str(save_img_path), img_src)

height, width = img_src.shape[:2]
image_info = {
Comment on lines +224 to +241
@staticmethod
def get_annotation_from_rectangle(vertex_info, height, width):
cx, cy, w, h = [float(i) for i in vertex_info]

cx = cx * width
cy = cy * height
box_w = w * width
box_h = h * height

x0 = max(cx - box_w / 2, 0)
y0 = max(cy - box_h / 2, 0)
x1 = min(x0 + box_w, width)
y1 = min(y0 + box_h, height)

segmentation = [[x0, y0, x1, y0, x1, y1, x0, y1]]
bbox = [x0, y0, box_w, box_h]
area = box_w * box_h
return segmentation, bbox, area
Comment on lines +282 to +288
@staticmethod
def read_yaml(yaml_path):
import yaml

with open(yaml_path, "rb") as f:
data = yaml.load(f, Loader=yaml.Loader)
return data
Comment on lines +247 to +251
new_points = np.copy(points)
new_points[:, 0] = points[:, 0] * width
new_points[:, 1] = points[:, 1] * height

segmentation = new_points.tolist()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants