hisence-yolo-detection/HisenceYoloDetection/MLResultDisplay.cs
2024-06-27 09:22:31 +08:00

217 lines
7.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HisenceYoloDetection
{
public class DetectResultDisplay
{
//深度学习 显示结果
private List<DetectionResultDetail> mlResultList = null;
public List<DetectionResultDetail> MLResultList
{
get => mlResultList;
set
{
if (mlResultList != value)
{
mlResultList = value;
}
}
}
string DisplayTxt = "";
Bitmap ResultImage = null;
PointF StartPoint = new PointF();
Brush FontBrush = new SolidBrush(Color.Green);
Pen DetectResultRectPen = new Pen(new SolidBrush(Color.Green));
public Font Font { get; private set; }
ResultState ResultState = ResultState.DetectNG;
Font DetectResultFont = new Font(new FontFamily("Tahoma"), 15, GraphicsUnit.World);
public int ImageWidth { get; set; }
public int ImageHeight { get; set; }
public DetectResultDisplay() { }
//public DetectResultDisplay(NetResult result, List<IndexedSpec> specs, ResultState resultState, int imageWidth)
//{
// ImageWidth = imageWidth;
// ResultState = resultState;
// displayTxt = resultState.ToString() + "\r\n";
// if (resultState != ResultState.OK)
// {
// fontBrush = new SolidBrush(Color.Red);
// }
// NetResult = result;
// SpecList = specs;
// Font = new Font(new FontFamily("Tahoma"), 35 * ImageWidth / 1400, GraphicsUnit.World);
// startPoint = new PointF(150 * ImageWidth / 1400, 150 * ImageWidth / 1400);
//}
public DetectResultDisplay(List<DetectionResultDetail> ResultDetails, Bitmap resultImage, string displayTxt)
{
ImageWidth = resultImage.Width;
ImageHeight = resultImage.Height;
var longSide = ImageWidth > ImageHeight ? ImageWidth : ImageHeight;
MLResultList = ResultDetails;
ResultImage = resultImage;
DisplayTxt = displayTxt;
if (ResultState != ResultState.OK)
{
FontBrush = new SolidBrush(Color.Red);
DetectResultRectPen = new Pen(new SolidBrush(Color.Red));
}
Font = new Font(new FontFamily("Tahoma"), 35 * longSide / 1400, GraphicsUnit.World);
DetectResultFont = new Font(new FontFamily("Tahoma"), 25 * longSide / 1400, GraphicsUnit.World);
StartPoint = new PointF(100 * ImageWidth / 1400, 100 * ImageHeight / 1400);
}
public object Clone()
{
return null;
}
public void Draw(Graphics g)
{
//画检测结果图
if (ResultImage != null && ResultState != ResultState.OK)
{
g.DrawImage(ResultImage, new Point(0, 0));
}
//画文字
if (!string.IsNullOrWhiteSpace(DisplayTxt))
{
g.DrawString(DisplayTxt, Font, FontBrush, StartPoint);
}
//画外接矩形+label 深度学习
if (MLResultList != null && MLResultList.Count > 0)
{
MLResultList.ForEach(d =>
{
g.DrawRectangle(DetectResultRectPen, d.Rect);
string locationTxt = $"{d.LabelDisplay}";
var locationX = d.Rect.X;
var locationY = d.Rect.Y <= 20 ? d.Rect.Y + 20 : d.Rect.Y - 20;
g.DrawString(locationTxt, DetectResultFont, FontBrush, locationX, locationY);
});
}
//画spec信息
//if (DetectResult != null && DetectResult.NetResult?.DetectDetails?.Count > 0)
//{
// DetectResult.NetResult?.DetectDetails.ForEach(d =>
// {
// g.DrawRectangle(defectRectPen, d.Rect);
// string locationTxt = $"{d.Rect.X},{d.Rect.Y}";
// g.DrawString(locationTxt, defectFont, fontBrush, d.Rect.X, d.Rect.Y - 5);
// });
//}
//float fontHeight = g.MeasureString(displayTxt, Font).Height;
//startPoint.Y += fontHeight * 1.2f;
//var defects = DetectResult.NetResult?.DetectDetails;
//if (defects != null && defects.Count > 0)
//{
// defects.ForEach(d =>
// {
// g.DrawString($"{d.ClassName} X:{d.Rect.X.ToString("f2")} Y:{d.Rect.Y.ToString("f2")} S:{d.Area}", Font, d.FinalResult == EnumHelper.ResultState.OK ? fontBrushOK : fontBrushNG, startPoint);
// startPoint.Y += fontHeight;
// });
//}
//DetectResult.Specs.ForEach(s =>
//{
// g.DrawString($"{s.Code}:{(s.ActualValue ?? 0).ToString("f2")}", Font, s.MeasureResult ?? false == true ? fontBrushOK : fontBrushNG, startPoint);
// startPoint.Y += fontHeight;
//});
}
public string GetDisplayText()
{
return "";
}
}
public class RectResultDisplay
{
ResultState ResultState = ResultState.DetectNG;
public string DisplayTxt = "";
Color FillColor = Color.Lime;
int FontSize = 15;
RectangleF Rect = new RectangleF();
bool IsFilled = false;
public RectResultDisplay() { }
public RectResultDisplay(ResultState _resultState, RectangleF _rect, string _displayTxt, Color _fillColor, bool _isFilled, int _fontSize)
{
ResultState = _resultState;
Rect = _rect;
DisplayTxt = _displayTxt;
FillColor = _fillColor;
IsFilled = _isFilled;
FontSize = _fontSize;
}
public object Clone()
{
RectResultDisplay rect = new RectResultDisplay();
rect.ResultState = ResultState;
rect.Rect = Rect;
rect.DisplayTxt = DisplayTxt;
rect.FillColor = FillColor;
rect.FontSize = FontSize;
rect.IsFilled = IsFilled;
return rect;
}
public void Draw(Graphics g)
{
g.DrawRectangle(new Pen(FillColor, 1), Rect.X, Rect.Y, Rect.Width, Rect.Height);
if (IsFilled)
{
g.FillRectangle(new SolidBrush(Color.FromArgb(20, FillColor)), Rect);
}
Font font = new Font("Tahoma", FontSize);
var txtSize = g.MeasureString(DisplayTxt, font);
g.DrawString(DisplayTxt, font, new SolidBrush(FillColor), (float)(Rect.X + Rect.Width / 2.0 - txtSize.Width / 2.0), Rect.Y + Rect.Height + 5);
}
public string GetDisplayText()
{
return $"{ResultState} {DisplayTxt} ({Rect.X},{Rect.Y},{Rect.Width},{Rect.Height})";
}
}
}