def test_write_inputimages_to_csv(self): all_input_rows=[] row1=InputRow() row1.imagefile="some image file 1" row1.total_pixels=101 row1.black_pixels=2 row1.salt_pepper=0.3 row1.max_distance=4 row1.line_count=1 all_input_rows.append(row1) row2=InputRow() row2.imagefile="some image file 2" row2.total_pixels=102 row2.black_pixels=300 row2.salt_pepper=0.3 row2.max_distance=2 row2.line_count=2 all_input_rows.append(row2) input_filename="test_input.csv" absolute_filename=self.get_absolute_filename_under_testfolder(filename=input_filename) CsvHelper.write_input_rows_to_csv(filename=absolute_filename,input_rows=all_input_rows) results_filehandle=open(absolute_filename,"r") actual_lines=list(results_filehandle) results_filehandle.close() self.assertEquals(len(actual_lines),1+len(all_input_rows)) self.assertTrue("imagefile,salt_pepper,max_distance,line_count,total_pixels,black_pixels" in actual_lines[0]) self.assertTrue(f"{row1.imagefile},{row1.salt_pepper},{row1.max_distance},{row1.line_count},{row1.total_pixels},{row1.black_pixels}" in actual_lines[1]) self.assertTrue(f"{row2.imagefile},{row2.salt_pepper},{row2.max_distance},{row2.line_count},{row2.total_pixels},{row2.black_pixels}" in actual_lines[2])
def generate_small_noisy_images_with_salt_pepper_rations_max_distances( salt_pepper_ratios: List[float], max_distances: List[float], sub_folder: str, lines_count: int): if (not sub_folder): raise Exception("Subfolder must be specified") folder_script = os.path.dirname(__file__) folder_results = os.path.join(folder_script, "./out/", sub_folder) if (os.path.exists(folder_results) == False): print("The folder %s was not found. Creating" % (folder_results)) os.mkdir(folder_results) print("The folder %s was created" % (folder_results)) print("Results will be generated in the output folder %s" % (folder_results)) csv_output_file = os.path.join(folder_results, "input_images.csv") input_rows: List[InputRow] = [] for salt_pepper in salt_pepper_ratios: for max_distance in max_distances: generator = NoisyImageGenerator( salt_pepper_ratio=salt_pepper, max_distance_between_points=max_distance, output_folder=folder_results, width=IMAGE_WIDTH, height=IMAGE_HEIGHT, max_lines=lines_count) generator.create_noisy_image() outputfile = generator.filename count_of_black_pixels = generator.count_of_blackpixels total_pixels = generator.total_pixel_count print( "\tGenerated noisy image with salt-pepper=%f, max_distance=%f , filename=%s, black_pixels=%d total_pixels=%d" % (salt_pepper, max_distance, outputfile, count_of_black_pixels, total_pixels)) new_row = InputRow() new_row.imagefile = outputfile new_row.total_pixels = total_pixels new_row.black_pixels = count_of_black_pixels new_row.salt_pepper = salt_pepper new_row.max_distance = max_distance new_row.line_count = lines_count input_rows.append(new_row) print("Total images generated=%d" % (len(input_rows))) CsvHelper.write_input_rows_to_csv(filename=csv_output_file, input_rows=input_rows) print("------------------------------------------------------")