1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Webカメラから映像取得・画像撮影

Last updated at Posted at 2021-01-13

はじめに

ラズパイ用に、USB接続のWebカメラをもらった。
今回はその動作確認とプログラムのメモ。

カメラ

使用するカメラはこちら。
Logicool Brio Ultra HD Pro Webcam

接続の確認

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

が追加された。

動作確認

1. guvcview

まずインストールしたのは guvcview というソフト。

$ sudo apt-get install guvcview

以下のコマンドで実行。

$ guvcview &

実行されたはいいものの、最初の画像取得後動かなかった。
動画もとってみたが上手く保存されず。

2. fswebcam

調べたが解決しないため諦めて、次は fswebcam というものをインストール。

$ sudo apt-get install fswebcam

実行方法は

$ fswebcam img.jpg

撮影した画像が /home/[ユーザー名]/img.jpg として保存される。

python上で動かす

以下のコードを実行。

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/画像/カメラ画像/photo.jpg"
        cv2.imwrite(path,frame)

cap.release()
cv2.destroyAllWindows()

キーボードの s を押すと撮影、 q を押すと終了する。
撮影した画像は指定された場所に photo.jpg として保存される。

参考サイトはこちら。
【Raspberry Pi】webカメラを接続する方法

まとめ

ソフトウェア上よりも自分で書いたプログラムのほうが動作が安定するなんて。
硬貨識別の課題がんばる。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?