mirror of
https://github.com/LdDl/go-darknet.git
synced 2025-09-27 03:56:18 +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())
|
||||
}
|
||||
|
||||
imgFalot32, err := darknet.Image2Float32(src)
|
||||
imgDarknet, err := darknet.Image2Float32(src)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
_ = imgFalot32
|
||||
// defer img.Close()
|
||||
|
||||
// dr, err := n.Detect(img)
|
||||
// if err != nil {
|
||||
// printError(err)
|
||||
// return
|
||||
// }
|
||||
defer imgDarknet.Close()
|
||||
|
||||
dr, err := n.Detect(&imgDarknet)
|
||||
if err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
_ = dr
|
||||
// log.Println("Network-only time taken:", dr.NetworkOnlyTimeTaken)
|
||||
// log.Println("Overall time taken:", dr.OverallTimeTaken, len(dr.Detections))
|
||||
// for _, d := range dr.Detections {
|
||||
|
35
image.go
35
image.go
@@ -1,25 +1,32 @@
|
||||
package darknet
|
||||
|
||||
// #include <darknet.h>
|
||||
// #include <stdlib.h>
|
||||
// #include "image.h"
|
||||
import "C"
|
||||
import (
|
||||
"C"
|
||||
"image"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// DarknetImage represents the image buffer.
|
||||
// type DarknetImage struct {
|
||||
// Width int
|
||||
// Height int
|
||||
// image C.image
|
||||
// }
|
||||
type DarknetImage struct {
|
||||
Width int
|
||||
Height int
|
||||
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 {
|
||||
return (*C.float)(unsafe.Pointer(&arr[0]))
|
||||
}
|
||||
|
||||
// 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()
|
||||
height := img.Bounds().Dy()
|
||||
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) {
|
||||
// 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) {
|
||||
// image sized = letterbox_image(*img, n->w, n->h);
|
||||
// struct network_box_result result = { NULL };
|
||||
// float *X = sized.data;
|
||||
// network_predict(n, X);
|
||||
// result.detections = get_network_boxes(n, img->w, img->h,thresh, hier_thresh, 0, 1, &result.detections_len);
|
||||
// if (nms) {
|
||||
// do_nms_sort(result.detections, result.detections_len, classes, nms);
|
||||
// }
|
||||
// free_image(sized);
|
||||
// return result;
|
||||
// }
|
||||
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);
|
||||
struct network_box_result result = { NULL };
|
||||
float *X = sized.data;
|
||||
network_predict(*n, X);
|
||||
result.detections = get_network_boxes(n, img->w, img->h,thresh, hier_thresh, 0, 1, &result.detections_len, 0);
|
||||
if (nms) {
|
||||
do_nms_sort(result.detections, result.detections_len, classes, nms);
|
||||
}
|
||||
free_image(sized);
|
||||
return result;
|
||||
}
|
||||
|
39
network.go
39
network.go
@@ -8,6 +8,7 @@ package darknet
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@@ -77,27 +78,27 @@ func (n *YOLONetwork) Close() error {
|
||||
}
|
||||
|
||||
// Detect specified image.
|
||||
// func (n *YOLONetwork) Detect(img *Image) (*DetectionResult, error) {
|
||||
// if n.cNet == nil {
|
||||
// return nil, errNetworkNotInit
|
||||
// }
|
||||
func (n *YOLONetwork) Detect(img *DarknetImage) (*DetectionResult, error) {
|
||||
if n.cNet == nil {
|
||||
return nil, errNetworkNotInit
|
||||
}
|
||||
|
||||
// startTime := time.Now()
|
||||
// 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()
|
||||
// defer C.free_detections(result.detections, result.detections_len)
|
||||
startTime := time.Now()
|
||||
C.network_predict_image(n.cNet, img.image)
|
||||
// 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()
|
||||
// defer C.free_detections(result.detections, result.detections_len)
|
||||
|
||||
// ds := makeDetections(img, result.detections, int(result.detections_len),
|
||||
// n.Threshold, n.Classes, n.ClassNames)
|
||||
// ds := makeDetections(img, result.detections, int(result.detections_len),
|
||||
// n.Threshold, n.Classes, n.ClassNames)
|
||||
|
||||
// endTimeOverall := time.Now()
|
||||
endTimeOverall := time.Now()
|
||||
|
||||
// out := DetectionResult{
|
||||
// Detections: ds,
|
||||
// NetworkOnlyTimeTaken: endTime.Sub(startTime),
|
||||
// OverallTimeTaken: endTimeOverall.Sub(startTime),
|
||||
// }
|
||||
out := DetectionResult{
|
||||
// Detections: ds,
|
||||
NetworkOnlyTimeTaken: endTime.Sub(startTime),
|
||||
OverallTimeTaken: endTimeOverall.Sub(startTime),
|
||||
}
|
||||
|
||||
// return &out, nil
|
||||
// }
|
||||
return &out, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user