1
1
import os
2
2
from datetime import timedelta , datetime
3
- import re
4
3
5
- import numpy as np
6
4
from PIL import Image
7
5
from PIL .ExifTags import TAGS
8
6
9
- # os path separator
7
+
10
8
sep = os .path .sep
11
9
12
10
@@ -58,129 +56,6 @@ def find_img_number_list(first, last, increment, number_string_length=4):
58
56
return num_list
59
57
60
58
61
- def remove_led (im_set , ix , iy , window_radius ):
62
- im_set [ix - window_radius :ix + window_radius , iy - window_radius :iy + window_radius ] = 0
63
-
64
-
65
- def find_led_pos (image , ix , iy , window_radius ):
66
- s_radius = window_radius // 2
67
- s = np .index_exp [ix - s_radius :ix + s_radius , iy - s_radius :iy + s_radius ]
68
- res = np .unravel_index (np .argmax (image [s ]), image [s ].shape )
69
- max_x = ix - s_radius + res [0 ]
70
- max_y = iy - s_radius + res [1 ]
71
- return max_x , max_y
72
-
73
-
74
- def match_leds_to_arrays_with_min_dist (dists_led_arrays_search_areas , edge_indices , config , search_areas ):
75
- ignore_indices = get_indices_of_ignored_leds (config )
76
-
77
- xs = search_areas [:, 1 ]
78
- ys = search_areas [:, 2 ]
79
-
80
- num_leds = search_areas .shape [0 ]
81
-
82
- # construct 2D array for LED indices sorted by line
83
- led_arrays = []
84
- for edge_idx in edge_indices :
85
- led_arrays .append ([])
86
-
87
- for iled in range (num_leds ):
88
- if iled in ignore_indices :
89
- continue
90
-
91
- idx_nearest_array = np .argmin (dists_led_arrays_search_areas [iled , :])
92
- # TODO: ask Lukas for need of following code
93
-
94
- # for il_repeat in range(len(edge_indices)):
95
- # i1 = edge_indices[idx_nearest_array][0]
96
- # i2 = edge_indices[idx_nearest_array][1]
97
- #
98
- # x_outer_led1 = xs[i1]
99
- # y_outer_led1 = ys[i1]
100
- # x_outer_led2 = xs[i2]
101
- # y_outer_led2 = ys[i2]
102
- #
103
- # x_led = xs[iled]
104
- # y_led = ys[iled]
105
- #
106
- # dist_led_outer_led1 = np.sqrt((x_outer_led1 - x_led) ** 2 + (y_outer_led1 - y_led) ** 2)
107
- # dist_led_outer_led2 = np.sqrt((x_outer_led2 - x_led) ** 2 + (y_outer_led2 - y_led) ** 2)
108
- # dist_outer_leds = np.sqrt((x_outer_led1 - x_outer_led2) ** 2 + (y_outer_led1 - y_outer_led2) ** 2) + 1e-8
109
- #
110
- # if dist_led_outer_led1 < dist_outer_leds and dist_led_outer_led2 < dist_outer_leds:
111
- # break
112
- #
113
- # dists_led_arrays_search_areas[iled, idx_nearest_array] *= 2
114
-
115
- led_arrays [idx_nearest_array ].append (iled )
116
- return led_arrays
117
-
118
-
119
- def calc_dists_between_led_arrays_and_search_areas (line_edge_indices , search_areas ):
120
- distances_led_arrays_search_areas = np .zeros ((search_areas .shape [0 ], len (line_edge_indices )))
121
-
122
- xs = search_areas [:, 1 ]
123
- ys = search_areas [:, 2 ]
124
-
125
- for line_edge_idx in range (len (line_edge_indices )):
126
- i1 = line_edge_indices [line_edge_idx ][0 ]
127
- i2 = line_edge_indices [line_edge_idx ][1 ]
128
-
129
- p1x = xs [i1 ]
130
- p1y = ys [i1 ]
131
- p2x = xs [i2 ]
132
- p2y = ys [i2 ]
133
-
134
- pd = np .sqrt ((p1x - p2x ) ** 2 + (p1y - p2y ) ** 2 )
135
- d = np .abs (((p2y - p1y ) * xs - (p2x - p1x ) * ys
136
- + p2x * p1y - p2y * p1x ) / pd )
137
-
138
- distances_led_arrays_search_areas [:, line_edge_idx ] = d
139
- return distances_led_arrays_search_areas
140
-
141
-
142
- def get_indices_of_outer_leds (config ):
143
- if config ['analyse_positions' ]['line_edge_indices' ] == 'None' :
144
- config .in_line_edge_indices ()
145
- with open ('config.ini' , 'w' ) as configfile :
146
- config .write (configfile )
147
- line_edge_indices = config .get2dnparray ('analyse_positions' , 'line_edge_indices' )
148
- # makes sure that line_edge_indices is a 2d list
149
- if len (line_edge_indices .shape ) == 1 :
150
- line_edge_indices = [line_edge_indices ]
151
- return line_edge_indices
152
-
153
-
154
- def get_indices_of_ignored_leds (config ):
155
- if config ['analyse_positions' ]['ignore_indices' ] != 'None' :
156
- ignore_indices = np .array ([int (i ) for i in config ['analyse_positions' ]['ignore_indices' ].split (' ' )])
157
- else :
158
- ignore_indices = np .array ([])
159
- return ignore_indices
160
-
161
-
162
- # switch to Lib/logging at some point
163
- def log_warnings (img_filename , fit_res , iled , line_number , data , s , cx , cy , conf ):
164
- res = ' ' .join (np .array_str (fit_res .x ).split ()).replace ('[ ' , '[' ).replace (' ]' , ']' ).replace (' ' , ',' )
165
- img_file_path = conf ['img_directory' ] + img_filename
166
- window_radius = int (conf ['window_radius' ])
167
-
168
- x , y , _ = fit_res .x
169
-
170
- im_x = x + cx - window_radius
171
- im_y = y + cy - window_radius
172
-
173
- log = f'Irregularities while fitting:\n { img_file_path } { iled } { line_number } { res } { fit_res .success } ' \
174
- f'{ fit_res .fun } { fit_res .nfev } { data [s ].shape [0 ]} { data [s ].shape [1 ]} { im_x } { im_y } { window_radius } { cx } ' \
175
- f'{ cy } { conf ["channel" ]} '
176
- if not os .path .exists ('.{}logfiles' .format (sep )):
177
- os .makedirs ('.{}logfiles' .format (sep ))
178
- logfile = open ('.{}logfiles{}warnings.log' .format (sep , sep ), 'a' )
179
- logfile .write (log )
180
- logfile .write ('\n ' )
181
- logfile .close ()
182
-
183
-
184
59
def build_img_data_string (build_type , config ):
185
60
img_data = ''
186
61
img_idx = 1
@@ -198,18 +73,6 @@ def build_img_data_string(build_type, config):
198
73
return img_data
199
74
200
75
201
- def append_fit_res_to_img_data (cx , cy , fit_res , iled , img_data , led_array_idx , mean_color_value , sum_color_value ,
202
- window_radius ):
203
- x , y , dx , dy , A , alpha , wx , wy = fit_res .x
204
- im_x = x + cx - window_radius
205
- im_y = y + cy - window_radius
206
- led_data = (f'{ iled :4d} ,{ led_array_idx :2d} ,{ im_x :10.4e} ,{ im_y :10.4e} ,{ dx :10.4e} ,{ dy :10.4e} ,{ A :10.4e} ,'
207
- f'{ alpha :10.4e} ,{ wx :10.4e} ,{ wy :10.4e} ,{ fit_res .success :12d} ,{ fit_res .fun :10.4e} ,'
208
- f'{ fit_res .nfev :9d} ,{ sum_color_value :10.4e} ,{ mean_color_value :10.4e} ' )
209
- img_data += led_data + '\n '
210
- return img_data
211
-
212
-
213
76
def save_analysis_infos (img_data ):
214
77
out_file = open ('.{}analysis{}image_infos_analysis.csv' .format (sep , sep ), 'w' )
215
78
out_file .write ("#ID,Name,Time[s],Experiment_Time[s]\n " )
@@ -224,18 +87,3 @@ def save_experiment_infos(img_data):
224
87
out_file .close ()
225
88
226
89
227
- def find_analysed_img_ids (config ):
228
- processed_imgs = []
229
- directory_content = os .listdir ('.{}analysis{}channel{}' .format (sep , sep , config ['channel' ]))
230
- for file_name in directory_content :
231
- img = re .search (r"([0-9]+)_led_positions.csv" , file_name )
232
- if img is not None :
233
- processed_imgs .append (int (img .group (1 )))
234
- return processed_imgs
235
-
236
-
237
- def save_list_of_remaining_imgs_needed_to_be_processed (remaining_imgs ):
238
- out_file = open ('images_to_process.csv' , 'w' )
239
- for i in list (remaining_imgs ):
240
- out_file .write ('{}\n ' .format (i ))
241
- out_file .close ()
0 commit comments