gpt4 book ai didi

python - 如何在 Snow Leopard 中使用 Python 捕获 iSight 帧?

转载 作者:太空狗 更新时间:2023-10-30 01:10:38 27 4
gpt4 key购买 nike

我有以下 PyObjC 脚本:

from Foundation import NSObject
import QTKit
error = None
capture_session = QTKit.QTCaptureSession.alloc().init()
print 'capture_session', capture_session
device = QTKit.QTCaptureDevice.defaultInputDeviceWithMediaType_(QTKit.QTMediaTypeVideo)
print 'device', device, type(device)
success = device.open_(error)
print 'device open success', success, error
if not success:
raise Exception(error)
capture_device_input = QTKit.QTCaptureDeviceInput.alloc().initWithDevice_(device)
print 'capture_device_input', capture_device_input, capture_device_input.device()
success = capture_session.addInput_error_(capture_device_input, error)
print 'session add input success', success, error
if not success:
raise Exception(error)
capture_decompressed_video_output = QTKit.QTCaptureDecompressedVideoOutput.alloc().init()
print 'capture_decompressed_video_output', capture_decompressed_video_output
class Delegate(NSObject):
def captureOutput_didOutputVideoFrame_withSampleBuffer_fromConnection_(self, captureOutput, videoFrame, sampleBuffer, connection):
print videoFrame, sampleBuffer, connection
delegate = Delegate.alloc().init()
print 'delegate', delegate
capture_decompressed_video_output.setDelegate_(delegate)
print 'output delegate:', capture_decompressed_video_output.delegate()
success = capture_session.addOutput_error_(capture_decompressed_video_output, error)
print 'capture session add output success', success, error
if not success:
raise Exception(error)
print 'about to run session', capture_session, 'with inputs', capture_session.inputs(), 'and outputs', capture_session.outputs()
capture_session.startRunning()
print 'capture session is running?', capture_session.isRunning()
import time
time.sleep(10)

程序没有报告任何错误,但 iSight 的绿灯从未被激活,委托(delegate)的帧捕获回调也从未被调用。这是我得到的输出:

$ python prueba.py 
capture_session <QTCaptureSession: 0x1006c16f0>
device Built-in iSight <objective-c class QTCaptureDALDevice at 0x7fff70366aa8>
device open success (True, None) None
capture_device_input <QTCaptureDeviceInput: 0x1002ae010> Built-in iSight
session add input success (True, None) None
capture_decompressed_video_output <QTCaptureDecompressedVideoOutput: 0x104239f10>
delegate <Delegate: 0x10423af50>
output delegate: <Delegate: 0x10423af50>
capture session add output success (True, None) None
about to run session <QTCaptureSession: 0x1006c16f0> with inputs (
"<QTCaptureDeviceInput: 0x1002ae010>"
) and outputs (
"<QTCaptureDecompressedVideoOutput: 0x104239f10>"
)
capture session is running? True

PS:请不要回答我应该试试 PySight,我试过了,但它不起作用,因为 Xcode 无法在 64 位中编译 CocoaSequenceGrabber。

最佳答案

这里的问题是您没有事件循环。如果您想将其作为一个独立的脚本来执行,则必须弄清楚如何创建一个脚本。 PyObjC XCode 模板自动为您设置:

from PyObjCTools import AppHelper
AppHelper.runEventLoop()

尝试将其插入脚本的顶部,然而,显示 AppHelper(可能是 NSApplicationMain)中的某些内容需要一个 plist 文件来从中提取主类。你可以通过创建一个 setup.py 文件并使用 py2app 来获得它,就像这个来自 PyObjc talk 的例子一样。 :

from distutils.core import setup
import py2app
plist = dict(
NSPrincipalClass='SillyBalls',
)
setup(
plugin=['SillyBalls.py'],
data_files=['English.lproj'],
options=dict(py2app=dict(
extension='.saver',
plist=plist,
)),
)

关于python - 如何在 Snow Leopard 中使用 Python 捕获 iSight 帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1576593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com