私は、MacBook Proに組み込まれているApple iSightカメラからPython(バージョン2.7または2.6)とPyObjC(バージョン2.2)を使用して単一のフレームをキャプチャしようとしています。PythonとPyObjCを使用してApple iSightからフレームをキャプチャする方法は?
出発点として、this old StackOverflow質問を使用しました。それが意味をなさないことを確認するために、私はApple's MyRecorderの例とクロスリファレンスしています。残念ながら、私のスクリプトは動作しません。
私の大きな疑問は、次のとおりです。
- 私はカメラを正しく初期化するだろうか?
- イベントループを正しく開始していますか?
- 私がやるべき他の設定はありましたか?
以下に貼り付けられたスクリプトの例では、startImageCapture()を呼び出した後、CaptureDelegateから「Got a frame ...」というメッセージが表示されます。ただし、カメラのライトがオンになることはなく、デリゲートのコールバックは決して実行されません。
また、startImageCapture()中にエラーは発生せず、すべての関数が成功すると主張し、正常にiSightデバイスを検出します。 pdbのセッションオブジェクトを分析すると、有効な入出力オブジェクトがあり、出力には代理人が割り当てられ、別のプロセスではデバイスは使用されておらず、startRunning()が呼び出された後にセッションは実行中とマークされます。あなたが提供することができます任意の助け
#!/usr/bin/env python2.7
import sys
import os
import time
import objc
import QTKit
import AppKit
from Foundation import NSObject
from Foundation import NSTimer
from PyObjCTools import AppHelper
objc.setVerbose(True)
class CaptureDelegate(NSObject):
def captureOutput_didOutputVideoFrame_withSampleBuffer_fromConnection_(self, captureOutput,
videoFrame, sampleBuffer,
connection):
# This should get called for every captured frame
print "Got a frame: %s" % videoFrame
class QuitClass(NSObject):
def quitMainLoop_(self, aTimer):
# Just stop the main loop.
print "Quitting main loop."
AppHelper.stopEventLoop()
def startImageCapture():
error = None
# Create a QT Capture session
session = QTKit.QTCaptureSession.alloc().init()
# Find iSight device and open it
dev = QTKit.QTCaptureDevice.defaultInputDeviceWithMediaType_(QTKit.QTMediaTypeVideo)
print "Device: %s" % dev
if not dev.open_(error):
print "Couldn't open capture device."
return
# Create an input instance with the device we found and add to session
input = QTKit.QTCaptureDeviceInput.alloc().initWithDevice_(dev)
if not session.addInput_error_(input, error):
print "Couldn't add input device."
return
# Create an output instance with a delegate for callbacks and add to session
output = QTKit.QTCaptureDecompressedVideoOutput.alloc().init()
delegate = CaptureDelegate.alloc().init()
output.setDelegate_(delegate)
if not session.addOutput_error_(output, error):
print "Failed to add output delegate."
return
# Start the capture
print "Initiating capture..."
session.startRunning()
def main():
# Open camera and start capturing frames
startImageCapture()
# Setup a timer to quit in 10 seconds (hack for now)
quitInst = QuitClass.alloc().init()
NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(10.0,
quitInst,
'quitMainLoop:',
None,
False)
# Start Cocoa's main event loop
AppHelper.runConsoleEventLoop(installInterrupt=True)
print "After event loop"
if __name__ == "__main__":
main()
ありがとう:
は、ここでは、コードです!
クールマン、btwあなたは自分自身を「受け入れた」とマークする必要があります:) –
このスクリプトはうまくいきますが、バイトファイルを書くときには失敗します。バイトモードでファイルを開くには、open( 'filename'、 'w')をopen( 'filename'、 'wb')に変更する必要があります。 – andli