Skip to content

Commit b88a321

Browse files
committed
v1.0.5 Updated OpenCVForUnity version to 2.5.9.
1 parent 1b6dd6c commit b88a321

File tree

5 files changed

+71
-52
lines changed

5 files changed

+71
-52
lines changed

Assets/YOLOv8WithOpenCVForUnityExample/Scripts/YOLOv8WithOpenCVForUnity/YOLOv8ClassPredictor.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using OpenCVForUnity.CoreModule;
22
using OpenCVForUnity.DnnModule;
33
using OpenCVForUnity.ImgprocModule;
4+
using OpenCVForUnity.UnityUtils;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -159,11 +160,11 @@ public virtual void visualize(Mat image, Mat results, bool print_results = false
159160
StringBuilder sb = null;
160161

161162
if (print_results)
162-
sb = new StringBuilder();
163+
sb = new StringBuilder(64);
163164

164165
ClassificationData bmData = getBestMatchData(results);
165166
int classId = (int)bmData.cls;
166-
string label = getClassLabel(bmData.cls) + ", " + String.Format("{0:0.00}", bmData.conf);
167+
string label = getClassLabel(bmData.cls) + ", " + bmData.conf.ToString("F2");
167168

168169
Scalar c = palette[classId % palette.Count];
169170
Scalar color = isRGB ? c : new Scalar(c.val[2], c.val[1], c.val[0], c.val[3]);
@@ -182,11 +183,11 @@ public virtual void visualize(Mat image, Mat results, bool print_results = false
182183
// Print results
183184
if (print_results)
184185
{
185-
sb.AppendLine(String.Format("Best match: " + getClassLabel(bmData.cls) + ", " + bmData));
186+
sb.AppendLine("Best match: " + getClassLabel(bmData.cls) + ", " + bmData.ToString());
186187
}
187188

188189
if (print_results)
189-
Debug.Log(sb);
190+
Debug.Log(sb.ToString());
190191
}
191192

192193
public virtual void dispose()
@@ -251,7 +252,7 @@ public ClassificationData(int cls, float conf)
251252

252253
public override string ToString()
253254
{
254-
return "cls:" + cls + " conf:" + conf;
255+
return "cls:" + cls.ToString() + " conf:" + conf.ToString();
255256
}
256257
};
257258

@@ -273,7 +274,7 @@ public virtual ClassificationData[] getData(Mat results)
273274
results_numx1.copyTo(getDataMat.col(1));
274275

275276
var dst = new ClassificationData[num];
276-
OpenCVForUnity.UtilsModule.MatUtils.copyFromMat(getDataMat, dst);
277+
MatUtils.copyFromMat(getDataMat, dst);
277278

278279
return dst;
279280
}

Assets/YOLOv8WithOpenCVForUnityExample/Scripts/YOLOv8WithOpenCVForUnity/YOLOv8ObjectDetector.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using OpenCVForUnity.CoreModule;
22
using OpenCVForUnity.DnnModule;
33
using OpenCVForUnity.ImgprocModule;
4+
using OpenCVForUnity.UnityUtils;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -324,7 +325,7 @@ public virtual void visualize(Mat image, Mat results, bool print_results = false
324325

325326
Imgproc.rectangle(image, new Point(left, top), new Point(right, bottom), color, 2);
326327

327-
string label = getClassLabel(classId) + ", " + String.Format("{0:0.00}", conf);
328+
string label = $"{getClassLabel(classId)}, {conf:F2}";
328329

329330
int[] baseLine = new int[1];
330331
Size labelSize = Imgproc.getTextSize(label, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);
@@ -338,20 +339,24 @@ public virtual void visualize(Mat image, Mat results, bool print_results = false
338339
// Print results
339340
if (print_results)
340341
{
341-
StringBuilder sb = new StringBuilder();
342+
StringBuilder sb = new StringBuilder(512);
342343

343344
for (int i = 0; i < data.Length; ++i)
344345
{
345346
var d = data[i];
346-
string label = getClassLabel(d.cls) + ", " + String.Format("{0:0}", d.conf);
347-
348-
sb.AppendLine(String.Format("-----------object {0}-----------", i + 1));
349-
sb.AppendLine(String.Format("conf: {0:0.0000}", d.conf));
350-
sb.AppendLine(String.Format("cls: {0:0}", label));
351-
sb.AppendLine(String.Format("box: {0:0} {1:0} {2:0} {3:0}", d.x1, d.y1, d.x2, d.y2));
347+
string label = getClassLabel(d.cls);
348+
349+
sb.AppendFormat("-----------object {0}-----------", i + 1);
350+
sb.AppendLine();
351+
sb.AppendFormat("conf: {0:F4}", d.conf);
352+
sb.AppendLine();
353+
sb.Append("cls: ").Append(label);
354+
sb.AppendLine();
355+
sb.AppendFormat("box: {0:F0} {1:F0} {2:F0} {3:F0}", d.x1, d.y1, d.x2, d.y2);
356+
sb.AppendLine();
352357
}
353358

354-
Debug.Log(sb);
359+
Debug.Log(sb.ToString());
355360
}
356361
}
357362

@@ -448,7 +453,7 @@ public DetectionData(int x1, int y1, int x2, int y2, float conf, int cls)
448453

449454
public override string ToString()
450455
{
451-
return "x1:" + x1 + " y1:" + y1 + "x2:" + x2 + " y2:" + y2 + " conf:" + conf + " cls:" + cls;
456+
return "x1:" + x1.ToString() + " y1:" + y1.ToString() + "x2:" + x2.ToString() + " y2:" + y2.ToString() + " conf:" + conf.ToString() + " cls:" + cls.ToString();
452457
}
453458
};
454459

