mirror of
https://github.com/LdDl/go-darknet.git
synced 2025-09-26 19:51:27 +08:00
need to check what happens to im.data
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -9,4 +9,7 @@ example/yolov3-416.weights
|
||||
example/yolov3.cfg
|
||||
example/yolov3.weights
|
||||
darknet.h
|
||||
*.so
|
||||
*.so
|
||||
predictions.png
|
||||
predictions.jpg
|
||||
main
|
@@ -1,6 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
char * get_class_name(char **names, int index, int names_len) {
|
||||
char *get_class_name(char **names, int index, int names_len) {
|
||||
if (index >= names_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
@@ -1,3 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
extern char * get_class_name(char **names, int index, int names_len);
|
||||
extern char *get_class_name(char **names, int index, int names_len);
|
@@ -3,20 +3,16 @@
|
||||
#include "detection.h"
|
||||
|
||||
|
||||
detection * get_detection(detection *dets, int index, int dets_len)
|
||||
{
|
||||
detection *get_detection(detection *dets, int index, int dets_len) {
|
||||
if (index >= dets_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return dets + index;
|
||||
}
|
||||
|
||||
float get_detection_probability(detection *det, int index, int prob_len)
|
||||
{
|
||||
float get_detection_probability(detection *det, int index, int prob_len) {
|
||||
if (index >= prob_len) {
|
||||
return .0;
|
||||
}
|
||||
|
||||
return det->prob[index];
|
||||
}
|
@@ -26,15 +26,12 @@ type DetectionResult struct {
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -44,7 +41,6 @@ func makeDetection(img *DarknetImage, det *C.detection, threshold float32, class
|
||||
dProbs = append(dProbs, dProb*100)
|
||||
}
|
||||
}
|
||||
|
||||
fImgW := C.float(img.Width)
|
||||
fImgH := C.float(img.Height)
|
||||
halfRatioW := det.bbox.w / 2.0
|
||||
@@ -65,7 +61,6 @@ func makeDetection(img *DarknetImage, det *C.detection, threshold float32, class
|
||||
ClassNames: dClassNames,
|
||||
Probabilities: dProbs,
|
||||
}
|
||||
|
||||
return &out
|
||||
}
|
||||
|
||||
|
@@ -3,5 +3,5 @@
|
||||
#include <darknet.h>
|
||||
|
||||
|
||||
extern detection * get_detection(detection *dets, int index, int dets_len);
|
||||
extern detection *get_detection(detection *dets, int index, int dets_len);
|
||||
extern float get_detection_probability(detection *det, int index, int prob_len);
|
||||
|
@@ -55,7 +55,6 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
_ = src
|
||||
|
||||
imgDarknet, err := darknet.Image2Float32(src)
|
||||
if err != nil {
|
||||
@@ -63,12 +62,11 @@ func main() {
|
||||
}
|
||||
defer imgDarknet.Close()
|
||||
|
||||
dr, err := n.Detect(&imgDarknet)
|
||||
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))
|
||||
|
24
image.c
24
image.c
@@ -1,22 +1,12 @@
|
||||
#include <darknet.h>
|
||||
|
||||
image new_darknet_image() {
|
||||
image img;
|
||||
img.w = 0;
|
||||
img.h = 0;
|
||||
img.c = 0;
|
||||
img.data = NULL;
|
||||
return img;
|
||||
void fill_image_f32(image* im, int w, int h, int c, float* data) {
|
||||
int i;
|
||||
for (i = 0; i < w*h*c; i++) {
|
||||
im->data[i] = data[i];
|
||||
}
|
||||
}
|
||||
|
||||
image prepare_image(image img, int w, int h, int c){
|
||||
img.w=w;
|
||||
img.h=h;
|
||||
img.c=c;
|
||||
return img;
|
||||
}
|
||||
|
||||
image new_image(int w, int h, int c){
|
||||
image out = make_image(w, h, c);
|
||||
return out;
|
||||
void set_data_f32_val(float* data, int index, float value) {
|
||||
data[index] = value;
|
||||
}
|
||||
|
34
image.go
34
image.go
@@ -26,34 +26,36 @@ func float_p(arr []float32) *C.float {
|
||||
}
|
||||
|
||||
// Image2Float32 Returns []float32 representation of image.Image
|
||||
func Image2Float32(img image.Image) (DarknetImage, error) {
|
||||
func Image2Float32(img image.Image) (*DarknetImage, error) {
|
||||
width := img.Bounds().Dx()
|
||||
height := img.Bounds().Dy()
|
||||
imgwh := width * height
|
||||
imgSize := imgwh * 3
|
||||
|
||||
ans := make([]float32, imgSize)
|
||||
ans := []float32{}
|
||||
red := []float32{}
|
||||
green := []float32{}
|
||||
blue := []float32{}
|
||||
for x := 0; x < width; x++ {
|
||||
for y := 0; y < height; y++ {
|
||||
r, g, b, _ := img.At(y, x).RGBA()
|
||||
rpix, gpix, bpix := float32(r>>8)/float32(255.0), float32(g>>8)/float32(255.0), float32(b>>8)/float32(255.0)
|
||||
ans[y+x*height] = rpix
|
||||
ans[y+x*height+imgwh] = gpix
|
||||
ans[y+x*height+imgwh+imgwh] = bpix
|
||||
red = append(red, rpix)
|
||||
green = append(green, gpix)
|
||||
blue = append(blue, bpix)
|
||||
}
|
||||
}
|
||||
ans = append(ans, red...)
|
||||
ans = append(ans, green...)
|
||||
ans = append(ans, blue...)
|
||||
|
||||
imgDarknet := DarknetImage{
|
||||
imgDarknet := &DarknetImage{
|
||||
Width: width,
|
||||
Height: height,
|
||||
// image: C.new_darknet_image(),
|
||||
image: C.make_image(C.int(width), C.int(height), 3),
|
||||
}
|
||||
// for i := range ans {
|
||||
// C.set_data_f32_val(imgDarknet.image.data, C.int(i), C.float(ans[i]))
|
||||
// }
|
||||
C.fill_image_f32(&imgDarknet.image, C.int(width), C.int(height), 3, float_p(ans))
|
||||
|
||||
// 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)
|
||||
|
||||
imgDarknet.image = C.load_image_color(C.CString("/home/dimitrii/Downloads/mega.jpg"), 416, 416)
|
||||
|
||||
// imgDarknet.image = C.load_image_color(C.CString("~/Downloads/mega.jpg"), 416, 416)
|
||||
return imgDarknet, nil
|
||||
}
|
||||
|
5
image.h
5
image.h
@@ -2,6 +2,5 @@
|
||||
|
||||
#include <darknet.h>
|
||||
|
||||
extern image new_darknet_image();
|
||||
extern image prepare_image(image img, int w, int h, int c);
|
||||
extern image new_image(int w, int h, int c);
|
||||
extern void fill_image_f32(image *im, int w, int h, int c, float* data);
|
||||
extern void set_data_f32_val(float* data, int index, float value);
|
16
network.c
16
network.c
@@ -6,13 +6,23 @@
|
||||
|
||||
#include "detection.h"
|
||||
|
||||
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 perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms, int letter_box) {
|
||||
image sized;
|
||||
int ww = img->w;
|
||||
int hh = img->h;
|
||||
int cc = img->c;
|
||||
if (letter_box) {
|
||||
// printf("using letter %d %d %d %d %d %d\n", letter_box, n->w, n->h, img->w, img->h, img->c);
|
||||
sized = letterbox_image(*img, n->w, n->h);
|
||||
} else {
|
||||
// printf("not using letter: %d %d %d %d %d %d\n", letter_box, n->w, n->h, img->w, img->h, img->c);
|
||||
sized = resize_image(*img, n->w, n->h);
|
||||
}
|
||||
struct network_box_result result = { NULL };
|
||||
float *X = sized.data;
|
||||
int letter_box = 0;
|
||||
network_predict_ptr(n, X);
|
||||
result.detections = get_network_boxes(n, img->w, img->h, thresh, hier_thresh, 0, 1, &result.detections_len, letter_box);
|
||||
// printf("Clang number of detections: %d\n", result.detections_len);
|
||||
if (nms) {
|
||||
do_nms_sort(result.detections, result.detections_len, classes, nms);
|
||||
}
|
||||
|
@@ -63,11 +63,8 @@ func (n *YOLONetwork) Init() error {
|
||||
// C.set_batch_network(n.cNet, 1)
|
||||
C.srand(2222222)
|
||||
|
||||
// Currently, hierarchal threshold is always 0.5.
|
||||
n.hierarchalThreshold = .5
|
||||
|
||||
// Currently NMS is always 0.4.
|
||||
n.nms = .45
|
||||
n.hierarchalThreshold = 0.5
|
||||
n.nms = 0.45
|
||||
|
||||
metadata := C.get_metadata(nCfg)
|
||||
n.Classes = int(metadata.classes)
|
||||
@@ -93,7 +90,7 @@ func (n *YOLONetwork) Detect(img *DarknetImage) (*DetectionResult, error) {
|
||||
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))
|
||||
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), C.int(0))
|
||||
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)
|
||||
|
@@ -8,4 +8,4 @@ struct network_box_result {
|
||||
};
|
||||
|
||||
extern int get_network_layer_classes(network *n, int index);
|
||||
extern struct network_box_result perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms);
|
||||
extern struct network_box_result perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms, int letter_box);
|
BIN
predictions.jpg
BIN
predictions.jpg
Binary file not shown.
Before Width: | Height: | Size: 2.1 MiB |
Reference in New Issue
Block a user