-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateClockFaceTensorImage.py
More file actions
164 lines (128 loc) · 5.94 KB
/
createClockFaceTensorImage.py
File metadata and controls
164 lines (128 loc) · 5.94 KB
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
# -*- coding: utf-8 -*-
"""image.ipynb のコピー
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1LDY8bRHe-l7Sg-vc-1T_wKEOQUF0yk63
"""
import os, sys, getopt
import tensorflow as tf
import math
import numpy as np
def tensor_to_png( image, filename):
image_enc = tf.io.encode_png(image)
fname = tf.constant(filename)
fwrite = tf.io.write_file(fname, image_enc)
def image_add_circle( np_image, x_m, y_m, radius, shade=255 ):
for angle in range(0,360):
x = int(radius * math.cos( angle*math.pi/180)) + x_m
y = int(radius * math.sin( angle*math.pi/180)) + y_m
np_image[x,y,0] = shade
np_image[x,y,1] = shade
np_image[x,y,2] = shade
def image_add_line( np_image, x_m, y_m, length, angle, shade=255, thickness=1):
for width in range( 0, thickness ):
for r in range(0, length):
x = -int( r * math.cos( angle*math.pi/180) ) + x_m
y = int( r * math.sin( angle*math.pi/180) ) + y_m
np_image[x,y,0] = shade
np_image[x,y,1] = shade
np_image[x,y,2] = shade
if width > 0:
x = -int( r * math.cos( angle*math.pi/180) ) + x_m + width
y = int( r * math.sin( angle*math.pi/180) ) + y_m + width
np_image[x,y,0] = shade
np_image[x,y,1] = shade
np_image[x,y,2] = shade
x = -int( r * math.cos( angle*math.pi/180) ) + x_m - width
y = int( r * math.sin( angle*math.pi/180) ) + y_m - width
np_image[x,y,0] = shade
np_image[x,y,1] = shade
np_image[x,y,2] = shade
def image_add_tick( np_image, x_m, y_m, radius,step=6, tick_len=1, shade=255):
for offset in range(0, tick_len ):
for angle in range(0,360, step):
x = int( ( radius + offset ) * math.sin( angle*math.pi/180) ) + x_m
y = int( ( radius + offset ) * math.cos( angle*math.pi/180) ) + y_m
np_image[x,y,0] = shade
np_image[x,y,1] = shade
np_image[x,y,2] = shade
def clock_face( clockface, diameter, hour, minute, hr_len, min_len, back_shade=255, hand_shade=255, thickness=1 ):
np_image = np.full( ( ( diameter) , ( diameter),3), back_shade )
x_m = int(diameter/2)
y_m = int(diameter/2)
image_add_line( np_image, x_m, y_m, hr_len, hour*30, shade = hand_shade, thickness=thickness )
image_add_line( np_image, x_m, y_m, min_len, minute*6, shade = hand_shade, thickness=thickness )
image = tf.convert_to_tensor( np_image, tf.float32 )
image = tf.cast( image, dtype=tf.uint8 )
clockface = tf.cast( clockface, dtype=tf.uint8 )
image = tf.math.add_n([clockface, image])
return image
def clock_face_gen( clockface, sub_dir, file_handle, diameter, hour_len, minute_len, thickness=1 ):
base_dir = os.getcwd()
full_dir = os.path.join( base_dir, sub_dir )
if not os.path.isdir(full_dir):
os.mkdir(full_dir)
clockface = tf.image.resize(clockface, [ diameter, diameter] )
for hour in range(0, 12 ):
for minute in range( 0, 60):
hour_precise = hour + minute/60
image = clock_face( clockface, diameter, hour_precise, minute, hour_len, minute_len, 240, 30, thickness=thickness )
filename = "clock_" + str(hour) + "_" + str(minute) + "_" + str(hour_len) + "_" + str(minute_len)+ "_" + str(thickness) + "_0.png"
filename_path = os.path.join(sub_dir, filename)
csv_line = filename_path + "," + str( math.sin( math.radians(hour_precise*30))/1.01 )
csv_line += "," + str( math.cos( math.radians(hour_precise*30))/1.01 )
csv_line += "," + str( math.sin( math.radians(minute*6))/1.01 )
csv_line += "," + str( math.cos( math.radians(minute*6))/1.01 ) + "\n"
file_handle.write(csv_line)
tensor_to_png( image, filename_path )
def clock_multiface_gen( image, dir_name, file_handle, diameter, hand_len_min=70, hand_len_max=90, thickness_max=1 ):
for thickness in range( 1, thickness_max+1):
for percent in range( hand_len_min, hand_len_max, 5):
clock_face_gen( image, dir_name, file_handle, diameter, int(diameter*percent/300 ), int(diameter*percent/200 ), thickness=thickness )
def clock_multiface_gen_csv( face, dir_name, csv_file, diameter, hand_len_min=70, hand_len_max=90, thickness_max=1 ):
# dir_name = os.path.join( curr_dir, dir )
base_dir = os.getcwd()
full_dir = os.path.join( base_dir, dir_name )
image = tf.keras.preprocessing.image.load_img(face)
# print( image)
image = tf.keras.preprocessing.image.img_to_array(image)
image = tf.cast( image, dtype=tf.uint8 )
#
if not os.path.isdir(full_dir):
os.mkdir(full_dir)
print('Created directory {}.'.format(full_dir))
with open( os.path.join( dir_name, csv_file ), 'w') as file_handle:
file_handle.write("filename, hs, hc, ms, mc\n")
clock_multiface_gen( image, dir_name, file_handle, diameter, thickness_max=thickness_max )
def main( argv ):
"""
:param argv: Command line arguments
"""
dir = None # Destination directory
csv = None # CSV File
face = None
try:
opts, args = getopt.getopt(argv,"hd:c:f:",["dir=","csv","face"])
except getopt.GetoptError:
print('python createClockFaceRawTensorImage.py -d <dir> -c <csv> -f <face>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('python createClockFaceTensorImage.py -d <dir> -c <csv> -f <face>')
sys.exit()
elif opt in ("-d", "--dir"):
dir = arg
elif opt in ("-c", "--csv"):
csv = arg
elif opt in ("-f", "--face"):
face = arg
if csv is None or dir is None:
print('python createClockFaceTensorImage.py -d <dir> -c <csv> -f <face>')
exit(2)
print(" Directory ", dir )
clock_multiface_gen_csv( face, dir, csv, 144, thickness_max=2 )
if __name__ == "__main__":
if len(sys.argv) != 7:
print('python createClockFaceTensorImage.py -d <dir> -c <csv> -f <face>')
sys.exit(2)
main(sys.argv[1:])