Question by
Zanolon · Nov 19, 2020 at 12:47 PM ·
datascreenshotdata storage
Annotations and Screenshots not lining up
I'm using Unity to take annotated screenshots of images for a machine learning project. For whatever reason, sometimes the label of the image and annotation number don't line up. Sometimes it'll be off by one, as in, image 7 will correspond to annotations 6. Does anyone see why that might occur in my code?
```
public class randomizer : MonoBehaviour {
public int model_num;
public float timer;
public float rand_anim_sleep_time;
float[] camera_coords = new float[3];
public string model_name;
public bool model_up;
public GameObject selected_model;
public GameObject camera;
public GameObject ball;
public Rect bound_box;
public int counter;
// Start is called before the first frame update
void Start()
{
camera = GameObject.Find("Main Camera");
ball = GameObject.Find("st_ball_000");
counter = 0;
}
public static Rect GUIRectWithObject(GameObject go)
{
Vector3 cen = go.GetComponent<Collider>().bounds.center;
Vector3 ext = go.GetComponent<Collider>().bounds.extents;
Vector2[] extentPoints = new Vector2[8]
{
HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z-ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z-ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z+ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z+ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z-ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z-ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z+ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z+ext.z))
};
Vector2 min = extentPoints[0];
Vector2 max = extentPoints[0];
foreach(Vector2 v in extentPoints)
{
min = Vector2.Min(min, v);
max = Vector2.Max(max, v);
}
return new Rect(min.x, min.y, max.x-min.x, max.y-min.y);
}
// Update is called once per frame
void Update()
{
// initializes new UnityEngine.Random model
if (timer == 0)
{
rand_anim_sleep_time = UnityEngine.Random.value;
model_num = UnityEngine.Random.Range(0, 10);
model_name = "model_00" + model_num.ToString();
selected_model = GameObject.Find(model_name);
Vector3 selected_model_position = new Vector3((UnityEngine.Random.value*6) - 3, 0, (UnityEngine.Random.value*6) - 3);
selected_model.transform.position = selected_model_position;
selected_model.transform.rotation = Quaternion.Euler(0, (UnityEngine.Random.value*360) - 180, 0);
Vector3 ball_transform_position = new Vector3( selected_model_position[0] + (UnityEngine.Random.value*2) - 1, .1f, selected_model_position[2] + (UnityEngine.Random.value*2) - 1);
ball.transform.position = ball_transform_position;
camera_coords[0] = (UnityEngine.Random.value*10) - 5; // [-5, 5]
camera_coords[1] = (UnityEngine.Random.value*2) + 1; // [1, 3]
camera_coords[2] = (UnityEngine.Random.value*-9) - 1; // [-1, -10]
camera.transform.position = new Vector3(camera_coords[0], camera_coords[1], camera_coords[2]);
camera.transform.LookAt(ball.transform);
camera.transform.rotation = Quaternion.Euler(UnityEngine.Random.value*40 - 20, UnityEngine.Random.value*40 - 20, UnityEngine.Random.value*40 - 20)
bound_box = GUIRectWithObject(ball);
selected_model.GetComponent<Animator>().Play("", -1, 0);
}
timer += Time.deltaTime;
// lets the animation play out before killing
if (timer > rand_anim_sleep_time && timer < rand_anim_sleep_time + Time.deltaTime)
{
string folderPath = Directory.GetCurrentDirectory() + "/imgs/";
var sr = File.AppendText(folderPath+"annotations.txt");
ScreenCapture.CaptureScreenshot(System.IO.Path.Combine(folderPath, counter.ToString() + @".png"));
sr.WriteLine(counter.ToString()+ ","+bound_box.ToString());
sr.Close();
}
if (timer > rand_anim_sleep_time + .1)
{
counter++;
Debug.Log(counter);
selected_model.transform.position = new Vector3(-10, -10, -10);
timer = 0;
}
}
} ```
Comment
Your answer
Follow this Question
Related Questions
Is there any tutorials on using MySQL and Unity? 0 Answers
how to initiate a file in the persistent data path using unity editor project tab ? 0 Answers
What is the best way to achieve local database for mobile? 0 Answers
Some help with Data Save/Load would be most appreciated 3 Answers
Save multiple inputs for one item and print out all items 0 Answers