-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdataset.py
770 lines (625 loc) · 32.4 KB
/
dataset.py
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# circledata.py
# Python script used for creating animations of circles moving on screen from one position to another
# This file should be able to run with specifying different parameters
# Parameters that will vary between animation that will be in the naming convention include:
# - Shape (in this case circle)
# - Direction (right/left/up/down/diagonal/bouncy)
# - Size
# - Speed
# - noise
# - index for cases
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import imageio.v2 as imageio
import argparse
import os
import random
import time
import pdb
# Path to store the results in
ROOT = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'Data\\dataset')
TEMP = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'Data\\temp')
parser = argparse.ArgumentParser(description="Specify the parameters of the data being created.")
parser.add_argument("-i", "--iterations", type=int, default=1,
help="Set the number of animations that will be created."
)
parser.add_argument("--shape", choices=["circle", "rectangle", "triangle"], default=None,
help="Pick the shape that will be used in creating data."
)
parser.add_argument("-r", "--radius", type=int, choices=range(10, 78), default=0,
help="Set the radius of the circle (10-77)."
)
parser.add_argument("-rw", "--rectwidth", type=int, choices=range(10, 76), default=0,
help="Set the width of the rectangle (10-75)."
)
parser.add_argument("-rh", "--rectheight", type=int, choices=range(10, 76), default=0,
help="Set the height of the rectangle (10-75)."
)
parser.add_argument("-b", "--base", type=int, choices=range(10, 76), default=0,
help="Set the base of the triangle (10-75)."
)
parser.add_argument("-th", "--triheight", type=int, choices=range(10, 76), default=0,
help="Set the height of the triangle (10-75)."
)
parser.add_argument("-d", "--direction", choices=["right", "left", "up", "down", "diagonal", "bouncy"], default=None,
help="Set the direction that the shape will move."
)
parser.add_argument("-s", "--speed", type=int, choices=range(20, 101), default=0,
help="Set the speed of the animations. This is percentage of the maximum speed of 2 pixels per frame."
)
parser.add_argument("-n", "--noise", type=int, choices=range(0, 101), default=0,
help="Set the percentage of noise to be added to the image.")
parser.add_argument("--seed", type=int, default=None,
help="Apply a seed to initialize the random number generator.")
def circle(direction, radius, speed, ind, diagonalDirection, ax, noise, include_noise, randNoise):
i = ind # This is the ith iteration of the program
# Define the initial position of the shape based on the direction (we don't want to go out of bounds!)
x0, y0 = setpositioncircle(direction, radius, diagonalDirection)
# Create the circle patch
circle = plt.Circle((x0, y0), radius, fc='white')
# Add the circle to the axis
ax.add_patch(circle)
# If noise is specified, generate the noisy matrix with that noise
if noise > 0:
noise_matrix1 = generate_noisy_matrix(256, noise)
noise_matrix2 = generate_noisy_matrix(256, noise)
# Generate the noisy matrix with the random noise if the flag to include noise is True
elif include_noise:
noise_matrix1 = generate_noisy_matrix(256, randNoise)
noise_matrix2 = generate_noisy_matrix(256, randNoise)
# For 50 frames...
for j in range(50):
# Move the circle in the direction given
if direction == "right":
x0, y0 = circleright(circle, speed, noise, randNoise, include_noise, x0, y0, radius, j + 1, i)
elif direction == "left":
x0, y0 = circleleft(circle, speed, noise, randNoise, include_noise, x0, y0, radius, j + 1, i)
elif direction == "up":
x0, y0 = circleup(circle, speed, noise, randNoise, include_noise, x0, y0, radius, j + 1, i)
elif direction == "down":
x0, y0 = circledown(circle, speed, noise, randNoise, include_noise, x0, y0, radius, j + 1, i)
elif direction == "diagonal":
x0, y0 = circlediagonal(circle, speed, noise, randNoise, include_noise, x0, y0, radius, j + 1, i, diagonalDirection)
# After the frames have been saved, go back and add noise to those frames if we have noise
if noise > 0 or include_noise:
add_noise(noise_matrix1, noise_matrix2, noise)
def setpositioncircle(direction, radius, diagonalDirection):
maxDistOver = 155 # Farthest over shape can be (x or y axis) without going off the board from the animation
x, y = 0, 0 # Initialize x and y
if direction == "right":
x, y = round(random.uniform(radius, maxDistOver - radius), 2), round(random.uniform(radius, 256 - radius), 2)
elif direction == "left":
x, y = round(random.uniform(256 - maxDistOver + radius, 256 - radius), 2), round(random.uniform(radius, 256 - radius), 2)
elif direction == "up":
x, y = round(random.uniform(radius, 256 - radius), 2), round(random.uniform(radius, maxDistOver - radius), 2)
elif direction == "down":
x, y = round(random.uniform(radius, 256 - radius), 2), round(random.uniform(256 - maxDistOver + radius, 256 - radius), 2)
elif direction == "diagonal":
if diagonalDirection == 1: # Up and right
x, y = round(random.uniform(radius, maxDistOver - radius), 2), round(random.uniform(radius, maxDistOver - radius), 2)
elif diagonalDirection == 2: # Down and right
x, y = round(random.uniform(radius, maxDistOver - radius), 2), round(random.uniform(256 - maxDistOver + radius, 256 - radius), 2)
elif diagonalDirection == 3: # Down and left
x, y = round(random.uniform(256 - maxDistOver + radius, 256 - radius), 2), round(random.uniform(256 - maxDistOver + radius, 256 - radius), 2)
elif diagonalDirection == 4: # Up and left
x, y = round(random.uniform(256 - maxDistOver + radius, 256 - radius), 2), round(random.uniform(radius, maxDistOver - radius), 2)
return x, y
def circleright(circle, speed, noise, randNoise, include_noise, x0, y0, radius, frame, iteration):
# Calculate the new position of the patch
x = x0 + ((speed / 100) * 2.0)
y = y0
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-c-r{radius}-r-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-c-r{radius}-r-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-c-r{radius}-r-{speed}-{noise}.jpg')
# Update the position of the patch
circle.set_center((x, y))
return x, y
def circleleft(circle, speed, noise, randNoise, include_noise, x0, y0, radius, frame, iteration):
# Calculate the new position of the circle
x = x0 - ((speed / 100) * 2.0)
y = y0
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-c-r{radius}-l-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-c-r{radius}-l-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-c-r{radius}-l-{speed}-{noise}.jpg')
# Update the position of the circle patch
circle.set_center((x, y))
return x, y
def circleup(circle, speed, noise, randNoise, include_noise, x0, y0, radius, frame, iteration):
# Calculate the new position of the circle
x = x0
y = y0 + ((speed / 100) * 2.0)
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-c-r{radius}-u-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-c-r{radius}-u-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-c-r{radius}-u-{speed}-{noise}.jpg')
# Update the position of the circle patch
circle.set_center((x, y))
return x, y
def circledown(circle, speed, noise, randNoise, include_noise, x0, y0, radius, frame, iteration):
# Calculate the new position of the circle
x = x0
y = y0 - ((speed / 100) * 2.0)
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-c-r{radius}-dwn-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-c-r{radius}-dwn-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-c-r{radius}-dwn-{speed}-{noise}.jpg')
# Update the position of the circle patch
circle.set_center((x, y))
return x, y
def circlediagonal(circle, speed, noise, randNoise, include_noise, x0, y0, radius, frame, iteration, diagonalDirection):
# Update x and y based on the random diagonal direction picked
if diagonalDirection == 1: # Up and right
x = x0 + ((speed / 100) * 2.0)
y = y0 + ((speed / 100) * 2.0)
elif diagonalDirection == 2: # Down and right
x = x0 + ((speed / 100) * 2.0)
y = y0 - ((speed / 100) * 2.0)
elif diagonalDirection == 3: # Down and left
x = x0 - ((speed / 100) * 2.0)
y = y0 - ((speed / 100) * 2.0)
elif diagonalDirection == 4: # Up and left
x = x0 - ((speed / 100) * 2.0)
y = y0 + ((speed / 100) * 2.0)
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-c-r{radius}-diag-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-c-r{radius}-diag-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-c-r{radius}-diag-{speed}-{noise}.jpg')
circle.set_center((x, y))
return x, y
def triangle(direction, base, height, speed, ind, diagonalDirection, ax, noise, include_noise, randNoise):
i = ind # This is the ith iteration of the program
# Define the initial position of the shape based on the direction (we don't want to go out of bounds!)
x1, y1, x2, y2, x3, y3 = setpositiontriangle(direction, base, height, diagonalDirection)
# Create the triangle patch
tri = plt.Polygon([(x1, y1), (x2, y2), (x3, y3)], fc="white")
# Add the triangle to the axis
ax.add_patch(tri)
# If noise is specified, generate the noisy matrix with that noise
if noise > 0:
noise_matrix1 = generate_noisy_matrix(256, noise)
noise_matrix2 = generate_noisy_matrix(256, noise)
# Generate the noisy matrix with the random noise if the flag to include noise is True
elif include_noise:
noise_matrix1 = generate_noisy_matrix(256, randNoise)
noise_matrix2 = generate_noisy_matrix(256, randNoise)
# Create the animation objects and save them
# For 50 frames...
for j in range(50):
# Move the circle in the direction given
if direction == "right":
x1, y1, x2, y2, x3, y3 = triangleright(tri, speed, noise, randNoise, include_noise, x1, y1, x2, y2, x3, y3, base, height, j + 1, i)
elif direction == "left":
x1, y1, x2, y2, x3, y3 = triangleleft(tri, speed, noise, randNoise, include_noise, x1, y1, x2, y2, x3, y3, base, height, j + 1, i)
elif direction == "up":
x1, y1, x2, y2, x3, y3 = triangleup(tri, speed, noise, randNoise, include_noise, x1, y1, x2, y2, x3, y3, base, height, j + 1, i)
elif direction == "down":
x1, y1, x2, y2, x3, y3 = triangledown(tri, speed, noise, randNoise, include_noise, x1, y1, x2, y2, x3, y3, base, height, j + 1, i)
elif direction == "diagonal":
x1, y1, x2, y2, x3, y3 = trianglediagonal(tri, speed, noise, randNoise, include_noise, x1, y1, x2, y2, x3, y3, base, height, j + 1, i, diagonalDirection)
# After the frames have been saved, go back and add noise to those frames if we have noise
if noise > 0 or include_noise:
add_noise(noise_matrix1, noise_matrix2, noise)
def setpositiontriangle(direction, base, height, diagonalDirection):
maxDistOver = 155 # Farthest over shape can be (x or y axis) without going off the board from the animation
x, y = 0, 0 # Initialize x and y
if direction == 'right':
x1, y1 = round(random.uniform(0, maxDistOver - base), 2), round(random.uniform(0, 256 - height), 2)
x2, y2 = x1 + base, y1
x3, y3 = x1 + (base / 2), y2 + height
elif direction == 'left':
x2, y2 = round(random.uniform(256 - maxDistOver + base, 256), 2), round(random.uniform(0, 256 - height), 2)
x1, y1 = x2 - base, y2
x3, y3 = x1 + (base / 2), y2 + height
elif direction == 'up':
x1, y1 = round(random.uniform(0, 256 - base), 2), round(random.uniform(0, maxDistOver - height), 2)
x2, y2 = x1 + base, y1
x3, y3 = x1 + (base / 2), y2 + height
elif direction == 'down':
x1, y1 = round(random.uniform(0, 256 - base), 2), round(random.uniform(256 - maxDistOver, 256 - height), 2)
x2, y2 = x1 + base, y1
x3, y3 = x1 + (base / 2), y2 + height
elif direction == 'diagonal':
if diagonalDirection == 1:
x1, y1 = round(random.uniform(0, maxDistOver - base), 2), round(random.uniform(0, maxDistOver - height), 2)
x2, y2 = x1 + base, y1
x3, y3 = x1 + (base / 2), y2 + height
elif diagonalDirection == 2:
x1, y1 = round(random.uniform(0, maxDistOver - base), 2), round(random.uniform(256 - maxDistOver, 256 - height), 2)
x2, y2 = x1 + base, y1
x3, y3 = x1 + (base / 2), y2 + height
elif diagonalDirection == 3:
x2, y2 = round(random.uniform(256 - maxDistOver + base, 256), 2), round(random.uniform(256 - maxDistOver, 256 - height), 2)
x1, y1 = x2 - base, y2
x3, y3 = x1 + (base / 2), y2 + height
elif diagonalDirection == 4:
x2, y2 = round(random.uniform(256 - maxDistOver + base, 256), 2), round(random.uniform(0, maxDistOver - height), 2)
x1, y1 = x2 - base, y2
x3, y3 = x1 + (base / 2), y2 + height
return x1, y1, x2, y2, x3, y3
def triangleright(tri, speed, noise, randNoise, include_noise, x1, y1, x2, y2, x3, y3, base, height, frame, iteration):
# Calculate the new position of the triangle
x1delta = x1 + ((speed / 100) * 2.0)
y1delta = y1
x2delta = x2 + ((speed / 100) * 2.0)
y2delta = y2
x3delta = x3 + ((speed / 100) * 2.0)
y3delta = y3
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-r-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-r-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-r-{speed}-{noise}.jpg')
# Update the position of the triangle patch
tri.set_xy([(x1delta, y1delta), (x2delta, y2delta), (x3delta, y3delta)])
return x1delta, y1delta, x2delta, y2delta, x3delta, y3delta
def triangleleft(tri, speed, noise, randNoise, include_noise, x1, y1, x2, y2, x3, y3, base, height, frame, iteration):
# Calculate the new position of the triangle
x1delta = x1 - ((speed / 100) * 2.0)
y1delta = y1
x2delta = x2 - ((speed / 100) * 2.0)
y2delta = y2
x3delta = x3 - ((speed / 100) * 2.0)
y3delta = y3
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-l-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-l-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-l-{speed}-{noise}.jpg')
# Update the position of the triangle patch
tri.set_xy([(x1delta, y1delta), (x2delta, y2delta), (x3delta, y3delta)])
return x1delta, y1delta, x2delta, y2delta, x3delta, y3delta
def triangleup(tri, speed, noise, randNoise, include_noise, x1, y1, x2, y2, x3, y3, base, height, frame, iteration):
# Calculate the new position of the triangle
x1delta = x1
y1delta = y1 + ((speed / 100) * 2.0)
x2delta = x2
y2delta = y2 + ((speed / 100) * 2.0)
x3delta = x3
y3delta = y3 + ((speed / 100) * 2.0)
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-u-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-u-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-u-{speed}-{noise}.jpg')
# Update the position of the triangle patch
tri.set_xy([(x1delta, y1delta), (x2delta, y2delta), (x3delta, y3delta)])
return x1delta, y1delta, x2delta, y2delta, x3delta, y3delta
def triangledown(tri, speed, noise, randNoise, include_noise, x1, y1, x2, y2, x3, y3, base, height, frame, iteration):
# Calculate the new position of the triangle
x1delta = x1
y1delta = y1 - ((speed / 100) * 2.0)
x2delta = x2
y2delta = y2 - ((speed / 100) * 2.0)
x3delta = x3
y3delta = y3 - ((speed / 100) * 2.0)
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-dwn-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-dwn-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-dwn-{speed}-{noise}.jpg')
# Update the position of the triangle patch
tri.set_xy([(x1delta, y1delta), (x2delta, y2delta), (x3delta, y3delta)])
return x1delta, y1delta, x2delta, y2delta, x3delta, y3delta
def trianglediagonal(tri, speed, noise, randNoise, include_noise, x1, y1, x2, y2, x3, y3, base, height, frame, iteration, diagonalDirection):
# Calculate the new position of the triangle
if diagonalDirection == 1: # Up and right
x1delta = x1 + ((speed / 100) * 2.0)
y1delta = y1 + ((speed / 100) * 2.0)
x2delta = x2 + ((speed / 100) * 2.0)
y2delta = y2 + ((speed / 100) * 2.0)
x3delta = x3 + ((speed / 100) * 2.0)
y3delta = y3 + ((speed / 100) * 2.0)
elif diagonalDirection == 2: # Down and right
x1delta = x1 + ((speed / 100) * 2.0)
y1delta = y1 - ((speed / 100) * 2.0)
x2delta = x2 + ((speed / 100) * 2.0)
y2delta = y2 - ((speed / 100) * 2.0)
x3delta = x3 + ((speed / 100) * 2.0)
y3delta = y3 - ((speed / 100) * 2.0)
elif diagonalDirection == 3: # Down and left
x1delta = x1 - ((speed / 100) * 2.0)
y1delta = y1 - ((speed / 100) * 2.0)
x2delta = x2 - ((speed / 100) * 2.0)
y2delta = y2 - ((speed / 100) * 2.0)
x3delta = x3 - ((speed / 100) * 2.0)
y3delta = y3 - ((speed / 100) * 2.0)
elif diagonalDirection == 4: # Up and left
x1delta = x1 - ((speed / 100) * 2.0)
y1delta = y1 + ((speed / 100) * 2.0)
x2delta = x2 - ((speed / 100) * 2.0)
y2delta = y2 + ((speed / 100) * 2.0)
x3delta = x3 - ((speed / 100) * 2.0)
y3delta = y3 + ((speed / 100) * 2.0)
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-diag-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-diag-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-t-b{base}h{height}-diag-{speed}-{noise}.jpg')
# Update the position of the triangle patch
tri.set_xy([(x1delta, y1delta), (x2delta, y2delta), (x3delta, y3delta)])
return x1delta, y1delta, x2delta, y2delta, x3delta, y3delta
def rectangle(direction, width, height, speed, ind, diagonalDirection, ax, noise, include_noise, randNoise):
i = ind # This is the ith iteration of the program
# Define the initial position of the shape based on the direction (we don't want to go out of bounds!)
x0, y0 = setpositionrectangle(direction, width, height, diagonalDirection)
# Create the circle patch
rect = plt.Rectangle((x0, y0), width, height, fc='white')
# Add the circle to the axis
ax.add_patch(rect)
# If noise is specified, generate the noisy matrix with that noise
if noise > 0:
noise_matrix1 = generate_noisy_matrix(256, noise)
noise_matrix2 = generate_noisy_matrix(256, noise)
# Generate the noisy matrix with the random noise if the flag to include noise is True
elif include_noise:
noise_matrix1 = generate_noisy_matrix(256, randNoise)
noise_matrix2 = generate_noisy_matrix(256, randNoise)
# For 50 frames...
for j in range(50):
# Move the circle in the direction given
if direction == "right":
x0, y0 = rectangleright(rect, speed, noise, randNoise, include_noise, x0, y0, width, height, j + 1, i)
elif direction == "left":
x0, y0 = rectangleleft(rect, speed, noise, randNoise, include_noise, x0, y0, width, height, j + 1, i)
elif direction == "up":
x0, y0 = rectangleup(rect, speed, noise, randNoise, include_noise, x0, y0, width, height, j + 1, i)
elif direction == "down":
x0, y0 = rectangledown(rect, speed, noise, randNoise, include_noise, x0, y0, width, height, j + 1, i)
elif direction == "diagonal":
x0, y0 = rectanglediagonal(rect, speed, noise, randNoise, include_noise, x0, y0, width, height, j + 1, i, diagonalDirection)
# After the frames have been saved, go back and add noise to those frames if we have noise
if noise > 0 or include_noise:
add_noise(noise_matrix1, noise_matrix2, noise)
def setpositionrectangle(direction, width, height, diagonalDirection):
maxDistOver = 155 # Farthest over shape can be (x or y axis) without going off the board from the animation
x, y = 0, 0 # Initialize x and y
if direction == "right":
x, y = round(random.uniform(0, maxDistOver - width), 2), round(random.uniform(0, 256 - height), 2)
elif direction == 'left':
x, y = round(random.uniform(maxDistOver, 256 - width), 2), round(random.uniform(0, 256 - height), 2)
elif direction == 'up':
x, y = round(random.uniform(0, 256 - width), 2), round(random.uniform(0, maxDistOver - height), 2)
elif direction == 'down':
x, y = round(random.uniform(0, 256 - width), 2), round(random.uniform(maxDistOver, 256 - height), 2)
elif direction == 'diagonal':
if diagonalDirection == 1:
x, y = round(random.uniform(0, maxDistOver - width), 2), round(random.uniform(0, maxDistOver - height), 2)
elif diagonalDirection == 2:
x, y = round(random.uniform(0, maxDistOver - width), 2), round(random.uniform(maxDistOver, 256 - height), 2)
elif diagonalDirection == 3:
x, y = round(random.uniform(maxDistOver, 256 - width), 2), round(random.uniform(maxDistOver, 256 - height), 2)
elif diagonalDirection == 4:
x, y = round(random.uniform(maxDistOver, 256 - width), 2), round(random.uniform(0, maxDistOver - height), 2)
return x, y
def rectangleright(rect, speed, noise, randNoise, include_noise, x0, y0, width, height, frame, iteration):
# Calculate the new position of the patch
x = x0 + ((speed / 100) * 2.0)
y = y0
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-r-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-r-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-r-{speed}-{noise}.jpg')
# Update the position of the patch
rect.set_xy((x, y))
return x, y
def rectangleleft(rect, speed, noise, randNoise, include_noise, x0, y0, width, height, frame, iteration):
# Calculate the new position of the patch
x = x0 - ((speed / 100) * 2.0)
y = y0
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-l-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-l-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-l-{speed}-{noise}.jpg')
# Update the position of the patch
rect.set_xy((x, y))
return x, y
def rectangleup(rect, speed, noise, randNoise, include_noise, x0, y0, width, height, frame, iteration):
# Calculate the new position of the circle
x = x0
y = y0 + ((speed / 100) * 2.0)
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-u-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-u-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-u-{speed}-{noise}.jpg')
# Update the position of the patch
rect.set_xy((x, y))
return x, y
def rectangledown(rect, speed, noise, randNoise, include_noise, x0, y0, width, height, frame, iteration):
# Calculate the new position of the circle
x = x0
y = y0 - ((speed / 100) * 2.0)
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-dwn-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-dwn-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-dwn-{speed}-{noise}.jpg')
# Update the position of the patch
rect.set_xy((x, y))
return x, y
def rectanglediagonal(rect, speed, noise, randNoise, include_noise, x0, y0, width, height, frame, iteration, diagonalDirection):
# Update x and y based on the random diagonal direction picked
if diagonalDirection == 1: # Up and right
x = x0 + ((speed / 100) * 2.0)
y = y0 + ((speed / 100) * 2.0)
elif diagonalDirection == 2: # Down and right
x = x0 + ((speed / 100) * 2.0)
y = y0 - ((speed / 100) * 2.0)
elif diagonalDirection == 3: # Down and left
x = x0 - ((speed / 100) * 2.0)
y = y0 - ((speed / 100) * 2.0)
elif diagonalDirection == 4: # Up and left
x = x0 - ((speed / 100) * 2.0)
y = y0 + ((speed / 100) * 2.0)
# Save the current frame
if noise > 0:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-diag-{speed}-{noise}.jpg')
elif include_noise:
plt.savefig(f'{TEMP}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-diag-{speed}-{randNoise}.jpg')
else:
plt.savefig(f'{ROOT}\\{iteration+1:05}-{frame:02}-r-w{width}h{height}-diag-{speed}-{noise}.jpg')
# Update the position of the patch
rect.set_xy((x, y))
return x, y
def generate_noisy_matrix(n, true_percentage):
# Initialize a matrix of values from [0, 255]
random_matrix = np.random.randint(0, 256, size=(n, n), dtype=np.uint8)
# Boolean mask where 'true_percentage' of the values are taken fromm the matrix and the rest are white
boolean_mask = np.random.choice([False, True], size=(n, n), p=[1 - true_percentage/100, true_percentage/100])
# Take the values from the matrix or white based on the mask
noisy_matrix = np.where(boolean_mask, random_matrix, 0)
return noisy_matrix
def add_noise(noise_matrix1, noise_matrix2, noise):
# Get a list of the files in the folder where we saved the frames of the animation
img_list = os.listdir(TEMP)
for img in img_list:
# Read the image as a numpy array
filepath = os.path.join(TEMP, img)
img_arr = np.array(imageio.imread(filepath))
# Put the image in grayscale to make the shape (256, 256)
img_arr = np.dot(img_arr[..., :3], [0.2989, 0.5870, 0.1140])
img_arr = np.round(img_arr, 0)
shape_mask = np.where(img_arr != 0) # Get a mask of the indexes where the shape is
# Add noise matrix to the img numpy array
img_arr += noise_matrix1
# Adjust the matrix so the noise adjustment looks better
img_arr[shape_mask] -= noise_matrix1[shape_mask]
img_arr[shape_mask] -= noise_matrix2[shape_mask] # For the shape area
img_arr = np.clip(img_arr, 0, 255).astype(np.uint8) # Get everything between 0-255
# Make the figure
dpi = 142
fig2 = plt.figure(2, figsize=(256/dpi, 256/dpi), dpi=dpi)
fig2.set_facecolor('black')
plt.imshow(img_arr, cmap='gray')
plt.axis('off')
fig2.savefig(os.path.join(ROOT, img))
os.remove(os.path.join(TEMP, img))
plt.close()
def main(args):
if not os.path.exists(ROOT):
os.makedirs(ROOT)
if args.seed is not None:
random.seed(args.seed)
np.random.seed(args.seed)
if not os.path.exists(ROOT):
os.makedirs(ROOT)
for i in range(args.iterations):
# If a value wasn't specified, check a flag that will tell the program to make that value random each iteration
randDirection, randRadius, randBase, randTriangleHeight, randWidth, randRectangleHeight, randSpeed = False, False, False, False, False, False, False
if args.direction is None:
randDirection = True
if args.radius == 0:
randRadius = True
if args.base == 0:
randBase = True
if args.triheight == 0:
randTriangleHeight = True
if args.rectwidth == 0:
randWidth = True
if args.rectheight == 0:
randRectangleHeight = True
if args.speed == 0:
randSpeed = True
# Set a random value for the parameters we want randomized for each animation, or what we specified it to be
if randDirection:
direction = random.choice(["right", "left", "up", "down", "diagonal"])
else:
direction = args.direction
if randRadius:
radius = random.randint(10, 77)
else:
radius = args.radius
if randBase:
base = random.randint(10, 75)
else:
base = args.base
if randTriangleHeight:
triheight = random.randint(10, 75)
else:
triheight = args.triheight
if randWidth:
rectWidth = random.randint(10, 75)
else:
rectWidth = args.rectWidth
if randRectangleHeight:
rectHeight = random.randint(10, 75)
else:
rectHeight = args.rectHeight
if randSpeed:
speed = random.randint(20, 100)
else:
speed = args.speed
# Coin flip to decide if to include noise or not when specified
include_noise = np.random.choice([True, False])
# The noise to add if include_noise is True
randNoise = np.random.randint(1, 61)
# Create a figure that's 256x256 pixels
dpi = 142
fig = plt.figure(1, figsize=(256/dpi, 256/dpi), dpi=dpi)
# Create an axis with no axis labels or ticks and a black background
ax = fig.add_subplot(111)
ax.axis('off')
fig.set_facecolor("black")
ax.set_facecolor("black")
# Set the plot axis to by 256 x 256 to match the pixels
ax.set_xlim(0, 256)
ax.set_ylim(0, 256)
# Randomly pick which diagonal direction to go (up and right, down and right, down and left, up and left) for the function
diagonalDirection = random.randint(1, 4)
shape = None
if args.shape is None:
shape = random.choice(["circle", "triangle", "rectangle"])
else:
shape = args.shape
# Make a temp directory to store the files before adding noise
if not os.path.exists(TEMP):
os.makedirs(TEMP)
if shape == "circle":
circle(direction, radius, speed, i, diagonalDirection, ax, args.noise, include_noise, randNoise)
elif shape == "triangle":
triangle(direction, base, triheight, speed, i, diagonalDirection, ax, args.noise, include_noise, randNoise)
elif shape == "rectangle":
rectangle(direction, rectWidth, rectHeight, speed, i, diagonalDirection, ax, args.noise, include_noise, randNoise)
# Delete that temp directory
os.rmdir(TEMP)
plt.close()
if __name__ == "__main__":
main(parser.parse_args())