-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_products_MAIN.py
170 lines (135 loc) · 6.88 KB
/
make_products_MAIN.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: tasos
"""
import os
from glob import glob
import numpy as np
from make_products_from_VIDEO_Files import sort_in_C_recordings,make_products_from_video_files, bgr2rgb
from make_products_from_VIDEO_Files import series_normalize,uint8_matlab_compatible,datetime2matlabdn
import cv2
from scipy.io import savemat
from datetime import datetime,timedelta
import time
import scipy.io as spio
# %% FUNCTIONS HERE
class Timer(object):
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
if self.name:
print('[%s]' % self.name,)
print('Elapsed time:'+str(timedelta(seconds=int((time.time() - self.tstart)))))
def process_my_videos_N_save_results(video_fnames_of_this_hour,day,hour,camIP,out_path,station_parameters):
make_sigmac1 = True
timex,immin,immax,snap,sigmaa,sigmac = make_products_from_video_files(video_fnames_of_this_hour,day,hour,station_parameters,make_sigmac=make_sigmac1)
day = day.replace("-", "_")
# save JPEGs
for ii in ['timex','immin','immax','snap','sigma']:
if ii != 'sigma':
fname2save = out_path+camIP+'/'+'figures'+'/'+ii+'/'
fname2save += ii+'_'+day+'_'+str(int(hour)).zfill(2)+'_00_00.jpg'
eval("cv2.imwrite(fname2save,"+ii+")")
else:
sigma2jpeg = series_normalize(sigmaa,vmin = 0, vmax = 255)
sigma2jpeg = uint8_matlab_compatible(sigma2jpeg)
fname2save = out_path+camIP+'/'+'figures'+'/'+ii+'/'
fname2save += ii+'_'+day+'_'+str(int(hour)).zfill(2)+'_00_00.jpg'
cv2.imwrite(fname2save,sigma2jpeg)
# save MATs
format_date = '%Y_%m_%d_%H_%M_%S'
datetime_object = datetime.strptime(day+'_'+str(int(hour)).zfill(2)+'_00_00',format_date)
sdate = datetime2matlabdn(datetime_object)
mat_dict={'im':bgr2rgb(immin),'date':day,'time':str(int(hour)).zfill(2)+'_00_00','sdate':sdate}
new_dict = {'immin':mat_dict}
fname2saveMAT = out_path+camIP+'/'+'data'+'/'+'immin'+'/'
fname2saveMAT += 'immin'+'_'+day+'_'+str(int(hour)).zfill(2)+'_00_00.mat'
savemat(fname2saveMAT,new_dict,do_compression=True)
mat_dict={'im':bgr2rgb(immax),'date':day,'time':str(int(hour)).zfill(2)+'_00_00','sdate':sdate}
new_dict = {'immax':mat_dict}
fname2saveMAT = out_path+camIP+'/'+'data'+'/'+'immax'+'/'
fname2saveMAT += 'immax'+'_'+day+'_'+str(int(hour)).zfill(2)+'_00_00.mat'
savemat(fname2saveMAT,new_dict,do_compression=True)
mat_dict={'im':bgr2rgb(timex),'date':day,'time':str(int(hour)).zfill(2)+'_00_00','sdate':sdate}
new_dict = {'timex':mat_dict}
fname2saveMAT = out_path+camIP+'/'+'data'+'/'+'timex'+'/'
fname2saveMAT += 'timex'+'_'+day+'_'+str(int(hour)).zfill(2)+'_00_00.mat'
savemat(fname2saveMAT,new_dict,do_compression=True)
mat_dict={'im':bgr2rgb(snap),'date':day,'time':str(int(hour)).zfill(2)+'_00_00','sdate':sdate}
new_dict = {'snap':mat_dict}
fname2saveMAT = out_path+camIP+'/'+'data'+'/'+'snap'+'/'
fname2saveMAT += 'snap'+'_'+day+'_'+str(int(hour)).zfill(2)+'_00_00.mat'
savemat(fname2saveMAT,new_dict,do_compression=True)
mat_dict={'im':sigmaa,'imcol':bgr2rgb(sigmac),'sdate':sdate, 'date':day,'time':str(int(hour)).zfill(2)+'_00_00'}
new_dict = {'sigmaa':mat_dict}
fname2saveMAT = out_path+camIP+'/'+'data'+'/'+'sigma'+'/'
fname2saveMAT += 'sigma'+'_'+day+'_'+str(int(hour)).zfill(2)+'_00_00.mat'
savemat(fname2saveMAT,new_dict,do_compression=True)
def get_station_parameters(main_options_fname):
station_parameters = spio.loadmat(main_options_fname, simplify_cells=True)
station_parameters =station_parameters['mainopts']
if station_parameters['cname'] == 'Peyia1':
station_parameters['pcam'] = '5-CORAL_BAY_CAM'
# kapoios MALAKAS ton epaikse edw
return(station_parameters)
def make_repository_processed_paths(cam_IPs,out_path):
paths1 = ['data','figures']
paths2 = ['immin','immax','sigma','snap','stack','timex']
for ip in cam_IPs:
for p1 in paths1:
for p2 in paths2:
path = out_path+ip+'/'+p1+'/'+p2
isExist = os.path.exists(path)
if not isExist:
os.makedirs(path)
def get_hours_of_videos(all_Videos_fnames):
hours_of_videos = np.empty(len(all_Videos_fnames))
hours_of_videos[:] = np.nan
for ii in range(len(all_Videos_fnames)):
tmp = os.path.basename(os.path.normpath(all_Videos_fnames[ii]))
tmp = tmp[:-4] #remove extenstion
tmp = tmp.split('_')
tmp=tmp[-1]
tmp=tmp[0:2]
hours_of_videos[ii] = int(tmp)
return(hours_of_videos)
def process_current_hour(recordings_path,constant_subpath_recordings,out_path,camIP,day,hour,station_parameters):
all_Videos_fnames = sorted(glob(recordings_path+day+'/'+constant_subpath_recordings+'/'+"*.3gp"))
hours_of_videos = get_hours_of_videos(all_Videos_fnames)
video_list_needed_indexes = np.where(hours_of_videos==hour2process)[0]
video_list_needed_indexes = video_list_needed_indexes.astype(int)
video_fnames_of_this_hour=[all_Videos_fnames[x] for x in video_list_needed_indexes]
video_fnames_of_this_hour = sort_in_C_recordings(video_fnames_of_this_hour)
print("Day:"+day+ ' hour:'+str(hour2process)+' starting processing')
process_my_videos_N_save_results(video_fnames_of_this_hour,day,hour,camIP,out_path,station_parameters)
# %% MAIN
if __name__ == "__main__":
DEMO_mode = True
# %% PARAMETERS HERE
main_options_fname = 'main_options_Peyia.mat'
station_parameters = get_station_parameters(main_options_fname)
cam_IPs = [ station_parameters['ips'] ] # it must me be a list, e.g. cam_IPs = ['10_10_10_111']
# TO DO: fix above line to work using multiple cameras
constant_subpath_recordings = station_parameters['pcam'] #e.g. constant_subpath_recordings = '5-CORAL_BAY_CAM'
# C:\Recordinds Equivaent
recordings_path ='/media/tasos/9CD49202D491DF38/Users/Tasos/Desktop/examples/C_Recording'
#D:\Repository processed equivalent path
out_path = '/media/tasos/9CD49202D491DF38/Users/Tasos/Desktop/examples/d_repository_processed_tmpFS'
TEMP_FS_PATH = '/mnt/tmp'
if recordings_path[-1] != '/':
recordings_path +='/'
if out_path[-1] != '/':
out_path +='/'
make_repository_processed_paths(cam_IPs,out_path)
if DEMO_mode:
day = '2022-12-11'
hour2process = int(10)
else: #run data of current hour
hour2process = int(time.strftime("%H")) #current hour
day = time.strftime("%Y-%m-%d")
for camIP in cam_IPs:
with Timer('only break frames'):
process_current_hour(recordings_path,constant_subpath_recordings,out_path,camIP,day,hour2process,station_parameters)