xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #1231: webcam.py

File webcam.py, 3.5 KB (added by Antoine Martin, 4 years ago)

working capture script

Line 
1from Foundation import NSObject
2from PyObjCTools import AppHelper
3import QTKit
4PIXEL_FORMAT_NAMES = {}
5for x in dir(QTKit):
6    if x.startswith("kCVPixelFormatType_"):
7        v = getattr(QTKit, x)
8        PIXEL_FORMAT_NAMES[v] = x[len("kCVPixelFormatType_"):]
9
10print("PIXEL_FORMAT_NAMES=%s" % PIXEL_FORMAT_NAMES)
11import time
12time.sleep(1)
13
14error = None
15capture_session = QTKit.QTCaptureSession.alloc().init()
16print('capture_session', capture_session)
17device = QTKit.QTCaptureDevice.defaultInputDeviceWithMediaType_(QTKit.QTMediaTypeVideo)
18print('device', device, type(device))
19success = device.open_(error)
20print('device open success', success, error)
21if not success:
22    raise Exception(error)
23capture_device_input = QTKit.QTCaptureDeviceInput.alloc().initWithDevice_(device)
24print('capture_device_input', capture_device_input, capture_device_input.device())
25success = capture_session.addInput_error_(capture_device_input, error)
26print('session add input success', success, error)
27if not success:
28    raise Exception(error)
29capture_decompressed_video_output = QTKit.QTCaptureDecompressedVideoOutput.alloc().init()
30print('capture_decompressed_video_output', capture_decompressed_video_output)
31
32class Delegate(NSObject):
33    def captureOutput_didOutputVideoFrame_withSampleBuffer_fromConnection_(self, captureOutput, videoFrame, sampleBuffer, connection):
34        print("********************************************************")
35        vf = videoFrame
36        print("videoFrame=%s" % vf)
37        #print(dir(videoFrame))
38        QTKit.CVPixelBufferRetain(vf)
39        print("colorspace=%s" % QTKit.CVImageBufferGetColorSpace(vf))
40        pf = QTKit.CVPixelBufferGetPixelFormatType(vf)
41        print("pixel format=%s" % pf)
42        if pf!=QTKit.kCVPixelFormatType_32ARGB:
43            print("sorry, pixel format %s not supported" % PIXEL_FORMAT_NAMES.get(pf, pf))
44            return
45        print("width=%s" % QTKit.CVPixelBufferGetWidth(vf))
46        print("height=%s" % QTKit.CVPixelBufferGetHeight(vf))
47        print("stride=%s" % QTKit.CVPixelBufferGetBytesPerRow(vf))
48        size = QTKit.CVPixelBufferGetDataSize(vf)
49        print("datasize=%s" % size)
50        print("planar=%s" % QTKit.CVPixelBufferIsPlanar(vf))
51        print("padding=%s" % (QTKit.CVPixelBufferGetExtendedPixels(vf, None, None, None, None),))
52        QTKit.CVPixelBufferLockBaseAddress(vf, 0)
53        buf = QTKit.CVPixelBufferGetBaseAddress(vf)
54        print("address=%s" % buf)
55        print("as buffer=%s" % buf.as_buffer(size))
56        QTKit.CVPixelBufferUnlockBaseAddress(vf, 0)
57        QTKit.CVPixelBufferRelease(vf)
58        #print("bytesPerRow=%s" % getattr(videoFrame, "bytesPerRow", 0))
59        #print("ContainsRGB=%s" % getattr(videoFrame.attributes, "ContainsRGB", None))
60        #print("sampleBuffer=%s" % sampleBuffer)
61        #print("connection=%s" % connection)
62        print("********************************************************")
63delegate = Delegate.alloc().init()
64print('delegate', delegate)
65capture_decompressed_video_output.setDelegate_(delegate)
66print('output delegate:', capture_decompressed_video_output.delegate())
67success = capture_session.addOutput_error_(capture_decompressed_video_output, error)
68print('capture session add output success', success, error)
69if not success:
70    raise Exception(error)
71print('about to run session', capture_session, 'with inputs', capture_session.inputs(), 'and outputs', capture_session.outputs())
72capture_session.startRunning()
73print('capture session is running?', capture_session.isRunning())
74#AppHelper.runEventLoop()
75AppHelper.runConsoleEventLoop()