-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydic.py
244 lines (209 loc) · 10.1 KB
/
pydic.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
"""
Created on Fri Apr 19 09:08:55 2019
@author: Quentin LUCOT
Modified on Jan 18 2021 by Aflah Elouneg:
- Resetting the video ZOI
- Adding path list for .CSV tables
- Change the outputs tree structure
- Displaying displacement fields on video frames
"""
#########################################
###############Imports###################
#########################################
import csv
import os
import shutil
import errno
import zipfile
import cv2 #conda install cv2 or pip install cv2
import tkinter as tk
from tkinter import filedialog
from matplotlib import pyplot as plt
import numpy as np
from scipy import stats
import math
import imp
import time
import importlib
import sys
#########################################
#############Parameters##################
#########################################
def make_new_directory(directory):
try:
os.makedirs(directory)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(directory):
pass
else:
raise
def suction_dic(data_directory_path, pydic_methods, SAVE_VIDEO_FRAMES = False,
DISP_FIELD_IMAGES = False):
'''
data_directory_path : the folder containing videos for a single test series
'''
pydic = imp.load_source('pydic_methods', pydic_methods)
# URL to import pydic_methods.py
fps = 14. #video framerate (for Cutiscan : 14fps)
image_dimensions = (1280,1024) # Video resolution (px)
WS = 24
GS = 20
skin_diameter = 5.0 # ZOI diameter (mm)
#Video ZOI (px)
min_x = 154 #px
max_x = 154 + 960 #px
min_y = 62 #px
max_y = 62 + 960 #px
scale_disp = 5 # Displacement field vectors scale on images
directories_path_list = []
avi_files_path_list = []
frames_directories_path_list = []
tables_directories_path_list = []
disps_directories_path_list = []
#Video listing
for root, dirs, files in os.walk(data_directory_path):
for file in files:
if file.endswith('avi') :
directories_path_list.append(root)
avi_files_path_list.append(os.path.join(root,file))
frames_folder = os.path.join(root, 'FRAME')
frames_directories_path_list.append(frames_folder)
correl_wind_size = (WS, WS) # Size of the correlation windows (px)
correl_grid_size = (GS, GS) # Size of the interval (dx,dy) of the correlation grid (px)
#########################################
############Core Program#################
#########################################
#Center position of ZOI
if (max_x+min_x)%2 !=0:
max_x=max_x+1
if (max_y+min_y)%2 !=0:
max_y=max_y+1
resolution=skin_diameter/(max_x-min_x)
mean_x=(min_x+max_x)/2
mean_y=(min_y+max_y)/2
if (((max_x+min_x)/2-min_x)%correl_grid_size[0])!=0:
if (min_x-correl_grid_size[0]+((max_x+min_x)/2-min_x)%correl_grid_size[0]) <0:
min_x=min_x+((max_x+min_x)/2-min_x)%correl_grid_size[0]
else:
min_x=min_x-correl_grid_size[0]+((max_x+min_x)/2-min_x)%correl_grid_size[0]
if ((max_x-min_x)%correl_grid_size[0])!=0:
if (max_x+correl_grid_size[0]-(max_x-min_x)%correl_grid_size[0])>image_dimensions[0]:
max_x=max_x-(max_x-min_x)%correl_grid_size[0]
else:
max_x=max_x+correl_grid_size[0]-(max_x-min_x)%correl_grid_size[0]
if (((max_y+min_y)/2-min_y)%correl_grid_size[1])!=0:
if (min_y-correl_grid_size[1]+((max_y+min_y)/2-min_y)%correl_grid_size[1]) <0:
min_y=min_y+((max_y+min_y)/2-min_y)%correl_grid_size[1]
else:
min_y=min_y-correl_grid_size[1]+((max_y+min_y)/2-min_y)%correl_grid_size[1]
if ((max_y-min_y)%correl_grid_size[1])!=0:
if (max_y+correl_grid_size[1]-(max_y-min_y)%correl_grid_size[1])>image_dimensions[1]:
max_y=max_y-(max_y-min_y)%correl_grid_size[1]
else:
max_y=max_y+correl_grid_size[1]-(max_y-min_y)%correl_grid_size[1]
#Frame extraction
for i in range(0, len(directories_path_list)):
make_new_directory(frames_directories_path_list[i])
cap = cv2.VideoCapture(avi_files_path_list[i])
success,image = cap.read()
count = 0
print(f'Extracting new frames ...')
while success:
sys.stdout.write("=".format("="))
sys.stdout.flush()
# time.sleep(0.1)
os.chdir(frames_directories_path_list[i])
cv2.imwrite("frame%04d.png" % count, image)
success,image = cap.read()
count += 1
# sys.stdout.write('\n')
print(f'|{count} frames have been successfully extracted')
cap.release()
cv2.destroyAllWindows()
#Pydic excetution and data scraping
table_files_path_list=[]
os.chdir(frames_directories_path_list[i])
points = pydic.init("./*.png", correl_wind_size, correl_grid_size, "./result.dic",
area_of_intersest=((min_x, min_y),
(max_x+correl_grid_size[0], max_y+correl_grid_size[1])),)
pydic.grid_list=[]
pydic.read_dic_file('./result.dic',
interpolation='raw', save_image=DISP_FIELD_IMAGES,
scale_disp=scale_disp,
)
for root, dirs, files in os.walk(frames_directories_path_list[i]):
for file in files:
if file.endswith('csv'):
table_files_path_list.append(os.path.join(root,file))
data_angle_norm_disp=[]
data_angle_disp_r=[]
rval=[]
for l in range (0,8):
data_angle_norm_disp.append([])
data_angle_disp_r.append([])
rval.append(['frame number','time in seconds'])
for j in range(0,len(table_files_path_list)):
with open(table_files_path_list[j],mode='r') as read_file:
csv_reader=csv.reader(read_file,delimiter=',')
data=[]
for l in range(0,8):
data_angle_norm_disp[l].append([j,round(float(j)/fps,3)])
data_angle_disp_r[l].append([j,round(float(j)/fps,3)])
data=list(csv_reader)
data[0]=['index','index_x','index_y','pos_x(px)','pos_y(px)','pos_x(mm)','pos_y(mm)','disp_x(px)', 'disp_y(px)','disp_x(um)','disp_y(um)','strain_xx','strain_yy','strain_xy','r(px)','r(mm)','theta','disp_r(px)','disp_theta(px)','disp_r(um)','disp_theta(um)','norm_disp(px)','norm_disp(um)']
for k in range(1,len(data)):
data[k].append(0.)
data[k].append(float(data[k][7]))
data[k].append(-float(data[k][8]))
data[k].append(float(data[k][9]))
data[k][3]=float(data[k][3])-mean_x
data[k][4]=-float(data[k][4])+mean_y
data[k][7]=float(data[k][5])
data[k][8]=-float(data[k][6])
data[k][5]=resolution*data[k][3]
data[k][6]=resolution*data[k][4]
data[k][9]=float(data[k][7])*resolution*1000.
data[k][10]=float(data[k][8])*resolution*1000.
data[k].append(math.sqrt(data[k][3]**2+data[k][4]**2))
data[k].append(math.sqrt(data[k][5]**2+data[k][6]**2))
data[k].append(math.degrees(math.atan2(data[k][6],data[k][5])))
data[k].append(float(data[k][7])*math.cos(math.radians(float(data[k][16])))+float(data[k][8])*math.sin(math.radians(float(data[k][16]))))
data[k].append(-float(data[k][7])*math.sin(math.radians(float(data[k][16])))+float(data[k][8])*math.cos(math.radians(float(data[k][16]))))
data[k].append(float(data[k][17])*resolution*1000.)
data[k].append(float(data[k][18])*resolution*1000.)
data[k].append(math.sqrt(data[k][7]**2+data[k][8]**2))
data[k].append(float(data[k][21])*resolution*1000.)
for l in range (0,8):
angle=l*45-135
if data[k][16]==angle and float(data[k][15])<skin_diameter/2:
data_angle_norm_disp[l][j].append(data[k][22])
data_angle_disp_r[l][j].append(abs(float(data[k][19])))
if ("r = "+str(round(float(data[k][15]),3))+ "mm") not in rval[l]:
rval[l].append(("r = "+str(round(float(data[k][15]),3))+ "mm"))
with open(table_files_path_list[j],mode='w',newline='') as write_file:
csv_writer=csv.writer(write_file)
csv_writer.writerows(data)
read_file.close()
write_file.close()
tables_figures_path = os.path.join(frames_directories_path_list[i], 'disp')
shutil.move(tables_figures_path, directories_path_list[i])
disp_figures_path = os.path.join(frames_directories_path_list[i], 'result')
shutil.move(disp_figures_path, directories_path_list[i])
# os.remove(os.path.join(frames_directories_path_list[i], 'pydic'))
os.remove(os.path.join(frames_directories_path_list[i], 'result.dic'))
try:
shutil.rmtree(os.path.join(directories_path_list[i], 'TABLE'),
ignore_errors=True)
except:
pass
try:
shutil.rmtree(os.path.join(directories_path_list[i], 'DISP_FIG'),
ignore_errors=True)
except:
pass
os.rename(os.path.join(directories_path_list[i], 'result'),
os.path.join(directories_path_list[i], 'TABLE'))
os.rename(os.path.join(directories_path_list[i], 'disp'),
os.path.join(directories_path_list[i], 'DISP_FIG'))
if not SAVE_VIDEO_FRAMES:
shutil.rmtree(frames_directories_path_list[i])