Re: How to get (void*) data into a Python array
Ronald Oussoren <[email protected]>
| Newsgroups | gmane.comp.python.pyobjc.devel |
|---|---|
| Message-ID | <[email protected]> |
On 4 Feb, 2011, at 18:16, Trevor Bentley wrote: > Hello, > > I am working on a small Python script to grab a single frame from the > iSight camera using PyObjC. I want to import the frame into a python > numpy array for manipulation, and I want to write it exclusively in > Python. I have fairly limited experience in Python and Objective-C, and > no prior experience with PyObjC. > > I have made some progress, and currently it is able to find the camera, > open it, and capture a frame. The frame data is (theoretically) > available in my QTCaptureDecompressedVideoOutput delegate's callback > function: > > - (void)captureOutput:(QTCaptureOutput *)captureOutput > didOutputVideoFrame:(CVImageBufferRef)videoFrame > withSampleBuffer:(QTSampleBuffer *)sampleBuffer > fromConnection:(QTCaptureConnection *)connection > > So I added "pdb.set_trace()" at the beginning of the callback, and am > trying to figure out how to get the raw bytes available in Python. > > The closest I have come is examining sampleBuffer: > -------------------------------------------------- > (Pdb) sampleBuffer > <QTSampleBuffer: 0x1044b42d0> > (Pdb) sampleBuffer.numberOfSamples() > 1 > (Pdb) sampleBuffer.lengthForAllSamples() > 2624000 > (Pdb) sampleBuffer.bytesForAllSamples() > 4674363392L That's a bug, PyObjC should be smart enough to know that it supposed to return a buffer (string/bytes/...) in bytesForAllSamples. I will fix this in a future version, but don't know yet when that will be. Sadly enough I'll have to write some C code for that. I haven't used ctypes myself beyond simple hello-world level scripts, but it should be possible to tell ctypes that the integer you get back from bytesForSamples is actually a 'void*', and that should give you a way to copy the buffer into a python string. Ronald ------------------------------------------------------------------------------ The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE: Pinpoint memory and threading errors before they happen. Find and fix more than 250 security defects in the development cycle. Locate bottlenecks in serial and parallel code that limit performance. http://p.sf.net/sfu/intel-dev2devfeb