GOD SAVE STACKOVERFLOW

This commit is contained in:
Dimitrii
2020-02-20 18:10:11 +03:00
parent 48544ab339
commit bc4192f727
8 changed files with 519249 additions and 73 deletions

View File

@@ -1,6 +1,7 @@
{
"files.associations": {
"detection.h": "c",
"network.h": "c"
"network.h": "c",
"random": "c"
}
}

View File

@@ -59,22 +59,22 @@ func main() {
}
// 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)
// 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 {

24
image.c
View File

@@ -13,4 +13,26 @@ void set_data_f32_val(float* data, int index, float value) {
image resize_image_golang(image im, int w, int h) {
return resize_image(im, w, h);
}
}
image make_empty_image(int w, int h, int c)
{
image out;
out.data = 0;
out.h = h;
out.w = w;
out.c = c;
return out;
}
image float_to_image(int w, int h, int c, float *data)
{
image out = make_empty_image(w,h,c);
fill_image_f32(&out, w, h, c, data);
// for (i = 0; i < w*h*c; i++) {
// out.data[i] = data[i];
// }
return out;
}

View File

@@ -4,9 +4,10 @@ package darknet
// #include "image.h"
import "C"
import (
"fmt"
"image"
"unsafe"
"golang.org/x/image/draw"
)
// DarknetImage represents the image buffer.
@@ -26,18 +27,21 @@ 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) (*DarknetImage, error) {
width := img.Bounds().Dx()
height := img.Bounds().Dy()
// https://stackoverflow.com/questions/33186783/get-a-pixel-array-from-from-golang-image-image/59747737#59747737
func image_2_array_pix(src image.Image) []float32 {
bounds := src.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
src_rgba := image.NewRGBA(src.Bounds())
draw.Copy(src_rgba, image.Point{}, src, src.Bounds(), draw.Src, nil)
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)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
idx_s := (y*width + x) * 4
pix := src_rgba.Pix[idx_s : idx_s+4]
rpix, gpix, bpix := float32(pix[0])/257.0, float32(pix[1])/257.0, float32(pix[2])/257.0
red = append(red, rpix)
green = append(green, gpix)
blue = append(blue, bpix)
@@ -46,7 +50,14 @@ func Image2Float32(img image.Image) (*DarknetImage, error) {
ans = append(ans, red...)
ans = append(ans, green...)
ans = append(ans, blue...)
return ans
}
// Image2Float32 Returns []float32 representation of image.Image
func Image2Float32(img image.Image) (*DarknetImage, error) {
ans := image_2_array_pix(img)
width := img.Bounds().Dx()
height := img.Bounds().Dy()
imgDarknet := &DarknetImage{
Width: width,
Height: height,
@@ -55,40 +66,7 @@ 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]))
// }
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)
C.fill_image_f32(&imgDarknet.image, C.int(width), C.int(height), 3, float_p(ans))
// 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
}
}

View File

@@ -4,4 +4,6 @@
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);
image resize_image_golang(image im, int w, int h);
extern image resize_image_golang(image im, int w, int h);
extern image make_empty_image(int w, int h, int c);
extern image float_to_image(int w, int h, int c, float *data);

View File

@@ -19,25 +19,24 @@ struct network_box_result perform_network_detect(network *n, image img, int clas
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");
// printf("\n>>>>>>>>>>>>>>Fourth Print (Golang)\n");
// for (int i = 0; i< 50; i++) {
// printf("%f\n", sized.data[i]);
// }
// printf("\n<<<<<<<<<<<<<<Done\n");
// sized.data = float*{0.815686, 0.592875, 0.645386, 0.731689, 0.659976};
// newImg := resize_image_golang(imgDarknet.image, 416, 416)
struct network_box_result result = { NULL };
float *X = sized.data;
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");
// 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);
@@ -50,7 +49,9 @@ struct network_box_result perform_network_detect(network *n, image img, int clas
if (nms) {
do_nms_sort(result.detections, result.detections_len, classes, nms);
}
free_image(sized);
return result;
}

519168
out.txt Normal file

File diff suppressed because it is too large Load Diff

4
tt.txt Normal file
View File

@@ -0,0 +1,4 @@
Try to load cfg: /home/dimitrii/alexeyab_last/darknet/train_mine/yolov3-common/common_inference.cfg, clear = 0
net.optimized_memory = 0
batch = 1, time_steps = 1, train = 1
Try to load weights: /home/dimitrii/alexeyab_last/darknet/backup-yolov3-common/common_220000.weights