156 lines
4.9 KiB
Markdown
156 lines
4.9 KiB
Markdown
# bytetrack-counter-go
|
|
|
|
Edge-computing live video analytics counter for industrial poultry processing. Connects to an RTSP camera, runs YOLO object detection on a Rockchip RKNN NPU, tracks objects with ByteTrack, and counts chickens ("ayam") crossing a virtual line. Closes counting batches when a cutting board ("talenan") crosses the line. Persists batch data to SQLite.
|
|
|
|
## Architecture
|
|
|
|
```
|
|
RTSP Camera --> gocv.VideoCapture --> YOLO (RKNN NPU) --> ByteTrack
|
|
|
|
|
Line-Crossing Logic
|
|
|
|
|
+-----> BatchStore (SQLite)
|
|
|
|
|
HUD Drawing --> Video Output / Live JPEG
|
|
```
|
|
|
|
## Requirements
|
|
|
|
- **Rockchip RK3588** (or compatible) with RKNN NPU
|
|
- **librknnrt** (Rockchip RKNN Runtime library, `>=1.6.0`)
|
|
- **OpenCV 4.x** with GoCV bindings
|
|
- **Go 1.23+**
|
|
- **CGo enabled** (required for RKNN and GoCV)
|
|
|
|
### Installing GoCV / OpenCV
|
|
|
|
On RK3588 (Armbian/Ubuntu):
|
|
|
|
```bash
|
|
sudo apt install -y libopencv-dev pkg-config
|
|
go get -u gocv.io/x/gocv
|
|
```
|
|
|
|
### Installing librknnrt
|
|
|
|
```bash
|
|
# Copy librknnrt.so to the device (obtained from Rockchip SDK)
|
|
sudo cp librknnrt.so /usr/lib/aarch64-linux-gnu/
|
|
sudo ldconfig
|
|
```
|
|
|
|
Verify:
|
|
|
|
```bash
|
|
ls /usr/lib/aarch64-linux-gnu/librknnrt.so
|
|
# Should also have: librknn_rk3588.so or similar NPU driver
|
|
```
|
|
|
|
## Compiling
|
|
|
|
Clone and build:
|
|
|
|
```bash
|
|
git clone https://git.proit.id/dsutanto/bytetrack-counter-go.git
|
|
cd bytetrack-counter-go
|
|
CGO_ENABLED=1 go build -o counter ./cmd/counter/
|
|
```
|
|
|
|
The `-o counter` flag places the binary at `./counter`. For production, copy it to `/opt/bytetrack-counter-go/counter`.
|
|
|
|
To verify the binary links correctly:
|
|
|
|
```bash
|
|
ldd ./counter | grep rknn
|
|
# Should show librknnrt.so => /usr/lib/aarch64-linux-gnu/librknnrt.so
|
|
```
|
|
|
|
### Cross-compiling from amd64
|
|
|
|
```bash
|
|
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 \
|
|
CGO_LDFLAGS="-L/path/to/rknn-libs -lrknnrt" \
|
|
go build -o counter ./cmd/counter/
|
|
```
|
|
|
|
## Configuration
|
|
|
|
All configuration is via environment variables. Copy the example and edit:
|
|
|
|
```bash
|
|
cp config.env.example .env
|
|
nano .env
|
|
```
|
|
|
|
Key settings:
|
|
|
|
| Variable | Default | Description |
|
|
|----------|---------|-------------|
|
|
| `SOURCE` | — | RTSP URL or video file path (required) |
|
|
| `MODEL_PATH` | — | Path to `.rknn` YOLO model (required) |
|
|
| `OUTPUT_DIR` | `/opt/jetson-counter` | Output directory for DB, state, CSV, video |
|
|
| `IMGSZ` | `320` | YOLO input image size (must match model) |
|
|
| `CONF` | `0.3` | Detection confidence threshold |
|
|
| `CORE_MASK` | `1` | RKNN NPU cores: 1=core0, 2=core1, 3=both, 7=all three |
|
|
| `NUM_CLASSES` | `2` | Number of YOLO classes |
|
|
| `CLASS_AYAM` | `ayam` | Object class to count |
|
|
| `CLASS_TALENAN` | `talenan` | Object class to close batches |
|
|
| `LINE_X_FRAC` | `0.5` | Counting line X position (fraction of frame width) |
|
|
| `CROSS_DIRECTION` | `rtl` | Crossing direction: `rtl`, `ltr`, or `both` |
|
|
| `DAILY_CUTOFF_TIME` | `20:00` | Time to roll over to next counting day |
|
|
| `BATCH_TIMEOUT_SECONDS` | `300` | Inactivity timeout to close a batch |
|
|
| `MIN_OBJECT_PER_BATCH` | `60` | Minimum chickens per batch |
|
|
| `OPENCV_FFMPEG_CAPTURE_OPTIONS` | — | FFmpeg options for low-latency RTSP |
|
|
|
|
See `config.env.example` for the full list.
|
|
|
|
## Running
|
|
|
|
### Interactive (foreground)
|
|
|
|
```bash
|
|
set -a && source .env && set +a
|
|
./counter
|
|
```
|
|
|
|
### systemd (production)
|
|
|
|
```bash
|
|
# Install binary and config
|
|
sudo mkdir -p /opt/bytetrack-counter-go
|
|
sudo cp counter /opt/bytetrack-counter-go/
|
|
sudo cp .env /opt/bytetrack-counter-go/
|
|
sudo cp config.env.example /opt/bytetrack-counter-go/
|
|
|
|
# Install and start service
|
|
sudo cp bytetrack-counter-go.service /etc/systemd/system/
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable bytetrack-counter-go
|
|
sudo systemctl start bytetrack-counter-go
|
|
|
|
# Monitor
|
|
sudo journalctl -u bytetrack-counter-go -f
|
|
```
|
|
|
|
## Directory Structure
|
|
|
|
```
|
|
.
|
|
├── cmd/counter/main.go # Application entrypoint
|
|
├── pkg/
|
|
│ ├── batchstore/store.go # SQLite batch persistence + JSON state
|
|
│ ├── bytetrack/tracker.go # ByteTrack multi-object tracker
|
|
│ ├── config/config.go # Env-based configuration loader
|
|
│ ├── drawing/drawing.go # OpenCV HUD drawing helpers
|
|
│ ├── kalman/kalman.go # 8-state Kalman filter
|
|
│ ├── rknn/rknn.go # CGo bridge to librknnrt
|
|
│ └── yolo/yolo.go # YOLO detector (RKNN inference + NMS)
|
|
├── internal/
|
|
│ ├── iou/iou.go # IoU computation + greedy matching
|
|
│ └── nms/nms.go # Non-maximum suppression
|
|
├── config.env.example # Example configuration
|
|
├── bytetrack-counter-go.service # systemd unit file
|
|
├── go.mod
|
|
└── go.sum
|
|
```
|