File size: 5,097 Bytes
9da7984
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# BirdNET Real-Time Detection

Real-time bird species detection using your microphone and the BirdNET ONNX model.

## Features

🎀 **Live Microphone Input**: Continuously captures and analyzes audio from your microphone
🐦 **Real-Time Detection**: Identifies bird species as they sing with configurable confidence thresholds
πŸ“Š **Live Display**: Dynamic terminal interface showing current detections and recent activity
⚑ **Optimized Performance**: Efficient audio processing with rolling buffers and threading
πŸ”§ **Configurable**: Adjustable confidence thresholds, update intervals, and display options

## Installation

### Manual Installation

Install required packages:

```bash
pip install sounddevice numpy librosa onnxruntime soundfile
```

## Usage

### Basic Usage

Start real-time detection with default settings:

```bash
python realtime_detection.py
```

### Advanced Options

```bash
# Higher confidence threshold for fewer false positives
python realtime_detection.py --confidence 0.3

# Show more detections
python realtime_detection.py --top-k 10

# Faster display updates
python realtime_detection.py --update-interval 0.5

# Custom model and labels
python realtime_detection.py --model custom_model.onnx --labels custom_labels.txt
```

### List Audio Devices

To see available microphones:

```bash
python realtime_detection.py --list-devices
```

## Command Line Arguments

- `--model`: Path to ONNX model file (default: `model.onnx`)
- `--labels`: Path to species labels file (default: `BirdNET_GLOBAL_6K_V2.4_Labels.txt`)
- `--confidence`: Minimum confidence threshold (default: 0.1)
- `--top-k`: Number of top predictions to show (default: 5)
- `--update-interval`: Display update interval in seconds (default: 1.0)
- `--list-devices`: List available audio input devices

## Display Interface

The real-time interface shows:

### Current Detections

```
🐦 Current Detections (Top 5):
----------------------------------------
 1. American Robin
    β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ 0.8542

 2. Song Sparrow
    β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ 0.3214
```

### Recent Activity

```
πŸ“Š Recent Activity (Last 10):
----------------------------------------
14:25:32 - American Robin (0.854)
14:25:28 - Song Sparrow (0.321)
14:25:15 - House Finch (0.287)
```

## Technical Details

### Audio Processing

- **Sample Rate**: 48kHz (BirdNET requirement)
- **Window Size**: 3 seconds (144,000 samples)
- **Buffer**: 6-second rolling buffer for continuous analysis
- **Processing**: 100ms audio blocks with threaded processing

### Performance Features

- **Non-blocking Audio**: Uses threading for audio capture and processing
- **Efficient Buffering**: Rolling deque buffer prevents memory buildup
- **Real-time Display**: Separate thread for UI updates
- **Graceful Shutdown**: Ctrl+C handling for clean exit

### Requirements

- **Python**: 3.7+ (recommended: 3.9+)
- **Audio Input**: Working microphone or audio input device
- **Memory**: ~200MB RAM for model and audio buffers
- **CPU**: Moderate CPU usage for real-time inference

## Troubleshooting

### Audio Issues

**No microphone detected:**

```bash
# List available devices
python realtime_detection.py --list-devices

# Check system audio settings
# Ensure microphone permissions are granted
```

**Audio quality issues:**

- Check microphone positioning (closer to birds)
- Reduce background noise
- Adjust confidence threshold

### Performance Issues

**High CPU usage:**

```bash
# Reduce update frequency
python realtime_detection.py --update-interval 2.0

# Increase confidence threshold
python realtime_detection.py --confidence 0.3
```

**Memory issues:**

- Close other applications
- The detector uses fixed-size buffers to prevent memory leaks

### Detection Issues

**No detections:**

- Lower confidence threshold: `--confidence 0.05`
- Check if birds are actually singing
- Verify microphone is working

**Too many false positives:**

- Increase confidence threshold: `--confidence 0.3`
- Reduce background noise
- Position microphone outdoors

## Example Sessions

### Backyard Birding

```bash
# Conservative detection for mixed environment
python realtime_detection.py --confidence 0.2 --top-k 3
```

### Bird Walk

```bash
# Sensitive detection for bird-rich areas
python realtime_detection.py --confidence 0.1 --top-k 8 --update-interval 0.5
```

### Indoor Testing

```bash
# High threshold for testing with recorded sounds
python realtime_detection.py --confidence 0.4 --top-k 5
```

## Tips for Best Results

1. **Positioning**: Place microphone outdoors or near open windows
2. **Timing**: Early morning and evening are typically best for bird activity
3. **Environment**: Quiet locations with minimal human/traffic noise
4. **Distance**: Microphone should be within 10-20 feet of singing birds
5. **Weather**: Calm, clear conditions provide best audio quality

## Stopping Detection

Press `Ctrl+C` at any time to stop the real-time detection and return to terminal.

The detector will display:

```
πŸ›‘ Detection stopped.
```