mirror of
https://github.com/LdDl/go-darknet.git
synced 2025-09-27 20:12:13 +08:00
Compare commits
5 Commits
v1.0
...
new_exampl
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0fd261e605 | ||
![]() |
4c39be01f9 | ||
![]() |
2dd85a84d6 | ||
![]() |
3a10d5a657 | ||
![]() |
95332e0877 |
13
README.md
13
README.md
@@ -1,10 +1,11 @@
|
||||
# go-darknet: Go bindings for Darknet
|
||||
|
||||
### This is fork of go-darknet https://github.com/gyonluks/go-darknet applied to FORK of Darknet https://github.com/AlexeyAB/darknet
|
||||
|
||||
[](https://godoc.org/github.com/LdDl/go-darknet)
|
||||
|
||||
go-darknet is a Go package, which uses Cgo to enable Go applications to use YOLO in [Darknet].
|
||||
# go-darknet: Go bindings for Darknet
|
||||
### go-darknet is a Go package, which uses Cgo to enable Go applications to use YOLO in [Darknet].
|
||||
|
||||
#### Since this repository https://github.com/gyonluks/go-darknet is no longer maintained I decided to move on and make little different bindings for Darknet.
|
||||
#### This bindings aren't for [official implementation](https://github.com/pjreddie/darknet) but for [AlexeyAB's fork](https://github.com/AlexeyAB/darknet).
|
||||
|
||||
|
||||
## Table of Contents
|
||||
|
||||
@@ -16,7 +17,7 @@ go-darknet is a Go package, which uses Cgo to enable Go applications to use YOLO
|
||||
|
||||
## Requirements
|
||||
|
||||
For proper codebase please use fork of [darknet](https://github.com/AlexeyAB/darknet). Latest commit I've tested [here](https://github.com/AlexeyAB/darknet/commit/64fb042c63637038671ae9d53c06165599b28912)
|
||||
For proper codebase please use fork of [darknet](https://github.com/AlexeyAB/darknet). Latest commit I've tested [here](https://github.com/AlexeyAB/darknet/commit/a234a5022333c930de08f2470184ef4e0c68356e)
|
||||
|
||||
In order to use go-darknet, `libdarknet.so` should be available in one of
|
||||
the following locations:
|
||||
|
@@ -2,33 +2,53 @@
|
||||
|
||||
This is an example Go application which uses go-darknet.
|
||||
|
||||
## Install
|
||||
|
||||
```shell
|
||||
go get github.com/LdDl/go-darknet
|
||||
go install github.com/LdDl/go-darknet/example
|
||||
|
||||
# Alternatively
|
||||
go build github.com/LdDl/go-darknet/example
|
||||
```
|
||||
|
||||
An executable named `example` should be in your `$GOPATH/bin`, if using
|
||||
`go install`; otherwise it will be in your current working directory (`$PWD`),
|
||||
if using `go build`.
|
||||
|
||||
## Run
|
||||
|
||||
Navigate to example folder:
|
||||
|
||||
```shell
|
||||
$GOPATH/bin/example
|
||||
cd $GOPATH/github.com/LdDl/go-darknet/example
|
||||
```
|
||||
or
|
||||
```go
|
||||
go run main.go -configFile=yolov3-320.cfg -dataConfigFile=coco.data -imageFile=sample.jpg -weightsFile=yolov3.weights
|
||||
|
||||
Download dataset (sample of image, coco.names, yolov3.cfg, yolov3.weights).
|
||||
```shell
|
||||
./download_data.sh
|
||||
```
|
||||
Note: you don't need *coco.data* file anymore, because script below does insert *coco.names* into 'names' filed in *yolov3.cfg* file (so AlexeyAB's fork can deal with it properly)
|
||||
So last rows in yolov3.cfg file will look like:
|
||||
```bash
|
||||
......
|
||||
[yolo]
|
||||
mask = 0,1,2
|
||||
anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
|
||||
classes=80
|
||||
num=9
|
||||
jitter=.3
|
||||
ignore_thresh = .7
|
||||
truth_thresh = 1
|
||||
random=1
|
||||
names = coco.names # this is path to coco.names file
|
||||
```
|
||||
Please ensure that `libdarknet.so` is in your `$LD_LIBRARY_PATH`.
|
||||
|
||||
## Notes
|
||||
Build and run program
|
||||
```
|
||||
go build main.go && ./main --configFile=yolov3.cfg --weightsFile=yolov3.weights --imageFile=sample.jpg
|
||||
```
|
||||
|
||||
Note that the bounding boxes' values are ratios. To get the actual values, use
|
||||
the ratios and multiply with either the image's width or height, depending on
|
||||
which ratio is used.
|
||||
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)
|
||||
```
|
@@ -7,9 +7,11 @@ import (
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
darknet "github.com/LdDl/go-darknet"
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
var configFile = flag.String("configFile", "",
|
||||
@@ -78,6 +80,16 @@ func main() {
|
||||
bBox.StartPoint.X, bBox.StartPoint.Y,
|
||||
bBox.EndPoint.X, bBox.EndPoint.Y,
|
||||
)
|
||||
|
||||
// Uncomment code below if you want save cropped objects to files
|
||||
// minX, minY := float64(bBox.StartPoint.X), float64(bBox.StartPoint.Y)
|
||||
// maxX, maxY := float64(bBox.EndPoint.X), float64(bBox.EndPoint.Y)
|
||||
// rect := image.Rect(round(minX), round(minY), round(maxX), round(maxY))
|
||||
// err := saveToFile(src, rect, fmt.Sprintf("crop_%d.jpeg", i))
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,3 +99,24 @@ func imageToBytes(img image.Image) ([]byte, error) {
|
||||
err := jpeg.Encode(buf, img, nil)
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
func round(v float64) int {
|
||||
if v >= 0 {
|
||||
return int(math.Floor(v + 0.5))
|
||||
}
|
||||
return int(math.Ceil(v - 0.5))
|
||||
}
|
||||
|
||||
func saveToFile(imgSrc image.Image, bbox image.Rectangle, fname string) error {
|
||||
rectcropimg := imaging.Crop(imgSrc, bbox)
|
||||
f, err := os.Create(fname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
err = jpeg.Encode(f, rectcropimg, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@ struct network_box_result perform_network_detect(network *n, image *img, int cla
|
||||
}
|
||||
struct network_box_result result = { NULL };
|
||||
float *X = sized.data;
|
||||
network_predict_ptr(n, X);
|
||||
network_predict(*n, X);
|
||||
int nboxes = 0;
|
||||
detection *dets = get_network_boxes(n, img->w, img->h, thresh, hier_thresh, 0, 1, &nboxes, letter_box);
|
||||
result.detections = get_network_boxes(n, img->w, img->h, thresh, hier_thresh, 0, 1, &result.detections_len, letter_box);
|
||||
|
Reference in New Issue
Block a user