Previous → Play with Raspberry Pi Zero WH Camera Module Part 0 Next → Play with Raspberry Pi Zero WH Camera Module Part 2
In this article, we will set up python and check the operation of the camera module.
-[1. Python initial settings](# 1-python initial settings) -[1.1. Link settings](# 11-Link settings) -[2. Camera Module Connection](# 2-Camera Module Connection) -[2.1. Connection Settings](# 21-Connection Settings) -[2.2. Still image shooting test](# 22-Still image shooting test) -[3. Addition of various python libraries](# 3-Addition of various python libraries) - 3.1. OpenCV -[4. Try using OpenCV](# 4-Try using opencv) -[4.1. Display test](# 41-Display test)
In the version of Raspberry Pi OS introduced this time, the symbolic link of python was put on python2 (2.7.16), so I will change the link to python3 (3.7.3). As a side note, the Python library on the Raspberry Pi OS seems to be faster with apt-get than with pip.
#Replacing symbolic links
$ cd /usr/bin
$ sudo unlink python
$ sudo ln -s python3 python
After connecting the camera module cable to the Raspberry Pi main unit, enable the camera from "Settings" → "Raspberry Pi Settings" → "Interface". Then check if the camera module is connected properly.
#Check camera connection
$ vcgencmd get_camera
If "supported = 1 detected = 1" is displayed, the connection is successful.
As a test, I will take a still image.
#By default/home/Save to pi
$ sudo raspistill -o image.jpg
3.1. OpenCV
First, install the libraries that OpenCV depends on.
# libhdf5-xxx version check(This version is 103)
$ sudo apt-cache search libhdf5
#Installation of various libraries
$ sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103
$ sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
$ sudo apt-get install libatlas-base-dev
$ sudo apt-get install libjasper-dev
Then install OpenCV.
#OpenCV installation
$ sudo pip3 --default-timeout=1000 install opencv-python
If this is left as it is, ʻimport cv2may throw an error, so set the environment variable. Add the following to "~ / .bashrc" and execute
source ~ / .bashrc`.
# ~/.Added to bashrc
export LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1
This code displays the screen captured from the camera in grayscale and ends when the q key is pressed.
capture_test.py
import cv2
def capture():
# 0 is a camera number.
cap = cv2.VideoCapture(0)
print('### Break is \'q\' key.')
while(cap.isOpened()):
# 'ret' is a boolean.
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
# Break is 'q' key.
if cv2.waitKey(1) & 0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__=='__main__':
capture()
Recommended Posts