[PYTHON] Play with the Raspberry Pi Zero WH camera module Part 1

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)

1. python initialization

1.1. Link settings

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

2. Camera module connection

2.1. Connection settings

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.

2.2. Still image shooting test

As a test, I will take a still image.

#By default/home/Save to pi
$ sudo raspistill -o image.jpg

3. Addition of various python libraries

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 executesource ~ / .bashrc`.

# ~/.Added to bashrc
export LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1

4. Try it with OpenCV

4.1. Display test

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

Play with the Raspberry Pi Zero WH camera module Part 1
Display images taken with the Raspberry Pi camera module
Try to visualize the room with Raspberry Pi, part 1
Let's make a cycle computer with Raspberry Pi Zero (W, WH)
Play around with the pythonista3 ui module
Discord bot with python raspberry pi zero with [Notes]
Observe the Geminids meteor shower with Raspberry Pi 4
Play with your Ubuntu desktop on your Raspberry Pi 4
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
GPGPU with Raspberry Pi
Raspberry Pi video camera
DigitalSignage with Raspberry Pi
Using the digital illuminance sensor TSL2561 with Raspberry Pi
How to use the Raspberry Pi relay module Python
Control brushless motors with GPIOs on Raspberry Pi Zero
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Make an umbrella reminder with Raspberry Pi Zero W
Face detection from images taken with Raspberry Pi camera
A memorandum when making a surveillance camera with Raspberry Pi
Display USB camera video with Python OpenCV with Raspberry Pi
Mutter plants with Raspberry Pi
Create a web surveillance camera with Raspberry Pi and OpenCV
Python beginner opens and closes interlocking camera with Raspberry Pi
I tried using the DS18B20 temperature sensor with Raspberry Pi
I made a surveillance camera with my first Raspberry PI.
Play with ASE MD module
Building a distributed environment with the Raspberry PI series (Part 2: PiServer analysis and alternative system design)
Real-time classification of multiple objects in the camera image with deep learning of Raspberry Pi 3 B + & PyTorch
I tried to automate the watering of the planter with Raspberry Pi
I learned how the infrared remote control works with Raspberry Pi
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Periodically log the value of Omron environment sensor with Raspberry Pi
Let's make an IoT shirt with Lambda, Kinesis, Raspberry Pi [Part 1]
[Raspberry Pi] Stepping motor control with Raspberry Pi
Play with a turtle with turtle graphics (Part 1)
Use vl53l0x with Raspberry Pi (python)
raspberry pi 1 model b, node-red part 17
Servo motor control with Raspberry Pi
Serial communication with Raspberry Pi + PySerial
OS setup with Raspberry Pi Imager
Play with PIR sensor module [DSUN-PIR]
Try L Chika with raspberry pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Using a webcam with Raspberry Pi
Building a distributed environment with the Raspberry PI series (Part 1: Summary of availability of diskless clients by model)
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
Let me show you the performance of the new Raspberry pi zero (v1.3)!
Use python on Raspberry Pi 3 to light the LED with switch control!
Logging the value of Omron environment sensor with Raspberry Pi (USB type)
WeWork office keys can now be unlocked / locked with an IC card using the smart lock "SESAME mini" and Raspberry Pi Zero WH.
Measure SIM signal strength with Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
Ask for Pi with the bc command
"Honwaka Notification Lamp" on Raspberry Pi Part 2
Play handwritten numbers with python Part 2 (identify)
Raspberry Pi Security Infrared Camera (Python Edition)
Hello World with Raspberry Pi + Minecraft Pi Edition
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Build a Tensorflow environment with Raspberry Pi [2020]