Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def comp_1_img_database():
- print(' : comparando 1 imagem contra a database inteira')
- # Function to perform the file comparison and return the result
- def compare_files(file1, file2):
- images = []
- images.append(file1)
- images.append(file2)
- return enrollment_gamb(images, match_recursive)
- # Check if /images folder exists
- if (not os.path.isdir('../images')) :
- print(' : pasta de animais (../images) nao encontrada')
- return
- folder_path = '../images/' # Specify the path to your folder
- # Get the root directory of the script
- script_root = os.path.dirname(os.path.abspath(__file__))
- # Function to generate tables for each subfolder recursively
- def generate_tables(subfolder_path):
- # Get the parent folder name and subfolder name
- parent_folder, subfolder = os.path.split(subfolder_path)
- # Get a list of all files in the subfolder
- files = [file for file in os.listdir(subfolder_path) if os.path.isfile(os.path.join(subfolder_path, file))]
- # Create an empty table
- table = [[None] * (len(files) + 1) for _ in range(len(files) + 1)]
- # Fill the table with file comparisons
- for i, file1 in enumerate(files):
- for j, file2 in enumerate(files):
- if i == j:
- table[i + 1][j + 1] = '-'
- else:
- comparison_result = compare_files(os.path.join(subfolder_path, file1), os.path.join(subfolder_path, file2))
- table[i + 1][j + 1] = comparison_result
- # Add the filenames as headers
- table[0] = [None] + files
- for i in range(len(files)):
- table[i + 1][0] = files[i]
- # Save the table to a text file in the script's root directory with the parent and subfolder's name
- output_file = os.path.join(script_root, f"{parent_folder}_{subfolder}_comparison_table.txt")
- with open(output_file, 'w') as f:
- # Update the header
- header = ['{:<15}'.format(cell) if cell is not None else '{:<15}'.format('') for cell in table[0]]
- header += ['{:<15}'.format("Average"), '{:<15}'.format(">0 Average")]
- f.write(''.join(header))
- f.write('\n')
- # Write the table rows to the file
- for row in table[1:]:
- formatted_row = [
- '{:<15.3f}'.format(cell) if isinstance(cell, float) else '{:<15}'.format(cell) if cell is not None else '{:<15}'.format('-')
- for cell in row
- ]
- values_greater_than_zero = [cell for cell in row[1:] if isinstance(cell, float) and cell > 0.00]
- average = sum(cell for cell in row[1:] if isinstance(cell, float)) / (len(row) - 1)
- greater_than_zero_average = sum(values_greater_than_zero) / len(values_greater_than_zero) if values_greater_than_zero else 0.00
- formatted_row.append('{:<15.3f}'.format(average))
- formatted_row.append('{:<15.3f}'.format(greater_than_zero_average))
- f.write(''.join(formatted_row))
- f.write('\n')
- print(f"Comparison table for subfolder '{parent_folder}/{subfolder}' has been saved to {output_file}.")
- # Traverse through the subfolders recursively
- for root, dirs, files in os.walk(folder_path):
- for subfolder in dirs:
- subfolder_path = os.path.join(root, subfolder)
- generate_tables(subfolder_path)
- return
Advertisement
Add Comment
Please, Sign In to add comment