@@ -458,7 +463,7 @@ public virtual DetectionData[] getData(Mat results)
458463
return new DetectionData[0];
459464

460465
var dst = new DetectionData[results.rows()];
461-
OpenCVForUnity.UtilsModule.MatUtils.copyFromMat(results, dst);
466+
MatUtils.copyFromMat(results, dst);
462467

463468
return dst;
464469
}

Assets/YOLOv8WithOpenCVForUnityExample/Scripts/YOLOv8WithOpenCVForUnity/YOLOv8PoseEstimater.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using OpenCVForUnity.CoreModule;
22
using OpenCVForUnity.DnnModule;
33
using OpenCVForUnity.ImgprocModule;
4+
using OpenCVForUnity.UnityUtils;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -47,6 +48,8 @@ public class YOLOv8PoseEstimater
4748
MatOfFloat confidences;
4849
MatOfInt class_ids;
4950

51+
float[] landmarks_buffer;
52+
5053
public YOLOv8PoseEstimater(string modelFilepath, string classesFilepath, Size inputSize, float confThreshold = 0.25f, float nmsThreshold = 0.45f, int topK = 300, int backend = Dnn.DNN_BACKEND_OPENCV, int target = Dnn.DNN_TARGET_CPU)
5154
{
5255
// initialize
@@ -384,7 +387,7 @@ public virtual void visualize(Mat image, Mat results, bool print_results = false
384387

385388
Imgproc.rectangle(image, new Point(left, top), new Point(right, bottom), color, 2);
386389

387-
string label = getClassLabel(classId) + ", " + String.Format("{0:0.00}", conf);
390+
string label = $"{getClassLabel(classId)}, {conf:F2}";
388391

389392
int[] baseLine = new int[1];
390393
Size labelSize = Imgproc.getTextSize(label, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);
@@ -398,20 +401,24 @@ public virtual void visualize(Mat image, Mat results, bool print_results = false
398401
// Print results
399402
if (print_results)
400403
{
401-
StringBuilder sb = new StringBuilder();
404+
StringBuilder sb = new StringBuilder(512);
402405

403406
for (int i = 0; i < data.Length; ++i)
404407
{
405408
var d = data[i];
406-
string label = getClassLabel(d.cls) + ", " + String.Format("{0:0}", d.conf);
407-
408-
sb.AppendLine(String.Format("-----------object {0}-----------", i + 1));
409-
sb.AppendLine(String.Format("conf: {0:0.0000}", d.conf));
410-
sb.AppendLine(String.Format("cls: {0:0}", label));
411-
sb.AppendLine(String.Format("box: {0:0} {1:0} {2:0} {3:0}", d.x1, d.y1, d.x2, d.y2));
409+
string label = getClassLabel(d.cls);
410+
411+
sb.AppendFormat("-----------object {0}-----------", i + 1);
412+
sb.AppendLine();
413+
sb.AppendFormat("conf: {0:F4}", d.conf);
414+
sb.AppendLine();
415+
sb.Append("cls: ").Append(label);
416+
sb.AppendLine();
417+
sb.AppendFormat("box: {0:F0} {1:F0} {2:F0} {3:F0}", d.x1, d.y1, d.x2, d.y2);
418+
sb.AppendLine();
412419
}
413420

414-
Debug.Log(sb);
421+
Debug.Log(sb.ToString());
415422
}
416423
}
417424

@@ -431,26 +438,27 @@ public virtual void visualize_kpts(Mat image, Mat kpts, int radius = 5, bool kpt
431438

432439
kpt_line &= is_pose; //# `kpt_line=True` for now only supports human pose plotting
433440

434-
float[] landmarks = new float[kpts.cols()];
441+
if (landmarks_buffer == null)
442+
landmarks_buffer = new float[kpts.cols()];
435443

436444
for (int i = 0; i < kpts.rows(); ++i)
437445
{
438-
kpts.get(i, 0, landmarks);
446+
kpts.get(i, 0, landmarks_buffer);
439447

440448
// draw points
441449
int k_ind = 0;
442-
for (int j = 0; j < landmarks.Length; j += 3)
450+
for (int j = 0; j < landmarks_buffer.Length; j += 3)
443451
{
444452
Scalar c = is_pose ? pose_palette[kpt_color_ind[k_ind]] : palette[k_ind % palette.Count];
445453
Scalar color_k = isRGB ? c : new Scalar(c.val[2], c.val[1], c.val[0], c.val[3]);
446454
k_ind++;
447455

448-
float x_coord = landmarks[j];
449-
float y_coord = landmarks[j + 1];
456+
float x_coord = landmarks_buffer[j];
457+
float y_coord = landmarks_buffer[j + 1];
450458

451459
if (x_coord % image.width() != 0 && y_coord % image.height() != 0)
452460
{
453-
float conf = landmarks[j + 2];
461+
float conf = landmarks_buffer[j + 2];
454462
if (conf < 0.5)
455463
continue;
456464

@@ -464,14 +472,14 @@ public virtual void visualize_kpts(Mat image, Mat kpts, int radius = 5, bool kpt
464472
for (int p = 0; p < skeleton.GetLength(0); p++)
465473
{
466474
int pos1_ind = (skeleton[p, 0] - 1) * 3;
467-
float pos1_x = landmarks[pos1_ind];
468-
float pos1_y = landmarks[pos1_ind + 1];
469-
float conf1 = landmarks[pos1_ind + 2];
475+
float pos1_x = landmarks_buffer[pos1_ind];
476+
float pos1_y = landmarks_buffer[pos1_ind + 1];
477+
float conf1 = landmarks_buffer[pos1_ind + 2];
470478

471479
int pos2_ind = (skeleton[p, 1] - 1) * 3;
472-
float pos2_x = landmarks[pos2_ind];
473-
float pos2_y = landmarks[pos2_ind + 1];
474-
float conf2 = landmarks[pos2_ind + 2];
480+
float pos2_x = landmarks_buffer[pos2_ind];
481+
float pos2_y = landmarks_buffer[pos2_ind + 1];
482+
float conf2 = landmarks_buffer[pos2_ind + 2];
475483

476484
if (conf1 < 0.5f || conf2 < 0.5f)
477485
continue;
@@ -584,7 +592,7 @@ public DetectionData(int x1, int y1, int x2, int y2, float conf, int cls)
584592

585593
public override string ToString()
586594
{
587-
return "x1:" + x1 + " y1:" + y1 + "x2:" + x2 + " y2:" + y2 + " conf:" + conf + " cls:" + cls;
595+
return "x1:" + x1.ToString() + " y1:" + y1.ToString() + "x2:" + x2.ToString() + " y2:" + y2.ToString() + " conf:" + conf.ToString() + " cls:" + cls.ToString();
588596
}
589597
};
590598

@@ -594,7 +602,7 @@ public virtual DetectionData[] getData(Mat results)
594602
return new DetectionData[0];
595603

596604
var dst = new DetectionData[results.rows()];
597-
OpenCVForUnity.UtilsModule.MatUtils.copyFromMat(results, dst);
605+
MatUtils.copyFromMat(results, dst);
598606

599607
return dst;
600608
}

Assets/YOLOv8WithOpenCVForUnityExample/Scripts/YOLOv8WithOpenCVForUnity/YOLOv8SegmentPredictor.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using OpenCVForUnity.CoreModule;
22
using OpenCVForUnity.DnnModule;
33
using OpenCVForUnity.ImgprocModule;
4+
using OpenCVForUnity.UnityUtils;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -470,7 +471,7 @@ public virtual void visualize(Mat image, Mat results, bool print_results = false
470471

471472
Imgproc.rectangle(image, new Point(left, top), new Point(right, bottom), color, 2);
472473

473-
string label = getClassLabel(classId) + ", " + String.Format("{0:0.00}", conf);
474+
string label = $"{getClassLabel(classId)}, {conf:F2}";
474475

475476
int[] baseLine = new int[1];
476477
Size labelSize = Imgproc.getTextSize(label, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);
@@ -484,20 +485,24 @@ public virtual void visualize(Mat image, Mat results, bool print_results = false
484485
// Print results
485486
if (print_results)
486487
{
487-
StringBuilder sb = new StringBuilder();
488+
StringBuilder sb = new StringBuilder(512);
488489

489490
for (int i = 0; i < data.Length; ++i)
490491
{
491492
var d = data[i];
492-
string label = getClassLabel(d.cls) + ", " + String.Format("{0:0}", d.conf);
493-
494-
sb.AppendLine(String.Format("-----------object {0}-----------", i + 1));
495-
sb.AppendLine(String.Format("conf: {0:0.0000}", d.conf));
496-
sb.AppendLine(String.Format("cls: {0:0}", label));
497-
sb.AppendLine(String.Format("box: {0:0} {1:0} {2:0} {3:0}", d.x1, d.y1, d.x2, d.y2));
493+
string label = getClassLabel(d.cls);
494+
495+
sb.AppendFormat("-----------object {0}-----------", i + 1);
496+
sb.AppendLine();
497+
sb.AppendFormat("conf: {0:F4}", d.conf);
498+
sb.AppendLine();
499+
sb.Append("cls: ").Append(label);
500+
sb.AppendLine();
501+
sb.AppendFormat("box: {0:F0} {1:F0} {2:F0} {3:F0}", d.x1, d.y1, d.x2, d.y2);
502+
sb.AppendLine();
498503
}
499504

500-
Debug.Log(sb);
505+
Debug.Log(sb.ToString());
501506
}
502507
}
503508

@@ -691,7 +696,7 @@ public DetectionData(int x1, int y1, int x2, int y2, float conf, int cls)
691696

692697
public override string ToString()
693698
{
694-
return "x1:" + x1 + " y1:" + y1 + "x2:" + x2 + " y2:" + y2 + " conf:" + conf + " cls:" + cls;
699+
return "x1:" + x1.ToString() + " y1:" + y1.ToString() + "x2:" + x2.ToString() + " y2:" + y2.ToString() + " conf:" + conf.ToString() + " cls:" + cls.ToString();
695700
}
696701
};
697702

@@ -701,7 +706,7 @@ public virtual DetectionData[] getData(Mat results)
701706
return new DetectionData[0];
702707

703708
var dst = new DetectionData[results.rows()];
704-
OpenCVForUnity.UtilsModule.MatUtils.copyFromMat(results, dst);
709+
MatUtils.copyFromMat(results, dst);
705710

706711
return dst;
707712
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- Windows / Mac / Linux / WebGL / Android / iOS
77
- Unity >= 2020.3.48f1+
88
- Scripting backend MONO / IL2CPP
9-
- [OpenCV for Unity](https://assetstore.unity.com/packages/tools/integration/opencv-for-unity-21088?aid=1011l4ehR) 2.5.8+
9+
- [OpenCV for Unity](https://assetstore.unity.com/packages/tools/integration/opencv-for-unity-21088?aid=1011l4ehR) 2.5.9+
1010

1111

1212
## Setup

0 commit comments

Comments
 (0)