Using goroutines ans channels for object detection

This commit is contained in:
Endre Simo
2019-02-05 07:43:24 +02:00
parent 6500303155
commit ec271ca321

View File

@@ -23,7 +23,8 @@ func main() {}
//export FindFaces
func FindFaces(pixels []uint8) uintptr {
if len(pixels) > 0 {
pointCh := make(chan uintptr)
dets := clusterDetection(pixels, 480, 640)
result := make([][]int, len(dets))
@@ -32,28 +33,30 @@ func FindFaces(pixels []uint8) uintptr {
result[i] = append(result[i], dets[i].Row, dets[i].Col, dets[i].Scale)
}
}
fmt.Println(dets)
if len(result) > 0 {
// Since we cannot transfer a 2d array trough an array pointer
// we have to transform it into 1d array.
go func() {
det := make([]int, 0, len(result))
for _, v := range result {
det = append(det, v...)
}
fmt.Println(det)
// Include as a first slice element the number of detected faces.
// We need to transfer this value in order to define the Python array buffer length.
det = append([]int{len(result), 0, 0}, det...)
fmt.Println(det)
// Convert the slice into an array pointer.
s := *(*[]int)(unsafe.Pointer(&det))
p := uintptr(unsafe.Pointer(&s[0]))
runtime.KeepAlive(result)
// Ensure `det` is not freed by GC.
runtime.KeepAlive(det)
// return the pointer address
return p
}
pointCh <- p
}()
return <-pointCh
}
return 0
}