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 test_read_result_images_from_csv(self): row1=OutputRow() row1.imagefile="file1.png" row1.outputimagefile="output1.png" row1.actualthreshold=1.1 row1.thresholdfactor=11 row1.elapsed_time=1.123 row1.nearest_neighbour_distance_statistic=4.567 results_filename="test_output.csv" absolute_results_filename=self.get_absolute_filename_under_testfolder(filename=results_filename) expected_outputrows=[row1] CsvHelper.write_results_to_csv(filename=absolute_results_filename,output_rows=expected_outputrows) actual_rows:List[OutputRow]=CsvHelper.read_result_rows_from_csv(filename=absolute_results_filename) self.assertEquals(len(actual_rows),1) self.assertEqual(row1.imagefile,actual_rows[0].imagefile) self.assertEqual(row1.outputimagefile,actual_rows[0].outputimagefile) self.assertEqual(row1.thresholdfactor,actual_rows[0].thresholdfactor) self.assertEqual(row1.actualthreshold,actual_rows[0].actualthreshold) self.assertEqual(row1.elapsed_time,actual_rows[0].elapsed_time) self.assertEqual(row1.nearest_neighbour_distance_statistic,actual_rows[0].nearest_neighbour_distance_statistic) pass
def test_write_results_to_csv(self): row1=OutputRow() row1.imagefile="file1.png" row1.outputimagefile="output1.png" row1.actualthreshold=1.1 row1.thresholdfactor=11 row1.elapsed_time=1.123 row1.nearest_neighbour_distance_statistic=4.567 row2=OutputRow() row2.imagefile="file2.png" row2.outputimagefile="output2.png" row2.actualthreshold=1.1 row2.thresholdfactor=11 row2.elapsed_time=5.678 row2.nearest_neighbour_distance_statistic=3.456 results_filename="test_output.csv" absolute_results_filename=self.get_absolute_filename_under_testfolder(filename=results_filename) expected_outputrows=[row1,row2] CsvHelper.write_results_to_csv(filename=absolute_results_filename,output_rows=expected_outputrows) self.assertTrue(os.path.exists(absolute_results_filename)) results_filehandle=open(absolute_results_filename,"r") actual_lines=list(results_filehandle) results_filehandle.close() self.assertEquals(len(actual_lines),1+len(expected_outputrows)) self.assertTrue("inputimagefile,outputimagefile,threshold,thresholdfactor" in actual_lines[0]) self.assertTrue(f"{row1.imagefile},{row1.outputimagefile},{row1.actualthreshold},{row1.thresholdfactor},{round(row1.nearest_neighbour_distance_statistic,3)},{round(row1.elapsed_time,3)}" in actual_lines[1]) pass
def generate_report_for_input_csv(input_images_csvfile: str): input_image_folder = os.path.dirname(input_images_csvfile) results_csv_file = find_results_csv_from_input_csv( inputfilename=input_images_csvfile) if (results_csv_file == None): print( f"\tNo results csv found for {Util.display_leaf_folders_from_path(path=input_image_folder,count=3)}" ) return input_rows = CsvHelper.read_input_rows_from_csv( filename=input_images_csvfile) result_rows = CsvHelper.read_result_rows_from_csv( filename=results_csv_file) view_model = ResultsViewModel( inputrows=input_rows, outputrows=result_rows, inputfolder=os.path.dirname(input_images_csvfile), resultsfolder=os.path.dirname(results_csv_file)) html_file = generate_html_filepath_from_input( inputfilename=input_images_csvfile) report_generator = ResultsGenerator(viewmodel=view_model, filename=html_file) report_generator.generate_report() # #The code below exemplifies the structural validity pass
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("------------------------------------------------------")
def generate_report_for_input_csv(input_images_csvfile: str): input_image_folder = os.path.dirname(input_images_csvfile) results_csv_file = find_results_csv_from_input_csv( inputfilename=input_images_csvfile) if (results_csv_file == None): print( f"\tNo results csv found for {Util.display_leaf_folders_from_path(path=input_image_folder,count=3)}" ) return input_rows = CsvHelper.read_input_rows_from_csv( filename=input_images_csvfile) result_rows = CsvHelper.read_result_rows_from_csv( filename=results_csv_file) #The code below exemplifies the structural validity #we will go with the expectation that the input rows in the CSV is in the order we desire for salt_pepper_key, g in groupby(input_rows, key=lambda x: x.salt_pepper): files_in_group = list(g) print( f"\tProcessing salt_pepper={salt_pepper_key}, found {len(files_in_group)} input files" ) for file_in_group in files_in_group: matching_result_rows = list( filter(lambda x: x.imagefile == file_in_group.imagefile, result_rows)) print( f"\t\t{file_in_group.imagefile}...result files={len(matching_result_rows)}...max_distance={file_in_group.max_distance}" ) for result_file in matching_result_rows: print( f"\t\t\t{result_file.outputimagefile}...tfac={result_file.thresholdfactor}...threshold={result_file.actualthreshold}" ) #TODO This works - you now need to find a way to present this information pass
def execute_ransac_on_files(csvfile: str, threshold_factors: List[float]): csv_results_file = None # initialize this to the path of a csv file in output_folder, all results will be written here input_datarows = CsvHelper.read_input_rows_from_csv(filename=csvfile) ransac_results: List[OutputRow] = [] new_directory_for_results = create_empty_folder_for_results( csvfile=csvfile) new_csv_file_for_results = os.path.join(new_directory_for_results, "result_image.csv") input_image_folder = os.path.dirname(csvfile) for input_datarow in input_datarows: for threshold_factor in threshold_factors: print( f'image={input_datarow.imagefile}, no of expected lines={input_datarow.line_count}, threshold factor={threshold_factor}' ) absolute_input_file = os.path.join(input_image_folder, input_datarow.imagefile) (result_filename, actual_threshold, nne_statistic, elapsed_time) = run_ransac_on_single_image( inputfilepath=absolute_input_file, outputfolder=new_directory_for_results, max_lines_to_find=input_datarow.line_count, threshold_factor=threshold_factor) result = OutputRow() result.imagefile = input_datarow.imagefile result.thresholdfactor = threshold_factor result.outputimagefile = result_filename result.actualthreshold = round(actual_threshold, 2) result.elapsed_time = round(elapsed_time, 2) result.nearest_neighbour_distance_statistic = round( nne_statistic, 2) ransac_results.append(result) pass print(f"Completed processing {len(input_datarows)} image files.") CsvHelper.write_results_to_csv(output_rows=ransac_results, filename=new_csv_file_for_results)