[PYTHON] Raspberry Pi 4B initial setting

1.First of all

I bought Raspberry 4B a while ago, so I will leave it as a memorandum of settings. In addition, writing of OS image etc. is not described this time. Because the OS is included in the SD card of the kit, and the installation work has been greatly simplified compared to before, so I thought that there was no need to write it. Also, opencv is 4.5.1 (latest version as of 2021.1.3) (for no particular reason). I couldn't install it with pip3 install opencv-python because I often get an error when displaying an image as a window and it is difficult to deal with it. (The cause is unknown)

Purchased: LABISTS Raspberry Pi 4 4B (4GB memory)

2. Raspberry Pi initial settings

Describe the initial setting method. Describes library update, function activation, Japanese setting, and default user deletion.

2-1. Library update

Execute the following command to update the library. You will be asked if you want to run it halfway, so type y (yes).

$ sudo apt update
$ sudo apt upgrade

2-2. Enabling the function

Enable the function for camera and remote control (remote control settings are not described this time). First, execute the following command to enable the function.

$ sudo raspi-config

The following screen will appear. Enable 3 Interface Option → P1 camera. If you do remote desktop, enable P2 and P3. (Do you need P2?)

remmina_ICTcircle_192.168.11.11_202111-1547.684396.pngremmina_ICTcircle_192.168.11.11_202111-1541.667595.png

2-3. Japanese support

Japanese uses fcitx-mozc. (According to the environment of the main PC)

$ sudo apt-get install fcitx-mozc
$ im-config -n fcitx
$ sudo shutdown -r now

After rebooting, launch Fcitx Configuration from Preferences.

remmina_ICTcircle_192.168.11.11_202111-15029.627441.png

In the Input Method tab, there is a + button at the bottom, so select this and select mozc from the windows that appear. remmina_ICTcircle_192.168.11.11_202111-15236.581880.png

Switch the tab to Global Config, select [Trigger Input Method], and press [Half-width/Full-width key] in the window that appears to set the trigger key. (With this setting, the on/off setting for Japanese input will work)

remmina_ICTcircle_192.168.11.11_202111-15246.981421.png

2-4. Delete existing user pi

If you are using an initial user due to security issues, it seems that you can use it as a stepping stone to break in. There is also a story that data was extracted by NASA in the past ( There is also a source like this ). It seems that there is no particular problem for personal use, but I will do something that can be easily done just in case. The official describes the method, so proceed as it is. The user name to be created is XXX. (Please read as appropriate)

2-4-1. Creating a new user

$ sudo adduser XXX
$ sudo usermod -a -G adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,input,netdev,gpio,i2c,spi XXX

2-4-2. Eliminate pi auto-login processing

The process to change lightdm.conf and autologin.conf this time is not officially described, but it is done because an error occurs at the time of deluser. The error is that deluser can't because the process tty1 is always up. There was a person who had a similar event in Forum , and described how to deal with it. (I couldn't just process lightdm.conf, but maybe I could just change autologin.conf.)

$sudo nano /etc/lightdm/lightdm.conf

remmina_ICTcircle_192.168.11.11_202111-162447.929147.png

$ sudo nano /etc/systemd/system/[email protected]/autologin.conf

autologin_1.jpg

$ sudo systemctl daemon-reload
$ sudo systemctl restart [email protected]
$ sudo pkill -u pi

After this, log in as the new user XXX.

$ sudo deluser -remove-home pi

The initial setting is completed by the above operation.

3. OpenCV installation

Since I have a Raspberry Pi, I want to suppress image relations. Therefore, we will introduce OpenCV, which is a major library.

3-1. Preparation

Refer to OpenCV official . There was a statement that it was necessary to officially prepare a related library. Therefore, install the following library.

$ sudo apt install build-essential
$ sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

After preparing the library, drop the source file from github. I will drop it in my home directory and work on it.

$ cd
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git

3-2. Build

Build using the file you dropped earlier. First, create a build directory inside the opencv directory and work in it.

$ mkdir opencv/build
$ cd opencv/build

