if pre_c != len(CHARS) - 1: no_repeat_blank_label.append(pre_c)
时间: 2024-05-19 08:13:00 浏览: 145
# If the previous character was not a blank, add it to the no_repeat_blank_label list
if pre_c != len(CHARS) - 1:
no_repeat_blank_label.append(pre_c)
相关问题
class MString: def __init__(self, chars: list): # write your code here self.chars = chars def isEquivalent(self, ms: 'MString'): # write your code here if self.chars == ms.chars: return True els
e: return False
def reverse(self):
# write your code here
reversed_chars = self.chars[::-1]
return MString(reversed_chars)
def concat(self, ms: 'MString'):
# write your code here
concatenated_chars = self.chars + ms.chars
return MString(concatenated_chars)
def __str__(self):
return ''.join(self.chars)
def __repr__(self):
return 'MString(' + repr(self.chars) + ')'
class CardPredictor: def __del__(self): self.save_traindata() def train_svm(self): # 识别英文字母和数字 self.model = SVM(C=1, gamma=0.5) # 识别中文 self.modelchinese = SVM(C=1, gamma=0.5) if os.path.exists("svm.dat"): self.model.load("svm.dat") else: chars_train = [] chars_label = [] for root, dirs, files in os.walk("train\\chars2"): if len(os.path.basename(root)) > 1: continue root_int = ord(os.path.basename(root)) for filename in files: filepath = os.path.join(root, filename) digit_img = cv2.imread(filepath) digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY) chars_train.append(digit_img) chars_label.append(root_int) chars_train = list(map(deskew, chars_train)) chars_train = preprocess_hog(chars_train) chars_label = np.array(chars_label) self.model.train(chars_train, chars_label) if os.path.exists("svmchinese.dat"): self.modelchinese.load("svmchinese.dat") else: chars_train = [] chars_label = [] for root, dirs, files in os.walk("train\\charsChinese"): if not os.path.basename(root).startswith("zh_"): continue pinyin = os.path.basename(root) index = provinces.index(pinyin) + PROVINCE_START + 1 # 1是拼音对应的汉字 for filename in files: filepath = os.path.join(root, filename) digit_img = cv2.imread(filepath) digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY) chars_train.append(digit_img) chars_label.append(index) chars_train = list(map(deskew, chars_train)) chars_train = preprocess_hog(chars_train) chars_label = np.array(chars_label) self.modelchinese.train(chars_train, chars_label)
这段代码实现了一个卡牌识别器,使用 SVM 算法来识别卡牌上的数字、英文字母和汉字。它将训练数据从文件中读取,对于数字和英文字母,训练数据位于 "train\\chars2" 文件夹中;对于汉字,训练数据位于 "train\\charsChinese" 文件夹中。训练数据中的每张图片都经过了去倾斜、HOG 特征提取等预处理操作,最后使用 SVM 算法进行训练。训练好的模型可以保存到文件中以便于之后的使用。当程序结束时,会自动保存训练数据。
阅读全文