仿照例子做了字符训练,删除了 delete_file (TrainingFileName)这一行,把图片A的两行字符分两次训练加到文件b.trf,然后在另一个程序里使用这个b.trf识别A的所有字符,但是结果只有第二次训练的字符有效,第一次训练的字符识别错了,但是在OCR训练文件浏览器里,b.trf有两次的所有字符,为什么第一次训练的字符不能识别呢?
- * Step 0: Preparations
- FontName := 'model'
- *
- * Step 1: Segmentation
- dev_update_window ('off')
- read_image (image, 'E:/学习&测试程序/halcon练习/钢印读取-代码训练/TrainCodes_ground_truth/1669094653_image.png')
- get_image_size (image, Width, Height)
- dev_close_window ()
- dev_open_window (0, 0, Width, Height, 'black', WindowID)
- set_display_font (WindowID, 27, 'mono', 'true', 'false')
- *gen_rectangle1 (ROI_0, 787.493, 126.966, 1032.9, 1266.73)
- gen_rectangle1 (ROI_0, 1060.25, 406.914, 1269.43, 1243.34)
- reduce_domain (image, ROI_0, ImageReduced)
- threshold (ImageReduced, RawSegmentation, 0, 95)
- fill_up_shape (RawSegmentation, RemovedNoise, 'area', 1, 5)
- opening_circle (RemovedNoise, ThickStructures, 2.5)
- fill_up (ThickStructures, Solid)
- opening_rectangle1 (Solid, Cut, 1, 7)
- connection (Cut, ConnectedPatterns)
- intersection (ConnectedPatterns, ThickStructures, NumberCandidates)
- select_shape (NumberCandidates, Numbers, 'area', 'and', 2000, 99999)
- sort_region (Numbers, FinalNumbers, 'character', 'true', 'column')
- dev_display (image)
- dev_set_color ('green')
- dev_set_line_width (2)
- dev_set_shape ('rectangle1')
- dev_set_draw ('margin')
- dev_display (FinalNumbers)
- *
- * Step2: Training file generation
- *TrainingNames := ['C','A','6', 'D', 'M', '2', '-', '4', '2']
- TrainingNames := ['5','3','9', '1', '5', '1', '4', '6']
- TrainingFileName := FontName + '.trf'
- sort_region (FinalNumbers, SortedRegions, 'character', 'true', 'row')
- shape_trans (SortedRegions, RegionTrans, 'rectangle1')
- area_center (RegionTrans, Area, Row, Column)
- MeanRow := mean(Row)
- dev_set_check ('~give_error')
- *delete_file (TrainingFileName)
- dev_set_check ('give_error')
- for I := 0 to |TrainingNames| - 1 by 1
- select_obj (SortedRegions, CharaterRegions, I + 1)
- append_ocr_trainf (CharaterRegions, ImageReduced, TrainingNames[I], TrainingFileName)
- disp_message (WindowID, TrainingNames[I], 'image', MeanRow - 160, Column[I] - 6, 'yellow', 'false')
- endfor
- *
- * Step3: Training
- CharNames := uniq(sort(TrainingNames))
- create_ocr_class_mlp (8, 10, 'constant', 'default', CharNames, 10, 'none', 10, 42, OCRHandle)
- trainf_ocr_class_mlp (OCRHandle, TrainingFileName, 200, 1, 0.01, Error, ErrorLog)
- write_ocr_class_mlp (OCRHandle, FontName)
复制代码
|