保存图片更新试试

This commit is contained in:
2025-04-02 14:41:26 +08:00
parent 409089e2ca
commit babc40d36a
6 changed files with 472 additions and 43 deletions

View File

@ -3,10 +3,42 @@ using System.ComponentModel;
using System.Drawing.Imaging;
using AntdUI;
using DH.Commons.Enums;
using HalconDotNet;
using OpenCvSharp;
namespace DH.Commons.Base
{
public class MatSet
{
public DateTime ImageTime { get; set; } = DateTime.Now;
private string id = "";
public string Id
{
get
{
if (string.IsNullOrWhiteSpace(id))
{
id = ImageTime.ToString("HHmmssfff");
}
return id;
}
set
{
id = value;
}
}
public string CameraId { get; set; }
public Mat _mat { get; set; } = null;
public ImageFormat _imageFormat { get; set; } = ImageFormat.Jpeg;
public virtual void Dispose()
{
_mat?.Dispose();
_mat = null;
}
}
public class CameraBase : NotifyProperty
{

View File

@ -9,6 +9,7 @@ using AntdUI;
using static DH.Commons.Enums.EnumHelper;
using System.Text.Json.Serialization;
using DH.Commons.Enums;
using System.Drawing.Imaging;
namespace DH.Commons.Base
{
@ -104,20 +105,20 @@ namespace DH.Commons.Base
/// </summary>
public class DetectionResultDetail
{
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string LabelBGR { get; set; }//识别到对象的标签BGR
public int LabelNo { get; set; } // 识别到对象的标签索引
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string LabelName { get; set; }//识别到对象的标签名称
public double Score { get; set; }//识别目标结果的可能性、得分
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string LabelDisplay { get; set; }//识别到对象的 显示信息
@ -138,10 +139,10 @@ namespace DH.Commons.Base
public class MLResult
{
public bool IsSuccess = false;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string ResultMessage;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public Bitmap ResultMap;
public List<DetectionResultDetail> ResultDetails = new List<DetectionResultDetail>();
@ -165,8 +166,7 @@ namespace DH.Commons.Base
public bool IsGPU;
public int GPUId;
public float Score_thre;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public MLInit(string modelFile, bool isGPU, int gpuId, float score_thre)
@ -191,7 +191,25 @@ namespace DH.Commons.Base
}
public class DetectStationResult
{
public string Pid { get; set; }
public DateTime ImageTime { get; set; } = DateTime.Now;
private string id = "";
public string Id
{
get
{
if (string.IsNullOrWhiteSpace(id))
{
id = ImageTime.ToString("HHmmssfff");
}
return id;
}
set
{
id = value;
}
}
public string Pid { get; set; }
public string TempPid { get; set; }
@ -237,35 +255,24 @@ namespace DH.Commons.Base
/// 预处理阶段已经NG
/// </summary>
public bool IsPreTreatNG { get; set; } = false;
/// <summary>
/// 检测原图
/// </summary>
public Bitmap DetectionOriginImage { get; set; }
/// <summary>
/// 目标检测NG
/// </summary>
public bool IsObjectDetectNG { get; set; } = false;
public ImageFormat ImageFormat { get; set; } = ImageFormat.Jpeg;
public DateTime EndTime { get; set; }
public string ImageSaveDirectory { get; set; }
public int StationDetectElapsed { get; set; }
public static string NormalizeAndClean(string input)
{
public bool SaveOKOriginal = false;
public bool SaveNGOriginal = false;
public bool SaveOKDetect = false;
public bool SaveNGDetect = false;
if (input == null) return null;
// Step 1: 标准化字符编码为 Form C (规范组合)
string normalizedString = input.Normalize(NormalizationForm.FormC);
// Step 2: 移除所有空白字符,包括制表符和换行符
string withoutWhitespace = Regex.Replace(normalizedString, @"\s+", "");
// Step 3: 移除控制字符 (Unicode 控制字符,范围 \u0000 - \u001F 和 \u007F)
string withoutControlChars = Regex.Replace(withoutWhitespace, @"[\u0000-\u001F\u007F]+", "");
// Step 4: 移除特殊的不可见字符(如零宽度空格等)
string cleanedString = Regex.Replace(withoutControlChars, @"[\u200B\u200C\u200D\uFEFF]+", "");
return cleanedString;
}
}
public class RelatedCamera : NotifyProperty
@ -502,7 +509,7 @@ namespace DH.Commons.Base
private AntList<SizeTreatParam> _sizeTreatParamList = new AntList<SizeTreatParam>();
private CustomizedPoint _showLocation = new CustomizedPoint();
private string _imageSaveDirectory;
private bool _saveOKOriginal = false;
private bool _saveNGOriginal = false;
private bool _saveOKDetect = false;
@ -620,7 +627,19 @@ namespace DH.Commons.Base
OnPropertyChanged(nameof(IsAddStation));
}
}
[Category("图片保存")]
[DisplayName("图片保存文件夹")]
[Description("图片保存文件夹")]
public virtual string ImageSaveDirectory
{
get => _imageSaveDirectory;
set
{
if (_imageSaveDirectory == value) return;
_imageSaveDirectory = value;
OnPropertyChanged(nameof(ImageSaveDirectory));
}
}
[Category("1.预处理(视觉算子)")]
[DisplayName("预处理-算法文件路径")]
public string HalconAlgorithemPath_Pre

View File

@ -0,0 +1,107 @@

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DH.Commons.Helper;
namespace DH.Commons.Enums
{
public class ImageSaveHelper
{
public event Action<DateTime, string> OnImageSaveExceptionRaised;
//private string baseDirectory = "";
//public string BaseDirectory
//{
// get => baseDirectory;
// set
// {
// baseDirectory = value;
// if (string.IsNullOrWhiteSpace(baseDirectory) || !Path.IsPathRooted(baseDirectory))
// {
// baseDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
// }
// }
//}
public bool EnableImageSave { get; set; } = true;
public ImageSaveHelper() { }
public ImageSaveHelper(bool enableImageSave = true)
{
EnableImageSave = enableImageSave;
}
object lockObj = new object();
////耗时操作从 _taskFactory分配线程
//public TaskFactory _taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning);
readonly ConcurrentQueue<ImageSaveSet> _imageQueue = new ConcurrentQueue<ImageSaveSet>();
Task _saveTask = null;
readonly object _saveLock = new object();
public async void ImageSaveAsync(ImageSaveSet set)
{
if (!EnableImageSave)
return;
await Task.Run(() =>
{
_imageQueue.Enqueue(set);
lock (_saveLock)
{
if (_saveTask == null)
{
_saveTask = Task.Run(async () =>
{
try
{
while (true)
{
while (_imageQueue.Count > 0)
{
if (_imageQueue.TryDequeue(out ImageSaveSet saveSet))
{
if (!Directory.Exists(Path.GetDirectoryName(saveSet.FullName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(saveSet.FullName));
}
if (saveSet.SaveImage != null)
{
saveSet.SaveImage.Save(saveSet.FullName, saveSet.ImageFormat);
saveSet.SaveImage.Dispose();
}
saveSet = null;
}
}
await Task.Delay(2000);
}
}
catch (Exception ex)
{
OnImageSaveExceptionRaised?.Invoke(DateTime.Now, $"图片保存异常:{ex.GetExceptionMessage()}");
}
});
}
}
});
}
}
public class ImageSaveSet
{
public string FullName { get; set; }//带后缀 全路径
public Bitmap SaveImage { get; set; }
public ImageFormat ImageFormat { get; set; } = ImageFormat.Jpeg;
}
}