def digit_letter_data_preprocess(): train_data = np.ones((1, 120)) train_label = np.array([0]) test_data = np.ones((1, 120)) test_label = np.array([0]) isfirst_whole = True percent = 0.7 # 训练集比例 CharsIndentifier.m_debug = False basepath = 'resources/train/ann' for i in range(0, len(digit_letter_mapping)): remain_path = digit_letter_mapping.get(i, 'badpath') if remain_path == 'badpath': print 'jump index %d' % i continue filepath = os.path.join(basepath, remain_path[1]) for parent, dirs, files in os.walk(filepath): print "目录:", filepath, "文件数:", len(files) isfirst_char = True for picture in files: char_path = os.path.join(parent, picture) # print char_path char_img = cv2.imread(char_path, cv2.IMREAD_GRAYSCALE) threadhold_value, char_threshold = cv2.threshold( char_img, 20, 255, cv2.THRESH_BINARY) # print char_img.shape # print char_threshold feature = CharsIndentifier.features(char_threshold, kPredictSize) # print feature.shape if isfirst_char: temp_train_data = feature temp_train_label = np.array([i]) isfirst_char = False else: temp_train_data = np.vstack([temp_train_data, feature]) temp_train_label = np.hstack([temp_train_label, i]) if isfirst_whole: train_data = temp_train_data train_label = temp_train_label isfirst_whole = False else: train_data = np.vstack([train_data, temp_train_data]) train_label = np.hstack([train_label, temp_train_label]) print 'digit_letter_data_preprocess' print train_data.shape print train_label.shape return (train_data, train_label)
def test_chars_identify(): print "test_chars_identify" img_plate = cv2.imread("resources/image/chars_identify.jpg",cv2.IMREAD_COLOR) # 车牌分割 segmented = CharsSegmenter.charsSegment(img_plate) licence = '' CharsIndentifier.initModel() for index, char in enumerate(segmented): print 'char:' print char # licence += CharsIndentifier.identify(char) char_feature = CharsIndentifier.features(char,kPredictSize) if index == 0: char_text = CharsIndentifier.identifyChinese(char_feature) else: char_text = CharsIndentifier.identifyDigitLetter(char_feature) licence += char_text print "识别结果:" print licence
def test_chars_identify(): print "test_chars_identify" img_plate = cv2.imread("resources/image/chars_identify.jpg", cv2.IMREAD_COLOR) # 车牌分割 segmented = CharsSegmenter.charsSegment(img_plate) licence = '' CharsIndentifier.initModel() for index, char in enumerate(segmented): print 'char:' print char # licence += CharsIndentifier.identify(char) char_feature = CharsIndentifier.features(char, kPredictSize) if index == 0: char_text = CharsIndentifier.identifyChinese(char_feature) else: char_text = CharsIndentifier.identifyDigitLetter(char_feature) licence += char_text print "识别结果:" print licence
def plateRecognize(inMat): PlateLocater.m_debug = False CharsIndentifier.m_debug = False Result = PlateLocater.fuzzyLocate(inMat) if m_debug: print '候选车牌数量:',len(Result) index_loc = 0 for img in Result: index_loc += 1 cv2.imshow("候选车牌"+str(index_loc),img) cv2.waitKey(0) cv2.destroyAllWindows() resultVec = PlateJudger.platesJudge(Result) if m_debug: print 'SVM筛选后的车牌数量:',len(resultVec) index_loc = 0 for img in resultVec: index_loc += 1 cv2.imshow("SVM-"+str(index_loc),img) cv2.imwrite("debug/SVM-"+str(index_loc)+".jpg", img) cv2.waitKey(0) cv2.destroyAllWindows() lisences = list() for img_plate in resultVec: segmented = CharsSegmenter.charsSegment(img_plate) if m_debug: index_char = 0 for char in segmented: index_char += 1 cv2.imshow("chars_segment"+str(index_char), char) cv2.imwrite("debug/segmented-"+str(index_char)+".jpg", char) cv2.waitKey(0) cv2.destroyAllWindows() lisence = CharsIndentifier.identifyPlate(segmented) # print lisence lisences.append(lisence) return lisences
def chinese_data_preprocess(): train_data = np.ones((1, 120)) train_label = np.array([0]) test_data = np.ones((1, 120)) test_label = np.array([0]) isfirst_whole = True percent = 0.7 # 训练集比例 np.random.seed(int(time.time())) CharsIndentifier.m_debug = False basepath = 'resources/train/ann' for i in range(0, len(province_mapping)): print '处理', province_mapping[i][1], '中...' chinese_path = province_mapping[i][0] filepath = os.path.join(basepath, chinese_path) print "目录:", filepath for parent, dirs, files in os.walk(filepath): isfirst_char = True for picture in files: char_path = os.path.join(parent, picture) print char_path char_img = cv2.imread(char_path, cv2.IMREAD_GRAYSCALE) threadhold_value, char_threshold = cv2.threshold( char_img, 20, 255, cv2.THRESH_BINARY) # print char_img.shape # print char_threshold feature = CharsIndentifier.features(char_threshold, kPredictSize) print feature.shape if isfirst_char: temp_data = feature temp_label = np.array([i]) isfirst_char = False else: temp_data = np.vstack([temp_data, feature]) temp_label = np.hstack([temp_label, i]) # print temp_data.shape # print temp_label.shape # 乱序化数据 # 随机数种子为当前时间 在之前确定 indices = np.random.permutation(temp_data.shape[0]) temp_data = temp_data[indices, :] temp_label = temp_label[indices] # 切分训练集和测试集 slice_index = int(temp_data.shape[0] * percent) temp_train_data = temp_data[0:slice_index, :] temp_train_label = temp_label[0:slice_index] temp_test_data = temp_data[slice_index:, :] temp_test_label = temp_label[slice_index:] # print temp_train_data.shape # print temp_test_data.shape if isfirst_whole: train_data = temp_train_data train_label = temp_train_label test_data = temp_test_data test_label = temp_test_label isfirst_whole = False else: train_data = np.vstack([train_data, temp_train_data]) train_label = np.hstack([train_label, temp_train_label]) test_data = np.vstack([test_data, temp_test_data]) test_label = np.hstack([test_label, temp_test_label]) print train_data.shape print test_data.shape joblib.dump(train_data, 'data/ann_chinese_train_data.pkl') joblib.dump(train_label, 'data/ann_chinese_train_label.pkl') joblib.dump(test_data, 'data/ann_chinese_test_data.pkl') joblib.dump(test_label, 'data/ann_chinese_test_label.pkl')