Display USB camera video with Python OpenCV with Raspberry Pi

It's a very rudimentary story ... Probably the right way to make a surveillance camera with a Raspberry Pi and a genuine camera module, not with a USB camera, but with a Raspberry Pi camera jessie version (motion + v4l2 driver)](https://qiita. If you load the bcm2835-v4l2.ko kernel module as explained in com / rukihena / items / 95da3860f9ca86c39f8d), you should be able to do it by the following procedure, but I do not have the actual camera for Raspberry Pi, so I have confirmed it. not.

Preparation

  1. sudo apt-get install usbutils python3-opencv libcanberra-gtk3-module v4l-utils qv4l2
  2. In case of Ubuntu, the camera cannot be operated unless it belongs to the video group, so use sudo adduser login name video.
  3. If that doesn't work, use qv4l2 to see if you can see the camera image.
  4. You can check the frame rate and resolution supported by the camera with v4l2-ctl --list-formats-ext.

Connection confirmation

Make sure that the output of lsusb -t has Class = Video, Driver = uvcvideo

pi@raspberrypi:~ $ lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 2: Dev 3, If 3, Class=Audio, Driver=snd-usb-audio, 480M
        |__ Port 2: Dev 3, If 1, Class=Video, Driver=uvcvideo, 480M
        |__ Port 2: Dev 3, If 2, Class=Audio, Driver=snd-usb-audio, 480M
        |__ Port 2: Dev 3, If 0, Class=Video, Driver=uvcvideo, 480M
        |__ Port 3: Dev 4, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 4: Dev 5, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 4: Dev 5, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M

Python program

If you run the following program as python3 capture.py, the image from the camera will be displayed. If you use Ubuntu Mate Raspberry Pi, you cannot access / dev / video0, so please use sudo chmod a + rw / dev / video0.

capture.py


import cv2

capture = cv2.VideoCapture(0)
if capture.isOpened() is False:
  raise IOError

while(True):
  try:
    ret, frame = capture.read()
    if ret is False:
      raise IOError
    cv2.imshow('frame',frame)
    cv2.waitKey(1)
  except KeyboardInterrupt:
    #CTRL when finished+Press C
    break

capture.release()
cv2.destroyAllWindows()

Recommended Posts

Display USB camera video with Python OpenCV with Raspberry Pi
Camera capture with Python + OpenCV
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Use vl53l0x with Raspberry Pi (python)
Face recognition with camera with opencv3 + python2.7
Python beginner opens and closes interlocking camera with Raspberry Pi
Save video frame by frame with Python OpenCV
Raspberry Pi Security Infrared Camera (Python Edition)
Image acquisition from camera with Python + OpenCV
Working with GPS on Raspberry Pi 3 Python
I tried L-Chika with Raspberry Pi 4 (Python edition)
Get CPU information of Raspberry Pi with Python
Connect to MySQL with Python on Raspberry Pi
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Measure CPU temperature of Raspberry Pi with Python
GPGPU with Raspberry Pi
Binarization with OpenCV / Python
Python, OpenCV camera capture
DigitalSignage with Raspberry Pi
I got an error when I put opencv in python3 with Raspberry Pi [Remedy]
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Detect mask wearing status with OpenCV and Raspberry Pi
Display and shoot webcam video with Python Kivy [GUI]
USB boot with Raspberry Pi 4 Model B (3) LVM edition
Face detection from images taken with Raspberry Pi camera
[Windows] [Python] Camera calibration of fisheye lens with OpenCV
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
A memorandum when making a surveillance camera with Raspberry Pi
Let's operate GPIO of Raspberry Pi with Python CGI
Raspberry Pi with Elixir, which is cooler than Python
Mutter plants with Raspberry Pi
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
YouTube video management with Python 3
[Python] Using OpenCV with Python (Basic)
Loop video loading with opencv
Raspberry Pi + Python + OpenGL memo
Face detection with Python + OpenCV
raspberry pi 1 model b, python
Using OpenCV with Python @Mac
Install OpenCV4 on Raspberry Pi 3
Update Python for Raspberry Pi to 3.7 or later with pyenv
Play with the Raspberry Pi Zero WH camera module Part 1
I tried running Movidius NCS with python of Raspberry Pi3
Create an LCD (16x2) game with Raspberry Pi and Python
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
Create a simple video analysis tool with python wxpython + openCV
SSD 1306 OLED can be used with Raspberry Pi + python (Note)
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
I made a surveillance camera with my first Raspberry PI.
Convert video to black and white with ffmpeg + python + opencv
Cut out frames from video by 1 second with Python + OpenCV
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
Organic EL character display SO1602 Python library for operation implemented with AdaFruit specifications (for Raspberry Pi)
Shining life with Python and OpenCV
[Raspberry Pi] Stepping motor control with Raspberry Pi
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Control the motor with a motor driver using python on Raspberry Pi 3!
Use PointGrey camera with Python (PyCapture2)
[Python] Using OpenCV with Python (Image Filtering)