# rpicv2

**Automatic Raspberry Pi Camera driver for OpenCV.**
No driver setup. No manual commands. Just import and go.

```python
from rpicv2 import Camera

cam = Camera()
frame = cam.read()
```

---

## How It Works

`rpicv2` automatically:
1. Loads `v4l2loopback` to create a virtual `/dev/video0`
2. Pipes the `libcamera-vid` feed through `ffmpeg` into that virtual device
3. Opens the device with OpenCV so you can use it like any normal webcam

---

## Requirements

Install system dependencies first:

```bash
sudo apt-get install v4l2loopback-dkms ffmpeg
```

`libcamera` comes pre-installed on Raspberry Pi OS.

---

## Installation

```bash
pip install rpicv2
```

Or install from source:

```bash
git clone https://github.com/AsrayGopa/rpicv2.git
cd rpicv2
pip install .
```

---

## Usage

### Basic

```python
from rpicv2 import Camera

cam = Camera()

while True:
    frame = cam.show()  # displays frame in a window

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cam.release()
```

### Context Manager

```python
from rpicv2 import Camera
import cv2

with Camera() as cam:
    while True:
        frame = cam.read()
        cv2.imshow("Feed", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
```

### Custom Resolution / FPS

```python
cam = Camera(width=1920, height=1080, fps=60)
```

---

## API

### `Camera(width=1280, height=720, fps=30, device=0)`

| Parameter | Default | Description |
|-----------|---------|-------------|
| `width`   | `1280`  | Frame width in pixels |
| `height`  | `720`   | Frame height in pixels |
| `fps`     | `30`    | Frames per second |
| `device`  | `0`     | `/dev/videoN` device number |

### Methods

| Method | Returns | Description |
|--------|---------|-------------|
| `read()` | `np.ndarray` | Read one frame |
| `show(window="RPiCam")` | `np.ndarray` | Read and display one frame |
| `release()` | `None` | Stop pipeline and release resources |

---

## License

MIT
