yolov5/data_process/data_process.ipynb

131 lines
3.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "82e802bd",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import cv2 as cv\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5be9c4de",
"metadata": {},
"outputs": [],
"source": [
"img = cv.imread(r\"D:\\lzy\\yolov5\\yolov5\\data_org\\yolo_dataset\\test\\images\\9_jpg.rf.eb8e232922688878a850763f2127e8fd.jpg\")\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9ea190dd",
"metadata": {},
"outputs": [],
"source": [
"cv.imshow(\"img\", img)\n",
"if cv.waitKey(0) == ord(\"q\"):\n",
" cv.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "335bf1ae",
"metadata": {},
"outputs": [],
"source": [
"img_folder = r\"D:\\lzy\\yolov5\\yolov5\\data_org\\yolo_dataset\\train\\images\"\n",
"label_path = r\"D:\\lzy\\yolov5\\yolov5\\data_org\\dataset\\train\\_annotations.coco.json\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b8fe5313",
"metadata": {},
"outputs": [],
"source": [
"img_path_list = os.listdir(img_folder)\n",
"\n",
"for img_name in img_path_list:\n",
" with open(os.path.join(img_folder.replace(\"images\", \"labels\"), img_name.replace(\".jpg\", \".txt\")), \"w\") as f:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "101e48e9",
"metadata": {},
"outputs": [],
"source": [
"# 读取文件\n",
"with open(label_path, 'r') as f:\n",
" content = json.load(f)\n",
"pass"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6edd8ee3",
"metadata": {},
"outputs": [],
"source": [
"# 创建一个id到图片名称的映射以字典形式存储\n",
"dict_id2img = dict()\n",
"for img_info in content['images']:\n",
" dict_id2img[img_info['id']] = img_info['file_name']\n",
"pass"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a9923894",
"metadata": {},
"outputs": [],
"source": [
"# 开始读取标签信息处理后写入txt文件\n",
"for label_info in content['annotations']:\n",
" img_name = dict_id2img[label_info['image_id']]\n",
" class_name = label_info['category_id']\n",
" x_center = (label_info['bbox'][0] + label_info['bbox'][2] / 2) / 640\n",
" y_center = (label_info['bbox'][1] + label_info['bbox'][3] / 2) / 640\n",
" w = label_info['bbox'][1] / 640\n",
" h = label_info['bbox'][3] / 640\n",
" with open(os.path.join(img_folder.replace(\"images\", \"labels\"), img_name.replace(\".jpg\", \".txt\")), 'a') as f:\n",
" f.write(\"{} {} {} {} {}\\n\".format(class_name, x_center, y_center, w, h))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "yolov5",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}