143 lines
5.6 KiB
C#
143 lines
5.6 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;
|
|
|
|
namespace MilPattern {
|
|
/// <summary>
|
|
/// String Reader ¸ðµâ
|
|
/// </summary>
|
|
internal static class MilStrRead {
|
|
private static Logger _logger = new Logger(typeof(MilStrRead).Name);
|
|
|
|
public static MIL_ID CreateContext(MIL_ID milSystem) {
|
|
MIL_ID contextId = MIL.M_NULL;
|
|
|
|
// ÃÖÃÊ¿¡ ÇÑ ¹ø Default ÇÒ´ç
|
|
MIL.MstrAlloc(milSystem, MIL.M_FONT_BASED, MIL.M_DEFAULT, ref contextId);
|
|
|
|
return contextId;
|
|
}
|
|
|
|
public static MIL_ID LoadContext(MIL_ID milSystem, string filePath) {
|
|
MIL_ID contextId = MIL.M_NULL;
|
|
MIL.MstrRestore(filePath, milSystem, MIL.M_DEFAULT, ref contextId);
|
|
if (HasModel(contextId)) {
|
|
MIL.MstrPreprocess(contextId, MIL.M_DEFAULT);
|
|
}
|
|
|
|
return contextId;
|
|
}
|
|
|
|
public static int SaveContext(MIL_ID contextId, string filePath) {
|
|
if (contextId != MIL.M_NULL && filePath != null) {
|
|
MIL.MstrSave(filePath, contextId, MIL.M_DEFAULT);
|
|
return E1.OK;
|
|
}
|
|
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
public static void FreeContext(MIL_ID contextId) {
|
|
if (contextId != MIL.M_NULL) {
|
|
MIL.MstrFree(contextId);
|
|
}
|
|
}
|
|
|
|
public static MIL_ID CreateResultContext(MIL_ID contextId) {
|
|
var milSystem = GetSystemOfContext(contextId);
|
|
|
|
return MIL.MstrAllocResult(milSystem, MIL.M_DEFAULT, MIL.M_NULL);
|
|
}
|
|
|
|
private static MIL_ID GetSystemOfContext(MIL_ID contextId) {
|
|
return (MIL_ID)MIL.MstrInquire(contextId, MIL.M_DEFAULT, MIL.M_OWNER_SYSTEM, MIL.M_NULL);
|
|
}
|
|
|
|
public static bool HasModel(MIL_ID contextId) {
|
|
return MIL.MstrInquire(contextId, MIL.M_DEFAULT, MIL.M_NUMBER_OF_STRING_MODELS + 1, MIL.M_NULL) > 0;
|
|
}
|
|
|
|
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.MstrRead(contextId, image, 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;
|
|
MIL_INT strReadNum = 0;
|
|
MIL.MstrGetResult(contextResult, MIL.M_GENERAL, MIL.M_STRING_NUMBER + MIL.M_TYPE_MIL_INT, ref strReadNum);
|
|
|
|
if (strReadNum > 0) {
|
|
var sb = new StringBuilder(64);
|
|
MIL.MstrGetResult(contextResult, MIL.M_DEFAULT, MIL.M_STRING, sb);
|
|
string readString = sb.ToString().Replace(" ", string.Empty); // °ø¹é Á¦°Å
|
|
|
|
double angleDeg = 0;
|
|
double score = 0;
|
|
MIL.MstrGetResult(contextResult, MIL.M_DEFAULT, MIL.M_STRING_ANGLE, ref angleDeg);
|
|
MIL.MstrGetResult(contextResult, MIL.M_DEFAULT, MIL.M_STRING_SCORE, ref score);
|
|
|
|
if (paramExt.NoLengthLimit) {
|
|
if (score > paramExt.Acceptance) {
|
|
results.Add(new OcrResult(
|
|
ocrType: eOcrType.StrRead,
|
|
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,
|
|
status: eFoundStatus.FoundOK));
|
|
}
|
|
}
|
|
else {
|
|
if (readString.Length == paramExt.CustomTextLength) {
|
|
if (score > paramExt.Acceptance) {
|
|
results.Add(new OcrResult(
|
|
ocrType: eOcrType.StrRead,
|
|
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,
|
|
status: eFoundStatus.FoundOK));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return new PatternResult() {
|
|
MoreInfo = eOcrType.StrRead,
|
|
Status = (results.Count > 0) ? eFoundStatus.FoundOK : eFoundStatus.NotFound,
|
|
Results = results
|
|
};
|
|
}
|
|
|
|
public static void Draw(PatternResult patternResult, IMilCameraDisplay milCameraDisplay, MIL_ID resultContextId) {
|
|
milCameraDisplay.DrawStrReadResult(resultContextId, patternResult.FindRoi);
|
|
}
|
|
}
|
|
}
|