[C#/COMMON/ML.NET/.NET5] ONNX와 YOLOv5를 사용해 이미지 객체 인식하기
■ ONNX와 YOLOv5를 사용해 이미지 객체를 인식하는 방법을 보여준다. [소스 이미지] [결과 이미지] ▶ RectangleExtension.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System.Drawing; namespace TestProject { /// <summary> /// 사각형 확장 /// </summary> public static class RectangleExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 영역 구하기 - GetArea(rectangle) /// <summary> /// 영역 구하기 /// </summary> /// <param name="rectangle">사각형</param> /// <returns>영역</returns> public static float GetArea(this RectangleF rectangle) { return rectangle.Width * rectangle.Height; } #endregion } } |
▶ YOLOModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
using System.Collections.Generic; namespace TestProject { /// <summary> /// YOLO 모델 /// </summary> public abstract class YOLOModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 너비 - Width /// <summary> /// 너비 /// </summary> public abstract int Width { get; set; } #endregion #region 높이 - Height /// <summary> /// 높이 /// </summary> public abstract int Height { get; set; } #endregion #region 깊이 - Depth /// <summary> /// 깊이 /// </summary> public abstract int Depth { get; set; } #endregion #region 차원 - Dimension /// <summary> /// 차원 /// </summary> public abstract int Dimension { get; set; } #endregion #region 스트라이드 배열 - StrideArray /// <summary> /// 스트라이드 배열 /// </summary> public abstract float[] StrideArray { get; set; } #endregion #region 앵커 배열 - AnchorArray /// <summary> /// 앵커 배열 /// </summary> public abstract float[][][] AnchorArray { get; set; } #endregion #region 도형 배열 - ShapeArray /// <summary> /// 도형 배열 /// </summary> public abstract int[] ShapeArray { get; set; } #endregion #region 신뢰도 - Confidence /// <summary> /// 신뢰도 /// </summary> public abstract float Confidence { get; set; } #endregion #region 다중 신뢰도 - MultipleConfidence /// <summary> /// 다중 신뢰도 /// </summary> public abstract float MultipleConfidence { get; set; } #endregion #region 오버랩 - Overlap /// <summary> /// 오버랩 /// </summary> public abstract float Overlap { get; set; } #endregion #region 출력 배열 - OutputArray /// <summary> /// 출력 배열 /// </summary> public abstract string[] OutputArray { get; set; } #endregion #region 레이블 리스트 - LabelList /// <summary> /// 레이블 리스트 /// </summary> public abstract List<YOLOLabel> LabelList { get; set; } #endregion #region 탐지 사용 여부 - UseDetect /// <summary> /// 탐지 사용 여부 /// </summary> public abstract bool UseDetect { get; set; } #endregion } } |
▶ YOLOCOCOP5Model.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
using System.Collections.Generic; namespace TestProject { /// <summary> /// YOLO COCO P5 모델 /// </summary> public class YOLOCOCOP5Model : YOLOModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 너비 - Width /// <summary> /// 너비 /// </summary> public override int Width { get; set; } = 640; #endregion #region 높이 - Height /// <summary> /// 높이 /// </summary> public override int Height { get; set; } = 640; #endregion #region 깊이 - Depth /// <summary> /// 깊이 /// </summary> public override int Depth { get; set; } = 3; #endregion #region 차원 - Dimension /// <summary> /// 차원 /// </summary> public override int Dimension { get; set; } = 85; #endregion #region 스트라이드 배열 - StrideArray /// <summary> /// 스트라이드 배열 /// </summary> public override float[] StrideArray { get; set; } = new float[] { 8, 16, 32 }; #endregion #region 앵커 배열 - AnchorArray /// <summary> /// 앵커 배열 /// </summary> public override float[][][] AnchorArray { get; set; } = new float[][][] { new float[][] { new float[] { 010, 13 }, new float[] { 016, 030 }, new float[] { 033, 023 } }, new float[][] { new float[] { 030, 61 }, new float[] { 062, 045 }, new float[] { 059, 119 } }, new float[][] { new float[] { 116, 90 }, new float[] { 156, 198 }, new float[] { 373, 326 } } }; #endregion #region 도형 배열 - ShapeArray /// <summary> /// 도형 배열 /// </summary> public override int[] ShapeArray { get; set; } = new int[] { 80, 40, 20 }; #endregion #region 신뢰도 - Confidence /// <summary> /// 신뢰도 /// </summary> public override float Confidence { get; set; } = 0.20f; #endregion #region 다중 신뢰도 - MultipleConfidence /// <summary> /// 다중 신뢰도 /// </summary> public override float MultipleConfidence { get; set; } = 0.25f; #endregion #region 오버랩 - Overlap /// <summary> /// 오버랩 /// </summary> public override float Overlap { get; set; } = 0.45f; #endregion #region 출력 배열 - OutputArray /// <summary> /// 출력 배열 /// </summary> public override string[] OutputArray { get; set; } = new[] { "output" }; #endregion #region 레이블 리스트 - LabelList /// <summary> /// 레이블 리스트 /// </summary> public override List<YOLOLabel> LabelList { get; set; } = new List<YOLOLabel>() { new YOLOLabel { ID = 1, Name = "person" }, new YOLOLabel { ID = 2, Name = "bicycle" }, new YOLOLabel { ID = 3, Name = "car" }, new YOLOLabel { ID = 4, Name = "motorcycle" }, new YOLOLabel { ID = 5, Name = "airplane" }, new YOLOLabel { ID = 6, Name = "bus" }, new YOLOLabel { ID = 7, Name = "train" }, new YOLOLabel { ID = 8, Name = "truck" }, new YOLOLabel { ID = 9, Name = "boat" }, new YOLOLabel { ID = 10, Name = "traffic light" }, new YOLOLabel { ID = 11, Name = "fire hydrant" }, new YOLOLabel { ID = 12, Name = "stop sign" }, new YOLOLabel { ID = 13, Name = "parking meter" }, new YOLOLabel { ID = 14, Name = "bench" }, new YOLOLabel { ID = 15, Name = "bird" }, new YOLOLabel { ID = 16, Name = "cat" }, new YOLOLabel { ID = 17, Name = "dog" }, new YOLOLabel { ID = 18, Name = "horse" }, new YOLOLabel { ID = 19, Name = "sheep" }, new YOLOLabel { ID = 20, Name = "cow" }, new YOLOLabel { ID = 21, Name = "elephant" }, new YOLOLabel { ID = 22, Name = "bear" }, new YOLOLabel { ID = 23, Name = "zebra" }, new YOLOLabel { ID = 24, Name = "giraffe" }, new YOLOLabel { ID = 25, Name = "backpack" }, new YOLOLabel { ID = 26, Name = "umbrella" }, new YOLOLabel { ID = 27, Name = "handbag" }, new YOLOLabel { ID = 28, Name = "tie" }, new YOLOLabel { ID = 29, Name = "suitcase" }, new YOLOLabel { ID = 30, Name = "frisbee" }, new YOLOLabel { ID = 31, Name = "skis" }, new YOLOLabel { ID = 32, Name = "snowboard" }, new YOLOLabel { ID = 33, Name = "sports ball" }, new YOLOLabel { ID = 34, Name = "kite" }, new YOLOLabel { ID = 35, Name = "baseball bat" }, new YOLOLabel { ID = 36, Name = "baseball glove" }, new YOLOLabel { ID = 37, Name = "skateboard" }, new YOLOLabel { ID = 38, Name = "surfboard" }, new YOLOLabel { ID = 39, Name = "tennis racket" }, new YOLOLabel { ID = 40, Name = "bottle" }, new YOLOLabel { ID = 41, Name = "wine glass" }, new YOLOLabel { ID = 42, Name = "cup" }, new YOLOLabel { ID = 43, Name = "fork" }, new YOLOLabel { ID = 44, Name = "knife" }, new YOLOLabel { ID = 45, Name = "spoon" }, new YOLOLabel { ID = 46, Name = "bowl" }, new YOLOLabel { ID = 47, Name = "banana" }, new YOLOLabel { ID = 48, Name = "apple" }, new YOLOLabel { ID = 49, Name = "sandwich" }, new YOLOLabel { ID = 50, Name = "orange" }, new YOLOLabel { ID = 51, Name = "broccoli" }, new YOLOLabel { ID = 52, Name = "carrot" }, new YOLOLabel { ID = 53, Name = "hot dog" }, new YOLOLabel { ID = 54, Name = "pizza" }, new YOLOLabel { ID = 55, Name = "donut" }, new YOLOLabel { ID = 56, Name = "cake" }, new YOLOLabel { ID = 57, Name = "chair" }, new YOLOLabel { ID = 58, Name = "couch" }, new YOLOLabel { ID = 59, Name = "potted plant" }, new YOLOLabel { ID = 60, Name = "bed" }, new YOLOLabel { ID = 61, Name = "dining table" }, new YOLOLabel { ID = 62, Name = "toilet" }, new YOLOLabel { ID = 63, Name = "tv" }, new YOLOLabel { ID = 64, Name = "laptop" }, new YOLOLabel { ID = 65, Name = "mouse" }, new YOLOLabel { ID = 66, Name = "remote" }, new YOLOLabel { ID = 67, Name = "keyboard" }, new YOLOLabel { ID = 68, Name = "cell phone" }, new YOLOLabel { ID = 69, Name = "microwave" }, new YOLOLabel { ID = 70, Name = "oven" }, new YOLOLabel { ID = 71, Name = "toaster" }, new YOLOLabel { ID = 72, Name = "sink" }, new YOLOLabel { ID = 73, Name = "refrigerator" }, new YOLOLabel { ID = 74, Name = "book" }, new YOLOLabel { ID = 75, Name = "clock" }, new YOLOLabel { ID = 76, Name = "vase" }, new YOLOLabel { ID = 77, Name = "scissors" }, new YOLOLabel { ID = 78, Name = "teddy bear" }, new YOLOLabel { ID = 79, Name = "hair drier" }, new YOLOLabel { ID = 80, Name = "toothbrush" } }; #endregion #region 탐지 사용 여부 - UseDetect /// <summary> /// 탐지 사용 여부 /// </summary> public override bool UseDetect { get; set; } = true; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - YOLOCOCOP5Model() /// <summary> /// 생성자 /// </summary> public YOLOCOCOP5Model() { } #endregion } } |