251 lines
8.0 KiB
C#
251 lines
8.0 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.IO;
|
|
using System.Drawing;
|
|
using System.Collections.Generic;
|
|
|
|
namespace MilPattern {
|
|
public class ShapeModelFinder : PatternBase, IPatternFinder, IPatternTrain, IDisposable {
|
|
public string FILE_EXT_PATTERN { get; set; } = ".mmf";
|
|
private MmfParamExt _paramExt = null;
|
|
|
|
private Logger _logger;
|
|
private PatternInfo _patternInfo;
|
|
private MIL_ID _contextId = MIL.M_NULL;
|
|
private MIL_ID _resultContextId = MIL.M_NULL;
|
|
|
|
public ShapeModelFinder(PatternInfo patternInfo) {
|
|
_patternInfo = patternInfo;
|
|
_logger = new Logger(
|
|
$"ShapeModelFinder_{patternInfo.GetPatternIndex()}");
|
|
InitContext();
|
|
}
|
|
|
|
private void InitContext() {
|
|
if (E1.OK != Load()) {
|
|
Create();
|
|
}
|
|
}
|
|
|
|
#region IPatternFinder
|
|
public string GetPatternFilePath(string patternName = PatternConstants.FILE_NAME_PATTERN) {
|
|
return Path.Combine(new string[] {
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
patternName + FILE_EXT_PATTERN
|
|
});
|
|
}
|
|
|
|
public int Load() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
|
|
if (!File.Exists(patternFilePath)) {
|
|
_logger.I($"Pattern File does NOT exist : {patternFilePath}");
|
|
return E2.NOT_FOUND;
|
|
}
|
|
|
|
FreeContexts();
|
|
_contextId = MmfCommon.LoadContext(_patternInfo.MilSystemId, patternFilePath);
|
|
_resultContextId = MmfCommon.CreateResultContext(_contextId);
|
|
_paramExt = LoadParamExt();
|
|
ExtractFindRoiFromMmf();
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private void ExtractFindRoiFromMmf() {
|
|
if (!IsParamExtExist()) {
|
|
_logger.I($"Extract Find ROI : {_patternInfo}");
|
|
var findRoiMmf = MmfCommon.GetFindRoiFromMmf(_contextId);
|
|
_logger.I($"findRoiMmf : {findRoiMmf}");
|
|
|
|
_paramExt.FindROI = findRoiMmf.Clone() as ROI;
|
|
MmfCommon.ClearFindRoi(_contextId);
|
|
Save();
|
|
}
|
|
}
|
|
|
|
public int Save() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (!Directory.Exists(patternDirPath)) {
|
|
Directory.CreateDirectory(patternDirPath);
|
|
}
|
|
|
|
SaveParamExt(_paramExt);
|
|
return MmfCommon.SaveContext(_contextId, patternFilePath);
|
|
}
|
|
|
|
public int Create() {
|
|
FreeContexts();
|
|
_paramExt = LoadParamExt();
|
|
if (_paramExt.IsQuickFind) {
|
|
_contextId = MmfCommon.CreateQuickFindContext(_patternInfo.MilSystemId);
|
|
}
|
|
else {
|
|
_contextId = MmfCommon.CreateContext(_patternInfo.MilSystemId);
|
|
}
|
|
_resultContextId = MmfCommon.CreateResultContext(_contextId);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Remove() {
|
|
FreeContexts();
|
|
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (Directory.Exists(patternDirPath)) {
|
|
Directory.Delete(patternDirPath, recursive: true);
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public ROI GetFindROI() {
|
|
return _paramExt.FindROI.Clone() as ROI;
|
|
}
|
|
|
|
public int SetFindROI(ROI findRoi) {
|
|
if (ROI.IsNullOrEmpty(findRoi)) {
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
_paramExt.FindROI = findRoi.Clone() as ROI;
|
|
return E1.OK;
|
|
}
|
|
|
|
public IRunParam GetRunParam() {
|
|
var runParam = MmfCommon.GetRunParam(_contextId);
|
|
runParam.MmfParamExt = _paramExt.Clone() as MmfParamExt;
|
|
|
|
return runParam;
|
|
}
|
|
|
|
public int SetRunParam(IRunParam runParam) {
|
|
_paramExt = ((ModelFinderRunParam)runParam).MmfParamExt.Clone() as MmfParamExt;
|
|
return MmfCommon.SetRunParam(_contextId, runParam);
|
|
}
|
|
|
|
public PatternResult Find(object image) {
|
|
return MmfCommon.Find(_contextId, _resultContextId, (MIL_ID)image, _paramExt.FindROI.Clone() as ROI, true);
|
|
}
|
|
|
|
public PatternResult Find(object image, ROI findRoi) {
|
|
return MmfCommon.Find(_contextId, _resultContextId, (MIL_ID)image, findRoi, true);
|
|
}
|
|
|
|
public PatternResult GetNotFoundPatternResult() {
|
|
return MmfCommon.GetNotFoundPatternResult();
|
|
}
|
|
|
|
public object GetTool() {
|
|
return _contextId;
|
|
}
|
|
|
|
public void Draw(
|
|
PatternResult patternResult, ICameraDisplay cameraDisplay, bool isFixturePattern = false) {
|
|
MmfCommon.Draw(patternResult, cameraDisplay as IMilCameraDisplay, isFixturePattern, _resultContextId);
|
|
}
|
|
|
|
public Bitmap GetThumbnail() {
|
|
var bitmap = ThumbnailUtil.GetDefaultImage();
|
|
if (!IsTrained()) {
|
|
return bitmap;
|
|
}
|
|
|
|
MIL_ID modelImage = MIL.M_NULL;
|
|
try {
|
|
modelImage = MmfImage.GetGrayModelImage(_contextId);
|
|
var preProcParam = GetPreProcParamForThumbnail();
|
|
ImgPreProcessor.GetMorphedImage(modelImage, modelImage, preProcParam);
|
|
return MilEnvironment.ConvertToBitmap(modelImage);
|
|
}
|
|
catch (Exception e) {
|
|
_logger.FuncE(e.Message);
|
|
}
|
|
finally {
|
|
MilEnvironment.FreeImage(modelImage);
|
|
}
|
|
|
|
return bitmap;
|
|
}
|
|
|
|
private PreProcessingParam GetPreProcParamForThumbnail() {
|
|
return new PreProcessingParam() {
|
|
BinParam = new BinarizationParam() {
|
|
Enabled = true,
|
|
},
|
|
MorphParam = new MorphologyParam() {
|
|
Enabled = true,
|
|
Types = new List<eMorphologyType>() {
|
|
eMorphologyType.Dilate,
|
|
eMorphologyType.None,
|
|
eMorphologyType.None,
|
|
eMorphologyType.None,
|
|
eMorphologyType.None,
|
|
},
|
|
RepeatCounts = new List<int>() { 1, 0, 0, 0, 0 }
|
|
}
|
|
};
|
|
}
|
|
#endregion
|
|
|
|
#region IPatternTrain
|
|
public bool IsTrained() {
|
|
return MmfCommon.HasModel(_contextId);
|
|
}
|
|
|
|
public ITrainParam GetTrainParam() {
|
|
return MmfShape.GetTrainParam(_contextId);
|
|
}
|
|
|
|
public int Train(ITrainParam trainParam) {
|
|
return MmfShape.Train(_contextId, trainParam);
|
|
}
|
|
#endregion
|
|
|
|
#region IDisposible
|
|
public void Dispose() {
|
|
FreeContexts();
|
|
}
|
|
|
|
private void FreeContexts() {
|
|
MmfCommon.FreeContext(_contextId);
|
|
MmfCommon.FreeContext(_resultContextId);
|
|
|
|
_contextId = MIL.M_NULL;
|
|
_resultContextId = MIL.M_NULL;
|
|
}
|
|
#endregion
|
|
|
|
#region ShapeModelFinder Specific
|
|
public bool IsParamExtExist() {
|
|
return MmfParamExt.Exists(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
internal MmfParamExt LoadParamExt() {
|
|
return MmfParamExt.Load(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
internal int SaveParamExt(MmfParamExt mmfParamExt) {
|
|
return MmfParamExt.Save(
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
mmfParamExt);
|
|
}
|
|
#endregion
|
|
|
|
public int GetFindNumber() {
|
|
var runParam = MmfCommon.GetRunParam(_contextId);
|
|
return runParam.FindNumber;
|
|
}
|
|
}
|
|
}
|