実現したいこと
ウェブカメラの画像をpythonで表示・保存しようとしています。
ウェブカメラ(0、1、2とします)は3台あって、各々PC本体のUSBポートまたはUSBハブに接続して使用します。
0と1をPC本体のUSBポートに、2をUSBハブに接続すると3つの画面を順番に保存することができました。
0をPC本体のUSBポートに、1と2をUSBハブに接続すると0と1の画面しか保存できず、2の画面は保存できず、下のようなエラーメッセージが出ました。原因と対策を教えてください。
発生している問題・エラーメッセージ
C:・・・\Python\testCV157 [ WARN:1@20.231] global cap_msmf.cpp:471 `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): OnReadSample() is called with error status: -1072875772 [ WARN:1@20.245] global cap_msmf.cpp:483 `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): async ReadSample() call is failed with error status: -1072875772 [ WARN:0@20.253] global cap_msmf.cpp:1759 CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -1072875772 Traceback (most recent call last): File "・・・\Python\testCV157\testCV157.py", line 37, in <module> schedule.run_pending() File "・・・\Python\Python311\Lib\site-packages\schedule\__init__.py", line 822, in run_pending default_scheduler.run_pending() File "・・・\Python\Python311\Lib\site-packages\schedule\__init__.py", line 100, in run_pending self._run_job(job) File "・・・\Python\Python311\Lib\site-packages\schedule\__init__.py", line 172, in _run_job ret = job.run() ^^^^^^^^^ File "・・・\Python\Python311\Lib\site-packages\schedule\__init__.py", line 693, in run ret = self.job_func() ^^^^^^^^^^^^^^^ File "・・・\Python\testCV157\testCV157.py", line 30, in job cv2.imwrite(fname2, frame2) cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:783: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
該当のソースコード
import cv2 import datetime import schedule import time capture0 = cv2.VideoCapture(0) capture1 = cv2.VideoCapture(1) capture2 = cv2.VideoCapture(2) def job():#一台ずつ順番に撮影し、その時刻で保存する ret, frame0 = capture0.read() strdate0=datetime.datetime.now().strftime('%Y%m%dT%H%M%S.%f') fname0=".\\files3\\"+"image0_" + strdate0 + ".png" cv2.imwrite(fname0, frame0) ret, frame1 = capture1.read() strdate1=datetime.datetime.now().strftime('%Y%m%dT%H%M%S.%f') fname1=".\\files3\\"+"image1_" + strdate1 + ".png" cv2.imwrite(fname1, frame1) ret, frame2 = capture2.read() strdate2=datetime.datetime.now().strftime('%Y%m%dT%H%M%S.%f') fname2=".\\files3\\"+"image2_" + strdate2 + ".png" cv2.imwrite(fname2, frame2) #do job every 10 seconds schedule.every(1/6).minutes.do(job) while True: schedule.run_pending() time.sleep(1)
試したこと
デバイスマネージャ→カメラで、HD USB Cameraは3台とも正常動作していることを確認しました。
別のPythonプログラムを実行した結果は以下の通りです。
port number 0 Find!
port number 1 Find!
port number 2 Find!
Number of connected camera: 3
cv2.CAP_DSHOW(DirectShow)を明示的に指定して実行した結果、0と1には正しい画像が保存されていましたが、2には真っ暗な画像が保存されていました。ソースコードは以下の通りです。
Python
1def job(): 2 for camera_number in range(0, 3): 3 # for windows -> cv2.CAP_DSHOW 4 cap = cv2.VideoCapture(camera_number,cv2.CAP_DSHOW) 5 ret, frame = cap.read() 6 7 if camera_number==0: 8 strdate0=datetime.datetime.now().strftime('%Y%m%dT%H%M%S.%f') 9 fname0=".\\files4\\"+"image0_" + strdate0 + ".png" 10 cv2.imwrite(fname0, frame) 11 12 elif camera_number==1: 13 strdate1=datetime.datetime.now().strftime('%Y%m%dT%H%M%S.%f') 14 fname1=".\\files4\\"+"image1_" + strdate1 + ".png" 15 cv2.imwrite(fname1, frame) 16 17 elif camera_number==2: 18 strdate2=datetime.datetime.now().strftime('%Y%m%dT%H%M%S.%f') 19 fname2=".\\files4\\"+"image2_" + strdate2 + ".png" 20 cv2.imwrite(fname2, frame)


回答1件
あなたの回答
tips
プレビュー