Bananaware

gamb

Jun 22nd, 2023
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. def comp_1_img_database():
  2. print(' : comparando 1 imagem contra a database inteira')
  3.  
  4. # Function to perform the file comparison and return the result
  5. def compare_files(file1, file2):
  6. images = []
  7. images.append(file1)
  8. images.append(file2)
  9. return enrollment_gamb(images, match_recursive)
  10.  
  11. # Check if /images folder exists
  12. if (not os.path.isdir('../images')) :
  13. print(' : pasta de animais (../images) nao encontrada')
  14. return
  15.  
  16. folder_path = '../images/' # Specify the path to your folder
  17.  
  18. # Get the root directory of the script
  19. script_root = os.path.dirname(os.path.abspath(__file__))
  20.  
  21. # Function to generate tables for each subfolder recursively
  22. def generate_tables(subfolder_path):
  23. # Get the parent folder name and subfolder name
  24. parent_folder, subfolder = os.path.split(subfolder_path)
  25.  
  26. # Get a list of all files in the subfolder
  27. files = [file for file in os.listdir(subfolder_path) if os.path.isfile(os.path.join(subfolder_path, file))]
  28.  
  29. # Create an empty table
  30. table = [[None] * (len(files) + 1) for _ in range(len(files) + 1)]
  31.  
  32. # Fill the table with file comparisons
  33. for i, file1 in enumerate(files):
  34. for j, file2 in enumerate(files):
  35. if i == j:
  36. table[i + 1][j + 1] = '-'
  37. else:
  38. comparison_result = compare_files(os.path.join(subfolder_path, file1), os.path.join(subfolder_path, file2))
  39. table[i + 1][j + 1] = comparison_result
  40.  
  41. # Add the filenames as headers
  42. table[0] = [None] + files
  43. for i in range(len(files)):
  44. table[i + 1][0] = files[i]
  45.  
  46. # Save the table to a text file in the script's root directory with the parent and subfolder's name
  47. output_file = os.path.join(script_root, f"{parent_folder}_{subfolder}_comparison_table.txt")
  48. with open(output_file, 'w') as f:
  49. # Update the header
  50. header = ['{:<15}'.format(cell) if cell is not None else '{:<15}'.format('') for cell in table[0]]
  51. header += ['{:<15}'.format("Average"), '{:<15}'.format(">0 Average")]
  52. f.write(''.join(header))
  53. f.write('\n')
  54.  
  55. # Write the table rows to the file
  56. for row in table[1:]:
  57. formatted_row = [
  58. '{:<15.3f}'.format(cell) if isinstance(cell, float) else '{:<15}'.format(cell) if cell is not None else '{:<15}'.format('-')
  59. for cell in row
  60. ]
  61. values_greater_than_zero = [cell for cell in row[1:] if isinstance(cell, float) and cell > 0.00]
  62. average = sum(cell for cell in row[1:] if isinstance(cell, float)) / (len(row) - 1)
  63. greater_than_zero_average = sum(values_greater_than_zero) / len(values_greater_than_zero) if values_greater_than_zero else 0.00
  64. formatted_row.append('{:<15.3f}'.format(average))
  65. formatted_row.append('{:<15.3f}'.format(greater_than_zero_average))
  66. f.write(''.join(formatted_row))
  67. f.write('\n')
  68.  
  69. print(f"Comparison table for subfolder '{parent_folder}/{subfolder}' has been saved to {output_file}.")
  70.  
  71. # Traverse through the subfolders recursively
  72. for root, dirs, files in os.walk(folder_path):
  73. for subfolder in dirs:
  74. subfolder_path = os.path.join(root, subfolder)
  75. generate_tables(subfolder_path)
  76.  
  77. return
  78.  
Advertisement
Add Comment
Please, Sign In to add comment