-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate_random_palette.py
71 lines (52 loc) · 1.71 KB
/
generate_random_palette.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
import sys, getopt
import shutil
import os
import json
def _getvocpallete(num_cls):
n = num_cls
pallete = [0]*(n*3)
for j in range(0, n):
lab = j
pallete[j*3+0] = 0
pallete[j*3+1] = 0
pallete[j*3+2] = 0
i = 0
while (lab > 0):
pallete[j*3+0] |= (((lab >> 0) & 1) << (7-i))
pallete[j*3+1] |= (((lab >> 1) & 1) << (7-i))
pallete[j*3+2] |= (((lab >> 2) & 1) << (7-i))
i = i + 1
lab >>= 3
return pallete
def get_configuration(model_name):
base_models_dir = './model'
try:
model_path = os.path.join(base_models_dir, model_name)
except Exception as ex:
raise ex
path = os.path.join(model_path,'configuration.json')
try:
with open(path) as f:
configuration = json.loads(f.read())
except Exception as ex:
raise ex
return configuration["classes"],model_path
def main(argv):
try:
if not argv or argv[0] == '-h':
print 'generate_random_palette.py -m <ModelName>'
sys.exit()
elif argv[0] in ("-m", "--model"):
class_num,model_path = get_configuration(argv[1])
palette= _getvocpallete(class_num)
full_path = model_path + '/palette.txt'
with open(full_path,'w') as filehandle:
for i in palette:
filehandle.write('%s\n' % i)
filehandle.close()
print("Done!")
except getopt.GetoptError:
print 'generate_random_palette.py -m <ModelName>'
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])