add yolov8 to coco#21
Open
Lwen1243 wants to merge 2 commits into
Open
Conversation
Member
|
感谢,这个等我有时间了整理一下,再merge进去,发新的版本。 |
There was a problem hiding this comment.
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.pyimplementing a YOLOv8-to-COCO converter (CLI + conversion logic). - Added
tests/test_yolov8_to_coco.pyto validate output directories and annotation JSON files are created. - Updated
.gitignoreto ignoretest/and normalized the.DS_Storeentry.
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() |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
增加了yolov8 格式 转 coco代码