-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundingBoxer.cs
215 lines (200 loc) · 9.51 KB
/
BoundingBoxer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Newtonsoft.Json.Converters;
public class BoundingBoxer : MonoBehaviour
{
public Camera cam;
public List<GameObject> objects;
[HideInInspector]
public Dictionary<string, int> classes;
private List<Dictionary<string,string>> categories;
[HideInInspector]
public List<Dictionary<string,float>> annotations;
[HideInInspector]
public List<int> images;
[HideInInspector]
public int framecount;
[HideInInspector]
public int id = 0;
// Dictionary<string,float> niryoannot;
public string path;
GameObject[] objectsTaged;
// Start is called before the first frame update
void Start()
{
classes = new Dictionary<string, int>();
images = new List<int>();
annotations = new List<Dictionary<string, float>>();
// niryoannot = GetNiryoBounds();
Debug.Log($"cam dims {cam.pixelHeight}, {cam.pixelWidth}");
// staticSceneAnnotator(); // for DanielMaciejLeoProject
}
// Update is called once per frame
public void getbb() {
// for automatic data generation we use list of objects that is generated by scence controlled
// we iterate over it, getting poses of each object
// there if object is emtyp it means that we want a static scene, thus we itereate over the objects in the scence
foreach (GameObject obj in objects) {
var bds = obj.GetComponent<Renderer>().bounds;
// getiing max and min point of the bb
Vector3 min_pt = bds.min;
Vector3 max_pt = bds.max;
// Debug.Log($"in world {min_pt} {max_pt}");
// transfering them to the camera coordinates
max_pt = cam.WorldToScreenPoint(max_pt);
min_pt = cam.WorldToScreenPoint(min_pt);
Debug.Log($"original {bds.min}, on the screen {max_pt}, backtoorgin {cam.ScreenToWorldPoint(max_pt)}");
// bb coordinates for the coco annot
Dictionary<string,float> newannot = new Dictionary<string,float>();
newannot["x"] = min_pt.x;
newannot["y"] = cam.pixelHeight - max_pt.y;
newannot["width"] = max_pt.x - min_pt.x;
Debug.Log($"in canm {min_pt} {max_pt}");
// SleepTimeout(15);
newannot["height"] = max_pt.y - min_pt.y;
newannot["category_id"] = ((float)obj.layer);
newannot["image_id"] = ((float)framecount);
// Debug.Log($"{min_pt.x}, {newannot["y"]} {newannot["height"]} {newannot["width"]}");
// float image_id =
annotations.Add(newannot);
}
annotations.Add(GetNiryoBounds());
// after all the annotations were assign we have to zeroout the list
objects = new List<GameObject>();
}
Dictionary<string,float> GetNiryoBounds() {
GameObject niryo = GameObject.Find("niryo_one");
// Bounds bds = niryo.GetComponent<Renderer>().bounds
// // created = new GameObject[maxObjects];
Collider m_Collider = niryo.GetComponent<Collider>();
//Fetch the center of the Collider volume
// Vector3 m_Center = m_Collider.bounds.center;
// //Fetch the size of the Collider volume
// Vector3 m_Size = m_Collider.bounds.size;
//Fetch the minimum and maximum bounds of the Collider volume
Vector3 min_pt = m_Collider.bounds.min;
Vector3 max_pt = m_Collider.bounds.max;
//Output this data into the console
Dictionary<string,float> newannot = new Dictionary<string,float>();
max_pt = cam.WorldToScreenPoint(max_pt);
min_pt = cam.WorldToScreenPoint(min_pt);
// hardcoded - > change to more felx v
if (max_pt.y > cam.pixelHeight)
max_pt.y = 512f;
newannot["x"] = min_pt.x;
newannot["y"] = cam.pixelHeight - max_pt.y;
newannot["width"] = max_pt.x - min_pt.x;
Debug.Log($"in canm {min_pt} {max_pt}");
// SleepTimeout(15);
newannot["height"] = max_pt.y - min_pt.y;
newannot["category_id"] = ((float)niryo.layer);
newannot["image_id"] = ((float)framecount);
return newannot;
// Debug.Log("Collider Center : " + m_Center);
// Debug.Log("Collider Size : " + m_Size);
// Debug.Log("Collider bound Minimum : " + m_Min);
// Debug.Log("Collider bound Maximum : " + m_Max);
// Debug.Log($" robot min {min_pt} ,robot max {max_pt}");
}
public void createText() {
using (StreamWriter writer = new StreamWriter($"{path}/labels.json"))
// creating annotation in coco format https://gist.github.com/akTwelve/c7039506130be7c0ad340e9e862b78d9
{
writer.WriteLine("{");
writer.WriteLine("\"images\": [ ");
foreach (int im in images) {
writer.WriteLine("{");
writer.WriteLine("\"id\": "+ $"{im},");
writer.WriteLine("\"file_name\": "+ $"\"{im}_img.png\",");
writer.WriteLine("\"height\": 512,");
writer.WriteLine("\"width\": 512");
if (im == images[images.Count-1]) {
writer.WriteLine("}");
} else {
writer.WriteLine("},");
}
}
writer.WriteLine("],");
writer.WriteLine("\"annotations\": [");
foreach (Dictionary<string,float> annot in annotations) {
writer.WriteLine("{");
writer.WriteLine($"\"id\": {id},");
id++;
writer.WriteLine($"\"category_id\": {annot["category_id"]},"); // po prostu id not category_id
writer.WriteLine($"\"image_id\": {annot["image_id"]},");
writer.WriteLine($"\"bbox\": [{annot["x"]},{annot["y"]},{annot["width"]},{annot["height"]}]");
if (annot == annotations[annotations.Count-1]) {
writer.WriteLine("}");
} else {
writer.WriteLine("},");
}
}
writer.WriteLine("],");
writer.WriteLine("\"categories\": [");
writer.WriteLine("{");
writer.WriteLine($"\"id\": {6},");
writer.WriteLine($"\"name\": \"bottle\"");
writer.WriteLine("},");
writer.WriteLine("{");
writer.WriteLine($"\"id\": {8},");
writer.WriteLine($"\"name\": \"fruit\"");
writer.WriteLine("},");
writer.WriteLine("{");
writer.WriteLine($"\"id\": {10},");
writer.WriteLine($"\"name\": \"robot\"");
writer.WriteLine("},");
writer.WriteLine("{");
writer.WriteLine($"\"id\": {9},");
writer.WriteLine($"\"name\": \"torch\"");
writer.WriteLine("}");
writer.WriteLine("]");
// the end of the file
writer.WriteLine("}");
}
}
public void staticSceneAnnotator(string name = "labels") {
objectsTaged = GameObject.FindGameObjectsWithTag("object");
GameObject[] mainBase = GameObject.FindGameObjectsWithTag("bases");
using (StreamWriter writer = new StreamWriter($"{path}/labels/{name}.json")) {
// adding base first
writer.WriteLine("{");
writer.WriteLine($"\"{0}\":");
writer.WriteLine("{");
int length = mainBase[0].gameObject.name.Length;
name = mainBase[0].gameObject.name;
writer.WriteLine($"\"name\": \"{name.Remove(length-7, 7)}\",");
writer.Write($"\"position\": [0,0,0],");
writer.Write($"\"rotation\": [0,0,0,1],");
writer.Write($"\"scale\": [1,1,1]");
writer.WriteLine("},");
for (int i = 0; i < objectsTaged.Length; i++) {
// adding every object present in the scene
writer.WriteLine($"\"{i+1}\":");
writer.WriteLine("{");
length = objectsTaged[i].gameObject.name.Length;
name = objectsTaged[i].gameObject.name;
writer.WriteLine($"\"name\": \"{name.Remove(length-7, 7)}\",");
writer.Write($"\"position\": [{objectsTaged[i].gameObject.transform.position.x},{objectsTaged[i].gameObject.transform.position.z},{objectsTaged[i].gameObject.transform.position.y}],");
writer.Write($"\"rotation\": [{objectsTaged[i].gameObject.transform.rotation.x},{objectsTaged[i].gameObject.transform.rotation.z},{objectsTaged[i].gameObject.transform.rotation.y}, {objectsTaged[i].gameObject.transform.rotation.w}],");
writer.Write($"\"scale\": [{objectsTaged[i].gameObject.transform.localScale.x},{objectsTaged[i].gameObject.transform.localScale.z},{objectsTaged[i].gameObject.transform.localScale.y}]");
if (i == objectsTaged.Length-1) {
writer.WriteLine("}");
} else
writer.WriteLine("},");
}
writer.WriteLine("}");
}
Debug.Log("Annotations created!");
// Quit();
}
public void Quit() {
#if UNITY_STANDALONE
Application.Quit();
#endif
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
}