-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.go
More file actions
114 lines (103 loc) · 2.04 KB
/
util.go
File metadata and controls
114 lines (103 loc) · 2.04 KB
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
package openpose
import (
"encoding/json"
"math"
"os"
"gorgonia.org/tensor"
)
// RoundInt returns int round
func RoundInt(v float64) int {
return int(math.Round(v))
}
// rollAxis returns np.rollaxis
func rollAxis(values [][][]float32, axis int, start int) [][][]float32 {
d1 := len(values)
d2 := len(values[0])
d3 := len(values[0][0])
flat := make([]float32, 0, d1*d2*d3)
for _, v1 := range values {
for _, v2 := range v1 {
for _, v := range v2 {
flat = append(flat, v)
}
}
}
d := tensor.New(tensor.WithShape(d1, d2, d3), tensor.WithBacking(flat))
dout, _ := d.RollAxis(axis, start, true)
shape := dout.Shape()
ret := make([][][]float32, 0, shape[0])
var i int
for i < shape[0] {
var y int
yv := make([][]float32, 0, shape[1])
for y < shape[1] {
var x int
xv := make([]float32, 0, shape[2])
for x < shape[2] {
v, _ := dout.At(i, y, x)
xv = append(xv, v.(float32))
x++
}
yv = append(yv, xv)
y++
}
ret = append(ret, yv)
i++
}
return ret
}
// matAverage returns np.average
func matAverage(mat [][][]float32) float32 {
var (
total float32
count float32
)
for _, v1 := range mat {
for _, v2 := range v1 {
for _, v := range v2 {
total += v
count++
}
}
}
return total / count
}
func maxiumFilter(mat [][]float32, threshold float32) [2][]int {
var ret [2][]int
for y, rows := range mat {
for x, v := range rows {
if v <= threshold {
continue
}
ret[0] = append(ret[0], y)
ret[1] = append(ret[1], x)
}
if len(ret[0]) == 0 {
ret[0] = make([]int, 0)
}
if len(ret[1]) == 0 {
ret[1] = make([]int, 0)
}
}
return ret
}
func scales(h, w float64, factor, minSize float64) []float64 {
minl := h
if minl > w {
minl = w
}
m := 12.0 / minSize
minl = minl * m
var scales []float64
for count := 0; minl > 12.0; {
scales = append(scales, m*math.Pow(factor, float64(count)))
minl = minl * factor
count++
}
return scales
}
func dump(obj interface{}, filePath string) {
fn, _ := os.Create(filePath)
defer fn.Close()
json.NewEncoder(fn).Encode(obj)
}