1
- import os , sys , cv2
1
+ import os , sys , cv2 , shutil
2
2
import numpy as np
3
3
from PIL import Image , ImageDraw
4
+ from fpdf import FPDF
4
5
5
6
from lib .aux import *
6
7
from lib .showSequence import *
7
8
from lib .aux import *
8
9
9
10
10
11
def ap_checkErrors (prj ):
12
+
13
+ if not os .path .exists (f'projects/{ prj .name } /logs' ):
14
+ os .makedirs (f'projects/{ prj .name } /logs' )
15
+
16
+ if not os .path .exists (f'projects/{ prj .name } /logs/READ_ME.pdf' ):
17
+ #shutil.copyfile('lib/analyseProject_functions/CheckErrors_Documentation.txt', f'projects/{prj.name}/logs/READ_ME.txt')
18
+
19
+ # save FPDF() class into
20
+ # a variable pdf
21
+ pdf = FPDF ()
22
+
23
+ # Add a page
24
+ pdf .add_page ()
25
+
26
+ # set style and size of font
27
+ # that you want in the pdf
28
+ pdf .set_font ('Courier' , size = 11 )
29
+
30
+ # open the text file in read mode
31
+ f = open ('lib/analyseProject_functions/CheckErrors_Documentation.txt' , 'r' )
32
+
33
+ # insert the texts in pdf
34
+ for x in f :
35
+ pdf .cell (w = 0 , h = 5 , txt = x , ln = 1 , align = 'L' )
36
+
37
+ # save the pdf with name .pdf
38
+ pdf .output (f'projects/{ prj .name } /logs/READ_ME.pdf' )
39
+
11
40
while True :
12
41
os .system ("clear" )
13
42
printHeader ()
@@ -19,6 +48,7 @@ def ap_checkErrors(prj):
19
48
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
20
49
┃ 1 : Search class errors ┃
21
50
┃ 2 : Search incoherence errors ┃
51
+ ┃ 3 : Search displacement errors ┃
22
52
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
23
53
24
54
0 : Exit""" )
@@ -36,16 +66,23 @@ def ap_checkErrors(prj):
36
66
if not os .path .exists (f'projects/{ prj .name } /logs/repeated_img' ):
37
67
os .makedirs (f'projects/{ prj .name } /logs/repeated_img' )
38
68
69
+ dirs = sorted (os .listdir (f'projects/{ prj .name } /individual' ))
70
+ files_aux = sorted (os .listdir (f'projects/{ prj .name } /individual/{ dirs [0 ]} ' ))
71
+ printProgressBar (0 , len (dirs )* len (files_aux ), prefix = 'Analysing individual masks:' , suffix = 'Complete' , length = 50 )
72
+
39
73
with open (f'projects/{ prj .name } /logs/repeated_class.txt' , 'w' ) as log :
40
- for cls in sorted (os .listdir (f'projects/{ prj .name } /individual' )):
74
+
75
+ for i ,cls in enumerate (dirs ):
76
+
41
77
error_flag = False
42
78
43
- for i ,file in enumerate (sorted (os .listdir (f'projects/{ prj .name } /individual/{ cls } ' ))):
79
+ files = sorted (os .listdir (f'projects/{ prj .name } /individual/{ cls } ' ))
80
+ for j ,file in enumerate (files ):
44
81
img = cv2 .imread (f'projects/{ prj .name } /individual/{ cls } /{ file } ' ,cv2 .IMREAD_GRAYSCALE )
45
82
contours , hierarchy = cv2 .findContours (img ,cv2 .RETR_EXTERNAL ,cv2 .CHAIN_APPROX_SIMPLE )
46
83
47
84
if len (contours ) > 1 :
48
- print (f'{ bcolors .FAIL } { cls } ## { file } ## { len (contours )} ## { bcolors .ENDC } ' )
85
+ print (f'\r { bcolors .FAIL } { cls } ## { file } ## { len (contours )} { bcolors .ENDC } { "" . join ([ * ( " " for k in range ( 68 ))]) } ' )
49
86
50
87
if not error_flag :
51
88
log .write (f'### { prj .classes [int (cls )- 1 ].name } ({ cls } ) ###\n ' )
@@ -74,9 +111,12 @@ def ap_checkErrors(prj):
74
111
cv2 .imwrite (f'projects/{ prj .name } /logs/repeated_img/{ file } ' , result )
75
112
76
113
log .write ('\n ' )
77
- if i + 1 == len (sorted (os .listdir (f'projects/{ prj .name } /individual/{ cls } ' ))) and error_flag : log .write ('\n ' )
78
114
79
- input ('Continue' )
115
+ printProgressBar (i * len (files ) + j + 1 , len (dirs )* len (files ), prefix = 'Analysing individual masks:' , suffix = 'Complete' , length = 50 )
116
+
117
+ if j + 1 == len (sorted (os .listdir (f'projects/{ prj .name } /individual/{ cls } ' ))) and error_flag : log .write ('\n ' )
118
+
119
+ input ('\n Continue' )
80
120
81
121
elif choice == '2' :
82
122
@@ -181,5 +221,78 @@ def ap_checkErrors(prj):
181
221
182
222
input ('\n Continue' )
183
223
224
+ elif choice == '3' :
225
+
226
+ if not os .path .exists (f'projects/{ prj .name } /logs/displacement_img' ):
227
+ os .makedirs (f'projects/{ prj .name } /logs/displacement_img' )
228
+
229
+ with open (f'projects/{ prj .name } /logs/displacement_class.txt' , 'w' ) as log :
230
+ dirs = sorted (os .listdir (f'projects/{ prj .name } /individual' ))
231
+
232
+ files_aux = sorted (os .listdir (f'projects/{ prj .name } /individual/{ dirs [0 ]} ' ))
233
+ printProgressBar (0 , len (dirs )* len (files_aux ), prefix = 'Analysing individual masks:' , suffix = 'Complete' , length = 50 )
234
+
235
+ for i ,cls in enumerate (dirs ):
236
+ error_flag = False
237
+
238
+ files = sorted (os .listdir (f'projects/{ prj .name } /individual/{ cls } ' ))
239
+ for j ,file in enumerate (files ):
240
+
241
+ pre_img = cv2 .imread (f'projects/{ prj .name } /individual/{ cls } /{ files [j - 1 ]} ' ,cv2 .IMREAD_GRAYSCALE )
242
+
243
+ current_img = cv2 .imread (f'projects/{ prj .name } /individual/{ cls } /{ file } ' ,cv2 .IMREAD_GRAYSCALE )
244
+
245
+ contours_pre_img , hierarchy = cv2 .findContours (pre_img ,cv2 .RETR_EXTERNAL ,cv2 .CHAIN_APPROX_SIMPLE )
246
+ contours_current_img , hierarchy = cv2 .findContours (current_img ,cv2 .RETR_EXTERNAL ,cv2 .CHAIN_APPROX_SIMPLE )
247
+
248
+ if j == 0 or len (contours_pre_img ) == 0 or len (contours_current_img ) == 0 :
249
+ printProgressBar (i * len (files ) + j + 1 , len (dirs )* len (files ), prefix = 'Analysing individual masks:' , suffix = 'Complete' , length = 50 )
250
+ continue
251
+
252
+ else :
253
+ overlap = cv2 .bitwise_and (pre_img ,current_img )
254
+ contours_overlap , hierarchy = cv2 .findContours (overlap ,cv2 .RETR_EXTERNAL ,cv2 .CHAIN_APPROX_SIMPLE )
255
+
256
+ if len (contours_overlap )== 0 : overlap_percentage = 0.00
257
+ else :
258
+ area_overlap = cv2 .countNonZero (overlap )
259
+ area_current_img = cv2 .countNonZero (current_img )
260
+
261
+ overlap_percentage = round ((area_overlap / area_current_img ) * 100 , 2 )
262
+
263
+ if overlap_percentage < 5 and error_flag : error_flag = not error_flag
264
+ elif overlap_percentage < 5 and not error_flag :
265
+
266
+ print (f'\r { bcolors .FAIL } { cls } ## { file } ## { overlap_percentage } %{ bcolors .ENDC } { "" .join ([* (" " for k in range (69 ))])} ' )
267
+
268
+ im = cv2 .imread (f'projects/{ prj .name } /img/{ file .split ("_" )[0 ]} .jpg' ,cv2 .IMREAD_GRAYSCALE )
269
+ im_rgb = cv2 .cvtColor (im , cv2 .COLOR_GRAY2BGR )
270
+
271
+ im_rgb_copy = im_rgb .copy ()
272
+
273
+ for cont in contours_pre_img : im_rgb_copy = cv2 .drawContours (im_rgb_copy , [cont ], - 1 , (0 , 0 , 255 ), - 1 )
274
+ for cont in contours_current_img : im_rgb_copy = cv2 .drawContours (im_rgb_copy , [cont ], - 1 , (255 , 0 , 0 ), - 1 )
275
+ for cont in contours_overlap : im_rgb_copy = cv2 .drawContours (im_rgb_copy , [cont ], - 1 , (0 , 255 , 0 ), - 1 )
276
+
277
+ alpha = 0.8
278
+ filled = cv2 .addWeighted (im_rgb , alpha , im_rgb_copy , 1 - alpha , 0 )
279
+
280
+ for cont in contours_pre_img : filled = cv2 .drawContours (filled , [cont ], - 1 , (0 , 0 , 255 ), 0 )
281
+ for cont in contours_current_img : filled = cv2 .drawContours (filled , [cont ], - 1 , (255 , 0 , 0 ), 0 )
282
+ for cont in contours_overlap : filled = cv2 .drawContours (filled , [cont ], - 1 , (0 , 255 , 0 ), 0 )
283
+
284
+ image = cv2 .putText (filled , f'{ overlap_percentage } %' , (40 , 40 ), cv2 .FONT_HERSHEY_SIMPLEX , 0.5 , (0 , 255 , 0 ), 1 , cv2 .LINE_AA )
285
+
286
+ log .write (f'\t { file } \t [{ overlap_percentage } %]' )
287
+
288
+ cv2 .imwrite (f'projects/{ prj .name } /logs/displacement_img/{ file } ' , filled )
289
+
290
+ log .write ('\n ' )
291
+
292
+ printProgressBar (i * len (files ) + j + 1 , len (dirs )* len (files ), prefix = 'Analysing individual masks:' , suffix = 'Complete' , length = 50 )
293
+
294
+
295
+ input ('\n Continue' )
296
+
184
297
else :
185
298
input (f'\n { bcolors .FAIL } Unexpected option, press a key to continue...{ bcolors .ENDC } ' )
0 commit comments