> For the complete documentation index, see [llms.txt](https://notes.lcsrg.me/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.lcsrg.me/home-setup/raspberry/live-cam.md).

# Live cam

Next, we want to configure a live cam connected to the Raspberry. With a cam, there can be multiple applications:

* 24h live stream of whatever. Exposing a port in the local network should suffice.
* Take a picture at a certain point of the day and save it. Then, timelapse can be done.

source:&#x20;

To use cv2:

* Create virtual environment `python3 -m venv venv`
* Activate it `. venv/bin/activate`
* Install cv2 `pip install opencv-python`
* Install extra libs required `sudo apt-get install libatlas-base-dev`
* Run following script

```python
import cv2
import time

cap = cv2.VideoCapture(0)
ret, frame = cap.read()

def takePicture():
    (grabbed, frame) = cap.read()
    showimg = frame
    # cv2.imshow('img1', showimg)  # display the captured image
    cv2.waitKey(2000)
    #time.sleep(1) # Wait 300 miliseconds
    image = 'test-image.png'
    cv2.imwrite(image, frame)
    cap.release()
    return image

print(takePicture())
```

Play with waitKey(\*) to let some seconds to focus light. Also, adjust cam.

{% hint style="info" %}
<https://github.com/opencv/opencv-python>

<https://raspberrypi-guide.github.io/electronics/using-usb-webcams>

<https://stackoverflow.com/questions/53347759/importerror-libcblas-so-3-cannot-open-shared-object-file-no-such-file-or-dire>

<https://gist.github.com/Luxato/384a7c8a6bfdeba09d54cb5bd6b2c9fb>
{% endhint %}
