LoginSignup
0
2

More than 3 years have passed since last update.

webカメラを起動して静止画を撮影しローカルに保存

Posted at

やりたいこと

  • PCについているwebカメラを起動
  • 静止画を撮影
  • ローカルに保存する

環境

  • MacBook Pro (13-inch, 2019, Two Thunderbolt 3 ports)
  • python 3.7.0
  • opencv 3.4.2

実装

# VideoCaptureクラスのインスタンスを生成
    # 内蔵webカメラ使用時は0を指定
    cap = cv2.VideoCapture(0)

    # 正常に読み込めなかった場合
    if cap.isOpened() is False:
        print("IO Error")
    # 正常に読み込まれた場合
    else:
        # readメソッド
        # 返り値1 ret: フレームの画像が読み込めたか /  返り値2  frame: 画像の配列(ndarray)
        ret, frame = cap.read()
        image_path = "./images/"
        if (ret):
            # imwriteメソッド
            # 引数1:画像のパス 引数2:画像を表すndarrayオブジェクト
            # 画像パスの拡張子は.jpgや.pngが使用できる。
            cv2.imwrite(image_path + "image.png", frame)
        else:
            print("Read Error")

    cap.release() # カメラデバイスを終了する

0
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
0
2