[PYTHON] Display images taken with the Raspberry Pi camera module

I attached the camera module V2.1 to my Raspberry Pi 4, so I tried to create a simple program. I had a lot of trouble doing that, so I'll leave a note so I don't have to look it up again.

Programs using openCV

openCV has a function to capture the camera and also has a screen display function, which is very convenient.

test.py


import cv2
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH,640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
while True:
    _,image = camera.read()
#Line to add effect
    cv2.imshow('camera',image)
    key = cv2.waitKey(1)
    if key != -1:
        break
camera.release()
cv2.destroyAllWindows()

Pressing a key on the window will stop it.

Program description

The camera is initialized in the lower part. If you do not enter a specific value for the screen size, the image will be corrupted. Also, the argument of VideoCapture is the device ID, so it may change depending on the environment.

python


camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH,640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,480)

Taking images continuously is done in the lower part. The camera.read method puts new image data in frame every time the loop goes around.

python


while True:
    _,image = camera.read()

The window display is done in the lower part. It will not be displayed without the waitKey method.

python


    cv2.imshow('camera',image)
    key = cv2.waitKey(1)

The process of closing the opened window is done in the lower part.

python


camera.release()
cv2.destroyAllWindows()

Program using picamera and pygame

Only openCV is good, but considering the icon display and mouse operation on the same window, it seems more convenient to use pygame. (By the way, I tried to capture images in the picamera module.) There were many sites on the net that used the capture method, but this time I use the capture_continuous method.

test2.py


import picamera
from picamera.array import PiRGBArray
import pygame
import cv2                                                           #For effects
width = 480
height = 640
camera = picamera.PiCamera()
camera.resolution = (height,width)
pygame.init()
screen = pygame.display.set_mode((width,height))
rawCapture = PiRGBArray(camera,size=(height,width))
for frame in camera.capture_continuous(rawCapture,format='rgb',use_video_port=True):
    image = frame.array
#Line to add effect
    surf = pygame.surfarray.make_surface(image)
    screen.blit(surf,(0,0))
    pygame.display.flip()
    rawCapture.truncate(0)

To quit, press Ctrl + C to stop the program.

Program description

The camera is initialized in the lower part. The size of the image to be captured is determined by resolution, but for some reason the vertical and horizontal arrangements are reversed.

python


camera = picamera.PiCamera()
camera.resolution = (height,width)

The window display of pygame is done in the lower part.

python


pygame.init()
screen = pygame.display.set_mode((width,height))

Taking images continuously is done in the lower part. The capture_continuous method creates a generator and puts new image data in the frame every time the loop goes around. In addition, use_video_port = True aims at speeding up by extracting data directly from the video port. Also, new images cannot be captured without the final truncate method.

python


rawCapture = PiRGBArray(camera,size=(height,width))
for frame in camera.capture_continuous(rawCapture,format='rgb',use_video_port=True):
    ....
    ....
    rawCapture.truncate(0)

The description of pygame is done in the lower part. The image is made into an RGB 3D array (480,640,3) in the frame.array part. The pygame.surfarray.make_surface method converts it to the surface format that can be used for pygame, the blit method writes it to the screen, and the flip method updates the screen.

python


    image = frame.array
    surf = pygame.surfarray.make_surface(image)
    screen.blit(surf,(0,0))
    pygame.display.flip()

Addition of effects (common to openCV and picamera + pygame)

(Although the picamera module also has effects) Here, I will add the effects of openCV. You can change the program's #adding effect line to an edge detection image by changing it as shown below.

python


    image = cv2.Canny(image,100,200)

The image variable that receives the image data is in the RGB array format and can be calculated in the same way as the numpy array, so you can directly manipulate the RGB values ​​to edit the image.

at the end

I'm worried that the image loading is slow with either method. I would like to find out if there is a way to make it faster.

Recommended Posts

Display images taken with the Raspberry Pi camera module
Face detection from images taken with Raspberry Pi camera
Play with the Raspberry Pi Zero WH camera module Part 1
Display USB camera video with Python OpenCV with Raspberry Pi
Image display taken with the built-in ISIGHT
Observe the Geminids meteor shower with Raspberry Pi 4
GPGPU with Raspberry Pi
Raspberry Pi video camera
DigitalSignage with Raspberry Pi
Using the digital illuminance sensor TSL2561 with Raspberry Pi
Try to visualize the room with Raspberry Pi, part 1
How to use the Raspberry Pi relay module Python
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
A memorandum when making a surveillance camera with Raspberry Pi
Mutter plants with Raspberry Pi
Python beginner opens and closes interlocking camera with Raspberry Pi
Upload images taken with an action camera with a Mapillay script
I tried using the DS18B20 temperature sensor with Raspberry Pi
I made a surveillance camera with my first Raspberry PI.
Input and output display images directly using the frame buffer (/ dev / fb0) on Raspberry Pi
Control the display of RGB LED matirix electric bulletin board freely with Raspberry Pi 3B +
[Raspberry Pi] Stepping motor control with Raspberry Pi
Control the motor with a motor driver using python on Raspberry Pi 3!
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
I tried to automate the watering of the planter with Raspberry Pi
Serial communication with Raspberry Pi + PySerial
OS setup with Raspberry Pi Imager
Try L Chika with raspberry pi
Make a Kanji display compass with Raspberry Pi and Sense Hat
VPN server construction with Raspberry Pi
Real-time classification of multiple objects in the camera image with deep learning of Raspberry Pi 3 B + & PyTorch
Graph display of household power consumption with 3GPI and Raspberry Pi
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
Try moving 3 servos with Raspberry Pi
Using a webcam with Raspberry Pi
Periodically log the value of Omron environment sensor with Raspberry Pi
The fastest way to get camera images regularly with python opencv
Display the signal strength RSSI of a specific SSID (raspberry pi (linux))
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
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)
Measure SIM signal strength with Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
Ask for Pi with the bc command
Raspberry Pi Security Infrared Camera (Python Edition)
Play around with the pythonista3 ui module
Hello World with Raspberry Pi + Minecraft Pi Edition
Build a Tensorflow environment with Raspberry Pi [2020]
Try using the camera with Python's OpenCV
Display Python 3 in the browser with MAMP
Get BITCOIN LTP information with Raspberry PI
Try fishing for smelt with Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Use the Grove sensor on the Raspberry Pi
Improved motion sensor made with Raspberry Pi
Try Object detection with Raspberry Pi 4 + Coral
Power SG-90 servo motor with raspberry pi
Working with sensors on Mathematica on Raspberry Pi
Use PIR motion sensor with raspberry Pi