-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainController.cs
190 lines (146 loc) · 5.94 KB
/
MainController.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
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainController : MonoBehaviour
{
private System.Random random;
void Awake()
{
QualitySettings.vSyncCount = 0; // VSync must be disabled
Application.targetFrameRate = 120;
}
// Use this for initialization
void Start()
{
Debug.Log("MainController start()");
random = new System.Random();
}
// Update is called once per frame
void Update()
{
if (Utils.shouldQuit())
{
return;
}
string fname;
switch (Utils.now_mode)
{
case Utils.states.NORMAL:
ChangeBackgroundImage();
PedestrianLoader model_loader = gameObject.GetComponent<PedestrianLoader>();
// randomly set light and shadow position
Light lightComp = GameObject.FindWithTag("Light_Brightness").GetComponent<Light>();
lightComp.intensity = (float) (0.8 + 1.0 * random.NextDouble());
Vector3 lightRotation = new Vector3(50, random.Next(-20, 20), 0);
lightComp.transform.eulerAngles = lightRotation;
// This is where images got saved
fname = Utils.result_image_folder + Utils.now_image_num.ToString(Utils.saving_format) + ".png";
Debug.Log(fname);
Application.CaptureScreenshot(fname);
// save joints
model_loader.SaveJointCoordinate();
break;
case Utils.states.SEG:
model_loader = gameObject.GetComponent<PedestrianLoader>();
ChangeBackgroundImageToBlack();
fname = Utils.result_segmentation_folder + Utils.now_image_num.ToString(Utils.saving_format) + ".png";
Debug.Log(fname);
Application.CaptureScreenshot(fname);
break;
default:
break;
}
// Make blur should go here
Utils.update();
}
void ChangeBackgroundImage()
{
Debug.Log("MainController.ChangeBackgroundImage()");
GameObject bgObj = GameObject.Find("BackgroundPlane");
// set background
var fn = Utils.background_fns[Utils.now_image_num % Utils.total_background_num];
Debug.Log(String.Format("<color=blue>change background to {0}</color>", fn));
var bytes = System.IO.File.ReadAllBytes(fn);
var image = new Texture2D(1, 2);
image.LoadImage(bytes);
Texture tex = bgObj.GetComponent<Renderer>().material.mainTexture;
if (tex != null)
UnityEngine.Object.Destroy(tex);
bgObj.GetComponent<Renderer>().material.mainTexture = image;
}
void ChangeBackgroundImageToBlack()
{
Debug.Log("MainController.ChangeBackgroundImageToBlack()");
// destroy background image
GameObject bgObj = GameObject.Find("BackgroundPlane");
Destroy(bgObj.GetComponent<Renderer>().material.mainTexture);
Debug.Log("Background Plane Texture Destroyed !");
}
/*
void MakeBlur(int idx, int w, float sigma)
{
Debug.Log("Blurring");
var filepath = Utils.result_image_folder + idx.ToString(Utils.saving_format) + ".png";
var bytes = System.IO.File.ReadAllBytes(filepath);
var image = new Texture2D(1, 1);
var image_blur = new Texture2D(1, 1);
image.LoadImage(bytes);
image_blur.LoadImage(bytes);
Texture2D[] segmentation = new Texture2D[5];
for (int i = 0; i < 5; i++)
{
filepath = Application.dataPath + "/../result/segmentation/" + idx.ToString("0000000") + "_" + i.ToString("00") + ".png";
var bytes_segmentation = System.IO.File.ReadAllBytes(filepath);
segmentation[i] = new Texture2D(1, 1);
segmentation[i].LoadImage(bytes_segmentation);
}
int width = 368;
int height = 368;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
bool bSeg = false;
for (int i = 0; i < 5; i++)
{
Color color = segmentation[i].GetPixel(x, y);
if (color.r == 1.0f && color.g == 1.0f && color.b == 1.0f)
bSeg = true;
}
Color mean_c = new Color(0.0f, 0.0f, 0.0f, 0.0f);
if (bSeg)
{
// blur
float sum_weight = 0.0f;
for (int wx = -w; wx <= w; wx++)
{
for (int wy = -w; wy <= w; wy++)
{
int new_x = x + wx;
int new_y = y + wy;
float weight = 1.0f / Mathf.Sqrt(2.0f * Mathf.PI * sigma * sigma) * Mathf.Exp(-(wx * wx + wy * wy) / (2.0f * (sigma * sigma)));
Color c = image.GetPixel(new_x, new_y);
mean_c += c * weight;
sum_weight += weight;
}
}
mean_c /= sum_weight;
//Color image_c = image.GetPixel (x, y);
//mean_c.a = image_c.a;
image_blur.SetPixel(x, y, mean_c);
}
}
}
byte[] bytes_png = image_blur.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/../result/image/" + idx.ToString("0000000") + "_blur.png", bytes_png);
UnityEngine.Object.Destroy(image);
UnityEngine.Object.Destroy(image_blur);
for (int i = 0; i < 5; i++)
{
filepath = Application.dataPath + "/../result/segmentation/" + idx.ToString("0000000") + "_" + i.ToString("00") + ".png";
UnityEngine.Object.Destroy(segmentation[i]);
}
}*/
}