-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
executable file
·1635 lines (1522 loc) · 106 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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from PIL import Image
import client
#from edflow.data.dataset import DatasetMixin
import numpy as np
import matplotlib
#matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import tkinter
import json
import math
import logging
import copy
import time
import os
from os.path import expanduser
class dataset_cuboids():
def __init__(self, dataset_name = None, unique_data_folder = True, debug_log = False, use_unity_build = True, dataset_directory=None, absolute_path=False, port_range = [49990,50050]):
"""Sets up logging, the config, necessary paths and a client instance form :class:`~client.client_communicator_to_unity`.
:param dataset_name: If this is not default the created images and parameters are saved into a folder nested in ``data/dataset/`` containing the dataset_name, defaults to None
:type dataset_name: string or None, optional
:param unique_data_folder: If ``True`` the name of the folder for your dataset will start with a time stamp. If ``False`` the name of the folder will only contain the name of the dataset and can be used across many instances of this class, defaults to True
:type unique_data_folder: bool, optional
:param use_unity_build: If this is set to true image generation will work automatically with the Unity build. Otherwise you have to manually set the Unity editor to play, defaults to True
:type use_unity_build: bool, optional
:param debug_log: If there should be more information displayed in the console for debugging, defaults to False
:type debug_log: bool, optional
"""
# Set up log level
if debug_log:
log_level = logging.DEBUG
else:
log_level = logging.INFO
# Set up a client and start unity
print("before client")
self.uc = client.client_communicator_to_unity(use_unity_build=use_unity_build, log_level = log_level, port_range = port_range)
print("after client")
self.file_directory = os.path.dirname(os.path.realpath(__file__)) #os.path.split(os.path.abspath(__file__))
# Use already existing logger from pthon for dataset as well
self.log_path = self.file_directory + "/log/python_dataset_and_client.log"
self.logger = self.uc.logger
# Create file handler
self.fh = logging.FileHandler(self.log_path)
self.fh.setLevel(logging.DEBUG)
# Add formatter
self.formatter_fh = logging.Formatter('%(asctime)s; %(filename)s - line: %(lineno)d -%(levelname)s: %(message)s')
self.fh.setFormatter(self.formatter_fh)
# Different formatter for different log_level
# Add fh to logger
self.logger.addHandler(self.fh)
# Clear log at startup
with open(self.log_path, 'w'):
pass
if dataset_name != None:
assert type(dataset_name) == str , "In dataset.py, __init__() function: dataset_name has to be a string."
assert ' ' not in dataset_name, "In dataset.py, __init__() function: dataset_name does contain white spaces, leads to problems in saving and loading data."
self.init_time = time.strftime("%Y-%m-%d_%H:%M", time.gmtime())
#self.init_index = self.read_index()
# Create unique name for the folder where the dataset is saved
if dataset_name != None:
if unique_data_folder:
full_folder_name = self.init_time + "__" + dataset_name
else:
full_folder_name = dataset_name
if dataset_directory==None:
directory = "data/dataset/" + full_folder_name
else:
assert dataset_directory[-1] == "/" , "dataset_directory is string for a directory, has to end with '/'."
if absolute_path:
assert dataset_directory[0] == "/" , "dataset_directory is string for a directory, absolute_path is true --> has to start with '/'."
directory = dataset_directory + full_folder_name
else:
home = expanduser("~")
self.logger.debug("home directory: " + str(home))
directory = home + dataset_directory + full_folder_name
self.data_directory = directory
self.logger.debug("Dataset directory:" + directory)
self.logger.debug("Create config.")
# Create dataset config for random parameter creation and set the default intervalls for all random generated parameters
self.set_config()
self.logger.debug("Dataset initialised.\n")
def set_config(self, save_config = True, request_pose = False, request_three = False, same_scale=None, scale=[0.5,4], total_cuboids=[2,5], phi=[0,360], specify_branches=False, branches=[1,3], same_theta=None, theta=None,
same_material=None, specify_material=False, r=[0,1], g=[0,1], b=[0,1], a=1, metallic=[0,1], smoothness=[0,1], CameraRes_width= 1024, CameraRes_height=1024, Camera_FieldofView=90, CameraRadius = None, CameraTheta = [60,100], CameraPhi = [0,360], CameraVerticalOffset = None, Camera_solid_background = False,
totalPointLights=[5,12], PointLightsRadius=[5,20], PointLightsPhi=[0,360], PointLightsTheta=[0,90], PointLightsIntensity=[7,17], PointLightsRange=[5,25], same_PointLightsColor=None, PointLightsColor_r=[0,1], PointLightsColor_g=[0,1], PointLightsColor_b=[0,1], PointLightsColor_a=[0.5,1],
totalSpotLights=[3,7], SpotLightsRadius=[5,20], SpotLightsPhi=[0,360], SpotLightsTheta=[0,90], SpotLightsIntensity=[5,15], SpotLightsRange=[5,25], SpotAngle=[5,120], same_SpotLightsColor=None, SpotLightsColor_r=[0,1], SpotLightsColor_g=[0,1], SpotLightsColor_b=[0,1], SpotLightsColor_a=[0.5,1],
DirectionalLightTheta = [0,90], DirectionalLightIntensity = [1.0,5.0], specify_scale=False, specify_theta=False):
"""Sets a config for this class instace which determines the interval for all random parameters created in the function :meth:`~dataset.dataset_cuboids.create_random_parameters`. The meaning of all the parameters are explained in this function: :meth:`~client.client_communicator_to_unity.write_json_crane`.
Here are only those parameters mentioned which deviate from the ``standard_parameter``. You can also specify and set parameters which should not be generated randomly.
:param "save_config": This flag indicates if the config should be saved. It should be kept at the default: ``True``.
:type "save_config": bool, optional
:param "request_pose": This flag indicates if a groundtruth of the pose in form of an image has to be created.
:type "request_pose": bool, optional
:param "standard_parameter": Has to be a list with two floats. The first element describes the lower boundary and second element describes the upper boundary for the function :meth:`~dataset.dataset_cuboids.create_random_parameters` in which the variable is set randomly, defaults is a predefined list
:type "standard parameter": list, optional
:param same_scale: If ``None`` the boolean will be set randomly in :meth:`~dataset.dataset_cuboids.create_random_parameters`. Otherwise it will be set to the given boolean, defaults to None
:type same_scale: None or bool, optional
:param specify_branches: Mainly leave it at the default: False, but if you wish to set the parameter ``branches`` not randomly you can set it to ``True`` and specify them.
:type specify_branches: bool, optional
:param branches: If ``None`` there will be no branches which means one main branch. Else has to be a list with two integers. The amount of branches created in :meth:`~dataset.dataset_cuboids.create_random_parameters` at every cuboid will be chosen from a normal distribution where the second element of this list is interpreted als three sigma deviation, there is also a thrid option to set this parameter to a fixed value: you can use ``specyfy_branches = True`` and input a list with your desired values with a length of ``total_cuboids - 1``, defaults to [1,3]
:type branches: None or list, optional
:param same_theta: If ``None`` the boolean will be set randomly in :meth:`~dataset.dataset_cuboids.create_random_parameters`. Otherwise it will be set to the given boolean, defaults to None
:type same_theta: None or bool, optional
:param theta: If ``None`` the values for theta is set randomly between zero and ``360/total_cuboids``. Otherwise it has to be a list of length 2. If you want fixed values you can input a float or an int if ``same_theta = True``, if you want fixed values with ``same_theta = False`` you have to set ``specify_theta = True``, defaults to None
:type theta: None, list, float or int, optional
:param same_material: If ``None`` the boolean will be set randomly in :meth:`~dataset.dataset_cuboids.create_random_parameters`. Otherwise it will be set to the given boolean, defaults to None
:type same_material: None or bool, optional
:param CameraRes_width: The width Resolution of your image, default to 1024
:type CameraRes_width: int, optional
:param CameraRes_height: The height Resolution of your image, default to 1024
:type CameraRes_height: int, optional
:param Camera_FieldofView: The Fiel of View of the camera, default to 80
:type Camera_FieldofView: float or int, optional
:param CameraRadius: If a ``float`` or ``int`` is entered then the value in :meth:`~dataset.dataset_cuboids.create_random_parameters` will not be random, instead set to the given value. If it is a list it has to be a list of length two. If it is set to `None`it will be calculated to fit the enire crane in the picture, defaults to 10.0
:type CameraRadius: float, int, None or list, optional
:param CameraTheta: If ``float`` or ``int`` then the value in :meth:`~dataset.dataset_cuboids.create_random_parameters` will not be random, instead set to the given value. If it is a list it has to be a list of length two, defaults to [30,100]
:type CameraTheta: float, int or list, optional
:param CameraPhi: If input is ``float`` or ``int`` then the value in :meth:`~dataset.dataset_cuboids.create_random_parameters` will not be random, instead set to the given value. If it is a list it has to be a list of length two, defaults to [0,360]
:type CameraPhi: float, int or list, optional
:param CameraVerticalOffset: If ``None`` it is set to zero. If input is ``float`` or ``int`` then the value in :meth:`~dataset.dataset_cuboids.create_random_parameters` will not be random, instead set to the given ``float``. If it is a list it has to be a list of length two, defaults to None
:type CameraVerticalOffset: None, float, int or list, optional
:param totalPointLights: If ``None`` there will be no Pointlights created in :meth:`~dataset.dataset_cuboids.create_random_parameters`. Else it has to be a list of integers with the length two, defaults to [5,12]
:type totalPointLights: None or list, optional
:param same_PointLightsColor: If ``None`` the boolean will be chosen randomly, else the given boolean is used, defaults to None
:type same_PointLightsColor: None or bool, optional
:param totalSpotLights: If ``None`` there will be no Spotlights created in :meth:`~dataset.dataset_cuboids.create_random_parameters`. Else it has to be a list of integers with the length two, defaults to None
:type totalSpotLights: None or list, optional
:param same_SpotLightsColor: If ``None`` the boolean will be chosen randomly, else the given boolean is used, defaults to None
:type same_SpotLightsColor: None or bool, optional
:param DirectionalLightTheta: If ``None`` the ``DirectionalLightIntensity`` will be set to zero, elif has to be a list of floats with the length two, for a fixed value enter a ``float`` or ``int``, defaults to [0,90]
:type DirectionalLightTheta: None, float, int or list, optional
:param DirectionalLightIntensity: If ``None`` the ``DirectionalLightIntensity`` will be set to zero, elif has to be a list of floats with the length two, for a fixed value enter a ``float`` or ``int``, defaults to [0.1,1.8]
:type DirectionalLightIntensity: None, float, int or list, optional
:param specify_scale: If this is set ``True`` you can enter the fixed values for ``scale`` even if ``same_scale = False``, defaults to False
:type specify_scale: bool, optional
:param specify_theta: If this is set ``True`` you can enter the fixed values for ``theta`` even if ``same_theta = False``, defaults to False
:type specify_theta: bool, optional
"""
config = {}
# Create intervals for general properties
if type(total_cuboids)==int:
config["total_cuboids"]=total_cuboids
else:
assert len(total_cuboids) == 2, "total_cuboids either int or total_cuboids[0] is minimal limit and total_cuboids[1] is maximal limit for random generation of total_cuboids"
config["total_cuboids"]=total_cuboids
# Use the same_theta for every angle between two cubiods
if same_theta!=None:
assert type(same_theta)==bool, "Has to be bool or none. same_theta sets the bool of same_theta in getRandomJsonData. If it is None bool is set randomly."
config["same_theta"]=same_theta
if specify_theta:
assert same_theta != None, "You can not specify theta if same_theta is not specified"
if same_theta:
assert type(theta)in [float, int], "specify_theta and same_theta is true, theta has to be a float"
else:
assert len(theta) == total_cuboids-1, "specify_theta is true and same_theta is false, theta has to be a list of length total_cuboids-1."
#else:
# if same_theta == False:
#assert type(theta)==list, "If same_theta is None i.e. randomly choosen, theta can not be specified exactly, has to be choosen randomly as well. This means theta has to be a list of length 2 with theta[0] is minimal limit and theta[1] is maximal limit for random generation theta."
#assert len(theta)==2, "If same_theta is None i.e. randomly choosen, theta can not be specified exactly, has to be choosen randomly as well. This means theta has to be a list of length 2 with theta[0] is minimal limit and theta[1] is maximal limit for random generation theta."
config["specify_theta"]=specify_theta
config["theta"]=theta
# Phi describes by how much the crane is rotated
if type(phi)==list:
assert len(phi)==2, "Phi is a list, has to be of langth two. Or a float for not random generation."
else:
assert type(phi)in [float, int], "Phi is not a list then it hast to be a float."
config["phi"]=phi
# Use the same scale for every cubiod
if same_scale==None:
assert specify_scale==False, "You can not specify the scale if you do not specify same_scale."
assert type(scale)==list, "If same_scale = None i.e. randomly generated then scale hast to be also randomly generated.Which means scale[0] is minimal limit and scale[1] is maximal limit for random generation of the scale of the cubiods or a float not a list."
assert len(scale) == 2, "If same_scale = None i.e. randomly generated then scale hast to be also randomly generated.Which means scale[0] is minimal limit and scale[1] is maximal limit for random generation of the scale of the cubiods or a float not a list."
else:
assert type(same_scale)==bool, "Has to be bool or None; same_scale sets the bool of same_scale in getRandomJsonData. If it is None bool is set randomly."
config["same_scale"]=same_scale
# Vertical Scale of the cubiods
if specify_scale:
if same_scale:
assert type(scale)in [float, int], "specify_scale and same_scale is true; scale has to be a float."
else:
assert len(scale) == total_cuboids, "specify_scale is true and same_scale is flase; if you want to specify the scale is has to be length total_cuboids"
else:
if same_scale in [False,None]:
assert type(scale)==list, "If same_scale = None i.e. randomly generated then scale hast to be also randomly generated.Which means scale[0] is minimal limit and scale[1] is maximal limit for random generation of the scale of the cubiods or a float not a list."
assert len(scale) == 2, "If same_scale = None i.e. randomly generated then scale hast to be also randomly generated.Which means scale[0] is minimal limit and scale[1] is maximal limit for random generation of the scale of the cubiods or a float not a list."
config["specify_scale"]=specify_scale
config["scale"]=scale
# The upper limit for the arms is dicribing the three sigma variance of the guassion normal distribution for random generation of total_branches
if specify_branches:
if branches!=None:
assert len(branches)==total_cuboids-1, "Has to be list of length 2 or length total_cuboids-1 or type None; branches defines boundaries for how many arms could be created in getRandomJsonData. This means that there is a chance that the crane splits up in the given range. If it is None then there will be only one arm."
else:
if branches!=None:
assert type(branches) == list, "Has to be list of length 2 or length total_cuboids-1 or type None; branches defines boundaries for how many arms could be created in getRandomJsonData. This means that there is a chance that the crane splits up in the given range. If it is None then there will be only one arm."
assert len(branches) == 2, "Has to be list of length 2 or length total_cuboids-1 or type None; branches defines boundaries for how many arms could be created in getRandomJsonData. This means that there is a chance that the crane splits up in the given range. If it is None then there will be only one arm."
assert branches[0] > 0, "branches has to be 1 or greater to even create one Arm."
assert branches[1] > 0, "branches has to be 1 or greater to even create one Arm."
config["specify_branches"] = specify_branches
config["branches"] = branches
# Create intervals or fixed values for camera position
config["CameraRes_height"] = CameraRes_height
config["CameraRes_width"] = CameraRes_width
config["Camera_FieldofView"] = Camera_FieldofView
if type(CameraRadius)== list:
assert len(CameraRadius) == 2, "CameraRadius has to be a list len()==2 or a float for a fixed value."
elif CameraRadius == None:
CameraRadius = -1
else:
assert type(CameraRadius)in [float, int],"CameraRadius has to be a list len()==2 or a float for a fixed value."
config["CameraRadius"] = CameraRadius
if type(CameraTheta)== list:
assert len(CameraTheta) == 2, "CameraTheta has to be a list len()==2 or a float for a fixed value."
else:
assert type(CameraTheta)in [float, int], "CameraTheta has to be a list len()==2 or a float for a fixed value."
config["CameraTheta"] = CameraTheta
if type(CameraPhi)== list:
assert len(CameraPhi) == 2, "CameraPhi has to be a list len()==2 or a float for a fixed value."
else:
type(CameraPhi)in [float, int], "CameraPhi has to be a list len()==2 or a float for a fixed value."
config["CameraPhi"] = CameraPhi
if CameraVerticalOffset==None:
config["CameraVerticalOffset"] = 0
else:
if type(CameraVerticalOffset)== list:
assert len(CameraVerticalOffset) == 2, "CameraVerticalOffset has to be a list len()==2 or a float for a fixed value."
else:
assert type(CameraVerticalOffset)in [float, int], "CameraVerticalOffset has to be a list len()==2 or a float for a fixed value."
config["CameraVerticalOffset"] = CameraVerticalOffset
config["Camera_solid_background"] = Camera_solid_background
# Create intervals for Material properties
if same_material!=None:
assert type(same_material)==bool, "Has to be bool or None. same_material sets the bool of same_material in getRandomJsonData. If it is None bool is set randomly."
if specify_material:
if same_material:
assert type(r)in [float, int], "specify_material and same_material is true, material prpoertie has to be a float."
assert type(g)in [float, int], "specify_material and same_material is true, material prpoertie has to be a float."
assert type(b)in [float, int], "specify_material and same_material is true, material prpoertie has to be a float."
assert type(a)in [float, int], "specify_material and same_material is true, material prpoertie has to be a float."
assert type(metallic)in [float, int], "specify_material and same_material is true, material prpoertie has to be a float."
assert type(smoothness)in [float, int], "specify_material and same_material is true, material prpoertie has to be a float."
else:
assert type(r)==list, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert len(r)==total_cuboids, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert type(g)==list, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert len(g)==total_cuboids, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert type(b)==list, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert len(b)==total_cuboids, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert type(a)==list, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert len(a)==total_cuboids, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert type(metallic)==list, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert len(metallic)==total_cuboids, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert type(smoothness)==list, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
assert len(smoothness)==total_cuboids, "specify_material is true and same_material is false: material properie hast to be a list of lenght total_cuboids."
else:
assert specify_material==False, "you can not specify the material when same_material is None."
assert len(r) == 2, "r[0] is minimal limit and r[1] is maximal limit for random generation of the cuboidscolor r (red)"
assert len(g) == 2, "g[0] is minimal limit and g[1] is maximal limit for random generation of the cuboidscolor g"
assert len(b) == 2, "b[0] is minimal limit and b[1] is maximal limit for random generation of the cuboidscolor b"
#assert len(a) == 2, "a[0] is minimal limit and a[1] is maximal limit for random generation of the cuboidscolor a (alpha/transparency)"
assert len(metallic) == 2, "metallic[0] is minimal limit and metallic[1] is maximal limit for random generation of the cuboidsmaterial property metallic"
assert len(smoothness) == 2, "smoothness[0] is minimal limit and smoothness[1] is maximal limit for random generation of the cuboidsmaterial property smoothness"
config["specify_material"] = specify_material
config["r"]=r
config["g"]=g
config["b"]=b
config["a"]=a
config["metallic"]=metallic
config["smoothness"]=smoothness
config["same_material"] = same_material
# Create intervals for DirectionalLight
if DirectionalLightTheta==None or DirectionalLightIntensity==None:
config["DirectionalLightTheta"] = None
config["DirectionalLightIntensity"] = None
else:
if type(DirectionalLightTheta)==list:
assert len(DirectionalLightTheta) == 2, "DirectionalLightTheta has to be a float or a list with DirectionalLightTheta[0] is minimal limit and DirectionalLightTheta[1] is maximal limit for random generation of DirectionalLightTheta."
else:
assert type(DirectionalLightTheta)in [float, int], "DirectionalLightTheta has to be a float or a list with DirectionalLightTheta[0] is minimal limit and DirectionalLightTheta[1] is maximal limit for random generation of DirectionalLightTheta."
config["DirectionalLightTheta"] = DirectionalLightTheta
if type(DirectionalLightIntensity)==list:
assert len(DirectionalLightIntensity) == 2, "DirectionalLightIntensity has to be a float or a list with DirectionalLightIntensity[0] is minimal limit and DirectionalLightIntensity[1] is maximal limit for random generation of DirectionalLightIntensity."
else:
assert type(DirectionalLightIntensity)in [float, int], "DirectionalLightIntensity has to be a float or a list with DirectionalLightIntensity[0] is minimal limit and DirectionalLightIntensity[1] is maximal limit for random generation of DirectionalLightIntensity."
config["DirectionalLightIntensity"] = DirectionalLightIntensity
# Create intervals for PointLights
if totalPointLights == None:
config["totalPointLights"]=totalPointLights
else:
assert len(totalPointLights) == 2, "totalPointLights is not None which would mean no PointLights. totalPointLights[0] is minimal limit and totalPointLights[1] is maximal limit for random generation of totalPointLights."
config["totalPointLights"]=totalPointLights
assert len(PointLightsRadius) == 2, "PointLightsRadius[0] is minimal limit and PointLightsRadius[1] is maximal limit for random generation of PointLightsRadius."
config["PointLightsRadius"]=PointLightsRadius
assert len(PointLightsPhi) == 2, "PointLightsPhi[0] is minimal limit and PointLightsPhi[1] is maximal limit for random generation of PointLightsPhi."
config["PointLightsPhi"]=PointLightsPhi
assert len(PointLightsTheta) == 2, "PointLightsTheta[0] is minimal limit and PointLightsTheta[1] is maximal limit for random generation of PointLightsTheta."
config["PointLightsTheta"]=PointLightsTheta
assert len(PointLightsIntensity) == 2, "PointLightsIntensity[0] is minimal limit and PointLightsIntensity[1] is maximal limit for random generation of PointLightsIntensity."
config["PointLightsIntensity"]=PointLightsIntensity
assert len(PointLightsRange) == 2, "PointLightsRange[0] is minimal limit and PointLightsRange[1] is maximal limit for random generation of PointLightsRange."
config["PointLightsRange"]=PointLightsRange
if same_PointLightsColor!=None:
assert type(same_PointLightsColor)==bool, "Has to be bool or none. same_PointLightsColor sets the bool of same_PointLightsColor in getRandomJsonData. If its None bool is set randomly."
config["same_PointLightsColor"]=same_PointLightsColor
assert len(PointLightsColor_r) == 2, "PointLightsColor_r[0] is minimal limit and PointLightsColor_r[1] is maximal limit for random generation of PointLightsColor_r."
config["PointLightsColor_r"]=PointLightsColor_r
assert len(PointLightsColor_g) == 2, "PointLightsColor_g[0] is minimal limit and PointLightsColor_g[1] is maximal limit for random generation of PointLightsColor_g."
config["PointLightsColor_g"]=PointLightsColor_g
assert len(PointLightsColor_b) == 2, "PointLightsColor_b[0] is minimal limit and PointLightsColor_b[1] is maximal limit for random generation of PointLightsColor_b."
config["PointLightsColor_b"]=PointLightsColor_b
assert len(PointLightsColor_a) == 2, "PointLightsColor_a[0] is minimal limit and PointLightsColor_a[1] is maximal limit for random generation of PointLightsColor_a."
config["PointLightsColor_a"]=PointLightsColor_a
# Create intervals for SpotLights
if totalSpotLights == None:
config["totalSpotLights"]=totalSpotLights
else:
assert len(totalSpotLights) == 2, "totalSpotLights is not None which would mean no SpotLights. totalSpotLights[0] is minimal limit and totalSpotLights[1] is maximal limit for random generation of totalSpotLights."
config["totalSpotLights"]=totalSpotLights
assert len(SpotLightsRadius) == 2, "SpotLightsRadius[0] is minimal limit and SpotLightsRadius[1] is maximal limit for random generation of SpotLightsRadius."
config["SpotLightsRadius"]=SpotLightsRadius
assert len(SpotLightsPhi) == 2, "SpotLightsPhi[0] is minimal limit and SpotLightsPhi[1] is maximal limit for random generation of SpotLightsPhi."
config["SpotLightsPhi"]=SpotLightsPhi
assert len(SpotLightsTheta) == 2, "SpotLightsTheta[0] is minimal limit and SpotLightsTheta[1] is maximal limit for random generation of SpotLightsTheta."
config["SpotLightsTheta"]=SpotLightsTheta
assert len(SpotLightsIntensity) == 2, "SpotLightsIntensity[0] is minimal limit and SpotLightsIntensity[1] is maximal limit for random generation of SpotLightsIntensity."
config["SpotLightsIntensity"]=SpotLightsIntensity
assert len(SpotLightsRange) == 2, "SpotLightsRange[0] is minimal limit and SpotLightsRange[1] is maximal limit for random generation of SpotLightsRange."
config["SpotLightsRange"]=SpotLightsRange
assert len(SpotAngle) == 2, "SpotAngle[0] is minimal limit and SpotAngle[1] is maximal limit for random generation of SpotAngle."
config["SpotAngle"]=SpotAngle
if same_SpotLightsColor!=None:
assert type(same_SpotLightsColor)==bool, "Has to be bool or None. same_SpotLightsColor sets the bool of same_SpotLightsColor in getRandomJsonData. If its None bool is set randomly."
config["same_SpotLightsColor"]=same_SpotLightsColor
assert len(SpotLightsColor_r) == 2, "SpotLightsColor_r[0] is minimal limit and SpotLightsColor_r[1] is maximal limit for random generation of SpotLightsColor_r."
config["SpotLightsColor_r"]=SpotLightsColor_r
assert len(SpotLightsColor_g) == 2, "SpotLightsColor_g[0] is minimal limit and SpotLightsColor_g[1] is maximal limit for random generation of SpotLightsColor_g."
config["SpotLightsColor_g"]=SpotLightsColor_g
assert len(SpotLightsColor_b) == 2, "SpotLightsColor_b[0] is minimal limit and SpotLightsColor_b[1] is maximal limit for random generation of SpotLightsColor_b."
config["SpotLightsColor_b"]=SpotLightsColor_b
assert len(SpotLightsColor_a) == 2, "SpotLightsColor_a[0] is minimal limit and SpotLightsColor_a[1] is maximal limit for random generation of SpotLightsColor_a."
config["SpotLightsColor_a"]=SpotLightsColor_a
if request_three == True and request_pose == True:
assert 1==0, "request_three and request_pose can currently not be true at the same time."
config["request_pose"] = request_pose
config["request_three"] = request_three
config["seed"] = np.random.randint(np.iinfo(np.int32).max)
np.random.seed(config["seed"])
self.logger.debug("Config Seed: " + str(config["seed"]))
self.config = config
# Save the config for a dataset
if save_config:
if not os.path.exists(self.data_directory + "/config"):
os.makedirs(self.data_directory + "/config")
conf_path = self.data_directory + "/config/" + str(time.strftime("%Y-%m-%d_%H:%M:%S", time.gmtime())) + '_config.json'
i=0
while os.path.exists(conf_path):
i+=1
conf_path = self.data_directory + "/config/" + str(time.strftime("%Y-%m-%d_%H:%M:%S", time.gmtime())) + '_config(' + str(i) + ').json'
with open(conf_path, 'w') as f:
json.dump(config,f)
f.close()
def load_config(self, index_config=0, file_name=None):
"""This enables you to load an old config to replicate a dataset.
:param index_config: This will give you the latest config if set to zero, if set to one it will set the the second latest config and so on, defaults to ``0``
:type index_config: int, optional
:param file_name: This specifies the file name. If this is not default the specified file will be loaded, defaults to ``None``
:type file_name: string or None, optional
"""
if file_name!=None:
assert type(file_name)==str , "The file_name of the loading config has to be a string"
if file_name[-5:] != ".json":
file_name += ".json"
try:
with open(self.data_directory + "/config/" + file_name) as f:
self.config = json.load(f)
f.close()
except FileNotFoundError:
self.logger.debug("this config could not be loaded:" + self.data_directory + "/config/" + file_name + "; check for spelling mistakes for file_name: " + file_name + ", or if the file is in the right directory: " + self.data_directory + "/config/")
else:
files = os.listdir(self.data_directory + "/config/")
paths = [os.path.join(self.data_directory + "/config/", basename) for basename in files]
for i in range(1+index_config):
final = max(paths, key=os.path.getctime)
paths.remove(final)
try:
with open(final) as f:
self.config = json.load(f)
f.close()
except FileNotFoundError:
self.logger.debug("config could not be loaded:" + final + "; check that index_config is smaller than the amount of all config files -1; or if the file is in the right directory: " + self.data_directory + "/config/")
np.random.seed(self.config["seed"])
def create_random_parameters(self):
"""Creates random input parameters depending on your config which defines the interval for the generated parameters, the camera parameters are not set randomly
:param CameraRes_width: Image resolution width, defaults to 520
:type CameraRes_width: int, optional
:param CameraRes_height: Image resolution height, defaults to 520
:type CameraRes_height: int, optional
:return: A dictionary to use as input for function :meth:`~dataset.dataset_cuboids.create_json_string_from_parameters`.
:rtype: dictionary
"""
dictionary={}
# not random set variables for the Camera
dictionary["CameraRes_width"] = self.config["CameraRes_width"]
dictionary["CameraRes_height"] = self.config["CameraRes_height"]
dictionary["Camera_FieldofView"] = self.config["Camera_FieldofView"]
dictionary["request_pose"] = self.config["request_pose"]
# Create all parameters randomly
# Camera position
if type(self.config["CameraRadius"]) in [float, int]:
dictionary["CameraRadius"] = self.config["CameraRadius"]
else:
dictionary["CameraRadius"] = np.random.uniform(self.config["CameraRadius"][0],self.config["CameraRadius"][1])
if type(self.config["CameraTheta"]) in [float, int]:
dictionary["CameraTheta"] = self.config["CameraTheta"]
else:
dictionary["CameraTheta"] = np.random.uniform(self.config["CameraTheta"][0],self.config["CameraTheta"][1])
if type(self.config["CameraPhi"]) in [float, int]:
dictionary["CameraPhi"] = self.config["CameraPhi"]
else:
dictionary["CameraPhi"] = np.random.uniform(self.config["CameraPhi"][0],self.config["CameraPhi"][1])
if type(self.config["CameraVerticalOffset"]) in [float, int]:
dictionary["CameraVerticalOffset"] = self.config["CameraVerticalOffset"]
else:
dictionary["CameraVerticalOffset"] = np.random.uniform(self.config["CameraVerticalOffset"][0],self.config["CameraVerticalOffset"][1])
dictionary["Camera_solid_background"] = self.config["Camera_solid_background"]
# Create how many Cubiods are in one branch.
if type(self.config["total_cuboids"])==int:
total_cuboids = self.config["total_cuboids"]
else:
total_cuboids = np.random.randint(self.config["total_cuboids"][0],self.config["total_cuboids"][1])
# Create the angle and the intsaveensity of the directional light
if self.config["DirectionalLightIntensity"] == None:
DirectionalLightIntensity = 0
DirectionalLightTheta = 0
else:
if type(self.config["DirectionalLightIntensity"])==list:
DirectionalLightIntensity = np.random.uniform(self.config["DirectionalLightIntensity"][0], self.config["DirectionalLightIntensity"][1])
else:
DirectionalLightIntensity = self.config["DirectionalLightIntensity"]
if type(self.config["DirectionalLightTheta"]) ==list:
DirectionalLightTheta = np.random.uniform(self.config["DirectionalLightTheta"][0], self.config["DirectionalLightTheta"][1])
else:
DirectionalLightTheta = self.config["DirectionalLightTheta"]
# Create all properties of the point lights
PointLightRadius = []
PointLightPhi = []
PointLightTheta = []
PointLightIntensity=[]
PointLightRange = []
P_R=[]
P_G=[]
P_B=[]
P_A=[]
# Amount of Pointlights
if self.config["totalPointLights"] == None:
TotalPointLights = 0
else:
TotalPointLights = np.random.randint(self.config["totalPointLights"][0],self.config["totalPointLights"][1])
# If all Pointlights should have the same color
if(self.config["same_PointLightsColor"]==None):
Same_PLcolor = bool(np.random.randint(2,dtype=int))
else:
Same_PLcolor = self.config["same_PointLightsColor"]
# Create color for Pointlights
if(Same_PLcolor):
pr=np.random.uniform(self.config["PointLightsColor_r"][0],self.config["PointLightsColor_r"][1])
pg=np.random.uniform(self.config["PointLightsColor_g"][0],self.config["PointLightsColor_g"][1])
pb=np.random.uniform(self.config["PointLightsColor_b"][0],self.config["PointLightsColor_b"][1])
pa=np.random.uniform(self.config["PointLightsColor_a"][0],self.config["PointLightsColor_a"][1])
for i in range(TotalPointLights):
P_R.append(pr)
P_G.append(pg)
P_B.append(pb)
P_A.append(pa)
else:
P_R=np.random.uniform(self.config["PointLightsColor_r"][0],self.config["PointLightsColor_r"][1],TotalPointLights).tolist()
P_G=np.random.uniform(self.config["PointLightsColor_g"][0],self.config["PointLightsColor_g"][1],TotalPointLights).tolist()
P_B=np.random.uniform(self.config["PointLightsColor_b"][0],self.config["PointLightsColor_b"][1],TotalPointLights).tolist()
P_A=np.random.uniform(self.config["PointLightsColor_a"][0],self.config["PointLightsColor_a"][1],TotalPointLights).tolist()
# Create spherical coordinate position for all Pointlights
PointLightRadius=np.random.uniform(self.config["PointLightsRadius"][0],self.config["PointLightsRadius"][1],TotalPointLights).tolist()
PointLightRange=np.random.uniform(self.config["PointLightsRange"][0],self.config["PointLightsRange"][1],TotalPointLights).tolist()
PointLightPhi=np.random.uniform(self.config["PointLightsPhi"][0],self.config["PointLightsPhi"][1],TotalPointLights).tolist()
PointLightTheta=np.random.uniform(self.config["PointLightsTheta"][0],self.config["PointLightsTheta"][1],TotalPointLights).tolist()
PointLightIntensity=np.random.uniform(self.config["PointLightsIntensity"][0],self.config["PointLightsIntensity"][1],TotalPointLights).tolist()
# Create all properties of the Spotlights
SpotLightRadius = []
SpotLightPhi = []
SpotLightTheta = []
SpotLightIntensity=[]
SpotLightRange = []
SpotAngle = []
S_R=[]
S_G=[]
S_B=[]
S_A=[]
# Amount of Spotlights
if self.config["totalSpotLights"] == None:
TotalSpotLights = 0
else:
TotalSpotLights = np.random.randint(self.config["totalSpotLights"][0],self.config["totalSpotLights"][1])
# If all Spotlights should have the same color
if(self.config["same_SpotLightsColor"]==None):
Same_SLcolor = bool(np.random.randint(2))
else:
Same_SLcolor = self.config["same_SpotLightsColor"]
# Create color for Spotlights
if(Same_SLcolor):
sr=np.random.uniform(self.config["SpotLightsColor_r"][0],self.config["SpotLightsColor_r"][1])
sg=np.random.uniform(self.config["SpotLightsColor_g"][0],self.config["SpotLightsColor_g"][1])
sb=np.random.uniform(self.config["SpotLightsColor_b"][0],self.config["SpotLightsColor_b"][1])
sa=np.random.uniform(self.config["SpotLightsColor_a"][0],self.config["SpotLightsColor_a"][1])
for i in range(TotalSpotLights):
S_R.append(sr)
S_G.append(sg)
S_B.append(sb)
S_A.append(sa)
else:
S_R= np.random.uniform(self.config["SpotLightsColor_r"][0],self.config["SpotLightsColor_r"][1],TotalSpotLights).tolist()
S_G=np.random.uniform(self.config["SpotLightsColor_g"][0],self.config["SpotLightsColor_g"][1],TotalSpotLights).tolist()
S_B=np.random.uniform(self.config["SpotLightsColor_b"][0],self.config["SpotLightsColor_b"][1],TotalSpotLights).tolist()
S_A=np.random.uniform(self.config["SpotLightsColor_a"][0],self.config["SpotLightsColor_a"][1],TotalSpotLights).tolist()
# Create the positition of Spotlights
SpotLightRadius=np.random.uniform(self.config["SpotLightsRadius"][0],self.config["SpotLightsRadius"][1],TotalSpotLights).tolist()
SpotLightPhi=np.random.uniform(self.config["SpotLightsPhi"][0],self.config["SpotLightsPhi"][1],TotalSpotLights).tolist()
SpotLightTheta=np.random.uniform(self.config["SpotLightsTheta"][0],self.config["SpotLightsTheta"][1],TotalSpotLights).tolist()
# Create specific properties of the Spotlights
SpotLightIntensity=np.random.uniform(self.config["SpotLightsIntensity"][0],self.config["SpotLightsIntensity"][1],TotalSpotLights).tolist()
SpotLightRange=np.random.uniform(self.config["SpotLightsRange"][0],self.config["SpotLightsRange"][1],TotalSpotLights).tolist()
# The Angle specifies the "spread" of the lightcone created by the Spotlight
SpotAngle=np.random.uniform(self.config["SpotAngle"][0],self.config["SpotAngle"][1],TotalSpotLights).tolist()
# Amount of branches (Arms) and at which cubiod to branch. The first element of the list total_branchess counts the amount of branches at the first cubiod and and so on...
# if total_branches = [1,1,1,1,1] then there is only one "main" Branch and no splits
total_branches = []
if self.config["branches"]==None:
total_branches = None
else:
if self.config["specify_branches"]:
total_branches = self.config["branches"]
else:
if self.config["branches"][0] > 1:
self.logger.info("config['branches'][0] is bigger than 1. This means you create at every segment new arms. Change the dataset config if you do not want this.")
# the upper limit for the arms is dicribing the three sigma variance of the guassion normal distribution
arms = np.random.normal(0,(self.config["branches"][1] - self.config["branches"][0])/3, total_cuboids-1)
arms = np.absolute(arms) + self.config["branches"][0]
for i in range(total_cuboids-1):
if arms[i]<=1 :
total_branches.append(1)
else:
total_branches.append(int(arms[i]))
self.logger.debug("total_branches: "+str(total_branches))
# Decide if there should be only one theta angle for cubiods
if(self.config["same_theta"]==None):
Same_Theta = bool(np.random.randint(2))
else:
Same_Theta = self.config["same_theta"]
# Theta describes the angel between two cubiods next to each other
Theta = []
if(Same_Theta):
if self.config["theta"]==None:
Theta.append(np.random.uniform(0,360/total_cuboids))
elif type(self.config["theta"])==list:
assert len(self.config["theta"]) == 2, "self.config['theta'] is a list and Same_theta = True which means it has to be of length two or a float."
Theta.append(np.random.uniform(self.config["theta"][0],self.config["theta"][1]))
elif type(self.config["theta"])in [float, int]:
Theta.append(self.config["theta"])
else:
self.logger.error("Bad input parameters in self.config['theta'] in combination with self.config['same_theta'].")
else:
if self.config["theta"]==None:
Theta=np.random.uniform(0,360/total_cuboids,total_cuboids-1).tolist()
elif type(self.config["theta"])==list:
if self.config["specify_theta"]:
Theta = self.config["theta"]
else:
Theta=np.random.uniform(self.config["theta"][0],self.config["theta"][1],total_cuboids-1).tolist()
else:
self.logger.error("Bad input parameters in self.config['theta'] in combination with self.config['same_theta'].")
# Decide if all cubiods use the same Material
if(self.config["same_material"]==None):
Same_Material = bool(np.random.randint(2))
else:
Same_Material = self.config["same_material"]
R=[]
G=[]
B=[]
A=[]
Metallic=[]
Smoothness=[]
# Create the colors of the cubiods and the material property smoothness and metallic
if self.config["specify_material"]:
R=self.config["r"]
G=self.config["g"]
B=self.config["b"]
A=self.config["a"]
Metallic=self.config["metallic"]
Smoothness=self.config["smoothness"]
else:
if(Same_Material):
R=np.random.uniform(self.config["r"][0],self.config["r"][1])
G=np.random.uniform(self.config["g"][0],self.config["g"][1])
B=np.random.uniform(self.config["b"][0],self.config["b"][1])
if type(self.config["a"]) in [int,float]:
A=self.config["a"]
else:
A=np.random.uniform(self.config["a"][0],self.config["a"][1])
Metallic=np.random.uniform(self.config["metallic"][0],self.config["metallic"][1])
Smoothness=np.random.uniform(self.config["smoothness"][0],self.config["smoothness"][1])
else:
R=np.random.uniform(self.config["r"][0],self.config["r"][1],total_cuboids).tolist()
G=np.random.uniform(self.config["g"][0],self.config["g"][1],total_cuboids).tolist()
B=np.random.uniform(self.config["b"][0],self.config["b"][1],total_cuboids).tolist()
if type(self.config["a"]) in [int,float]:
for i in range(total_cuboids):
A.append(self.config["a"])
else:
A=np.random.uniform(self.config["a"][0],self.config["a"][1],total_cuboids).tolist()
Metallic=np.random.uniform(self.config["metallic"][0],self.config["metallic"][1],total_cuboids).tolist()
Smoothness=np.random.uniform(self.config["smoothness"][0],self.config["smoothness"][1],total_cuboids).tolist()
# Decide if all cubiods should have the same vertical scale
if(self.config["same_scale"]==None):
Same_Scale = bool(np.random.randint(2))
else:
Same_Scale = self.config["same_scale"]
# Create Scale
if(Same_Scale):
if type(self.config["scale"])==list:
Scale = np.random.uniform(self.config["scale"][0],self.config["scale"][1])
else:
Scale = self.config["scale"]
else:
if self.config["specify_scale"]:
Scale = self.config["scale"]
else:
Scale = np.random.uniform(self.config["scale"][0],self.config["scale"][1],total_cuboids).tolist()
# Create Phi the rotation of the crane
if type(self.config["phi"])in [float, int]:
Phi = self.config["phi"]
else:
Phi=np.random.uniform(self.config["phi"][0],self.config["phi"][1])
# Add all parameters to a dictionary
dictionary["total_cuboids"] = total_cuboids
dictionary["scale"] = Scale
dictionary["same_scale"] = Same_Scale
dictionary["total_branches"] = total_branches
dictionary["same_theta"] = Same_Theta
dictionary["theta"] = Theta
dictionary["phi"] = Phi
dictionary["same_material"] = Same_Material
dictionary["r"] = R
dictionary["g"] = G
dictionary["b"] = B
dictionary["a"] = A
dictionary["metallic"] = Metallic
dictionary["smoothness"] = Smoothness
dictionary["DirectionalLightTheta"] = DirectionalLightTheta
dictionary["DirectionalLightIntensity"] = DirectionalLightIntensity
if TotalPointLights == 0:
dictionary["totalPointLights"] = TotalPointLights
dictionary["PointLightsRadius"] = None
dictionary["PointLightsPhi"] = None
dictionary["PointLightsTheta"] = None
dictionary["PointLightsIntensity"] = None
dictionary["PointLightsRange"] = None
dictionary["PointLightsColor_r"] = None
dictionary["PointLightsColor_g"] = None
dictionary["PointLightsColor_b"] = None
dictionary["PointLightsColor_a"] = None
dictionary["same_PointLightsColor"] = None
else:
dictionary["totalPointLights"] = TotalPointLights
dictionary["PointLightsRadius"] = PointLightRadius
dictionary["PointLightsPhi"] = PointLightPhi
dictionary["PointLightsTheta"] = PointLightTheta
dictionary["PointLightsIntensity"] = PointLightIntensity
dictionary["PointLightsRange"] = PointLightRange
dictionary["PointLightsColor_r"] = P_R
dictionary["PointLightsColor_g"] = P_G
dictionary["PointLightsColor_b"] = P_B
dictionary["PointLightsColor_a"] = P_A
dictionary["same_PointLightsColor"] = Same_PLcolor
if TotalSpotLights == 0:
dictionary["totalSpotLights"] = TotalSpotLights
dictionary["SpotLightsRadius"] = None
dictionary["SpotLightsPhi"] = None
dictionary["SpotLightsTheta"] = None
dictionary["SpotLightsIntensity"] = None
dictionary["SpotLightsRange"] = None
dictionary["SpotLightsColor_r"] = None
dictionary["SpotLightsColor_g"] = None
dictionary["SpotLightsColor_b"] = None
dictionary["SpotLightsColor_a"] = None
dictionary["same_SpotLightsColor"] = None
dictionary["SpotLightsRange"] = None
dictionary["SpotAngle"] = None
else:
dictionary["totalSpotLights"] = TotalSpotLights
dictionary["SpotLightsRadius"] = SpotLightRadius
dictionary["SpotLightsPhi"] = SpotLightPhi
dictionary["SpotLightsTheta"] = SpotLightTheta
dictionary["SpotLightsIntensity"] = SpotLightIntensity
dictionary["SpotLightsRange"] = SpotLightRange
dictionary["SpotLightsColor_r"] = S_R
dictionary["SpotLightsColor_g"] = S_G
dictionary["SpotLightsColor_b"] = S_B
dictionary["SpotLightsColor_a"] = S_A
dictionary["same_SpotLightsColor"] = Same_SLcolor
dictionary["SpotLightsRange"] = SpotLightRange
dictionary["SpotAngle"] = SpotAngle
return dictionary
def create_parameters(self, same_scale=True, scale=2, total_cuboids=5, phi=90, total_branches=[1,2,1,4], same_theta=True, theta=20,
same_material=True, r=1, g=0.1, b=0.2, a=1, metallic=1, smoothness=0.5, CameraRes_width= 1024, CameraRes_height=1024, Camera_FieldofView=100, CameraRadius = 10.0, CameraTheta = 90, CameraPhi = 0, CameraVerticalOffset = 0, Camera_solid_background = False,
totalPointLights=2, PointLightsRadius=[5,6], PointLightsPhi=[0,95], PointLightsTheta=[45,60], PointLightsIntensity=[10,12], PointLightsRange=[10,10], same_PointLightsColor=True, PointLightsColor_r=1, PointLightsColor_g=1, PointLightsColor_b=1, PointLightsColor_a=1,
totalSpotLights=None, SpotLightsRadius=[5,20], SpotLightsPhi=[0,360], SpotLightsTheta=[0,90], SpotLightsIntensity=[5,15], SpotLightsRange=[5,25], SpotAngle=[5,120], same_SpotLightsColor=None, SpotLightsColor_r=[0,1], SpotLightsColor_g=[0,1], SpotLightsColor_b=[0,1], SpotLightsColor_a=[0.5,1],
DirectionalLightTheta = 60, DirectionalLightIntensity = 3):
dictionary = {}
dictionary["total_cuboids"] =total_cuboids
dictionary["same_scale"] = same_scale
dictionary["scale"] = scale
dictionary["same_theta"] = same_theta
dictionary["theta"] = theta
dictionary["phi"] = phi
dictionary["total_branches"] = total_branches
dictionary["same_material"] = same_material
dictionary["metallic"] = metallic
dictionary["smoothness"] = smoothness
dictionary["r"] = r
dictionary["g"] = g
dictionary["b"] = b
dictionary["a"] = a
dictionary["CameraRes_width"] = CameraRes_width
dictionary["CameraRes_height"] = CameraRes_height
dictionary["Camera_FieldofView"] = Camera_FieldofView
dictionary["CameraRadius"] = CameraRadius
dictionary["CameraTheta"] = CameraTheta
dictionary["CameraPhi"] = CameraPhi
dictionary["CameraVerticalOffset"] = CameraVerticalOffset
dictionary["Camera_solid_background"] = Camera_solid_background
if totalPointLights in [0,None]:
dictionary["totalPointLights"] = 0
dictionary["PointLightsRadius"] = None
dictionary["PointLightsPhi"] = None
dictionary["PointLightsTheta"] = None
dictionary["PointLightsIntensity"] = None
dictionary["PointLightsRange"] = None
dictionary["PointLightsColor_r"] = None
dictionary["PointLightsColor_g"] = None
dictionary["PointLightsColor_b"] = None
dictionary["PointLightsColor_a"] = None
dictionary["same_PointLightsColor"] = None
else:
dictionary["totalPointLights"] = totalPointLights
dictionary["same_PointLightsColor"] = same_PointLightsColor
dictionary["PointLightsColor_r"] = PointLightsColor_r
dictionary["PointLightsColor_g"] = PointLightsColor_g
dictionary["PointLightsColor_b"] = PointLightsColor_b
dictionary["PointLightsColor_a"] = PointLightsColor_a
dictionary["PointLightsRadius"] = PointLightsRadius
dictionary["PointLightsTheta"] = PointLightsTheta
dictionary["PointLightsPhi"] = PointLightsPhi
dictionary["PointLightsIntensity"] = PointLightsIntensity
dictionary["PointLightsRange"] = PointLightsRange
if totalSpotLights in [0,None]:
dictionary["totalSpotLights"] = 0
dictionary["SpotLightsRadius"] = None
dictionary["SpotLightsPhi"] = None
dictionary["SpotLightsTheta"] = None
dictionary["SpotLightsIntensity"] = None
dictionary["SpotLightsRange"] = None
dictionary["SpotLightsColor_r"] = None
dictionary["SpotLightsColor_g"] = None
dictionary["SpotLightsColor_b"] = None
dictionary["SpotLightsColor_a"] = None
dictionary["same_SpotLightsColor"] = None
dictionary["SpotLightsRange"] = None
dictionary["SpotAngle"] = None
else:
dictionary["totalSpotLights"] = totalSpotLights
dictionary["same_SpotLightsColor"] = same_SpotLightsColor
dictionary["SpotLightsColor_r"] = SpotLightsColor_r
dictionary["SpotLightsColor_g"] = SpotLightsColor_g
dictionary["SpotLightsColor_b"] = SpotLightsColor_b
dictionary["SpotLightsColor_a"] = SpotLightsColor_a
dictionary["SpotLightsRadius"] = SpotLightsRadius
dictionary["SpotLightsTheta"] = SpotLightsTheta
dictionary["SpotLightsPhi"] = SpotLightsPhi
dictionary["SpotLightsIntensity"] = SpotLightsIntensity
dictionary["SpotLightsRange"] = SpotLightsRange
dictionary["SpotAngle"] = SpotAngle
dictionary["DirectionalLightTheta"] = DirectionalLightTheta
dictionary["DirectionalLightIntensity"] = DirectionalLightIntensity
return dictionary
def create_json_string_from_parameters(self, dictionary):
"""
Inputs the parameters/dictionary into the function :meth:`~client.client_communicator_to_unity.write_json_crane`.
:param dictionary: A dictionary similar to one from :meth:`~dataset.dataset_cuboids.create_random_parameters` with all input parameters for the function :meth:`~client.client_communicator_to_unity.write_json_crane`.
:type dictionary: dictionary
:return: A string depending on your input parameters wich can be interpreted afterwards by the Unity script.
:rtype: string
"""
return self.uc.write_json_crane(request_pose = dictionary["request_pose"], total_cuboids=dictionary["total_cuboids"], same_scale=dictionary["same_scale"], scale=dictionary["scale"], same_theta=dictionary["same_theta"], theta=dictionary["theta"], phi=dictionary["phi"], total_branches=dictionary["total_branches"], same_material=dictionary["same_material"], metallic=dictionary["metallic"], smoothness=dictionary["smoothness"], r=dictionary["r"], g=dictionary["g"], b=dictionary["b"], a=dictionary["a"],
CameraRes_width=dictionary["CameraRes_width"], CameraRes_height=dictionary["CameraRes_height"], Camera_FieldofView=dictionary["Camera_FieldofView"], CameraRadius=dictionary["CameraRadius"], CameraTheta=dictionary["CameraTheta"], CameraPhi=dictionary["CameraPhi"], CameraVerticalOffset=dictionary["CameraVerticalOffset"], Camera_solid_background=dictionary["Camera_solid_background"],
totalPointLights=dictionary["totalPointLights"], same_PointLightsColor=dictionary["same_PointLightsColor"], PointLightsColor_r=dictionary["PointLightsColor_r"], PointLightsColor_g=dictionary["PointLightsColor_g"], PointLightsColor_b=dictionary["PointLightsColor_b"], PointLightsColor_a=dictionary["PointLightsColor_a"], PointLightsRadius=dictionary["PointLightsRadius"], PointLightsTheta=dictionary["PointLightsTheta"], PointLightsPhi=dictionary["PointLightsPhi"], PointLightsIntensity=dictionary["PointLightsIntensity"], PointLightsRange=dictionary["PointLightsRange"],
totalSpotLights=dictionary["totalSpotLights"], same_SpotLightsColor=dictionary["same_SpotLightsColor"], SpotLightsColor_r=dictionary["SpotLightsColor_r"], SpotLightsColor_g=dictionary["SpotLightsColor_g"], SpotLightsColor_b=dictionary["SpotLightsColor_b"], SpotLightsColor_a=dictionary["SpotLightsColor_a"], SpotLightsRadius=dictionary["SpotLightsRadius"], SpotLightsTheta=dictionary["SpotLightsTheta"], SpotLightsPhi=dictionary["SpotLightsPhi"], SpotLightsIntensity=dictionary["SpotLightsIntensity"], SpotLightsRange=dictionary["SpotLightsRange"],SpotAngle=dictionary["SpotAngle"],
DirectionalLightTheta=dictionary["DirectionalLightTheta"], DirectionalLightIntensity=dictionary["DirectionalLightIntensity"])
def save(self, dictionary, save_image = False, save_para = None):
"""Save parameter data or/and an image of a dictionary in the ``data/parameters`` folder or the ``data/images`` folder. The saved parameters can be loaded with :meth:`~dataset.dataset_cuboids.load_parameters` and then manipulated to create an altered image.
:param dictionary: A dictionary with keys as "index", "parameters" and if needed "image". For example a returned dictionary from :meth:`~dataset.dataset_cuboids.parameters_to_finished_data`.
:type dictionary: dictionary
:param save_para: If the parameters should be saved to ``data/parameters`` labeled with the index if available, if ``None`` and there is no seed save in the config, defaults to ``None``
:type save_para: bool, optional
:param save_image: If the image should be saved to ``data/images`` labeled with the index if available, defaults to ``False``
:type save_image: bool, optional
"""
# Save parameters.
if save_para==None:
if "seed" in self.config:
self.logger.debug("key: 'seed' is found in config")
save_para=False
else:
save_para=True
self.logger.debug("key: 'seed' is not found in config")
if save_para:
# Check and if necessary create directory
directory_para = self.data_directory + "/parameters"
if not os.path.exists(directory_para):
os.makedirs(directory_para)
# Check if the parameters and the index is in the dictionary.
if "parameters" in dictionary:
if "index" in dictionary:
if "request_three" in self.config and self.config["request_three"]:
for i in range(3):
with open(directory_para + "/parameters_index_" + str(dictionary["index"]) + "_" + str(i) + '.json', 'w') as f:
json.dump(dictionary["parameters"][i],f)
f.close()
else:
with open(directory_para + "/parameters_index_" + str(dictionary["index"]) + '.json', 'w') as f:
json.dump(dictionary["parameters"],f)
f.close()
else:
assert 1==0, "no index in dictionary"
'''
# Change the name of the json file if there is no index given.
fake_index = np.random.randint(0,1000)
with open(directory_para + "/parameters_NO_index_" + str(fake_index) + '.json', 'w') as f:
json.dump(dictionary["parameters"],f)
f.close()
'''
else:
self.logger.error("Image parameters could not be saved. No parameters found in dictionary.")
assert 1==0, "no parameters in dictionary"
# Save the image as png.
if save_image:
if "pose" in dictionary:
# Check and if necessary create directory
directory_images = self.data_directory + "/images"
directory_images_poses = self.data_directory + "/images_poses"
if not os.path.exists(directory_images):
os.makedirs(directory_images)
if not os.path.exists(directory_images_poses):
os.makedirs(directory_images_poses)
# Check if the image and the index is in the dictionary.
if "image" in dictionary:
if "index" in dictionary:
if "request_three" in self.config and self.config["request_three"]:
assert 1==0, "Pose images are not permitted with request_three images."
else:
Image.fromarray(dictionary["image"]).save(directory_images + "/image_index_" + str(dictionary["index"]) + '.png')
Image.fromarray(dictionary["pose"]).save(directory_images_poses + "/image_index_" + str(dictionary["index"]) + '.png')
else:
assert 1==0, "No index in dictionary."
'''
# Change the name of the image if there is no index given.
fake_index = np.random.randint(0,1000)
Image.fromarray(dictionary["image"]).save(directory_images + "/image_NO_index_" + str(fake_index) + '.png')
Image.fromarray(dictionary["pose"]).save(directory_images_poses + "/image_NO_index_" + str(fake_index) + '.png')
'''
else:
self.logger.error("Image could not be saved. No image data not found in dictionary.")
assert 1==0, "No index in dictionary."
else:
# Check and if necessary create directory
directory_images = self.data_directory + "/images"
if not os.path.exists(directory_images):
os.makedirs(directory_images)
# Check if the image and the index is in the dictionary.
if "image" in dictionary:
if "index" in dictionary:
if "request_three" in self.config and self.config["request_three"]:
for i in range(3):
Image.fromarray(dictionary["image"][i]).save(directory_images + "/image_index_" + str(dictionary["index"]) + "_" + str(i) + '.png')
else:
Image.fromarray(dictionary["image"]).save(directory_images + "/image_index_" + str(dictionary["index"]) + '.png')
else:
assert 1==0, "No index in dictionary."
'''
# Change the name of the image if there is no index given.
fake_index = np.random.randint(0,1000)
Image.fromarray(dictionary["image"]).save(directory_images + "/image_NO_index_" + str(fake_index) + '.png')
'''
else:
self.logger.error("Image could not be saved. No image data not found in dictionary.")
assert 1==0, "No image in dictionary."
def load_parameters(self,index = [-1], amount = 1):
"""Load and return a given amount of parameters as dictionaries in a list. If index is not specified the index will be chosen randomly.
:param index: If index is default it will be generated randomly else it has to be a list of integers the length of ``amount``, defaults to [-1]
:type index: list, optional
:param amount: The amount of dictionaries in the returned list, defaults to 1
:type amount: int, optional
:return: A list of parameters as dictionaries is returned except the amount is one or default the dictionary itself will be returned.
:rtype: list or dictionary
"""
if index[0]==-1:
index = np.random.randint(0,self.read_index(),amount)
else:
assert len(index)==amount, "Specified Index has to be len(Index):" + str(len(index)) + " equal to amount:" + str(amount)
parameter_list = []
# Load the amount of parameters and append them to the parameter_list
for i in range(amount):
while 1:
try:
f = open(self.data_directory + "/parameters/parameters_index_" + str(index[i]) + ".json")
parameter_list.append(json.load(f))
f.close()
break
except FileNotFoundError:
self.logger.debug("File with index" + str(index[i]) + "does not exist.")
index[i] +=1
# If the amount is 1 then return the parameters directly
if amount==1:
return parameter_list[0]
# otherwise return the list of the parameters
else:
return parameter_list
def parameters_to_finished_data(self, parameters, save_image = True, save_para = None, return_dict = True):
"""Input parameters and get an corresponding image.
:param parameters: Parameters to use for :meth:`~dataset.dataset_cuboids.create_json_string_from_parameters`.
:type parameters: dictionary
:param save_para: If the parameters should be saved at ``data/parameters``, if ``None`` they will not be created if a seed is available in the config, defaults to None
:type save_para: bool or None, optional
:param save_image: If the image should be saved at ``data/images``, defaults to True
:type save_image: bool, optional
:return: A dictionary with all relevant data in keys as "index","parameters" and "image".