1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

dSPACEAdvent Calendar 2024

Day 22

ControlDesk計測と同時にカメラを連動

Posted at

1. はじめに

計測中の様子をカメラで録画するときってありますよね.
別に撮るだけだったら特に苦ではないですが,計測データと録画映像とをリンクさせるとなると話が別です.
後でスタート位置が合うように調整するという,とてつもなく面倒な作業が待っています.
だったらそもそも計測開始時に録画を開始すればいいのではというお話です.

2. 必要なもの

  • USBカメラ
  • OpenCVライブラリ

3. 記述

まず,USBカメラのcapture IDを取得します.

import cv2

for i in range(20):
    cap = cv2.VideoCapture(i)
    if not cap.isOpened():
        continue 
    print(f"Camera {i}: {cap.get(cv2.CAP_PROP_FRAME_WIDTH)}x{cap.get(cv2.CAP_PROP_FRAME_HEIGHT)}")
    cap.release()

実行結果は後程.

以下のコードを実行すると別画面で録画始まり,Enterキーで終了します.

import cv2
import time

cap = cv2.VideoCapture(2, cv2.CAP_DSHOW)
    
fmt = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
fps = 30
size = (640, 360)
    
write = cv2.VideoWriter("C:/path/to/file/hoge.m4v", fmt, fps, size)
    
while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, size)
    write.write(frame)
    cv2.imshow('WebCam', frame)
        
    if cv2.waitKey(1) == 13:
        break
write.release()
cap.release()
cv2.destroyAllWindows()

後は,スレッドやRecodingの呼び出しと組み合わせれば完成です.

4. おわりに

USB出力でない一眼レフなどとも連動したいところです.

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?