12 Commits

Author SHA1 Message Date
Dimitrii
36d89f7409 mod 2022-03-17 13:38:36 +03:00
Dimitrii Lopanov
51c7e39572 Merge pull request #25 from lian/master
DarknetImage move float conversion into c code
2022-03-05 13:05:19 +03:00
Julian Langschaedel
7e34b459e6 DarknetImage move float conversion into c code
imgTofloat32 + fill_image_f32 took more than double the time since tofloat created needless allocations and iterate over each pixel, and fill_image in c then iterate over each pixel*3 again just to move it into the c image.
2022-03-02 19:19:47 +01:00
Dimitrii
a18a9cf3e0 rme 2022-02-05 18:21:10 +03:00
Dimitrii Lopanov
3d4b8507dc Checkout to stable YOLO v4 (#23)
* play with commit

* minor bench
2022-02-05 18:14:44 +03:00
Dimitrii
7edf795470 docs 2022-02-04 23:21:57 +03:00
Dimitrii
020dc47d85 go mod 2022-02-04 22:42:48 +03:00
Dimitrii Lopanov
f813086971 update .phone 2022-01-05 13:52:23 +03:00
Dimitrii Lopanov
7b59b7be33 upd rdme 2022-01-05 13:41:11 +03:00
Dimitrii Lopanov
774662381f misspell 2021-11-01 22:50:46 +03:00
Dimitrii Lopanov
d8cd9728c2 Merge pull request #20 from LdDl/install-cuda
update makefile
2021-08-18 13:09:48 +03:00
Dimitrii Lopanov
1b4b71fa8e Merge pull request #19 from LdDl/install-cuda
Install cuda
2021-08-18 13:04:42 +03:00
15 changed files with 78 additions and 70 deletions

13
.gitignore vendored
View File

@@ -1,9 +1,10 @@
example/main
example/coco.names
example/yolov3.cfg
example/yolov3.weights
example/yolov4.cfg
example/yolov4.weights
cmd/examples/main
cmd/examples/base_example/main
cmd/examples/coco.names
cmd/examples/yolov3.cfg
cmd/examples/yolov3.weights
cmd/examples/yolov4.cfg
cmd/examples/yolov4.weights
darknet.h
*.so
predictions.png

View File

@@ -1,8 +1,9 @@
.ONESHELL:
.PHONY: prepare_cuda prepare_cudnn download_darknet build_darknet build_darknet_gpu clean clean_cuda clean_cudnn
.PHONY: prepare_cuda prepare_cudnn download_darknet build_darknet build_darknet_gpu clean clean_cuda clean_cudnn sudo_install
# Latest battletested AlexeyAB version of Darknet commit
LATEST_COMMIT?=f056fc3b6a11528fa0522a468eca1e909b7004b7
# LATEST_COMMIT?=f056fc3b6a11528fa0522a468eca1e909b7004b7
LATEST_COMMIT?=9d40b619756be9521bc2ccd81808f502daaa3e9a
# Temporary folder for building Darknet
TMP_DIR?=/tmp/

View File

@@ -26,7 +26,7 @@
Because this repository https://github.com/gyonluks/go-darknet is no longer maintained.
**What is purpose of this bindings when you can have [GoCV](https://github.com/hybridgroup/gocv#gocv) (bindings to OpenCV) and it handle Darnet YOLO perfectly?**
**What is purpose of this bindings when you can have [GoCV](https://github.com/hybridgroup/gocv#gocv) (bindings to OpenCV) and it handle Darknet YOLO perfectly?**
Well, you don't need bunch of OpenCV dependencies and OpenCV itself sometimes.
@@ -35,7 +35,7 @@ Example of such project here: https://github.com/LdDl/license_plate_recognition#
## Requirements
You need to install fork of [darknet](https://github.com/AlexeyAB/darknet). Latest commit I've tested is [here](https://github.com/AlexeyAB/darknet/commit/d65909fbea471d06e52a2e4a41132380dc2edaa6)
You need to install fork of [darknet](https://github.com/AlexeyAB/darknet). Latest commit I've tested is [here](https://github.com/AlexeyAB/darknet/commit/9d40b619756be9521bc2ccd81808f502daaa3e9a). It corresponds last official [YOLOv4 release](https://github.com/AlexeyAB/darknet/releases/tag/yolov4)
Use provided [Makefile](Makefile).
@@ -64,13 +64,13 @@ go get github.com/LdDl/go-darknet
## Usage
Example Go program is provided in the [example] directory. Please refer to the code on how to use this Go package.
Example Go program is provided in the [examples] directory. Please refer to the code on how to use this Go package.
Building and running program:
* Navigate to [example] folder
* Navigate to [examples] folder
```shell
cd $GOPATH/github.com/LdDl/go-darknet/example/base_example
cd ${YOUR PATH}/github.com/LdDl/go-darknet/cmd/examples
```
* Download dataset (sample of image, coco.names, yolov4.cfg (or v3), yolov4.weights(or v3)).
@@ -106,23 +106,23 @@ Building and running program:
It will reduce amount of VRAM used for detector test.
* Build and run program
* Build and run example program
Yolo V4:
```shell
go build main.go && ./main --configFile=yolov4.cfg --weightsFile=yolov4.weights --imageFile=sample.jpg
go build -o base_example/main base_example/main.go && ./base_example/main --configFile=yolov4.cfg --weightsFile=yolov4.weights --imageFile=sample.jpg
```
Output should be something like this:
```shell
traffic light (9): 73.5039% | start point: (238,73) | end point: (251, 106)
traffic light (9): 73.5040% | start point: (238,73) | end point: (251, 106)
truck (7): 96.6401% | start point: (95,79) | end point: (233, 287)
truck (7): 96.4774% | start point: (662,158) | end point: (800, 321)
truck (7): 96.1841% | start point: (0,77) | end point: (86, 333)
truck (7): 46.8695% | start point: (434,173) | end point: (559, 216)
truck (7): 46.8694% | start point: (434,173) | end point: (559, 216)
car (2): 99.7370% | start point: (512,188) | end point: (741, 329)
car (2): 99.2533% | start point: (260,191) | end point: (422, 322)
car (2): 99.2532% | start point: (260,191) | end point: (422, 322)
car (2): 99.0333% | start point: (425,201) | end point: (547, 309)
car (2): 83.3919% | start point: (386,210) | end point: (437, 287)
car (2): 83.3920% | start point: (386,210) | end point: (437, 287)
car (2): 75.8621% | start point: (73,199) | end point: (102, 274)
car (2): 39.1925% | start point: (386,206) | end point: (442, 240)
bicycle (1): 76.3121% | start point: (189,298) | end point: (253, 402)
@@ -136,19 +136,19 @@ Building and running program:
Output should be something like this:
```shell
truck (7): 49.5197% | start point: (0,136) | end point: (85, 311)
car (2): 36.3747% | start point: (95,152) | end point: (186, 283)
truck (7): 48.4384% | start point: (95,152) | end point: (186, 283)
truck (7): 45.6590% | start point: (694,178) | end point: (798, 310)
car (2): 76.8379% | start point: (1,145) | end point: (84, 324)
truck (7): 25.5731% | start point: (107,89) | end point: (215, 263)
car (2): 99.8783% | start point: (511,185) | end point: (748, 328)
car (2): 99.8194% | start point: (261,189) | end point: (427, 322)
car (2): 99.6408% | start point: (426,197) | end point: (539, 311)
car (2): 74.5610% | start point: (692,186) | end point: (796, 316)
car (2): 72.8053% | start point: (388,206) | end point: (437, 276)
bicycle (1): 72.2932% | start point: (178,270) | end point: (268, 406)
person (0): 97.3026% | start point: (143,135) | end point: (268, 343)
truck (7): 49.5123% | start point: (0,136) | end point: (85, 311)
car (2): 36.3694% | start point: (95,152) | end point: (186, 283)
truck (7): 48.4177% | start point: (95,152) | end point: (186, 283)
truck (7): 45.6520% | start point: (694,178) | end point: (798, 310)
car (2): 76.8402% | start point: (1,145) | end point: (84, 324)
truck (7): 25.5920% | start point: (107,89) | end point: (215, 263)
car (2): 99.8782% | start point: (511,185) | end point: (748, 328)
car (2): 99.8193% | start point: (261,189) | end point: (427, 322)
car (2): 99.6405% | start point: (426,197) | end point: (539, 311)
car (2): 74.5627% | start point: (692,186) | end point: (796, 316)
car (2): 72.7975% | start point: (388,206) | end point: (437, 276)
bicycle (1): 72.2760% | start point: (178,270) | end point: (268, 406)
person (0): 97.3007% | start point: (143,135) | end point: (268, 343)
```
## Documentation
@@ -165,5 +165,5 @@ go-darknet follows [Darknet]'s [license].
[darknet.h]: https://github.com/AlexeyAB/darknet/blob/master/include/darknet.h
[include/darknet.h]: https://github.com/AlexeyAB/darknet/blob/master/include/darknet.h
[Makefile]: https://github.com/alexeyab/darknet/blob/master/Makefile
[example]: /example/base_example
[examples]: cmd/examples/base_example
[GoDoc]: https://godoc.org/github.com/LdDl/go-darknet

View File

@@ -9,7 +9,9 @@ import (
"log"
"math"
"os"
darknet "github.com/LdDl/go-darknet"
"github.com/disintegration/imaging"
)
@@ -60,7 +62,7 @@ func main() {
if err != nil {
panic(err.Error())
}
dr, err := n.Detect(imgDarknet)
if err != nil {
printError(err)

View File

@@ -11,7 +11,7 @@ import (
"log"
"net/http"
"github.com/LdDl/go-darknet"
darknet "github.com/LdDl/go-darknet"
)
var configFile = flag.String("configFile", "",

View File

Before

Width:  |  Height:  |  Size: 134 KiB

After

Width:  |  Height:  |  Size: 134 KiB

8
go.mod Normal file
View File

@@ -0,0 +1,8 @@
module github.com/LdDl/go-darknet
go 1.17
require (
github.com/disintegration/imaging v1.6.2
golang.org/x/image v0.0.0-20211028202545-6944b10bf410
)

8
go.sum Normal file
View File

@@ -0,0 +1,8 @@
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

15
image.c
View File

@@ -11,3 +11,18 @@ void set_data_f32_val(float* data, int index, float value) {
data[index] = value;
}
void to_float_and_fill_image(image* im, int w, int h, uint8_t* data) {
int x, y, idx_source;
int pixel_count = w * h;
int idx = 0;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
idx_source = (y*w + x) * 4;
im->data[(pixel_count*0) + idx] = (float)data[idx_source] / 255;
im->data[(pixel_count*1) + idx] = (float)data[idx_source+1] / 255;
im->data[(pixel_count*2) + idx] = (float)data[idx_source+2] / 255;
idx++;
}
}
}

View File

@@ -14,61 +14,33 @@ import (
type DarknetImage struct {
Width int
Height int
ans []float32
image C.image
}
// Close and release resources.
func (img *DarknetImage) Close() error {
C.free_image(img.image)
img.ans = nil
return nil
}
// https://stackoverflow.com/questions/33186783/get-a-pixel-array-from-from-golang-image-image/59747737#59747737
func imgTofloat32(src image.Image) []float32 {
func Image2Float32(src image.Image) (*DarknetImage, error) {
bounds := src.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
srcRGBA := image.NewRGBA(src.Bounds())
draw.Copy(srcRGBA, image.Point{}, src, src.Bounds(), draw.Src, nil)
srcRGBA := image.NewRGBA(bounds)
draw.Copy(srcRGBA, image.Point{}, src, bounds, draw.Src, nil)
red := make([]float32, 0, width*height)
green := make([]float32, 0, width*height)
blue := make([]float32, 0, width*height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
idxSource := (y*width + x) * 4
pix := srcRGBA.Pix[idxSource : idxSource+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)
}
}
srcRGBA = nil
ans := make([]float32, len(red)+len(green)+len(blue))
copy(ans[:len(red)], red)
copy(ans[len(red):len(red)+len(green)], green)
copy(ans[len(red)+len(green):], blue)
red = nil
green = nil
blue = nil
return ans
return ImageRGBA2Float32(srcRGBA)
}
// Image2Float32 Returns []float32 representation of image.Image
func Image2Float32(img image.Image) (*DarknetImage, error) {
// ans := imgTofloat32(img)
func ImageRGBA2Float32(img *image.RGBA) (*DarknetImage, error) {
width := img.Bounds().Dx()
height := img.Bounds().Dy()
imgDarknet := &DarknetImage{
Width: width,
Height: height,
ans: imgTofloat32(img),
image: C.make_image(C.int(width), C.int(height), 3),
}
C.fill_image_f32(&imgDarknet.image, C.int(width), C.int(height), 3, (*C.float)(unsafe.Pointer(&imgDarknet.ans[0])))
C.to_float_and_fill_image(&imgDarknet.image, C.int(width), C.int(height), (*C.uint8_t)(unsafe.Pointer(&img.Pix[0])))
return imgDarknet, nil
}

View File

@@ -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);
extern void to_float_and_fill_image(image *im, int w, int h, uint8_t* data);