利用unity制作自己的目标检测数据集
搭建场景
- 首先搭建需要进行训练的目标场景
- 对需要进行标注的三维模型设置标签,这里是为了便于射线去检测目标物体进而生成mask。
利用射线检测目标模型碰撞
1.这里直接放代码
Ray ray;
RaycastHit hitInfo;
public void savethepng(int id)
{
Texture2D tex = new Texture2D(720, 540);//初始化mask的大小,一般与当前game视图的大小一致
int[][] f = new int[720][];
for (int i = 0; i < 720; ++i)
{
f[i] = new int[540];
for (int j = 0; j < 540; ++j)
{
ray = cam.ScreenPointToRay(new Vector3(i, j, 0));//对相机照射到的视图中进行射线碰撞检测
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.transform.tag == "target")//设置待检测的目标
{
f[i][j] = 1;#检测到即设为1
}
continue;
}
f[i][j] = -1;
}
}
for (int i = 0; i < 720; ++i)
{
for (int j = 0; j < 50; ++j)
{
if (f[i][j] > 0)
{
tex.SetPixel(i, j, new Color(255, 255, 255));//对mask进行涂色
}
else
{
tex.SetPixel(i, j, new Color(0, 0, 0));
}
}
}
byte[] databytes = tex.EncodeToPNG();
fs = File.Open("保存路径" + id.ToString() + ".png", FileMode.OpenOrCreate);
fs.Write(databytes, 0, databytes.Length);
fs.Flush();
fs.Close();
}
提取外接矩形并生成label信息
1.利用opencv提取mask的最小外接矩形,并将其外接矩形的左上角点坐标及矩形的长和宽进行输出。
import cv2
import os
import numpy as np
label_path = "./img/"
for i in range(len(os.listdir(label_path))):
label_img = cv2.imread(label_path+"{}.png".format(i),0)
contours, _ = cv2.findContours(label_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
rect_x, rect_y, rect_w, rect_h = cv2.boundingRect(c)
cv2.rectangle(label_img, (rect_x, rect_y), (rect_x + rect_w, rect_y + rect_h),
(255, 255, 255), 2)
with open("./ground_truth.txt","a") as f:
f.write("{},{},{},{}\n".format(rect_x,rect_y,rect_w,rect_h))