MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
Annotation.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Drawing;
5using System.IO;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Xml.Serialization;
10
12{
20 [Serializable]
21 public class Annotation
22 {
23 int m_nInstanceId = 0;
24 NormalizedBBox m_bbox;
25
31 public Annotation(NormalizedBBox bbox, int nInstanceId = 0)
32 {
33 m_bbox = bbox;
34 m_nInstanceId = nInstanceId;
35 }
36
42 {
43 NormalizedBBox bbox = null;
44
45 if (m_bbox != null)
46 bbox = m_bbox.Clone();
47
48 return new Annotation(bbox, m_nInstanceId);
49 }
50
54 public int instance_id
55 {
56 get { return m_nInstanceId; }
57 set { m_nInstanceId = value; }
58 }
59
64 {
65 get { return m_bbox; }
66 set { m_bbox = value; }
67 }
68
73 public void Normalize(Rectangle rc)
74 {
75 float fxmin = 0;
76 float fymin = 0;
77 float fxmax = 1;
78 float fymax = 1;
79
80 if (m_bbox.xmin > rc.Left)
81 fxmin = (m_bbox.xmin - rc.Left) / rc.Width;
82
83 if (m_bbox.ymin > rc.Top)
84 fymin = (m_bbox.ymin - rc.Top) / rc.Height;
85
86 if (m_bbox.xmax < rc.Right)
87 fxmax = (m_bbox.xmax - rc.Left) / rc.Width;
88
89 if (m_bbox.ymax < rc.Bottom)
90 fymax = (m_bbox.ymax - rc.Top) / rc.Height;
91
92 m_bbox.Set(fxmin, fymin, fxmax, fymax);
93 }
94
99 public void Save(BinaryWriter bw)
100 {
101 bw.Write(m_nInstanceId);
102 m_bbox.Save(bw);
103 }
104
110 public static Annotation Load(BinaryReader br)
111 {
112 int nInstanceId = br.ReadInt32();
114
115 return new Annotation(bbox, nInstanceId);
116 }
117 }
118
122 [Serializable]
123 public class AnnotationGroup
124 {
125 int m_nGroupLabel = 0;
126 List<Annotation> m_rgAnnotations = new List<Annotation>();
127
133 public AnnotationGroup(List<Annotation> rgAnnotations = null, int nGroupLabel = 0)
134 {
135 if (rgAnnotations != null && rgAnnotations.Count > 0)
136 m_rgAnnotations.AddRange(rgAnnotations);
137
138 m_nGroupLabel = nGroupLabel;
139 }
140
146 {
147 List<Annotation> rg = null;
148
149 if (m_rgAnnotations != null)
150 {
151 rg = new List<Annotation>();
152
153 foreach (Annotation a in m_rgAnnotations)
154 {
155 rg.Add(a.Clone());
156 }
157 }
158
159 return new AnnotationGroup(rg, m_nGroupLabel);
160 }
161
167 {
168 float fMaxScore = 0;
169 int nMaxIdx = -1;
170
171 for (int i = 0; i < m_rgAnnotations.Count; i++)
172 {
173 if (m_rgAnnotations[i].bbox.score >= fMaxScore)
174 {
175 nMaxIdx = i;
176 fMaxScore = m_rgAnnotations[i].bbox.score;
177 }
178 }
179
180 if (nMaxIdx < 0)
181 return null;
182
183 return m_rgAnnotations[nMaxIdx];
184 }
185
190 public void Normalize(Rectangle rc)
191 {
192 foreach (Annotation a in m_rgAnnotations)
193 {
194 a.Normalize(rc);
195 }
196 }
197
201 public List<Annotation> annotations
202 {
203 get { return m_rgAnnotations; }
204 set { m_rgAnnotations = value; }
205 }
206
210 public int group_label
211 {
212 get { return m_nGroupLabel; }
213 set { m_nGroupLabel = value; }
214 }
215
220 public void Save(BinaryWriter bw)
221 {
222 bw.Write(m_nGroupLabel);
223 bw.Write(m_rgAnnotations.Count);
224
225 for (int i = 0; i < m_rgAnnotations.Count; i++)
226 {
227 m_rgAnnotations[i].Save(bw);
228 }
229 }
230
236 public static AnnotationGroup Load(BinaryReader br)
237 {
238 int nGroupLabel = br.ReadInt32();
239 int nCount = br.ReadInt32();
240 List<Annotation> rgAnnotations = new List<Annotation>();
241
242 for (int i = 0; i < nCount; i++)
243 {
244 rgAnnotations.Add(Annotation.Load(br));
245 }
246
247 return new AnnotationGroup(rgAnnotations, nGroupLabel);
248 }
249 }
250
254 [Serializable]
255 public class AnnotationGroupCollection : IEnumerable<AnnotationGroup>
256 {
257 List<AnnotationGroup> m_rgItems = new List<AnnotationGroup>();
258 Dictionary<int, string> m_rgLabels = new Dictionary<int, string>();
259 int m_nImageID = 0;
260 int m_nImageIdx = 0;
261 int m_nCreatorID = 0;
262 int m_nDatasetID = 0;
263 int m_nSourceID = 0;
264 bool m_bHasDataCriteria = false;
265 bool m_bHasDebugData = false;
266
271 {
272 }
273
277 public bool HasDataCriteria
278 {
279 get { return m_bHasDataCriteria; }
280 set { m_bHasDataCriteria = value; }
281 }
282
286 public bool HasDebugData
287 {
288 get { return m_bHasDebugData; }
289 set { m_bHasDebugData = value; }
290 }
291
295 public int ImageID
296 {
297 get { return m_nImageID; }
298 set { m_nImageID = value; }
299 }
300
304 public int ImageIdx
305 {
306 get { return m_nImageIdx; }
307 set { m_nImageIdx = value; }
308 }
309
313 public int CreatorID
314 {
315 get { return m_nCreatorID; }
316 set { m_nCreatorID = value; }
317 }
318
322 public int DatasetID
323 {
324 get { return m_nDatasetID; }
325 set { m_nDatasetID = value; }
326 }
327
331 public int SourceID
332 {
333 get { return m_nSourceID; }
334 set { m_nSourceID = value; }
335 }
336
340 public Dictionary<int, string> Labels
341 {
342 get { return m_rgLabels; }
343 set { m_rgLabels = value; }
344 }
345
349 public int Count
350 {
351 get { return m_rgItems.Count; }
352 }
353
359 public AnnotationGroup this[int nIdx]
360 {
361 get { return m_rgItems[nIdx]; }
362 set { m_rgItems[nIdx] = value; }
363 }
364
370 {
371 foreach (AnnotationGroup g in col)
372 {
373 m_rgItems.Add(g);
374 }
375 }
376
381 public void Add(AnnotationGroup g)
382 {
383 m_rgItems.Add(g);
384 }
385
391 public bool Remove(AnnotationGroup g)
392 {
393 return m_rgItems.Remove(g);
394 }
395
400 public void RemoveAt(int nIdx)
401 {
402 m_rgItems.RemoveAt(nIdx);
403 }
404
408 public void Clear()
409 {
410 m_rgItems.Clear();
411 }
412
417 public IEnumerator<AnnotationGroup> GetEnumerator()
418 {
419 return m_rgItems.GetEnumerator();
420 }
421
426 IEnumerator IEnumerable.GetEnumerator()
427 {
428 return m_rgItems.GetEnumerator();
429 }
430
436 {
437 Annotation bestAnnotation = null;
438 float fMaxScore = 0;
439
440 foreach (AnnotationGroup g in m_rgItems)
441 {
443
444 if (a != null && a.bbox.score >= fMaxScore)
445 {
446 fMaxScore = a.bbox.score;
447 bestAnnotation = a;
448 }
449 }
450
451 return bestAnnotation;
452 }
453
459 public AnnotationGroup Find(int nLabel)
460 {
461 foreach (AnnotationGroup g in m_rgItems)
462 {
463 if (g.group_label == nLabel)
464 return g;
465 }
466
467 return null;
468 }
469
475 {
477
478 foreach (AnnotationGroup g in m_rgItems)
479 {
480 col.Add(g.Clone());
481 }
482
483 foreach (KeyValuePair<int, string> kv in m_rgLabels)
484 {
485 col.m_rgLabels.Add(kv.Key, kv.Value);
486 }
487
488 col.ImageID = m_nImageID;
489 col.ImageIdx = m_nImageIdx;
490 col.CreatorID = m_nCreatorID;
491 col.DatasetID = m_nDatasetID;
492 col.SourceID = m_nSourceID;
493 col.HasDataCriteria = m_bHasDataCriteria;
494 col.HasDebugData = m_bHasDebugData;
495
496 return col;
497 }
498
503 public Tuple<int, int> GetMinMaxLabels()
504 {
505 int nMin = int.MaxValue;
506 int nMax = -int.MaxValue;
507
508 for (int i = 0; i < m_rgItems.Count; i++)
509 {
510 nMin = Math.Min(nMin, m_rgItems[i].group_label);
511 nMax = Math.Max(nMax, m_rgItems[i].group_label);
512 }
513
514 return new Tuple<int, int>(nMin, nMax);
515 }
516
521 public void Normalize(Rectangle rc)
522 {
523 foreach (AnnotationGroup g in m_rgItems)
524 {
525 g.Normalize(rc);
526 }
527 }
528
534 public static void SaveList(BinaryWriter bw, AnnotationGroupCollection rg)
535 {
536 bw.Write(rg.Count);
537
538 foreach (AnnotationGroup g in rg)
539 {
540 g.Save(bw);
541 }
542
543 bw.Write(rg.ImageID);
544 bw.Write(rg.ImageIdx);
545 bw.Write(rg.CreatorID);
546 bw.Write(rg.DatasetID);
547 bw.Write(rg.SourceID);
548 bw.Write(rg.HasDataCriteria);
549 bw.Write(rg.HasDebugData);
550 }
551
557 public static void SaveLabels(BinaryWriter bw, AnnotationGroupCollection rg)
558 {
559 bw.Write(rg.Labels.Count);
560 foreach (KeyValuePair<int, string> kv in rg.Labels)
561 {
562 bw.Write(kv.Key);
563 bw.Write(kv.Value);
564 }
565 }
566
572 public static AnnotationGroupCollection LoadList(BinaryReader br)
573 {
575 int nCount = br.ReadInt32();
576
577 for (int i = 0; i < nCount; i++)
578 {
579 rg.Add(AnnotationGroup.Load(br));
580 }
581
582 rg.ImageID = br.ReadInt32();
583 rg.ImageIdx = br.ReadInt32();
584 rg.CreatorID = br.ReadInt32();
585 rg.DatasetID = br.ReadInt32();
586 rg.SourceID = br.ReadInt32();
587 rg.HasDataCriteria = br.ReadBoolean();
588 rg.HasDebugData = br.ReadBoolean();
589
590 return rg;
591 }
592
598 public static Dictionary<int, string> LoadLabels(BinaryReader br)
599 {
600 Dictionary<int, string> rg = new Dictionary<int, string>();
601
602 int nCount = br.ReadInt32();
603 for (int i = 0; i < nCount; i++)
604 {
605 int nKey = br.ReadInt32();
606 string strVal = br.ReadString();
607
608 rg.Add(nKey, strVal);
609 }
610
611 return rg;
612 }
613
620 public static byte[] ToByteArray(AnnotationGroupCollection rg, bool bIncludeLabels = false)
621 {
622 using (MemoryStream ms = new MemoryStream())
623 using (BinaryWriter bw = new BinaryWriter(ms))
624 {
625 SaveList(bw, rg);
626
627 bw.Write(bIncludeLabels);
628 if (bIncludeLabels)
629 SaveLabels(bw, rg);
630
631 bw.Write(rg.ImageID);
632 bw.Write(rg.ImageIdx);
633 bw.Write(rg.CreatorID);
634 bw.Write(rg.DatasetID);
635 bw.Write(rg.SourceID);
636 bw.Write(rg.HasDataCriteria);
637 bw.Write(rg.HasDebugData);
638
639 ms.Flush();
640 return ms.ToArray();
641 }
642 }
643
650 {
651 using (MemoryStream ms = new MemoryStream(rg))
652 using (BinaryReader br = new BinaryReader(ms))
653 {
655
656 if (br.ReadBoolean())
657 col.Labels = LoadLabels(br);
658
659 col.ImageID = br.ReadInt32();
660 col.ImageIdx = br.ReadInt32();
661 col.CreatorID = br.ReadInt32();
662 col.DatasetID = br.ReadInt32();
663 col.SourceID = br.ReadInt32();
664 col.HasDataCriteria = br.ReadBoolean();
665 col.HasDebugData = br.ReadBoolean();
666
667 return col;
668 }
669 }
670 }
671}
Defines a collection of AnnotationGroups.
Definition: Annotation.cs:256
Dictionary< int, string > Labels
Get/set the label name mappings.
Definition: Annotation.cs:341
IEnumerator< AnnotationGroup > GetEnumerator()
Returns the enumerator for the collection.
Definition: Annotation.cs:417
static void SaveList(BinaryWriter bw, AnnotationGroupCollection rg)
Save an AnnotationGroupCollection to a binary writer.
Definition: Annotation.cs:534
int SourceID
Specifies the Data Source ID.
Definition: Annotation.cs:332
void Add(AnnotationGroupCollection col)
Add another AnnotationGroupCollection to this one.
Definition: Annotation.cs:369
int ImageIdx
Specifies the Image Index.
Definition: Annotation.cs:305
int ImageID
Specifies the ImageID.
Definition: Annotation.cs:296
bool HasDataCriteria
Get/set whether or not the image has a data criteria associated with it.
Definition: Annotation.cs:278
void Clear()
Clear all items from the collection.
Definition: Annotation.cs:408
static void SaveLabels(BinaryWriter bw, AnnotationGroupCollection rg)
Save the labels to a binary writer.
Definition: Annotation.cs:557
static AnnotationGroupCollection FromByteArray(byte[] rg)
Returns an AnnotationGroupCollection from a byte array.
Definition: Annotation.cs:649
int Count
Specifies the number of items in the collection.
Definition: Annotation.cs:350
Annotation GetMaxScoringAnnotation()
Returns the maximum scoring annotation within a collection of groups.
Definition: Annotation.cs:435
void RemoveAt(int nIdx)
Remove an item at a given index.
Definition: Annotation.cs:400
AnnotationGroup Find(int nLabel)
Find the annotation group with the given label.
Definition: Annotation.cs:459
bool HasDebugData
Get/set whether or not the image has debug data associated with it.
Definition: Annotation.cs:287
static Dictionary< int, string > LoadLabels(BinaryReader br)
Load the labels from a binary reader.
Definition: Annotation.cs:598
static byte[] ToByteArray(AnnotationGroupCollection rg, bool bIncludeLabels=false)
Saves a AnnotationGroupCollection to a byte array.
Definition: Annotation.cs:620
AnnotationGroupCollection Clone()
Return a copy of the collection.
Definition: Annotation.cs:474
static AnnotationGroupCollection LoadList(BinaryReader br)
Load an AnnotationGroupCollection from a binary stream.
Definition: Annotation.cs:572
int CreatorID
Specifies the Dataset Creator ID.
Definition: Annotation.cs:314
void Normalize(Rectangle rc)
Normalize all annotations to the given rectangle.
Definition: Annotation.cs:521
bool Remove(AnnotationGroup g)
Remove an AnnotationGroup from the collection if it exists.
Definition: Annotation.cs:391
int DatasetID
Specifies the Dataset ID.
Definition: Annotation.cs:323
Tuple< int, int > GetMinMaxLabels()
Return the min/max labels found int the collection.
Definition: Annotation.cs:503
void Add(AnnotationGroup g)
Add a new AnnotationGroup to the collection.
Definition: Annotation.cs:381
The AnnoationGroup class manages a group of annotations.
Definition: Annotation.cs:124
Annotation GetMaxScoringAnnotation()
Returns the maximum scoring annotation within a group.
Definition: Annotation.cs:166
AnnotationGroup Clone()
Create a copy of the annotation group.
Definition: Annotation.cs:145
int group_label
Get/set the group label.
Definition: Annotation.cs:211
void Normalize(Rectangle rc)
Normalize all annotations to the given rectangle.
Definition: Annotation.cs:190
AnnotationGroup(List< Annotation > rgAnnotations=null, int nGroupLabel=0)
The constructor.
Definition: Annotation.cs:133
static AnnotationGroup Load(BinaryReader br)
Load an annotation group using the binary reader.
Definition: Annotation.cs:236
List< Annotation > annotations
Get/set the group annoations.
Definition: Annotation.cs:202
void Save(BinaryWriter bw)
Save the annotation group to the binary writer.
Definition: Annotation.cs:220
The Annotation class is used by annotations attached to SimpleDatum's and used in SSD.
Definition: Annotation.cs:22
Annotation Clone()
Returns a copy of the Annotation.
Definition: Annotation.cs:41
Annotation(NormalizedBBox bbox, int nInstanceId=0)
The constructor.
Definition: Annotation.cs:31
void Normalize(Rectangle rc)
Normalize all annotations to the given rectangle.
Definition: Annotation.cs:73
int instance_id
Get/set the instance ID.
Definition: Annotation.cs:55
void Save(BinaryWriter bw)
Save the annotation data using the binary writer.
Definition: Annotation.cs:99
static Annotation Load(BinaryReader br)
Load the annotation using a binary reader.
Definition: Annotation.cs:110
NormalizedBBox bbox
Get/set the bounding box.
Definition: Annotation.cs:64
The NormalizedBBox manages a bounding box used in SSD.
float ymax
Get/set the y maximum.
float xmax
Get/set the x maximum.
NormalizedBBox Clone()
Return a copy of the object.
float xmin
Get/set the x minimum.
static NormalizedBBox Load(BinaryReader br)
Load and return a new NormalizedBbox.
float ymin
Get/set the y minimum.
void Set(float fxmin, float fymin, float fxmax, float fymax, int? nLabel=null, bool? bDifficult=null, float? fScore=null, float? fSize=null)
Set the values of the NormalizedBbox.
void Save(BinaryWriter bw)
Save the NormalizedBbox using the binary writer.
float score
Get/set the score.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12