mirror of
https://github.com/LdDl/go-darknet.git
synced 2025-10-05 15:46:49 +08:00
add network
This commit is contained in:
10
detection.go
Normal file
10
detection.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package darknet
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// DetectionResult represents the inference results from the network.
|
||||||
|
type DetectionResult struct {
|
||||||
|
// Detections []*Detection
|
||||||
|
NetworkOnlyTimeTaken time.Duration
|
||||||
|
OverallTimeTaken time.Duration
|
||||||
|
}
|
@@ -55,19 +55,18 @@ func main() {
|
|||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
imgFalot32, err := darknet.Image2Float32(src)
|
imgDarknet, err := darknet.Image2Float32(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
_ = imgFalot32
|
defer imgDarknet.Close()
|
||||||
// defer img.Close()
|
|
||||||
|
|
||||||
// dr, err := n.Detect(img)
|
|
||||||
// if err != nil {
|
|
||||||
// printError(err)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
dr, err := n.Detect(&imgDarknet)
|
||||||
|
if err != nil {
|
||||||
|
printError(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = dr
|
||||||
// log.Println("Network-only time taken:", dr.NetworkOnlyTimeTaken)
|
// log.Println("Network-only time taken:", dr.NetworkOnlyTimeTaken)
|
||||||
// log.Println("Overall time taken:", dr.OverallTimeTaken, len(dr.Detections))
|
// log.Println("Overall time taken:", dr.OverallTimeTaken, len(dr.Detections))
|
||||||
// for _, d := range dr.Detections {
|
// for _, d := range dr.Detections {
|
||||||
|
35
image.go
35
image.go
@@ -1,25 +1,32 @@
|
|||||||
package darknet
|
package darknet
|
||||||
|
|
||||||
// #include <darknet.h>
|
// #include <stdlib.h>
|
||||||
|
// #include "image.h"
|
||||||
|
import "C"
|
||||||
import (
|
import (
|
||||||
"C"
|
|
||||||
"image"
|
"image"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DarknetImage represents the image buffer.
|
// DarknetImage represents the image buffer.
|
||||||
// type DarknetImage struct {
|
type DarknetImage struct {
|
||||||
// Width int
|
Width int
|
||||||
// Height int
|
Height int
|
||||||
// image C.image
|
image C.image
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
// Close and release resources.
|
||||||
|
func (img *DarknetImage) Close() error {
|
||||||
|
C.free_image(img.image)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func float_p(arr []float32) *C.float {
|
func float_p(arr []float32) *C.float {
|
||||||
return (*C.float)(unsafe.Pointer(&arr[0]))
|
return (*C.float)(unsafe.Pointer(&arr[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Image2Float32 Returns []float32 representation of image.Image
|
// Image2Float32 Returns []float32 representation of image.Image
|
||||||
func Image2Float32(img image.Image) ([]float32, error) {
|
func Image2Float32(img image.Image) (DarknetImage, error) {
|
||||||
width := img.Bounds().Dx()
|
width := img.Bounds().Dx()
|
||||||
height := img.Bounds().Dy()
|
height := img.Bounds().Dy()
|
||||||
imgwh := width * height
|
imgwh := width * height
|
||||||
@@ -36,5 +43,15 @@ func Image2Float32(img image.Image) ([]float32, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ans, nil
|
imgDarknet := DarknetImage{
|
||||||
|
Width: width,
|
||||||
|
Height: height,
|
||||||
|
image: C.new_darknet_image(),
|
||||||
|
}
|
||||||
|
|
||||||
|
imgDarknet.image = C.prepare_image(imgDarknet.image, C.int(width), C.int(height), 3)
|
||||||
|
imgDarknet.image.data = float_p(ans)
|
||||||
|
// imgDarknet.image = C.resize_image(imgDarknet.image, 416, 416) // Do we need resize? (detection function actually does it)
|
||||||
|
|
||||||
|
return imgDarknet, nil
|
||||||
}
|
}
|
||||||
|
30
network.c
30
network.c
@@ -1,22 +1,22 @@
|
|||||||
// #include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
// #include <darknet.h>
|
#include <darknet.h>
|
||||||
|
|
||||||
// #include "network.h"
|
#include "network.h"
|
||||||
|
|
||||||
// int get_network_layer_classes(network *n, int index) {
|
// int get_network_layer_classes(network *n, int index) {
|
||||||
// return n->layers[index].classes;
|
// return n->layers[index].classes;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// struct network_box_result perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms) {
|
struct network_box_result perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms) {
|
||||||
// image sized = letterbox_image(*img, n->w, n->h);
|
image sized = letterbox_image(*img, n->w, n->h);
|
||||||
// struct network_box_result result = { NULL };
|
struct network_box_result result = { NULL };
|
||||||
// float *X = sized.data;
|
float *X = sized.data;
|
||||||
// network_predict(n, X);
|
network_predict(*n, X);
|
||||||
// result.detections = get_network_boxes(n, img->w, img->h,thresh, hier_thresh, 0, 1, &result.detections_len);
|
result.detections = get_network_boxes(n, img->w, img->h,thresh, hier_thresh, 0, 1, &result.detections_len, 0);
|
||||||
// if (nms) {
|
if (nms) {
|
||||||
// do_nms_sort(result.detections, result.detections_len, classes, nms);
|
do_nms_sort(result.detections, result.detections_len, classes, nms);
|
||||||
// }
|
}
|
||||||
// free_image(sized);
|
free_image(sized);
|
||||||
// return result;
|
return result;
|
||||||
// }
|
}
|
||||||
|
31
network.go
31
network.go
@@ -8,6 +8,7 @@ package darknet
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -77,27 +78,27 @@ func (n *YOLONetwork) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detect specified image.
|
// Detect specified image.
|
||||||
// func (n *YOLONetwork) Detect(img *Image) (*DetectionResult, error) {
|
func (n *YOLONetwork) Detect(img *DarknetImage) (*DetectionResult, error) {
|
||||||
// if n.cNet == nil {
|
if n.cNet == nil {
|
||||||
// return nil, errNetworkNotInit
|
return nil, errNetworkNotInit
|
||||||
// }
|
}
|
||||||
|
|
||||||
// startTime := time.Now()
|
startTime := time.Now()
|
||||||
// result := C.perform_network_detect(n.cNet, &img.image, C.int(n.Classes),
|
C.network_predict_image(n.cNet, img.image)
|
||||||
// C.float(n.Threshold), C.float(n.hierarchalThreshold), C.float(n.nms))
|
// result := C.perform_network_detect(n.cNet, &img.image, C.int(n.Classes), C.float(n.Threshold), C.float(n.hierarchalThreshold), C.float(n.nms))
|
||||||
// endTime := time.Now()
|
endTime := time.Now()
|
||||||
// defer C.free_detections(result.detections, result.detections_len)
|
// defer C.free_detections(result.detections, result.detections_len)
|
||||||
|
|
||||||
// ds := makeDetections(img, result.detections, int(result.detections_len),
|
// ds := makeDetections(img, result.detections, int(result.detections_len),
|
||||||
// n.Threshold, n.Classes, n.ClassNames)
|
// n.Threshold, n.Classes, n.ClassNames)
|
||||||
|
|
||||||
// endTimeOverall := time.Now()
|
endTimeOverall := time.Now()
|
||||||
|
|
||||||
// out := DetectionResult{
|
out := DetectionResult{
|
||||||
// Detections: ds,
|
// Detections: ds,
|
||||||
// NetworkOnlyTimeTaken: endTime.Sub(startTime),
|
NetworkOnlyTimeTaken: endTime.Sub(startTime),
|
||||||
// OverallTimeTaken: endTimeOverall.Sub(startTime),
|
OverallTimeTaken: endTimeOverall.Sub(startTime),
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return &out, nil
|
return &out, nil
|
||||||
// }
|
}
|
||||||
|
Reference in New Issue
Block a user