# Manual DepthAI installation

This guide will go through the first steps with setting up the OAK camera and DepthAI library.

 1. [Installing dependencies](#Installing%20dependencies)
 2. [Installing DepthAI](#Installing%20DepthAI)
 3. [Running OAK Viewer](#Running%20OAK%20Viewer)

### Installing dependencies

### Supported Platforms

 * [Ubuntu/Debian](#Manual%2520DepthAI%2520installation-Ubuntu%252FDebian)
 * [MacOS](#Manual%2520DepthAI%2520installation-MacOS)
 * [Windows 10/11](#Manual%2520DepthAI%2520installation-Windows%252010%252F11)
 * [Raspberry Pi](https://docs.luxonis.com/hardware/platform/deploy/to-rpi.md)
 * [Jetson Nano/Xavier](https://docs.luxonis.com/hardware/platform/deploy/to-jetson.md)
 * [Rock Pi](https://docs.luxonis.com/hardware/platform/deploy/to-rockpi.md)
 * [ROS](https://github.com/luxonis/depthai-ros)

 * [Windows 7](#Manual%2520DepthAI%2520installation-Windows%25207)
 * [Docker](#Manual%2520DepthAI%2520installation-Docker)
 * [WSL 2](#Manual%2520DepthAI%2520installation-WSL%25202)
 * [OpenSUSE](#Manual%2520DepthAI%2520installation-OpenSUSE)
 * [Kernel Virtual Machine (KVM)](#Manual%2520DepthAI%2520installation-Kernel%2520Virtual%2520Machine%2520(KVM))
 * [VMware](#Manual%2520DepthAI%2520installation-VMware)
 * [VirtualBox](#Manual%2520DepthAI%2520installation-VirtualBox)

Cant find your platform? [Contact us](https://discuss.luxonis.com/)

### Ubuntu/Debian

Execute the below command to install the required dependencies:

```bash
sudo wget -qO- https://docs.luxonis.com/install_dependencies.sh | bash
```

This should perform the following:

 * perform apt update and upgrade
 * install python3, pip3, cmake, git, udev (if not already installed)
 * install other dependencies like libusb, libudev, and others

> If OpenCV fails with illegal instruction after installing from PyPi, add:
> ```bash
> echo "export OPENBLAS_CORETYPE=ARMV8" >> ~/.bashrc
> source ~/.bashrc
> ```

### MacOS

Execute the below command to install the required dependencies:

```bash
curl -fL https://docs.luxonis.com/install_dependencies.sh | bash
```

This should perform the following:

 * install brew and git (if not already installed)

### Windows 10/11

You can install the requirements via the [Windows Installer](https://github.com/luxonis/depthai/releases) or manually using
Chocolatey package manager:

#### Installing via Chocolatey

To install Chocolatey and use it to install DepthAI’s dependencies do the following:

 * Right click on Start

 * Choose Windows PowerShell (Admin) and run the following:

```powershell
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
```

 * Close the PowerShell and then re-open another PowerShell (Admin) by repeating the first two steps.

 * Install Python and PyCharm

```powershell
choco install cmake git python pycharm-community -y
```

### Windows 7

Although we do not officially support Windows 7, members of the community [have had
success](https://discuss.luxonis.com/d/105-run-on-win7-sp1-x64-manual-instal-usb-driver) manually installing WinUSB using
[Zadig](https://zadig.akeo.ie/). After connecting your DepthAI device look for a device with USB ID: 03E7 2485 and install the
WinUSB driver by selecting WinUSB(v6.1.7600.16385) and then Install WCID Driver.

### Docker

We maintain a Docker image containing DepthAI, it’s dependencies and helpful tools in the [luxonis/depthai-library
repository](https://hub.docker.com/r/luxonis/depthai-library) on Docker Hub. It builds upon the
[luxonis/depthai-base](https://hub.docker.com/r/luxonis/depthai-base) image.

Run the rgb_preview.py example inside a Docker container on a Linux host (with the X11 windowing system):

```bash
docker pull luxonis/depthai-library
docker run --rm \
    --privileged \
    -v /dev/bus/usb:/dev/bus/usb \
    --device-cgroup-rule='c 189:* rmw' \
    -e DISPLAY=$DISPLAY \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    luxonis/depthai-library:latest \
    python3 /depthai-python/examples/ColorCamera/rgb_preview.py
```

To allow the container to update X11 you may need to run xhost local:root on the host.

> If you are using OAK POE device on Linux host machine, you should add --network=host argument to your docker command, so depthai
inside docker will be able to communicate with the OAK POE.

### WSL 2

Steps below were performed on WSL 2 running Ubuntu 20.04, while host machine was running Win10 20H2 (OS build 19042.1586).
Original tutorial [written here](https://discuss.luxonis.com/d/693-i-got-depthai-demo-to-run-in-wsl) by SputTheBot.

To get an OAK running on WSL 2, you first need to attach USB device to WSL 2. We have used
[usbipd-win](https://github.com/dorssel/usbipd-win/releases) (2.3.0) for that. Inside WSL 2 you also need to install depthai
dependencies ([Ubuntu/Debian](#Manual%2520DepthAI%2520installation-Ubuntu%252FDebian)) and [USB/IP client
tool](https://github.com/dorssel/usbipd-win/wiki/WSL-support#usbip-client-tools) (2 commands).

To attach the OAK camera to WSL 2, we have prepared a Python script below that you need to execute on the host computer (in Admin
mode).

```python
import time
import os
while True:
    output = os.popen('usbipd wsl list').read() # List all USB devices
    rows = output.split('\n')
    for row in rows:
        if ('Movidius MyriadX' in row or 'Luxonis Device' in row) and 'Not attached' in row: # Check for OAK cameras that aren't attached
            busid = row.split(' ')[0]
            out = os.popen(f'usbipd wsl attach --busid {busid}').read() # Attach an OAK camera
            print(out)
            print(f'Usbipd attached Myriad X on bus {busid}') # Log
    time.sleep(.5)
```

After that, you can check lsusb command inside the WSL 2 and you should be able to see Movidius MyriadX.

If instead you want to use usbipd-win >= 4.0, then the usbipd wsl command is not available anymore, so you need to use a different
script, originally [written here](https://discuss.luxonis.com/d/693-i-got-depthai-demo-to-run-in-wsl/5) by kazuya.

```python
import time
import subprocess

while True:
    output = subprocess.run('usbipd list', capture_output=True, encoding="UTF-8")
    rows = output.stdout.split('\n')
    for row in rows:
        if ('Movidius MyriadX' in row or 'Luxonis Device' in row) and 'Not shared' in row:
            busid = row.split(' ')[0]
            out = subprocess.run(f'usbipd bind -b {busid}', capture_output=True, encoding="UTF-8")
            print(out.stdout)
            print(f'Usbipd bind Myriad X')
        if ('Movidius MyriadX' in row or 'Luxonis Device' in row) and 'Shared' in row:
            busid = row.split(' ')[0]
            out = subprocess.run(f'usbipd attach -w -b {busid}', capture_output=True, encoding="UTF-8")
            print(out.stdout)
            print(f'Usbipd attached Myriad X on bus {busid}')
    time.sleep(0.5)
```

> Examples that don't show any frames (eg. IMU example) should work. We haven't spent enough time to get OpenCV display frames
inside WSL 2, but you could try it out yourself, some ideas
>
[here](https://stackoverflow.com/questions/65453763/python3-9-on-wsl2-ubuntu-20-04-how-to-display-image-using-cv2-opencv-python-4)
> .

### OpenSUSE

For openSUSE, available [in this official article](https://en.opensuse.org/SDB:Install_OAK_AI_Kit) on how to install the OAK
device on the openSUSE platform.

### Kernel Virtual Machine (KVM)

To access the OAK-D camera in the [Kernel Virtual Machine](https://www.linux-kvm.org/page/Main_Page), there is a need to attach
and detach USB devices on the fly when the host machine detects changes in the USB bus.

OAK-D camera changes the USB device type when it is used by DepthAI API. This happens in background when the camera is used
natively. But when the camera is used in a virtual environment the situation is different.

On your host machine, use the following code:

```text
SUBSYSTEM=="usb", ACTION=="bind", ENV{ID_VENDOR_ID}=="03e7", MODE="0666", RUN+="/usr/local/bin/movidius_usb_hotplug.sh depthai-vm"
SUBSYSTEM=="usb", ACTION=="remove", ENV{PRODUCT}=="3e7/2485/1", ENV{DEVTYPE}=="usb_device", MODE="0666", RUN+="/usr/local/bin/movidius_usb_hotplug.sh depthai-vm"
SUBSYSTEM=="usb", ACTION=="remove", ENV{PRODUCT}=="3e7/f63b/100", ENV{DEVTYPE}=="usb_device", MODE="0666", RUN+="/usr/local/bin/movidius_usb_hotplug.sh depthai-vm"
```

The script that the udev rule is calling (movidius_usb_hotplug.sh) should then attach/detach the USB device to the virtual
machine. In this case we need to call virsh command. For example, the script could do the following:

```text
#!/bin/bash
# Abort script execution on errors
set -e
if [ "${ACTION}" == 'bind' ]; then
  COMMAND='attach-device'
elif [ "${ACTION}" == 'remove' ]; then
  COMMAND='detach-device'
  if [ "${PRODUCT}" == '3e7/2485/1' ]; then
    ID_VENDOR_ID=03e7
    ID_MODEL_ID=2485
  fi
  if [ "${PRODUCT}" == '3e7/f63b/100' ]; then
    ID_VENDOR_ID=03e7
    ID_MODEL_ID=f63b
  fi
else
  echo "Invalid udev ACTION: ${ACTION}" >&2
  exit 1
fi
echo "Running virsh ${COMMAND} ${DOMAIN} for ${ID_VENDOR}." >&2
virsh "${COMMAND}" "${DOMAIN}" /dev/stdin <<END
<hostdev mode='subsystem' type='usb'>
  <source>
    <vendor id='0x${ID_VENDOR_ID}'/>
    <product id='0x${ID_MODEL_ID}'/>
  </source>
</hostdev>
END
exit 0
```

Note that when the device is disconnected from the USB bus, some udev environmental variables are not available (ID_VENDOR_ID or
ID_MODEL_ID), that is why you need to use PRODUCT environmental variable to identify which device has been disconnected.

The virtual machine where DepthAI API application is running should have defined a udev rules that identify the OAK-D camera. The
udev rule is described
[here](https://docs.luxonis.com/hardware/platform/deploy/usb-deployment-guide.md#USB%20deployment%20guide-Initial%20Connection-Debugging-Linux%20udev%20rules).

### VMware

Using the OAK-D device in a VMware requires some extra one-time settings that need to be set up for it to work.

First of all, make sure the USB controller is switched from USB2 to USB3. Go to Virtual Machine Settings -> USB Controller -> USB
compatibility and change to USB 3.1 (or USB 3.0 for older VMware versions, as available).

Depending on what state the device is, there could be two devices showing up, and both need to be routed to the VM. Those could be
visible at Player -> Removable Devices:

 * Intel Movidius MyriadX
 * Intel VSC Loopback Device or Intel Luxonis Device

In Linux OS, run these commands to give USB permissions for the regular user:

```bash
echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="03e7", MODE="0666"' | sudo tee /etc/udev/rules.d/80-movidius.rules
sudo udevadm control --reload-rules && sudo udevadm trigger
```

If Virtual Machine doesn’t detect the device, try the following: find and select option like Forget connection rule (for both
devices), then try running the DepthAI example again inside the VM. Choose to route to VM and select to not ask again (this is
important, as there is a timeout, and the device watchdog could get triggered if the host doesn’t start communication in few
seconds). You may need to repeat running the script a few times, until all gets set properly for VMware.

### VirtualBox

If you want to use VirtualBox to run the DepthAI source code, please make sure that you allow the VM to access the USB devices.
Also, be aware that by default, it supports only USB 1.1 devices, and DepthAI operates in two stages:

 1. For showing up when plugged in. We use this endpoint to load the firmware onto the device, which is a usb-boot technique. This
    device is USB2.

 2. For running the actual code. This shows up after USB booting and is USB3.

In order to support the DepthAI modes, you need to download and install [Oracle VM VirtualBox Extension
Pack](https://www.virtualbox.org/wiki/Downloads). Once this is installed, enable USB3 (xHCI) Controller in the USB settings.

Once this is done, you’ll need to route the Myriad as USB device from Host to the VBox. This is the filter for depthai before it
has booted, which is at that point a USB2 device:

Routing the not-yet-booted depthai to the VirtualBox

The last step is to add the USB Intel Loopback device. The depthai device boots its firmware over USB, and after it has booted, it
shows up as a new device.

This device shows just up when the depthai/OAK is trying to reconnect (during runntime, so right after running a pipeline on
depthai, such as python3 depthai_demo.py).

It might take a few tries to get this loopback device shown up and added, as you need to do this while depthai is trying to
connect after a pipeline has been built (and so it has at that point now booted its internal firmware over USB2).

For enabling it only once, you can see the loopback device here (after the pipeline has been started):

Find the loopback device right after you tell depthai to start the pipeline, and select it.

And then for permanently enabling this pass-through to virtual box, enable this in setting below: Making the USB Loopback Device
for depthai/OAK, to allow the booted device to communicate in virtualbox

Making the USB Loopback Device for depthai/OAK, to allow the booted device to communicate in virtualbox

And then for each additional depthai/OAK device you would like to pass through, repeat just this last loopback settings step for
each unit (as each unit will have its own unique ID).

### Installing DepthAI

After installing depthai dependencies, you can either refer to depthai-core for C++ development, or download the depthai Python
library via [PyPi](https://pypi.org/project/depthai/):

```bash
python3 -m pip install depthai
```

### Test Installation

We have [a set of examples](https://github.com/luxonis/depthai-python/tree/develop/examples) that should help you verify if your
setup was correct.

First, clone the [depthai-python](https://github.com/luxonis/depthai-python/tree/develop) repository and change directory into
this repo:

```bash
git clone https://github.com/luxonis/depthai-python.git
cd depthai-python
```

Next install the requirements for this repository. Note that we recommend installing the dependencies in a virtual environment, so
that they don't interfere with other Python tools/environments on your system.

 * For development machines like Mac/Windows/Ubuntu/etc., we recommend the [PyCharm](https://www.jetbrains.com/pycharm/) IDE, as
   it automatically makes/manages virtual environments for you, along with a bunch of other benefits. Alternatively, conda,
   pipenv, or virtualenv could be used directly (and/or with your preferred IDE).
 * For installations on resource-constrained systems, such as the Raspberry Pi or other small Linux systems, we recommend conda,
   pipenv, or virtualenv. To set up a virtual environment with virtualenv, run virtualenv venv && source venv/bin/activate.

Using a virtual environment (or system-wide, if you prefer), run the following to install the requirements for this example
repository:

```bash
cd examples
python3 install_requirements.py
```

Now, run the rgb_preview.py script from within examples directory to make sure everything is working:

```bash
python3 ColorCamera/rgb_preview.py
```

If all goes well a small window video display should appear. And example is shown below:

### Run Other Examples

After you have run this example, you can run other examples to learn about DepthAI possibilities. You can also proceed to:

 * Our tutorials, starting with a Hello World tutorial explaining the API usage step by step
   ([here](https://docs.luxonis.com/software/depthai/hello-world.md))
 * Our examples, containing implementations of various user use cases on DepthAI ([here](https://github.com/luxonis/oak-examples))

You can also proceed below to learn how to convert your own neural network to run on DepthAI.

And we also have online model training below, which shows you how to train and convert models for DepthAI:

 * [Online ML Training and model Conversion](https://github.com/luxonis/ai-tutorials/tree/master/colab-notebooks)

### Running OAK Viewer

[OAK Viewer](https://docs.luxonis.com/software-v3/depthai/tools/oak-viewer.md) is the visualization tool for DepthAI and OAK
cameras. It's a GUI application that will run a demo app by default, which will visualize all streams and run inference on the
device. It also allows you to change the configuration of the device. DepthAI viewer works for USB and POE cameras.

More info [here](https://docs.luxonis.com/software-v3/depthai/tools/oak-viewer.md).
