def digit_recognize(): mb = MultipleSvm.load_variables(Smo, RESULT_PATH) # file_path = '../resource/svm_wrong_digits/pic04_no17_real8_cal3.dataset' file_path = '../resource/svm_wrong_digits/pic04_no33_real8_cal3.dataset' # file_path = '../resource/svm_wrong_digits/pic15_no19_real5_cal6_1.dataset' # file_path = '../resource/svm_wrong_digits/pic15_no19_real5_cal6.dataset' number_ragion = numpy.mat(Image.read_from_number_file(file_path)) transfered_ragion = numpy_helper.transfer_1to255(number_ragion) # adjusted_ragion = main_sudoku.adjust_number_ragion(transfered_ragion) adjusted_ragion = adjust_number_ragion2(transfered_ragion) # adjusted_ragion = transfered_ragion Display.ragions([transfered_ragion, adjusted_ragion]) adjusted_ragion = numpy_helper.transfer_255to1(adjusted_ragion) number_matrix = main_sudoku.transfer_to_digit_matrix(adjusted_ragion) mb.dag_classify(number_matrix).ppl()
# square_ragion.mean().ppl() threshold_value = Ragion.cal_threshold_value(the_ragion, square_ragion, 0.69) thresholded_ragion = Image.threshold_white(the_ragion, threshold_value) # thresholded_ragion = cv2.adaptiveThreshold(the_ragion, 255, # cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, blockSize=7, C=2) cell_rect = nonzero_rect.analyze_from_center(thresholded_ragion) if cell_rect: cell_ragion = Rect.get_ragion(cell_rect, thresholded_ragion) cell_rect.pl() # Display.image(cell_ragion) file_path = Resource.get_test_path("sample_19_07_05_image.jpg") the_ragion = cv2.imread(file_path, 0) # the_ragion.mean().ppl() file_path = Resource.get_test_path("sample_19_square.jpg") square_ragion = cv2.imread(file_path, 0) # square_ragion.mean().ppl() threshold_value = Ragion.cal_threshold_value(the_ragion, square_ragion, 0.8) thresholded_ragion = Image.threshold_white(the_ragion, threshold_value) cell_rect = nonzero_rect.analyze_from_center(thresholded_ragion) if cell_rect: cell_ragion = Rect.get_ragion(cell_rect, thresholded_ragion) number_ragion = numpy_helper.transfer_255to1(cell_ragion) number_matrix = main_sudoku.transfer_to_digit_matrix(number_ragion) som_svm.dag_classify(number_matrix).pl() # thresholded_ragion.ppl() # Display.image(thresholded_ragion)
if __name__ == "__main__": from minitest import * import data_file_helper from picture_sudoku.helpers import numpy_helper from picture_sudoku.cv2_helpers.display import Display from picture_sudoku.cv2_helpers.image import Image from picture_sudoku.helpers.common import Resource from picture_sudoku import main_sudoku def show_number_matrix(number_matrix): binary_number_image = number_matrix.reshape((data_file_helper.IMG_SIZE, data_file_helper.IMG_SIZE)) number_image = numpy_helper.transfer_values_quickly(binary_number_image, {1: 255}) number_image = numpy.array(number_image, dtype=numpy.uint8) # Image.save_to_txt(number_image,'test1.dataset') Display.image(number_image) """ how to use training, please see the generator.py""" with test(MultipleSvm.dag_classify): dataset_matrix_hash = data_file_helper.get_dataset_matrix_hash(data_file_helper.FONT_TRAINING_PATH, (9,)) mb = MultipleSvm.load_variables(Smo, data_file_helper.FONT_RESULT_PATH) mb.dag_classify(dataset_matrix_hash[9][0]).must_equal(9) with test("special number"): file_name = "sample_18_01_26_resized.dataset" issue_ragion = Image.load_from_txt(Resource.get_path("test", file_name)) transfered_ragion = main_sudoku.transfer_to_digit_matrix(issue_ragion) mb = MultipleSvm.load_variables(Smo, data_file_helper.FONT_RESULT_PATH) mb.dag_classify(transfered_ragion).must_equal(1) # Display.binary_image(main_sudoku.resize_to_cell_size(issue_ragion))