LoginSignup
17
19

More than 3 years have passed since last update.

Mac内蔵カメラの動画をpythonでキャプチャ

Posted at

目的

Mac内蔵カメラの動画をpythonでキャプチャした際の備忘録です。

準備

環境:mac os x 10.13 High Sierra
python3
opencv2ライブラリ

コード

カメラから動画を撮影するのコードそのまま実行して動きました。

グレイスケール画像表示は下記

sample-grey.py
import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

カラー画像表示は下記

sample-color.py
import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

テスト

$ python sample-grey.py

スクリーンショット 2019-06-30 11.56.50.png

--

$ python sample-color.py

スクリーンショット 2019-06-30 11.57.25.png

備考

Videoデバイス番号はどこから参照すれば良いかわからず決め打ちしています。
(Videoデバイス番号はffmpegを下記の通りコマンド実行した際の、FaceTime HD Cameraが該当?確かではありません。)

$ ffmpeg -list_devices true -f avfoundation -i dummy
ffmpeg version 4.1.2 Copyright (c) 2000-2019 the FFmpeg developers
[AVFoundation input device @ 0x7feac1504980] AVFoundation video devices:
[AVFoundation input device @ 0x7feac1504980] [0] FaceTime HD Camera
[AVFoundation input device @ 0x7feac1504980] [1] Capture screen 0
[AVFoundation input device @ 0x7feac1504980] AVFoundation audio devices:
[AVFoundation input device @ 0x7feac1504980] [0] Soundflower (2ch)
[AVFoundation input device @ 0x7feac1504980] [1] Soundflower (64ch)
[AVFoundation input device @ 0x7feac1504980] [2] Built-in Microphone

参考

VideoCapture Class Reference
カメラから動画を撮影する

17
19
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
17
19