146 lines
5.7 KiB
C#
146 lines
5.7 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using Aurora.Api;
|
|
using MilCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Linq;
|
|
|
|
namespace MilPattern {
|
|
/// <summary>
|
|
/// Ocr Reader 모듈
|
|
/// </summary>
|
|
internal static class MilOcrRead {
|
|
private static Logger _logger = new Logger(typeof(MilOcrRead).Name);
|
|
|
|
public static MIL_ID CreateContext(MIL_ID milSystem) {
|
|
MIL_ID contextId = MIL.M_NULL;
|
|
|
|
// 최초에 한 번 Default 할당
|
|
MIL.MocrAllocFont(milSystem, MIL.M_USER_DEFINED + MIL.M_CONSTRAINED,
|
|
26, 8, 11, 1, 1, 6, 9, 1, 26, MIL.M_FOREGROUND_BLACK, ref contextId);
|
|
|
|
MIL.MocrControl(contextId, MIL.M_STRING_ANGLE, 0); // 0도 기준 문자열
|
|
MIL.MocrControl(contextId, MIL.M_STRING_ANGLE_DELTA_NEG, 10); // 10도 까지 편차 허용
|
|
MIL.MocrControl(contextId, MIL.M_STRING_ANGLE_DELTA_POS, 10); // 10도 까지 편차 허용
|
|
|
|
return contextId;
|
|
}
|
|
|
|
public static MIL_ID LoadContext(MIL_ID milSystem, string filePath) {
|
|
MIL_ID contextId = MIL.M_NULL;
|
|
MIL.MocrRestoreFont(filePath, MIL.M_RESTORE, milSystem, ref contextId);
|
|
if (HasModel(contextId)) {
|
|
MIL.MocrPreprocess(contextId, MIL.M_DEFAULT);
|
|
}
|
|
|
|
return contextId;
|
|
}
|
|
|
|
public static int SaveContext(MIL_ID contextId, string filePath) {
|
|
if (contextId != MIL.M_NULL && filePath != null) {
|
|
MIL.MocrSaveFont(filePath, MIL.M_SAVE, contextId);
|
|
return E1.OK;
|
|
}
|
|
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
public static void FreeContext(MIL_ID contextId) {
|
|
if (contextId != MIL.M_NULL) {
|
|
MIL.MocrFree(contextId);
|
|
}
|
|
}
|
|
|
|
public static MIL_ID CreateResultContext(MIL_ID contextId) {
|
|
var milSystem = GetSystemOfContext(contextId);
|
|
|
|
return MIL.MocrAllocResult(milSystem, MIL.M_DEFAULT, MIL.M_NULL);
|
|
}
|
|
|
|
private static MIL_ID GetSystemOfContext(MIL_ID contextId) {
|
|
return (MIL_ID)MIL.MocrInquire(contextId, MIL.M_OWNER_SYSTEM, MIL.M_NULL);
|
|
}
|
|
|
|
public static bool HasModel(MIL_ID contextId) {
|
|
return (contextId != null);
|
|
//return MIL.MocrInquire(contextId, MIL.M_NUMBER_OF_CODE_MODELS, MIL.M_NULL) > 0; // MODELS 조회하는 방법을 모르겠음..
|
|
}
|
|
|
|
public static PatternResult Find(
|
|
MIL_ID contextId, MIL_ID resultContextId, MIL_ID image, OcrParamExt paramExt) {
|
|
|
|
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, MIL.M_NULL);
|
|
|
|
MIL.MocrReadString(image, contextId, resultContextId);
|
|
|
|
double tactTime = 0;
|
|
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, ref tactTime);
|
|
|
|
var finderResult = ExtractResults(resultContextId, MilEnvironment.GetImageCenter(image), paramExt);
|
|
finderResult.TactTimeInMilliSec = MathUtil.ConvertUnitSecToMilli(tactTime);
|
|
|
|
return finderResult;
|
|
}
|
|
|
|
private static PatternResult ExtractResults(MIL_ID contextResult, ImagePoint imageCenter, OcrParamExt paramExt) {
|
|
List<OcrResult> results = new List<OcrResult>();
|
|
MIL_INT resultStatus = 0;
|
|
|
|
var sb = new StringBuilder(64);
|
|
MIL.MocrGetResult(contextResult, MIL.M_STRING, sb);
|
|
string readString = sb.ToString().Replace(" ", string.Empty); // 공백 제거
|
|
|
|
double angleDeg = 0;
|
|
double score = 0;
|
|
MIL.MocrGetResult(contextResult, MIL.M_STRING_ANGLE, ref angleDeg);
|
|
MIL.MocrGetResult(contextResult, MIL.M_STRING_SCORE, ref score);
|
|
|
|
if (paramExt.NoLengthLimit) {
|
|
if (score > paramExt.Acceptance) {
|
|
results.Add(new OcrResult(
|
|
ocrType: eOcrType.OcrRead,
|
|
position: new ImagePoint(paramExt.FindROI.Width / 2, paramExt.FindROI.Height / 2),
|
|
offset: new ImagePoint(),
|
|
angle: Angle.NormalizeAngle(Angle.CreateWithDegree(angleDeg)),
|
|
score: score,
|
|
text: sb.ToString(),
|
|
findRoi: paramExt.FindROI.Clone() as ROI,
|
|
eFoundStatus.FoundOK));
|
|
}
|
|
}
|
|
else {
|
|
if (readString.Length == paramExt.CustomTextLength) {
|
|
if (score > paramExt.Acceptance) {
|
|
results.Add(new OcrResult(
|
|
ocrType: eOcrType.OcrRead,
|
|
position: new ImagePoint(paramExt.FindROI.Width / 2, paramExt.FindROI.Height / 2),
|
|
offset: new ImagePoint(),
|
|
angle: Angle.NormalizeAngle(Angle.CreateWithDegree(angleDeg)),
|
|
score: score,
|
|
text: sb.ToString(),
|
|
findRoi: paramExt.FindROI.Clone() as ROI,
|
|
eFoundStatus.FoundOK));
|
|
}
|
|
}
|
|
}
|
|
|
|
return new PatternResult() {
|
|
MoreInfo = eOcrType.OcrRead,
|
|
Status = (results.Count > 0) ? eFoundStatus.FoundOK : eFoundStatus.NotFound,
|
|
Results = results
|
|
};
|
|
}
|
|
|
|
public static void Draw(PatternResult patternResult, IMilCameraDisplay milCameraDisplay, MIL_ID resultContextId) {
|
|
milCameraDisplay.DrawOcrReadResult(resultContextId, patternResult.FindRoi);
|
|
}
|
|
}
|
|
}
|