mirror of
https://github.com/LdDl/go-darknet.git
synced 2025-09-26 19:51:27 +08:00
got segfault
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -12,4 +12,5 @@ darknet.h
|
||||
*.so
|
||||
predictions.png
|
||||
predictions.jpg
|
||||
main
|
||||
main
|
||||
bad.list
|
@@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"log"
|
||||
"os"
|
||||
@@ -56,11 +58,23 @@ func main() {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
imgDarknet, err := darknet.Image2Float32(src)
|
||||
// bytes <<<<<<<<<<<<<
|
||||
imgBytes, err := imageToBytes(src)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
imgDarknet, err := darknet.ImageFromMemory(imgBytes, 4032, 3024)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
defer imgDarknet.Close()
|
||||
// bytes >>>>>>>>>>>>>
|
||||
|
||||
// imgDarknet, err := darknet.Image2Float32(src)
|
||||
// if err != nil {
|
||||
// panic(err.Error())
|
||||
// }
|
||||
// defer imgDarknet.Close()
|
||||
|
||||
dr, err := n.Detect(imgDarknet)
|
||||
if err != nil {
|
||||
@@ -82,3 +96,9 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func imageToBytes(img image.Image) ([]byte, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
err := jpeg.Encode(buf, img, nil)
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
4
image.c
4
image.c
@@ -10,3 +10,7 @@ void fill_image_f32(image* im, int w, int h, int c, float* data) {
|
||||
void set_data_f32_val(float* data, int index, float value) {
|
||||
data[index] = value;
|
||||
}
|
||||
|
||||
image resize_image_golang(image im, int w, int h) {
|
||||
return resize_image(im, w, h);
|
||||
}
|
37
image.go
37
image.go
@@ -4,6 +4,7 @@ package darknet
|
||||
// #include "image.h"
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"unsafe"
|
||||
)
|
||||
@@ -54,8 +55,40 @@ func Image2Float32(img image.Image) (*DarknetImage, error) {
|
||||
// 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))
|
||||
fmt.Println(ans[:100])
|
||||
// C.fill_image_f32(&imgDarknet.image, C.int(width), C.int(height), 3, float_p(ans))
|
||||
// newImg := C.resize_image_golang(imgDarknet.image, 416, 416)
|
||||
// r, g, b, _ := img.At(0, 0).RGBA()
|
||||
// rpix, gpix, bpix := float32(r>>8)/float32(255.0), float32(g>>8)/float32(255.0), float32(b>>8)/float32(255.0)
|
||||
// fmt.Println(rpix, gpix, bpix)
|
||||
|
||||
// imgDarknet.image = C.load_image_color(C.CString("~/Downloads/mega.jpg"), 416, 416)
|
||||
// imgDarknet.image = C.load_image_color(C.CString("/home/dimitrii/Downloads/mega.jpg"), 4032, 3024)
|
||||
return imgDarknet, nil
|
||||
}
|
||||
|
||||
// ImageFromMemory reads image file data represented by the specified byte
|
||||
// slice.
|
||||
func ImageFromMemory(buf []byte, width, height int) (*DarknetImage, error) {
|
||||
cBuf := C.CBytes(buf)
|
||||
defer C.free(cBuf)
|
||||
|
||||
imgd := C.make_image(C.int(width), C.int(height), 3)
|
||||
// var imgd C.image
|
||||
// imgd.w = width
|
||||
// imgd.h = height
|
||||
// imgd.c = 3
|
||||
|
||||
C.copy_image_from_bytes(imgd, (*C.char)(cBuf))
|
||||
img := DarknetImage{
|
||||
image: imgd,
|
||||
}
|
||||
|
||||
if img.image.data == nil {
|
||||
return nil, fmt.Errorf("nil image")
|
||||
}
|
||||
|
||||
img.Width = int(img.image.w)
|
||||
img.Height = int(img.image.h)
|
||||
|
||||
return &img, nil
|
||||
}
|
||||
|
3
image.h
3
image.h
@@ -3,4 +3,5 @@
|
||||
#include <darknet.h>
|
||||
|
||||
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);
|
||||
extern void set_data_f32_val(float* data, int index, float value);
|
||||
image resize_image_golang(image im, int w, int h);
|
44
network.c
44
network.c
@@ -6,22 +6,46 @@
|
||||
|
||||
#include "detection.h"
|
||||
|
||||
struct network_box_result perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms, int letter_box) {
|
||||
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;
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
printf("\n>>>>>>>>>>>>>>Fourth Print (Golang)\n");
|
||||
for (int i = 0; i< 100; i++) {
|
||||
printf("%f ", sized.data[i]);
|
||||
}
|
||||
printf("\n<<<<<<<<<<<<<<Done\n");
|
||||
|
||||
// newImg := resize_image_golang(imgDarknet.image, 416, 416)
|
||||
|
||||
|
||||
|
||||
struct network_box_result result = { NULL };
|
||||
float *X = sized.data;
|
||||
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);
|
||||
float *outnwt = network_predict_ptr(n, X);
|
||||
|
||||
printf("\n>>>>>>>>>>>>>>Golang out\n");
|
||||
for (int i = 0; i< 100; i++) {
|
||||
printf("%f ", outnwt[i]);
|
||||
}
|
||||
printf("\n<<<<<<<<<<<<<<Done\n");
|
||||
|
||||
int nboxes = 0;
|
||||
detection *dets = get_network_boxes(n, img.w, img.h, thresh, hier_thresh, 0, 1, &nboxes, letter_box);
|
||||
printf("Clang number of detections: %d %d %d %f %f %d\n", nboxes, img.w, img.h, thresh, hier_thresh, letter_box);
|
||||
|
||||
result.detections = get_network_boxes(n, img.w, img.h, thresh, hier_thresh, 0, 1, &result.detections_len, letter_box);
|
||||
printf("Golang number of detections: %d\n", result.detections_len);
|
||||
|
||||
// printf("Clang number of detections: %d\n", result.detections_len);
|
||||
if (nms) {
|
||||
do_nms_sort(result.detections, result.detections_len, classes, nms);
|
||||
|
@@ -90,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), C.int(0))
|
||||
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, int letter_box);
|
||||
extern struct network_box_result perform_network_detect(network *n, image img, int classes, float thresh, float hier_thresh, float nms, int letter_box);
|
Reference in New Issue
Block a user