-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealthBarsPixelsToTraningData.php
311 lines (247 loc) · 9.2 KB
/
HealthBarsPixelsToTraningData.php
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
define("RED", 0);
define("GREEN", 1);
define("BLUE", 2);
function MaxPool(&$img){
// Get the size info from the input image
$width = imagesx($img);
$height = imagesy($img);
// Determine the size of the pool image.
$max_pool_img_width = $width / 2;
while(($max_pool_img_width % 2)>0){
$max_pool_img_width++;// if it wont evenly divide into 2... enlarge
}
$max_pool_img_height = $height / 2;
while(($max_pool_img_height % 2)>0){
$max_pool_img_height++;// if it wont evenly divide into 2... enlarge
}
// Allocate resource in memory for the image
$max_pool_img = imagecreatetruecolor($max_pool_img_width, $max_pool_img_height);
$background = imagecolorallocate($max_pool_img, 0, 0, 0);
$max_row = 0;
$max_col = 0;
// Max Pooling
for($row = 0; $row < $width; $row += 2){
for($col = 0; $col < $height; $col+= 2){
// C0 C1 C2
//R0 [p1] [p2] [..]
//R1 [p3] [p4] [..]
//R2 [..] [..] [..]
// Get color
$p1 = @imagecolorat($img, $row, $col);
// R+G+B
$p1_r = ($p1 >> 16) & 0xFF;
$p1_g = ($p1 >> 8) & 0xFF;
$p1_b = $p1 & 0xFF;
$p1_color = $p1_r + $p1_g + $p1_b;
// Get color
$p2 = @imagecolorat($img, $row, $col+1);
// R+G+B
$p2_r = ($p2 >> 16) & 0xFF;
$p2_g = ($p2 >> 8) & 0xFF;
$p2_b = $p2 & 0xFF;
$p2_color = $p2_r + $p2_g + $p2_b;
// Get color
$p3 = @imagecolorat($img, $row+1, $col);
// R+G+B
$p3_r = ($p3 >> 16) & 0xFF;
$p3_g = ($p3 >> 8) & 0xFF;
$p3_b = $p3 & 0xFF;
$p3_color = $p3_r + $p3_g + $p3_b;
// Get color
$p4 = @imagecolorat($img, $row+1, $col+1);
// R+G+B
$p4_r = ($p4 >> 16) & 0xFF;
$p4_g = ($p4 >> 8) & 0xFF;
$p4_b = $p4 & 0xFF;
$p4_color = $p4_r + $p4_g + $p4_b;
$color = $p1;
// Find the brightest pixel
$max_pixel = max($p1_color, $p2_color, $p3_color, $p4_color);
if($max_pixel == $p1_color){
$color = $p1;
}
elseif($max_pixel == $p2_color){
$color = $p2;
}
elseif($max_pixel == $p3_color){
$color = $p3;
}
else{
$color = $p4;
}
// Paint pooled pixel
imagesetpixel($max_pool_img, $max_row, $max_col, $color);
$max_col++;
}
$max_col = 0;
$max_row++;
}
return $max_pool_img;
}
function Monochrome(&$img, $color_channel){
// Get the size info from the input image
$width = imagesx($img);
$height = imagesy($img);
// Allocate resource in memory for the image
$monochrome = imagecreatetruecolor($width, $height);
$background = imagecolorallocate($monochrome, 0, 0, 0);
// Loop through pixels
for($row = 0; $row < $width; $row++){
for($col = 0; $col < $height; $col++){
// Get pixel color channels
$p = imagecolorat($img, $row, $col);
$colors = imagecolorsforindex($img, $p);
// Extract desired channel
if($color_channel == RED){
$pixelcolor = imagecolorallocate($monochrome, $colors['red'], 0, 0);
}
elseif($color_channel == GREEN){
$pixelcolor = imagecolorallocate($monochrome, 0, $colors['green'], 0);
}
elseif($color_channel == BLUE){
$pixelcolor = imagecolorallocate($monochrome, 0, 0, $colors['blue']);
}
else{
$pixelcolor = $background;
}
// Change pixel to contain pure channel
imagesetpixel($monochrome, $row, $col, $pixelcolor);
}
}
return $monochrome;
}
// Take an image with height and width and return
// an array of floats for the desired color channel
function Flatten(&$img, $color_channel){
// Get the size info from the input image
$width = imagesx($img);
$height = imagesy($img);
// the flattened pixel data is stored here
$pixels = array();
// Loop through pixels
for($row = 0; $row < $width; $row++){
for($col = 0; $col < $height; $col++){
// Get pixel color channels
$p = imagecolorat($img, $row, $col);
$colors = imagecolorsforindex($img, $p);
// Extract desired channel
if($color_channel == RED){
$pixels[] = ColorToFloat($colors['red']);
}
elseif($color_channel == GREEN){
$pixels[] = ColorToFloat($colors['green']);
}
elseif($color_channel == BLUE){
$pixels[] = ColorToFloat($colors['blue']);
}
else{
$pixels[] = 0.00;
}
}
}
return $pixels;
}
function ExtractHealthbarFromScreenshot(&$source_im){
$x1 = 95;
$y1 = 725;
$x2 = 298 - $x1;
$x2 = 298 - $x1;
$y2 = 730 - $y1; // 733 whole thing
return imagecrop($source_im, ['x' => 95, 'y' => 725, 'width' => $x2, 'height' => $y2]);
}
// example will echo 0 - 255
//for($i = 0; $i <= 255; $i++){
// echo $i . ' ' . ColorToFloat($i) . PHP_EOL;
//}
function ColorToFloat($value)
{
$max = 255;
$increment = $max / 100;
return ($value / $increment) / 100;
}
// example will echo 0 - 1
//for($i = 0; $i <= 1; $i+=0.01){
// echo $i . ' ' . FloatToColor($i) . PHP_EOL;
//}
function FloatToColor($value)
{
$max = 255;
$increment = $max / 100;
return round(($value * 100) * $increment);
}
$image_kernel = array(
array(-1, -1, -1),
array(-1, 8, -1),
array(-1, -1, -1)
);
// Training batch number for data set
$training_set_batch = 4;
$screenshots = 'screenshots\\';
$filename = "HealthBar_Raw_States.txt"; // Generated by LogHealthbarTags.php
$file = fopen($filename, "r");
$screenshot_images = fread($file, filesize($filename));
fclose($file);
$screenshot_images = explode(PHP_EOL, $screenshot_images);
$i = 0;
foreach($screenshot_images as $data){
$data = explode(' ', $data);
@$screenshot_images[$i] = array();
@$screenshot_images[$i]['file'] = $data[0];
@$screenshot_images[$i]['stimpack'] = $data[1];
@$screenshot_images[$i]['radaway'] = $data[2];
$i++;
}
// Convert images to training data
$training_data = '';
foreach($screenshot_images as &$test_image){
// Load image
$health_bar_r = imagecreatefromjpeg($screenshots . $test_image['file']);
// Get image size
$width = imagesx($health_bar_r);
$height = imagesy($health_bar_r);
// Create image resources of the correct size
$health_bar_g = imagecreatetruecolor($width, $height);
$health_bar_b = imagecreatetruecolor($width, $height);
// Copy the image to the new image resources
imagecopy($health_bar_g, $health_bar_r, 0, 0, 0, 0, $width, $height);
imagecopy($health_bar_b, $health_bar_r, 0, 0, 0, 0, $width, $height);
// Extract healthbar if the image is full size
if($width >= 1280 && $height >= 800){
$health_bar_r = ExtractHealthbarFromScreenshot($health_bar_r);
$health_bar_g = ExtractHealthbarFromScreenshot($health_bar_g);
$health_bar_b = ExtractHealthbarFromScreenshot($health_bar_b);
}
// Split RGB Channels
$health_bar_r = Monochrome($health_bar_r, RED);
$health_bar_g = Monochrome($health_bar_g, GREEN);
$health_bar_b = Monochrome($health_bar_b, BLUE);
// Convolutions
imageconvolution($health_bar_r, $image_kernel, 1, -1);
imageconvolution($health_bar_g, $image_kernel, 1, -1);
imageconvolution($health_bar_b, $image_kernel, 1, -1);
// Pooling
$health_bar_r = MaxPool($health_bar_r);
$health_bar_g = MaxPool($health_bar_g);
$health_bar_b = MaxPool($health_bar_b);
// Save the split channel images if you want
//imagejpeg($health_bar_r, $screenshots . $test_image . '.HealthBarRconvo.png');
//imagejpeg($health_bar_g, $screenshots . $test_image . '.HealthBarGconvo.png');
//imagejpeg($health_bar_b, $screenshots . $test_image . '.HealthBarBconvo.png');
$red_inputs = Flatten($health_bar_r, RED);
$green_inputs = Flatten($health_bar_g, GREEN);
$blue_inputs = Flatten($health_bar_b, BLUE);
// Destroy image resources
imagedestroy($health_bar_r);
imagedestroy($health_bar_g);
imagedestroy($health_bar_b);
$training_data .= implode(' ', array_merge($red_inputs, $green_inputs, $blue_inputs)) . PHP_EOL . $test_image['stimpack'] . ' ' . $test_image['radaway'] . PHP_EOL;
}
$number_of_training_examples = count($screenshot_images) - 2;
$number_of_inputs = 612;
$number_of_outputs = 2;
// Write Data
$filename = 'Fallout_Bot_HealthBar_Training_Data_' . $training_set_batch . '_Convolutions.data';
$file = fopen($filename, "w");
fwrite($file, "$number_of_training_examples $number_of_inputs $number_of_outputs" . PHP_EOL . $training_data);
fclose($file);