1
+ import sys
2
+ import os
3
+ import glob
4
+
5
+ ## This script ensures same number of files in ground-truth and predicted folder.
6
+ ## When you encounter file not found error, it's usually because you have
7
+ ## mismatched numbers of ground-truth and predicted files.
8
+ ## You can use this script to move ground-truth and predicted files that are
9
+ ## not in the intersection into a backup folder (backup_no_matches_found).
10
+ ## This will retain only files that have the same name in both folders.
11
+
12
+ # change directory to the one with the files to be changed
13
+ path_to_gt = '../ground-truth'
14
+ path_to_pred = '../predicted'
15
+ backup_folder = 'backup_no_matches_found' # must end without slash
16
+
17
+ os .chdir (path_to_gt )
18
+ gt_files = glob .glob ('*.txt' )
19
+ if len (gt_files ) == 0 :
20
+ print ("Error: no .txt files found in" , path_to_gt )
21
+ sys .exit ()
22
+ os .chdir (path_to_pred )
23
+ pred_files = glob .glob ('*.txt' )
24
+ if len (pred_files ) == 0 :
25
+ print ("Error: no .txt files found in" , path_to_pred )
26
+ sys .exit ()
27
+
28
+ gt_files = set (gt_files )
29
+ pred_files = set (pred_files )
30
+ print ('total ground-truth files:' , len (gt_files ))
31
+ print ('total predicted files:' , len (pred_files ))
32
+ print ()
33
+
34
+ gt_backup = gt_files - pred_files
35
+ pred_backup = pred_files - gt_files
36
+
37
+
38
+ def backup (src_folder , backup_files , backup_folder ):
39
+ # non-intersection files (txt format) will be moved to a backup folder
40
+ if not backup_files :
41
+ print ('No backup required for' , src_folder )
42
+ return
43
+ os .chdir (src_folder )
44
+ ## create the backup dir if it doesn't exist already
45
+ if not os .path .exists (backup_folder ):
46
+ os .makedirs (backup_folder )
47
+ for file in backup_files :
48
+ os .rename (file , backup_folder + '/' + file )
49
+
50
+
51
+ backup (path_to_gt , gt_backup , backup_folder )
52
+ backup (path_to_pred , pred_backup , backup_folder )
53
+ if gt_backup :
54
+ print ('total ground-truth backup files:' , len (gt_backup ))
55
+ if pred_backup :
56
+ print ('total predicted backup files:' , len (pred_backup ))
57
+
58
+ intersection = gt_files & pred_files
59
+ print ('total intersected files:' , len (intersection ))
60
+ print ("Intersection completed!" )
0 commit comments