[PYTHON] Video acquisition / image shooting from a webcam

Introduction

I got a USB-connected webcam for Raspberry Pi. This time, the operation check and the memo of the program.

camera

Click here for the camera to use. Logicool Brio Ultra HD Pro Webcam

Check connection

$ lsusb
Bus 002 Device006: ID 046d:085e Logitech, Inc.
$ ls /dev/video*
/dev/video0  /dev/video1  /dev/video2  /dev/video3

Was added.

Operation check

  1. guvcview The first software I installed was guvcview.
$ sudo apt-get install guvcview

Execute with the following command.

$ guvcview &

It ran, but it didn't work after the first image acquisition. I tried to take a video, but it didn't save well.

  1. fswebcam I checked it, but it didn't work, so I gave up and installed fswebcam.
$ sudo apt-get install fswebcam

How to execute

$ fswebcam img.jpg

The captured image is saved as img.jpg in/home/[username]/.

Run on python

Execute the following code.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)

    key = cv2.waitKey(1)

    if key == ord('q'):
        break
    if key == ord('s'):
        path = "/home/kono/image/カメラimage/photo.jpg "
        cv2.imwrite(path,frame)

cap.release()
cv2.destroyAllWindows()

Press s on the keyboard to shoot, and press q to finish. The captured image is saved as photo.jpg in the specified location.

Click here for the reference site. [Raspberry Pi] How to connect a webcam

Summary

The operation of the program I wrote is more stable than that of the software. Coin identification issues Do your best.

Recommended Posts

Video acquisition / image shooting from a webcam
How to take a captured image from a video (OpenCV)
Make a Santa classifier from a Santa image
I tried to cut out a still image from the video
Image acquisition from camera with Python + OpenCV
A memo that detects and returns an image acquired from a webcam with Django's OpenCV
Generate a vertical image of a novel from text data
Get an image from a web page and resize it
Save the input video from the capture board as an image
Try creating a Kubernetes pod from a GitLab Container Registry image
Create a GCE instance from a GCR Docker image using terraform