Create a makefile. Use what you need after -D. Also, if you are concerned about the license in a company etc., it may be better to set OPENCV_ENABLE_NONFREE = OFF .

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D ENABLE_NEON=ON \
    -D ENABLE_VFPV3=ON \
    -D BUILD_TESTS=OFF \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D CMAKE_SHARED_LINKER_FLAGS=-latomic \
    -D BUILD_EXAMPLES=OFF ..
$ make -j4
$ sudo make install

The execution time for the [make -j4] part took about 95 minutes. (Starts at 20:22 and ends at 21:56)

This completes the installation of OpenCV.

test

$ python3
> import cv2

If there is no error here, the installation is successful. Now you can easily perform video processing and face detection using cv2.VideoCapture. Since it was a big deal, I performed face detection.

import cv2

#Store webcam video in variables
cap = cv2.VideoCapture(0)

#Path of face detection model included in OpenCV
path = '/usr/local/share/opencv4/haarcascades/haarcascade_frontalface_default.xml'
#Create an instance of a face detection classifier
face_model = cv2.CascadeClassifier(path)

#It is shown as a movie by outputting the images one by one to the screen by loop processing.
while 1:
    #Get video information as an image from variables. Substitute the image for frame.
    ret, frame = cap.read()
    #Convert to gray image and detect face with face detection classifier
    gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_model.detectMultiScale(gray_img)

    #Face detection results are stored in faces. The information contained in one detection result is x,y coordinate and detection area(Rectangle)Width and height of.
    for (x,y,w,h) in faces:
        #Draw a rectangle on the image from the detection result information. green(0,200,50), Line width 2pixel
        cv2.rectangle(frame, (x,y), (x+w, y+h), (0,200,50),2)

    #Output processing to window
    cv2.imshow('cap',frame)
    #End processing. Press q to exit
    if cv2.waitKey(1) & 0xFF==ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

It was detected without any problem. It is also possible to detect multiple faces.

Reference site

Change default user name

OpenCV

Recommended Posts

Raspberry Pi 4B initial setting
Raspberry pi initial setting (for myself)
Raspbian initial settings (Raspberry Pi 4)
raspberry pi 1 model b, python
Port FreeRTOS to Raspberry Pi 4B
raspberry pi 1 model b, node-red part 17
USB boot on Raspberry Pi 4 Model B
Raspberry Pi backup
Build OpenCV-Python environment on Raspberry Pi B +
Why detectMultiScale () is slow on Raspberry Pi B +
Introduced Ceph on Kubernetes on Raspberry Pi 4B (ARM64)
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
What is Raspberry Pi?
GPGPU with Raspberry Pi
pigpio on Raspberry pi
Raspberry Pi video camera
Raspberry Pi Bad Knowledge
Let's do Raspberry Pi?
DigitalSignage with Raspberry Pi
Raspberry Pi 4 setup memo
Cython on Raspberry Pi
Raspberry Pi system monitoring
From setting up Raspberry Pi to installing Python environment
Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot
USB boot with Raspberry Pi 4 Model B (3) LVM edition
Introduced python3-OpenCV3 to Raspberry Pi
Indoor monitoring using Raspberry Pi
Mutter plants with Raspberry Pi
Install Raspberry Pi OS (Raspbian)
[Note] Installing vmware ESXi on Arm Fling on Raspberry Pi 4B
I talked to Raspberry Pi
Introducing PyMySQL to raspberry pi3
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
Raspberry Pi + Python + OpenGL memo
[Python] numpy.empty initial value setting
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Programd automatic start at startup with Raspberry Pi 3B + systemd Summary
[Raspberry Pi] Stepping motor control with Raspberry Pi
Testing uart communication on Raspberry Pi
ConoHa VPS (ubuntu 18.04) Initial setting memo
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
USB over ethernet using Raspberry pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Serial communication with Raspberry Pi + PySerial
Install ghoto2 on Raspberry Pi (memo)
Output from Raspberry Pi to Line
OS setup with Raspberry Pi Imager
Try using ArUco on Raspberry Pi
Try L Chika with raspberry pi
OpenCV installation procedure on Raspberry Pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4