2009-10-16 20 views
5

Aşağıdaki PyObjC senaryosu var:Snow Leopard'da iSight çerçevelerini Python ile nasıl yakalarım?

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) 

program raporları hiç hata ama iSight'ım yeşil ışık aktive asla ve temsilcinin kare yakalama geri arama asla denir. Benim elde çıkışı var:

$ 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: Ben, ben PySight denemelisiniz cevap yok Lütfen ama Xcode 64 bit CocoaSequenceGrabber derleme olamaz çünkü işe yaramaz.

cevap

3

Sorununuz, burada bir olay döngüsünüz olmamasıdır. Bunu bağımsız bir komut dosyası olarak yapmak isterseniz, nasıl oluşturulacağını öğrenmeniz gerekir. PyObjC XCode otomatik ile sizin için ayarlanmış şablonların:

from PyObjCTools import AppHelper 
AppHelper.runEventLoop() 

Senaryonuzun üstündeki o eklemek için çalışılıyor, ancak, AppHelper (muhtemelen NSApplicationMain) içinde bir şey bir Plist dosyası ana sınıf ayıklamak için beklediğini gösterir dan. Bir PyObjc talk bu örneğe benzer bir setup.py dosyası oluşturma ve py2app kullanarak bu alabilirsiniz:

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, 
    )), 
) 
+1

@Dan: pointer için teşekkürler! Bu Mac OS X programlama ile ilk deneyimim ve kesinlikle clueless. Komut dosyasının sonunda, bunun yerine "Plist" e ihtiyaç duymak yerine, AppHelper.runConsoleEventLoop() işlevini çağırmaya çalıştım. Şimdi benim problemim ana iş parçasını ele geçirmesi ve asla geri dönmemesi. Bir modülde güzel bir şekilde müdahaleci olmayan bir şekilde sarmayı umuyordum. –

+0

Bir iş parçacığı oluşturabilir ve iş parçacığıyla işleyebilirsiniz. QT iş parçacığı değildir, ancak bu bağlamda, tüm QT öğelerinizi bir iş parçacığında yapmanız gerekir, bu da ana iş parçacığı değildir. Zamanlayıcılara da bakabilirsin, ama muhtemelen bunun için hala bir ana döngüye ihtiyacın var. – Dan

+0

Görünüşe göre, ana iş parçacığı olmalı. Eğer Thread (target = AppHelper.runConsoleEventLoop) .start() 'yi kullanırsam, bir sürü hata alıyorum ve hiçbir şey işe yaramıyor: ' 2009-10-20 12: 58: 32.075 Python [2054: 4903] *** __NSAutoreleaseNoPool(): Nesnenin 0x1018065b0 sınıfı NSCFString, hiçbir havuz yokken otomatik olarak yayınlandı - yalnızca sızdırıyor 2009-10-20 12: 58: 32.078 Python [2054: 4903] *** __NSAutoreleaseNoPool(): Nesne 0x101821130 sınıf NSCFString, havuz yerinde - sadece sızıntı 2009-10-20 12: 58: 32.078 Python [2054: 4903] *** __NSAutoreleaseNoPool(): Nesne 0x101828df0 sınıf NSCFString autorelease ' –

2

Andrew Straw dan motmot's camiface kütüphaneye denemek vermelidir. Aynı zamanda firewire kameraları ile çalışır, ama aynı zamanda, aradığınız şey olan isight ile de çalışır. öğretici itibaren

:

import motmot.cam_iface.cam_iface_ctypes as cam_iface 
import numpy as np 

mode_num = 0 
device_num = 0 
num_buffers = 32 

cam = cam_iface.Camera(device_num,num_buffers,mode_num) 
cam.start_camera() 
frame = np.asarray(cam.grab_next_frame_blocking()) 
print 'grabbed frame with shape %s'%(frame.shape,) 
+0

havalı! link için teşekkürler –

+0

http://www.incm.cnrs-mrs.fr/LaurentPerrinet/SimpleCellDemo – meduz

+0

bazı basit örnekleri görebilirsiniz bu orijinal kütüphane: https://github.com/motmot/libcamiface – dashesy

İlgili konular