[PYTHON] Connect two USB cameras to Raspberry Pi 4

Launch Raspberry Pi 4

Prepare a micro SD card 32GB

SD32GB.jpg

Buy class 10 It was 1480 yen for a set of 2 Gigastones (August 11, 2020) Amazon sales floor Your purchase is already FAT32 formatted and will be recognized as a disc on your Windows 10 PC. Disc32GB.png

Download NOOBS

Download page

NOOBS1.png Click the NOOBS icon and the screen will change as follows NOOBS2.png

Click the "Download ZIP" label under "NOOBS" to download the ZIP file.

Unzip the downloaded file "NOOBS_v3_4_0.zip (2,379,232KB)" and move the files and holder inside to the SD card removal disk.

Raspberry Pi 4 settings

    1. Insert SD card
  1. Connect your display with a small HDMI connector cable
    1. Insert the USB dongle of wireless mouse & keyboard into the USB port Connect the 4.5V power supply to the micro USB connector (turn on the switch)

Select "Japanese", select "Raspbian" as the OS, and proceed with the installation to start Rasbian.

raspbian.png

SSH and VNC settings

Reference page I tried connecting to Raspberry Pi 4 with SSH and VNC

Please refer to the e-book for details Book_Raspi.jpg Amazon sales floor of books

Via WiFi on Windows 10 PC

    1. How to operate commands with SSH
  1. How to display on the screen without connecting the display with VNC Is explained gently with figures.

Built-in OpenCV

Qiita posted on December 11, 2019 Easy to put OpenCV on Raspberry Pi4 To the street.

I installed it on August 15, 2020, but the prepared version was the same as Qiita above. I have installed Ver 4.1.0.25.

pip3 install opencv-contrib-python==4.1.0.25

Run python program

The latest version of Raspbian has a built-in Mu editor, so you can run python programs with this editor. Mu_icon.png Mu_Editor.png Click the mu icon to launch the editor Mu1.png

Please refer to the following HP for the operation method of Mu editor https://codewith.mu/

Connect two USB cameras

I connected two types of two types of USB cameras I had to the USB connector. IMG1.jpg

Simply two captures failed. So I decided to change the screen size and FPS. Here is the HP that I used as a reference https://dev.classmethod.jp/articles/opencv-webcam-setting/

Set the screen size to 800 x 600 pixels 15FPS seems to exceed the ability of RasPi4 due to an error or mouse not moving Cascus continuous operation is possible at 10 Fps At 5FPS, the screen display of the two cameras did not cause an error even after 10 hours.

It is a code that can be displayed on the screen for a long time in the end

#===============================
# Raspi4 conect to two USBCam
#
# Ver1 2020/08/16    T.F.
#===============================
# -*- coding: utf-8 -*-
import cv2

#-------------------------------
#Webcam
#-------------------------------
WIDTH = 800
HEIGHT = 600
#ID:0  FPS:5,7.5,10,15  //20
#ID:2  FPS:5    ,10,15      //20
FPS = 5

#-------------------------------
#
#-------------------------------
def cam_set(DEVICE_ID,WIDTH,HEIGHT,FPS):
    # video capture
    cap = cv2.VideoCapture(DEVICE_ID)

    #Format / resolution / FPS settings
    #cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M','J','P','G'))
    cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('Y','U','Y','V'))
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
    cap.set(cv2.CAP_PROP_FPS, FPS)

    #Acquisition of format / resolution / FPS
    fourcc = decode_fourcc(cap.get(cv2.CAP_PROP_FOURCC))
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    fps = cap.get(cv2.CAP_PROP_FPS)
    print("ID:{} fourcc:{} fps:{} width:{} height:{}".format(DEVICE_ID, fourcc, fps, width, height))

    return cap

#-------------------------------
#
#-------------------------------
def decode_fourcc(v):
        v = int(v)
        return "".join([chr((v >> 8 * i) & 0xFF) for i in range(4)])

#-------------------------------
#
#-------------------------------
def main():
    #--------------------------------------
    DEVICE_ID = 0
    cap1=cam_set(DEVICE_ID,WIDTH,HEIGHT,FPS)

    #--------------------------------------
    DEVICE_ID = 2
    cap2=cam_set(DEVICE_ID,WIDTH,HEIGHT,FPS)

    #--------------------------------------
    while True:
        #-----------------------
        #Camera image acquisition
        ret1, frame1 = cap1.read()
        if(frame1 is None):
            continue

        #-----------------------
        #Camera image acquisition
        ret2, frame2 = cap2.read()
        if(frame2 is None):
            continue

        #---------------------------
        im_h=cv2.hconcat([frame1,frame2])

        #---------------------------
        #Image display
        cv2.imshow('frame1', im_h)

        #--------------------------------------
        #Queue input judgment(1ms wait)
        #without waitKey, imshow()Cannot be displayed
        # 'q'Exit the loop when typed
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    #------------------------------
    #Destroy VideoCapture object
    cap1.release()
    cap2.release()
    cv2.destroyAllWindows()

#-------------------------------
if __name__ == '__main__':
	main()

Displayed screen

cam1.png

With the following code, the camera images were arranged horizontally and combined and displayed in one frame.

im_h=cv2.hconcat([frame1,frame2])

Future development

A stereo camera is constructed using two inexpensive USB cameras, and the distance from the "object detected" position shift to the object is calculated and used as the control eye of the robot arm.

Next, consider a light "object detection" program on RasPi4.

Recommended Posts

Connect two USB cameras to Raspberry Pi 4
Connect your Raspberry Pi to your smartphone using Blynk
Connect to MySQL with Python on Raspberry Pi
Introduced python3-OpenCV3 to Raspberry Pi
I talked to Raspberry Pi
Introducing PyMySQL to raspberry pi3
[Raspberry PI & Garmin GLO] Until you connect Bluetooth GPS to Raspberry Pi
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Port FreeRTOS to Raspberry Pi 4B
USB over ethernet using Raspberry pi
Output from Raspberry Pi to Line
[Raspberry Pi] Changed Python default to Python3
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
USB boot on Raspberry Pi 4 Model B
How to install NumPy on Raspberry Pi
Connect to the console of Raspberry PI and display local IP and SD information
How to use Raspberry Pi pie camera Python
Raspberry Pi backup
Connect to mysql
From setting up Raspberry Pi to installing Python environment
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Easy IoT to start with Raspberry Pi and MESH
Try to visualize the room with Raspberry Pi, part 1
How to use the Raspberry Pi relay module Python
USB boot with Raspberry Pi 4 Model B (3) LVM edition
Control power on / off of USB port of Raspberry Pi
Output to "7-segment LED" using python on Raspberry Pi 3!
Display USB camera video with Python OpenCV with Raspberry Pi
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?
Connect python to mysql
DigitalSignage with Raspberry Pi
Raspberry Pi 4 setup memo
Cython on Raspberry Pi
Raspberry Pi system monitoring
Easy introduction to home hack with Raspberry Pi and discord.py
Update Python for Raspberry Pi to 3.7 or later with pyenv
I want to disable interrupts on Raspberry Pi (≒ DI / EI)
I tried to automate [a certain task] using Raspberry Pi
Change the message displayed when logging in to Raspberry Pi
How to get temperature from switchBot thermo-hygrometer using raspberry Pi
[Raspberry Pi] Minimum device driver creation memo to output GPIO
Translate I2C device driver for Arduino to Python for Raspberry pi
I sent the data of Raspberry Pi to GCP (free)
[Memo] Connect Raspberry Pi Zero to the Internet with a single USB cable! (Use nfs, no SD card / keyboard / mouse / monitor required)