-
Notifications
You must be signed in to change notification settings - Fork 20
/
detection.go
76 lines (69 loc) · 1.94 KB
/
detection.go
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
package darknet
// #include <darknet.h>
//
// #include "detection.h"
import "C"
import (
"image"
"time"
)
// Detection represents a detection.
type Detection struct {
BoundingBox
ClassIDs []int
ClassNames []string
Probabilities []float32
}
// DetectionResult represents the inference results from the network.
type DetectionResult struct {
Detections []*Detection
NetworkOnlyTimeTaken time.Duration
OverallTimeTaken time.Duration
}
func makeDetection(img *DarknetImage, det *C.detection, threshold float32, classes int, classNames []string) *Detection {
if det == nil {
return &Detection{}
}
dClassIDs := make([]int, 0)
dClassNames := make([]string, 0)
dProbs := make([]float32, 0)
for i := 0; i < int(classes); i++ {
dProb := float32(C.get_detection_probability(det, C.int(i), C.int(classes)))
if dProb > threshold {
dClassIDs = append(dClassIDs, i)
cN := classNames[i]
dClassNames = append(dClassNames, cN)
dProbs = append(dProbs, dProb*100)
}
}
fImgW := C.float(img.Width)
fImgH := C.float(img.Height)
halfRatioW := det.bbox.w / 2.0
halfRatioH := det.bbox.h / 2.0
out := Detection{
BoundingBox: BoundingBox{
StartPoint: image.Point{
X: int((det.bbox.x - halfRatioW) * fImgW),
Y: int((det.bbox.y - halfRatioH) * fImgH),
},
EndPoint: image.Point{
X: int((det.bbox.x + halfRatioW) * fImgW),
Y: int((det.bbox.y + halfRatioH) * fImgH),
},
},
ClassIDs: dClassIDs,
ClassNames: dClassNames,
Probabilities: dProbs,
}
return &out
}
func makeDetections(img *DarknetImage, detections *C.detection, detectionsLength int, threshold float32, classes int, classNames []string) []*Detection {
// Make list of detection objects.
ds := make([]*Detection, detectionsLength)
for i := 0; i < int(detectionsLength); i++ {
det := C.get_detection(detections, C.int(i), C.int(detectionsLength))
d := makeDetection(img, det, threshold, classes, classNames)
ds[i] = d
}
return ds
}