Пример #1
0
def main(flip_v = False, alpha = 128, device = "/dev/spidev0.0"):
  with picamera.PiCamera() as camera:
    #camera.resolution = (640, 480)
    camera.resolution = (80, 60)
    camera.framerate = 12
    camera.vflip = flip_v
    camera.start_preview()
    camera.fullscreen = False
    # Add the overlay directly into layer 3 with transparency;
    # we can omit the size parameter of add_overlay as the
    # size is the same as the camera's resolution
    o = camera.add_overlay(np.getbuffer(a), size=(320,240), layer=3, alpha=int(alpha), crop=(0,0,80,60), vflip=flip_v)
    try:
      time.sleep(0.2) # give the overlay buffers a chance to initialize
      with Lepton(device) as l:
        while True:
          time.sleep(1) #slow down

          tmpfile = "tmp.jpg"
          image = capture(flip_v = False)
          cv2.imwrite(tmpfile, image)
          #Added by sco
          img = detect(tmpfile)

          #a[:lepton_buf.shape[0], :lepton_buf.shape[1], :] = lepton_buf
          if img is not None:
             a[:img.shape[0], :img.shape[1], :] = img
             o.update(np.getbuffer(a))
    except Exception:
      traceback.print_exc()
    finally:
      camera.remove_overlay(o)
Пример #2
0
def togglecolor(channel):
    global togsw,o,curcol,col,ovl,gui,alphaValue
    # step up the color to next in list
    curcol = colorcycle(colors,curcol)
    # map colorname to RGB value for new color
    col = colormap(curcol)
    # if overlay is inactive, ignore button:
    if togsw == 0:
        print "Color button pressed, but ignored --- Crosshair not visible."
    # if overlay is active, drop it, change color, then show it again
    else:
        print "Set new color: " + str(curcol) + "  RGB: " + str(col) 
        if guivisible == 0:
            # reinitialize array:
            ovl = np.zeros((height, width, 3), dtype=np.uint8)
            patternswitch(ovl,0)
            if 'o' in globals():
                camera.remove_overlay(o)
            o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue)
        else:
            # reinitialize array
            gui = np.zeros((height, width, 3), dtype=np.uint8)
            creategui(gui)
            patternswitch(gui,1)
            if 'o' in globals():
                camera.remove_overlay(o)
            o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue)
    return
def togglepatternZoomOut():
    global togsw,o,curpat,col,ovl,gui,alphaValue
    # if overlay is inactive, ignore button:
    if togsw == 0:
        zoom_out()
	ycenter = int(ycenter - int(math.fabs(zoomcount - 14))/2)
	if zoomcount == 0:
            ycenter = cdefaults.get('ycenter')
	print "Pattern button pressed, but ignored --- Crosshair not visible."
    # if overlay is active, drop it, change pattern, then show it again
    else:
        if guivisible == 0:
	    zoom_out()
            # reinitialize array:
            ovl = np.zeros((height, width, 3), dtype=np.uint8)
            patternswitcherZoomOut(ovl,0)
            if 'o' in globals():
                camera.remove_overlay(o)
            o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue)
        else:
	    zoom_out()
            # reinitialize array
            gui = np.zeros((height, width, 3), dtype=np.uint8)
            creategui(gui)
            patternswitcherZoomOut(gui,1)
            if 'o' in globals():
                camera.remove_overlay(o)
            o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue)
    return
Пример #4
0
    def notify(self, data):
        # publish the data remotely
        if self.pipe and len(self._remote_listeners) > 0:
            # TODO: is there any way to know how many recipients of the pipe?
            # If possible, we would detect it's 0, because some listener closed
            # without unsubscribing, and we would kick it out.
            # => use zmq_socket_monitor() to detect connection/disconnection and
            # update the count of subscribers, or detect when a remote_listener
            # is gone (if there is a way to associate it)

            # TODO thread-safe for self.pipe ?
            dformat = {"dtype": str(data.dtype), "shape": data.shape}
            self.pipe.send_pyobj(dformat, zmq.SNDMORE)
            self.pipe.send_pyobj(data.metadata, zmq.SNDMORE)
            try:
                if not data.flags["C_CONTIGUOUS"]:
                    # if not in C order, it will be received incorrectly
                    # TODO: if it's just rotated, send the info to reconstruct it
                    # and avoid the memory copy
                    raise TypeError("Need C ordered array")
                self.pipe.send(numpy.getbuffer(data), copy=False)
            except TypeError:
                # not all buffers can be sent zero-copy (e.g., has strides)
                # try harder by copying (which removes the strides)
                logging.debug("Failed to send data with zero-copy")
                data = numpy.require(data, requirements=["C_CONTIGUOUS"])
                self.pipe.send(numpy.getbuffer(data), copy=False)

        # publish locally
        DataFlowBase.notify(self, data)
Пример #5
0
def togglepattern2(channel):
    global togsw,o,curpat2,col,ovl,gui,alphaValue
    # if overlay is inactive, ignore button:
    if togsw == 0:
        print "Pattern button pressed, but ignored --- Crosshair not visible."
    # if overlay is active, drop it, change pattern, then show it again
    else:
        curpat2 += 1
        print "Set new pattern: " + str(curpat2) 
        if curpat2 > patterns.maxpat:     # this number must be adjusted to number of available patterns!
            curpat2 = 1
        if guivisible == 0:
            # reinitialize array:
            ovl = np.zeros((height, width, 3), dtype=np.uint8)
            patternswitcher(ovl,0)
            if 'o' in globals():
                camera.remove_overlay(o)
            o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue)
        else:
            # reinitialize array
            gui = np.zeros((height, width, 3), dtype=np.uint8)
            creategui(gui)
            patternswitcher(gui,1)
            if 'o' in globals():
                camera.remove_overlay(o)
            o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue)
    return
def togglepatternZoomIn():
    global togsw,o,curpat,col,ovl,gui,alphaValue,ycenter,zoomcount
    # if overlay is inactive, ignore button:
    if togsw == 0:
        print "Pattern button pressed, but ignored --- Crosshair not visible."
	zoom_in()
	ycenter = ycenter + zoomcount
    # if overlay is active, drop it, change pattern, then show it again
    else:
        if guivisible == 0:
            zoom_in()
	    # reinitialize array:
            ovl = np.zeros((height, width, 3), dtype=np.uint8)
            patternswitcherZoomIn(ovl,0)
	    if 'o' in globals():
                camera.remove_overlay(o)
            o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue)
	else:
            # reinitialize array
            zoom_in()
	    gui = np.zeros((height, width, 3), dtype=np.uint8)
	    creategui(gui)
            patternswitcherZoomIn(gui,1)
            if 'o' in globals():
                camera.remove_overlay(o)
            o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue)
    return
Пример #7
0
 def _testBitcast(self, x, datatype, shape):
   with self.test_session():
     tf_ans = tf.bitcast(x, datatype)
     out = tf_ans.eval()
     buff_after = np.getbuffer(out)
     buff_before = np.getbuffer(x)
     self.assertEqual(buff_before, buff_after)
     self.assertEqual(tf_ans.get_shape(), shape)
Пример #8
0
def npHalfArrayToOIIOFloatPixels(width, height, channels, npPixels):
    if oiio.VERSION < 10800:
        # Read half-float pixels into a numpy float pixel array
        oiioFloatsArray = np.frombuffer(np.getbuffer(np.float16(npPixels)), dtype=np.float32)
    else:
        # Read half-float pixels into a numpy float pixel array
        oiioFloatsArray = np.frombuffer(np.getbuffer(np.float16(npPixels)), dtype=np.uint16)

    return oiioFloatsArray
Пример #9
0
    def __init__(self, array, struct_arr_ptr):
        print "copying data to device"

        self.data = cuda.to_device(array)
        self.shape, self.dtype = array.shape, array.dtype

        cuda.memcpy_htod(int(struct_arr_ptr),
                         numpy.getbuffer(numpy.int32(len(array[0]))))
        cuda.memcpy_htod(int(struct_arr_ptr) + 8,
                         numpy.getbuffer(numpy.intp(int(self.data))))
Пример #10
0
def oiioFloatPixelsToNPHalfArray(width, height, channels, oiioFloats):

    if oiio.VERSION < 10800:
        # Read float pixels into a numpy half-float pixel array
        npPixels = np.frombuffer(np.getbuffer(np.float32(oiioFloats)), dtype=np.float16)
    else:
        # Convert uint16 values into a numpy half-float pixel array
        npPixels = np.frombuffer(np.getbuffer(np.uint16(oiioFloats)), dtype=np.float16)

    return npPixels
Пример #11
0
 def fset(self, inst, value):
     
     nprow = getattr(inst, 'NumpyArrayTable__'+self.name)
     #~ print 'fset',self.name,  nprow, value
     
     if nprow is None:
         nprow = self.NumpyArrayTableClass()
         setattr(inst, 'NumpyArrayTable__'+self.name, nprow)
     
     if value is None:
         if hasattr(inst, self.name+'_array') :
             delattr(inst, self.name+'_array')
         nprow.shape = None
         nprow.dtype = None
         nprow.blob = None
         nprow.units = None
         nprow.compress = None
         return 
     
     if self.arraytype == np.ndarray:
         assert (type(value) == np.ndarray) or (type(value) == np.memmap) , 'Value is not np.array or np.memmap but {}'.format(type(value))
     if self.arraytype == pq.Quantity:
         assert type(value) == pq.Quantity , '{} {} {} value is not pq.Quantity'.format(inst.__class__.__name__, self.name, value)
     
     shape = ('{},'*value.ndim)[:-1].format(*value.shape)
     if shape.endswith(',') : shape = shape[:-1]
     nprow.shape = shape
     
     nprow.dtype = value.dtype.str
     
     if self.compress == 'blosc':
         blob = blosc.compress(value.tostring(), typesize = value.dtype.itemsize, clevel= 9)
     else:
         if not value.flags['C_CONTIGUOUS']:
             buf = np.getbuffer(np.array(value, copy = True))
         else:     
             buf = np.getbuffer(value)
         if self.compress == 'zlib':
             blob = zlib.compress(buf)
         elif self.compress == 'lz4':
             blob = lz4.compress(buf)
         elif self.compress == 'snappy':
             blob = snappy.compress(buf)
         else :
             blob = buf
     nprow.compress = self.compress
     nprow.blob = blob
     
     if self.arraytype == pq.Quantity:
         nprow.units = value.dimensionality.string
     
     setattr(inst, self.name+'_array', value)
Пример #12
0
    def __init__(self, array, struct_arr_ptr):
        self.data = cuda.to_device(array)
        self.shape, self.dtype = array.shape, array.dtype
        """
        numpy.getbuffer() needed due to lack of new-style buffer interface for
        scalar numpy arrays as of numpy version 1.9.1

        see: https://github.com/inducer/pycuda/pull/60
        """
        cuda.memcpy_htod(int(struct_arr_ptr),
                         numpy.getbuffer(numpy.int32(array.size)))
        cuda.memcpy_htod(int(struct_arr_ptr) + 8,
                         numpy.getbuffer(numpy.uintp(int(self.data))))
Пример #13
0
    def __init__(self, array, struct_arr_ptr):
        self.data = cuda.to_device(array)
        self.shape, self.dtype = array.shape, array.dtype
        """
        numpy.getbuffer() needed due to lack of new-style buffer interface for
        scalar numpy arrays as of numpy version 1.9.1

        see: https://github.com/inducer/pycuda/pull/60
        """
        cuda.memcpy_htod(int(struct_arr_ptr),
                         numpy.getbuffer(numpy.int32(array.size)))
        cuda.memcpy_htod(
            int(struct_arr_ptr) + 8,
            numpy.getbuffer(numpy.uintp(int(self.data))))
Пример #14
0
 def __init__(self, array, ptr):
     assert (len(array.shape) == 3)
     if isinstance(array, pygpu.GpuArray):
         self.data = array.gpudata
     else:
         if array.dtype != np.float32:
             array = array.astype(np.float32)
         self.data = cuda.to_device(array)
     self.shape = array.shape
     self.dtype = array.dtype
     cuda.memcpy_htod(int(ptr), np.getbuffer(np.int32(array.shape[0])))
     cuda.memcpy_htod(int(ptr) + 8, np.getbuffer(np.int32(array.shape[1])))
     cuda.memcpy_htod(int(ptr) + 16, np.getbuffer(np.int32(array.shape[2])))
     cuda.memcpy_htod(int(ptr) + 24, np.getbuffer(np.intp(int(self.data))))
Пример #15
0
def toggleonoff(channel):
    global togsw,o,alphaValue
    if togsw == 1:
        print "Toggle Crosshair OFF"
        camera.remove_overlay(o)
        togsw = 0
    else:
        print "Toggle Crosshair ON"
        if guivisible == 0:
            o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue)
        else:
            o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue)
        togsw = 1
    return
Пример #16
0
def toggleonoff(channel):
    global togsw,o,alphaValue
    if togsw == 1:
        print "Toggle Crosshair OFF"
        camera.remove_overlay(o)
        togsw = 0
    else:
        print "Toggle Crosshair ON"
        if guivisible == 0:
            o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue)
        else:
            o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue)
        togsw = 1
    return
Пример #17
0
def modulate(values,
             wav_file,
             sample_rate=44100,
             n_channels=2,
             max_amp=32767,
             signal_freq=20):
    import numpy as np
    import wave
    from scipy.ndimage import zoom

    upsample_rate = float(sample_rate) / signal_freq
    T = float(len(values)) / signal_freq
    #n_samples = int(sample_rate*T)
    x = zoom(values, upsample_rate)
    n_samples = x.shape[0]
    t = np.linspace(0, T, n_samples)
    y = max_amp * np.sin(2 * 880 * np.pi * t)
    y *= x
    y *= float(max_amp) / np.abs(y.max())
    data = np.empty(n_samples * n_channels, dtype='int16')
    channel_index = np.arange(0, n_samples * n_channels, n_channels)
    data[channel_index] = y
    data[channel_index + 1] = data[channel_index]

    wav = wave.open(wav_file, 'wb')
    wav.setnchannels(n_channels)
    wav.setsampwidth(2)
    wav.setframerate(sample_rate)
    wav.setnframes(n_samples)
    wav.setcomptype('NONE', 'no compression')
    wav.writeframes(np.getbuffer(data))
    wav.close()
Пример #18
0
def get_overlay(fifo):
    # get the whole FIFO
    ir_raw = fifo.read()
    # trim to 128 bytes
    ir_trimmed = ir_raw[0:128]
    # go all numpy on it
    ir = np.frombuffer(ir_trimmed, np.uint16)
    # set the array shape to the sensor shape (16x4)
    ir = ir.reshape((16, 4))[::-1, ::-1]
    ir = img_as_float(ir)
    # stretch contrast on our heat map
    p2, p98 = np.percentile(ir, (2, 98))
    ir = exposure.rescale_intensity(ir, in_range=(p2, p98))
    # increase even further? (optional)
    # ir = exposure.equalize_hist(ir)

    # turn our array into pretty colors
    cmap = plt.get_cmap('spectral')
    rgba_img = cmap(ir)
    rgb_img = np.delete(rgba_img, 3, 2)

    # align the IR array with the camera
    tform = transform.AffineTransform(
        scale=SCALE, rotation=ROT, translation=OFFSET)
    ir_aligned = transform.warp(
        rgb_img, tform.inverse, mode='constant', output_shape=im.shape)
    # turn it back into a ubyte so it'll display on the preview overlay
    ir_byte = img_as_ubyte(ir_aligned)
    # return buffer
    return np.getbuffer(ir_byte)
Пример #19
0
def packHistogram(histo):
	"""
	Serialize historgram so it can be optimally streamed to the browser.

	Note: This function ensures 64-bit alignment of the data.
	"""

	# Start with histogram name
	buf = packString( histo.name )

	# Get number of events from histogram metadata
	nevts = 0
	if 'nevts' in histo.meta:
		nevts = int(histo.meta['nevts'])

	# Continue with histogram header (8 bytes)
	buf += struct.pack("<II", histo.bins, nevts )

	# Combine all numpy buffers into a multi-dimentional array
	npBuf = np.array([
			histo.y, histo.yErrPlus, histo.yErrMinus,
			histo.x, histo.xErrPlus, histo.xErrMinus
		])

	# Reshape array so the values are interleaved per bin,
	# like this: y, yErrPlus, yErrMinus, x, xErrPlus, xErrMinus
	npBuf = np.swapaxes(npBuf, 0, 1).flatten()

	# Dump numpy buffer
	buf += str(np.getbuffer(npBuf))

	# Return buffer
	return buf
Пример #20
0
def imgToData(frame, base64Encode = False):
	success, data = cv2.imencode('.jpg', frame)
	assert success
	data = np.getbuffer(data)
	if base64Encode:
		data = base64.b64encode(data)
	return data
Пример #21
0
def get_overlay(fifo):
    # get the whole FIFO
    ir_raw = fifo.read()
    # trim to 128 bytes
    ir_trimmed = ir_raw[0:128]
    # go all numpy on it
    ir = np.frombuffer(ir_trimmed, np.uint16)
    # set the array shape to the sensor shape (16x4)
    ir = ir.reshape((16, 4))[::-1, ::-1]
    ir = img_as_float(ir)
    # stretch contrast on our heat map
    p2, p98 = np.percentile(ir, (2, 98))
    ir = exposure.rescale_intensity(ir, in_range=(p2, p98))
    # increase even further? (optional)
    # ir = exposure.equalize_hist(ir)

    # turn our array into pretty colors
    cmap = plt.get_cmap('spectral')
    rgba_img = cmap(ir)
    rgb_img = np.delete(rgba_img, 3, 2)

    # align the IR array with the camera
    tform = transform.AffineTransform(scale=SCALE,
                                      rotation=ROT,
                                      translation=OFFSET)
    ir_aligned = transform.warp(rgb_img,
                                tform.inverse,
                                mode='constant',
                                output_shape=im.shape)
    # turn it back into a ubyte so it'll display on the preview overlay
    ir_byte = img_as_ubyte(ir_aligned)
    # return buffer
    return np.getbuffer(ir_byte)
Пример #22
0
 def do_create(self, offset, length):
     rgb, timestamp = freenect.sync_get_video()
     databuf = numpy.getbuffer(rgb.view(numpy.uint8))
     self.buf = gst.Buffer(databuf)
     self.buf.timestamp = 0
     self.buf.duration = pow(2, 63) - 1
     return gst.FLOW_OK, self.buf
Пример #23
0
    def run(self):
        with h5py.File(self._filepath, "r") as h5_file:
            (internal_path, slicing) = self._request_queue_recv.recv()
            # 'None' means stop the process.
            while internal_path is not None:
                try:
                    if METHOD == "shared-array":
                        read_roi = slice_to_roi(slicing, h5_file[internal_path].shape)
                        read_roi = numpy.array(read_roi)
                        read_shape = read_roi[1] - read_roi[0]
                        num_bytes = h5_file[internal_path].dtype.itemsize * bigintprod(read_shape)
                        assert num_bytes <= self.available_bytes, "I don't yet support really big slicings"
                        read_array = numpy.frombuffer(self.transfer_buffer, dtype=numpy.uint8, count=num_bytes)
                        read_array.setflags(write=True)
                        read_array = read_array.view(h5_file[internal_path].dtype).reshape(read_shape)

                        h5_file[internal_path].read_direct(read_array, slicing)

                    if METHOD == "pipe-bytes" or METHOD == "pipe-array":
                        read_array = h5_file[internal_path][slicing]
                except Exception as ex:
                    self._result_queue_send.send(ex)
                    raise
                else:
                    self._result_queue_send.send((read_array.shape, read_array.dtype))

                    if METHOD == "pipe-array":
                        self._result_queue_send.send(read_array)

                    if METHOD == "pipe-bytes":
                        self._result_queue_send.send_bytes(numpy.getbuffer(read_array))

                # Wait for the next request
                (internal_path, slicing) = self._request_queue_recv.recv()
Пример #24
0
        def modulate(values,
                     wav_file,
                     sample_rate=44100,
                     n_channels=2,
                     max_amp=32767,
                     x_freq=20):
            upsample_rate = float(sample_rate) / x_freq
            T = float(len(values)) / x_freq
            n_samples = int(sample_rate * T)
            x = np.empty(n_samples, dtype='float32')
            for i in range(len(values)):
                x[int(upsample_rate * i):int(upsample_rate *
                                             (i + 1))] = np.log(values[i] + 1)
            t = np.linspace(0, T, n_samples)
            y = max_amp * np.sin(2 * 880 * np.pi * t)
            y *= x
            y *= float(max_amp) / np.abs(y.max())
            data = np.empty(n_samples * n_channels, dtype='int16')
            channel_index = np.arange(0, n_samples * n_channels, n_channels)
            data[channel_index] = y
            data[channel_index + 1] = data[channel_index]

            wav = wave.open(wav_file, 'wb')
            wav.setnchannels(n_channels)
            wav.setsampwidth(2)
            wav.setframerate(sample_rate)
            wav.setnframes(n_samples)
            wav.setcomptype('NONE', 'no compression')
            wav.writeframes(np.getbuffer(data))
            wav.close()
Пример #25
0
def patternswitch(target, guitoggle):
    global o, alphaValue
    toggleonoff()
    if guitoggle == 1:
        creategui(gui)
    o = camera.add_overlay(np.getbuffer(target), layer=3, alpha=alphaValue)
    return
Пример #26
0
def write_SEGY(outfile, file_header, text, traces):
    with open(outfile, 'wb') as out:
        out.write(encode_text(text))
        out.write(SEGY_HEADER.wrap(file_header))
        for header, data in traces:
            out.write(TRACE_HEADER.wrap(header))
            out.write(np.getbuffer(data.byteswap()))
Пример #27
0
def _save_image(im_array, file_name):
    """
    Save an image as a file.

    The input image as a (3, height, width) array, with values in the range
    0..1. The first axis corresponds with the red, green and blue channels.

    """
    # Take a copy so we don't mutate the input.
    #im_array = im_array.copy()

    # PIL expects lines in bottom-to-top order
    for chan in range(3):
        im_array[chan, :, :] = numpy.flipud(im_array[chan, :, :])

    # Clamp values to 0..1
    numpy.clip(im_array, 0., 1.0, out=im_array)

    # Convert into a 1D array with values in the order expected by PIL.
    im_array = numpy.transpose(im_array, (1, 2, 0))
    dims = im_array.shape[1], im_array.shape[0]
    im_array = im_array.flatten()

    # Convert to bytes in the range 0..255
    im_array *= 255.
    im_array = numpy.uint8(im_array)

    # Save the image.
    im = PIL.Image.frombuffer("RGB", dims, numpy.getbuffer(im_array))
    im.save(file_name)
Пример #28
0
    def run(self):
        with h5py.File(self._filepath, 'r') as h5_file:
            (internal_path, slicing) = self._request_queue_recv.recv()
            # 'None' means stop the process.
            while internal_path is not None:
                try:
                    if METHOD == 'shared-array':
                        read_roi = slice_to_roi( slicing, h5_file[internal_path].shape )
                        read_roi = numpy.array(read_roi)
                        read_shape = read_roi[1] - read_roi[0]
                        num_bytes = h5_file[internal_path].dtype.itemsize * numpy.prod( read_shape )
                        assert num_bytes <= self.available_bytes, "I don't yet support really big slicings"
                        read_array = numpy.frombuffer( self.transfer_buffer, dtype=numpy.uint8, count=num_bytes )
                        read_array.setflags(write=True)
                        read_array = read_array.view(h5_file[internal_path].dtype).reshape( read_shape )
                         
                        h5_file[internal_path].read_direct(read_array, slicing)

                    if METHOD == 'pipe-bytes' or METHOD == 'pipe-array':
                        read_array = h5_file[internal_path][slicing]
                except Exception as ex:
                    self._result_queue_send.send( ex )
                    raise
                else:
                    self._result_queue_send.send( (read_array.shape, read_array.dtype) )

                    if METHOD == 'pipe-array':
                        self._result_queue_send.send( read_array )
                    
                    if METHOD == 'pipe-bytes':
                        self._result_queue_send.send_bytes( numpy.getbuffer(read_array) )
                
                # Wait for the next request
                (internal_path, slicing) = self._request_queue_recv.recv()
Пример #29
0
 def writeFrame(self, frame, header=None):
     if self.write:
         #self.streamer.stdin.write(frame.tostring())
         if header is not None:
             drawHeaderString(frame, header)
         self.streamer.stdin.write(np.getbuffer(frame))
         self.streamer.stdin.flush()
Пример #30
0
 def send(self, data):
     self.empty_sem.acquire()
     self.mutex.acquire()
     self.mapfile.seek(0)
     self.mapfile.write(np.getbuffer(data))
     self.mutex.release()
     self.fill_sem.release()
Пример #31
0
def write_SEGY(outfile, file_header, text, traces):
    with open(outfile, 'wb') as out:
        out.write(encode_text(text))
        out.write(SEGY_HEADER.wrap(file_header))
        for header, data in traces:
            out.write(TRACE_HEADER.wrap(header))
            out.write(np.getbuffer(data.byteswap()))
Пример #32
0
def determine_factor_value(inbuf, outbuf, var, wait_time_ns, last_best, last_best_ts, rate):
	out = []
	start_ts = inbuf.timestamp
	current = numpy.frombuffer(inbuf[:], dtype = numpy.float64)
	dt = 1/float(rate) * gst.SECOND
	for j, i in enumerate(current):
		current_ts = start_ts + j * dt
		diff = abs(i - last_best)
		if diff <= var:
			last_best = i
			last_best_ts = current_ts
			val = 1.0
		else:
			if (current_ts - last_best_ts > wait_time_ns) and not numpy.isnan(i) and not numpy.isinf(i):
				last_best = i
				last_best_ts = current_ts
				val = 1.0
			else:
				val = 0.0
		out.append(val)
	out = numpy.array(out, dtype = numpy.float64)
	output_samples = len(out)
	out_len = out.nbytes
	outbuf[:out_len] = numpy.getbuffer(out)
	return last_best, last_best_ts, output_samples
Пример #33
0
def store_images(paths, dbname):
  connection = sqlite3.connect(dbname)
  cursor = connection.cursor()
  cursor.execute("""CREATE TABLE IF NOT EXISTS %s(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    %s INTEGER NOT NULL,
    %s INTEGER NOT NULL,
    %s BLOB NOT NULL,
    %s BLOB NOT NULL);""" %
    (TABLE_NAME, WIDTH_COLUMN, HEIGHT_COLUMN, IMAGE_DATA_COLUMN, PATH_COLUMN))
  connection.commit()

  cursor.execute("""SELECT id from %s;""" % (TABLE_NAME))
  entry_count = len(cursor.fetchall())
  if entry_count == 0:
    for path in paths:
      img = np.array(Image.open(path).resize([IMAGE_WIDTH, IMAGE_HEIGHT]))
      buf = np.getbuffer(img)
      logger.info('%d. loading %s with %d bytes...' %
        (entry_count,path, len(buf)))
      cursor.execute("""INSERT INTO %s(%s, %s, %s, %s)
        VALUES(?, ?, ?, ?)""" % (TABLE_NAME, WIDTH_COLUMN, HEIGHT_COLUMN,
          IMAGE_DATA_COLUMN, PATH_COLUMN),
        (img.shape[1], img.shape[0], buf, buffer(path),))
      if entry_count % 10 == 0:
        connection.commit()
      entry_count += 1
  connection.commit()
  connection.close()
Пример #34
0
 def __new__(cls, variables_list, cids):
     zeros = np.zeros((len(variables_list), len(cids)), dtype=np.float64)
     array = np.ndarray.__new__(cls, zeros.shape, zeros.dtype, np.getbuffer(zeros))
     array.variables_list = variables_list
     array.cids_array = np.array(cids)
     array._rebuild_cached_info()
     return array
Пример #35
0
 def save(k, v, t):
     if t == "string":
         self.writeToAux(k, v)
     if t == "nparray":
         self.writeToAux(k, json.dumps(v, cls=NumpyEncoder))
     if t == "ndarray":
         self.writeToAux(k, np.getbuffer(v), True)
Пример #36
0
 def writeFrame(self, frame, header = None):
     if self.write:
         #self.streamer.stdin.write(frame.tostring())
         if header is not None:
                 drawHeaderString(frame, header)
         self.streamer.stdin.write(np.getbuffer(frame))
         self.streamer.stdin.flush()
Пример #37
0
def _save_image(im_array, file_name):
    """
    Save an image as a file.

    The input image as a (3, height, width) array, with values in the range
    0..1. The first axis corresponds with the red, green and blue channels.

    """
    # Take a copy so we don't mutate the input.
    #im_array = im_array.copy()

    # PIL expects lines in bottom-to-top order
    for chan in range(3):
        im_array[chan, :, :] = numpy.flipud(im_array[chan, :, :])

    # Clamp values to 0..1
    numpy.clip(im_array, 0., 1.0, out=im_array)

    # Convert into a 1D array with values in the order expected by PIL.
    im_array = numpy.transpose(im_array, (1, 2, 0))
    dims = im_array.shape[1], im_array.shape[0]
    im_array = im_array.flatten()

    # Convert to bytes in the range 0..255
    im_array *= 255.
    im_array = numpy.uint8(im_array)

    # Save the image.
    im = PIL.Image.frombuffer("RGB", dims, numpy.getbuffer(im_array))
    im.save(file_name)
Пример #38
0
    def recognize(self, data):
        # print 'sending %d bytes' % len(data)

        if (WRITE_WAV_FILE):
            # write to file
            filename = "out" + str(self.fileCounter)
            self.fileCounter += 1
            outfile = open(filename + ".raw", "wb")
            data.tofile(outfile)
            outfile.close()
            rawToWav(filename)

        buffer = np.getbuffer(data)

        r = Recognizer()
        try:
            result = r.recognize_google(audio_data=buffer,
                                        samplerate=SAMPLE_RATE,
                                        language=self.language)
            self.memory.raiseEvent("SpeechRecognition", result)
            print 'RESULT: ' + result
        except UnknownValueError:
            print 'ERR: Recognition error'
        except RequestError, e:
            print 'ERR: ' + str(e)
Пример #39
0
 def do_create(self, offset, length):
     depth, timestamp = freenect.sync_get_depth()
     databuf = numpy.getbuffer(depth)
     self.buf = gst.Buffer(databuf)
     self.buf.timestamp = 0
     self.buf.duration = pow(2, 63) -1
     return gst.FLOW_OK, self.buf
Пример #40
0
def test_ensure_contiguous_ndarray_shares_memory():
    typed_bufs = [
        ('u', 1, b'adsdasdas'),
        ('u', 1, bytes(20)),
        ('i', 8, np.arange(100, dtype=np.int64)),
        ('f', 8, np.linspace(0, 1, 100, dtype=np.float64)),
        ('i', 4, array.array('i', b'qwertyuiqwertyui')),
        ('u', 4, array.array('I', b'qwertyuiqwertyui')),
        ('f', 4, array.array('f', b'qwertyuiqwertyui')),
        ('f', 8, array.array('d', b'qwertyuiqwertyui')),
        ('i', 1, array.array('b', b'qwertyuiqwertyui')),
        ('u', 1, array.array('B', b'qwertyuiqwertyui')),
        ('u', 1, mmap.mmap(-1, 10))
    ]
    for expected_kind, expected_itemsize, buf in typed_bufs:
        a = ensure_contiguous_ndarray(buf)
        assert isinstance(a, np.ndarray)
        assert expected_kind == a.dtype.kind
        if isinstance(buf, array.array):
            assert buf.itemsize == a.dtype.itemsize
        else:
            assert expected_itemsize == a.dtype.itemsize
        if PY2:  # pragma: py3 no cover
            assert np.shares_memory(a, np.getbuffer(buf))
        else:  # pragma: py2 no cover
            assert np.shares_memory(a, memoryview(buf))
Пример #41
0
def Pack_Data(uniq_id,paraDict,matrix,key,timeStr,radius):
    offset=32
    store_size=offset+58+len(matrix)
    send_back=bytearray(store_size)
    #增加编码
    send_back[0:offset]=struct.pack('32s',uniq_id)
    #站点编号
    send_back[offset+0:offset+5]=key
    #站点经度
    send_back[offset+5:offset+9]=struct.pack('i',int(paraDict['stalon']*1000))
    #站点纬度
    send_back[offset+9:offset+13]=struct.pack('i',int(paraDict['stalat']*1000))
    #站点高度
    send_back[offset+13:offset+17]=struct.pack('i',int(paraDict['staheight']*1000))
    #数据块左上角经度
    send_back[offset+17:offset+21]=struct.pack('i',int(paraDict['xstartlon']*1000))
    #数据块左上角纬度
    send_back[offset+21:offset+25]=struct.pack('i',int(paraDict['ystartlat']*1000))
    #数据块宽度
    send_back[offset+25:offset+29]=struct.pack('i',int(paraDict['xsize']))
    #数据块高度
    send_back[offset+29:offset+33]=struct.pack('i',int(paraDict['ysize']))
    #x方向分辨率
    send_back[offset+33:offset+37]=struct.pack('i',int(paraDict['xres']*20))
    #y方向分辨率
    send_back[offset+37:offset+41]=struct.pack('i',int(paraDict['yres']*20))
    #时间世界时,12位,到分钟
    send_back[offset+41:offset+53]=struct.pack('12s',str(timeStr))
    #写入半径
    send_back[offset+53:offset+57]=struct.pack('i',int(radius))
    #最后一个字符
    send_back[offset+57:offset+58]=struct.pack('c','0')
    send_back[offset+58:]=np.getbuffer(matrix)
    return send_back
Пример #42
0
def writeUni(filename, header, content):
	#print("Writing '%s'" % filename) # debug
	#print("Strides "+format(content.strides))
	with gzip.open(filename, 'wb') as bytestream:

		# write the header of the uni file (old v3 header)
		#bytestream.write(b'MNT2') # v3
		#head_tuple = namedtuple('GenericDict', header.keys())(**header)
		#head_buffer = struct.pack('iiiiii256sQ', *head_tuple)

		# current header
		bytestream.write(b'MNT3') # new, v4
		head_tuple = namedtuple('HeaderV4', header.keys())(**header)
		head_buffer = struct.pack('iiiiii252siQ', *head_tuple)
		bytestream.write(head_buffer)

		# always convert to single precision floats
		if content.dtype!="float32":
			content = np.asarray(content, dtype="float32")

		# write grid content
		if (header['elementType'] == 2):
			# vec3 grid
			content = content.reshape(header['dimX']*header['dimY']*header['dimZ']*3, order='C')
		else:
			# int or scalar grid
			content = content.reshape(header['dimX']*header['dimY']*header['dimZ'], order='C')

		if sys.version_info >= (3,0):
			# changed for Python3
			bytestream.write(memoryview(content))
		else:
			bytestream.write(np.getbuffer(content))
Пример #43
0
def patternswitcher(target, guitoggle):
    global o
    # first remove existing overlay:
    if 'o' in globals():
        camera.remove_overlay(o)
    if guitoggle == 1:
        creategui(gui)
    # cycle through possible patterns:
    if curpat2 == 1:
        patterns.pattern1(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 2:
        patterns.pattern2(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 3:
        patterns.pattern3(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 4:
        patterns.pattern4(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 5:
        patterns.pattern5(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 6:
        patterns.pattern6(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 7:
        patterns.pattern7(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 8:
        patterns.pattern8(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 9:
        patterns.pattern9(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 10:
        patterns.pattern10(target, width, height, xcenter, ycenter, radius,
                           col)
    # Add the overlay directly into layer 3 with transparency;
    # we can omit the size parameter of add_overlay as the
    # size is the same as the camera's resolution
    o = camera.add_overlay(np.getbuffer(target), layer=3, alpha=160)
    return
Пример #44
0
def callback(in_data, frame_count, time_info, status):
    global debug, stack, channels, prefix, current_channel, current_value

    with lock:
        begsample = 0
        endsample = min(frame_count, stack.shape[0])
        dat = stack[begsample:endsample]
        # add zero-padding if required
        pad = np.zeros((frame_count - endsample, channels), dtype=np.float32)
        dat = np.concatenate((dat, pad), axis=0)
        # remove the current samples from the stack
        stack = stack[endsample:]

    if stack.shape[0] == 0 and current_channel != None:
        # send a trigger to indicate that the sample finished playing
        patch.setvalue("%s.%s" % (finished, current_channel), current_value)
        current_channel = None
        current_value = 0

    try:
        # this is for Python 2
        buf = np.getbuffer(dat)
    except:
        # this is for Python 3
        buf = dat.tobytes()

    return buf, pyaudio.paContinue
Пример #45
0
def numpy_to_image(image_numpy, encoding_name="bgr8", header=None):
    """
    Convert a numpy array to sensor_msgs/Image (in dict form)
    only works with bgra8
    """

    image_dict = dict()

    # the header
    image_dict['header'] = header

    image_shape = np.shape(image_numpy)
    image_dict['width'] = image_shape[1]
    image_dict['height'] = image_shape[0]
    image_num_channels = image_shape[2] if len(image_shape) >= 3 else 1
    # put in the data

    if sys.version_info >= (3, 0):
        buffer = (memoryview(image_numpy.copy())).tobytes()
        # literally translate it to string including b''
        encoded_str = str(base64.b64encode(buffer))
        # remove the b' in front and ' in of the string literal.
        encoded_str = encoded_str[2:-1]
        image_dict['data'] = encoded_str
    else:
        # the copy here will make the image to have a continous buffer in the memory
        buffer = np.getbuffer(image_numpy.copy())
        image_dict['data'] = base64.b64encode(buffer)

    image_dict['encoding'] = encoding_name
    image_dict['is_bigendian'] = 0
    image_dict['step'] = image_shape[1] * image_num_channels
    return image_dict
Пример #46
0
	def load(self, gen):
		with self._txn(write=True) as txn:
			try:
				for name, value in gen:
					txn.put(name, numpy.getbuffer(value))
			except lmdb.BadValsizeError as e:
				print name, value.shape, value
Пример #47
0
def patternswitcher(target,guitoggle):
    global o
    # first remove existing overlay:
    if 'o' in globals():
        camera.remove_overlay(o)
    if guitoggle == 1:
        creategui(gui)
    # cycle through possible patterns:
    if curpat2 == 1:
        patterns.pattern1(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 2:
        patterns.pattern2(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 3:
        patterns.pattern3(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 4:
        patterns.pattern4(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 5:
        patterns.pattern5(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 6:
        patterns.pattern6(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 7:
        patterns.pattern7(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 8:
        patterns.pattern8(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 9:
        patterns.pattern9(target, width, height, xcenter, ycenter, radius, col)
    if curpat2 == 10:
        patterns.pattern10(target, width, height, xcenter, ycenter, radius, col)
    # Add the overlay directly into layer 3 with transparency;
    # we can omit the size parameter of add_overlay as the
    # size is the same as the camera's resolution
    o = camera.add_overlay(np.getbuffer(target), layer=3, alpha=160)
    return
Пример #48
0
    def filePlayerCallback(self, in_data, numFrames, time_info, status):
        startTime = tm.time()

        if self.sampleIndex + numFrames >= self.numFrames:
            self.sampleIndex = 0

        inputBuffer = self.samples[self.sampleIndex *
                                   self.bytesPerFrameAllChannels:
                                   (self.sampleIndex + numFrames) *
                                   self.bytesPerFrameAllChannels]
        inputIntArray = np.frombuffer(inputBuffer, dtype='<i2')
        self.inputFrames[:] = pcm2float(inputIntArray).reshape(
            -1, self.numChannels).T
        self.sampleIndex += numFrames

        #logging.info('AudioStreamProcessor: setting processFramesEvent')
        self.processFramesDoneEvent.clear()
        self.processFramesEvent.set()
        #logging.info('AudioStreamProcessor: waiting for processFramesDoneEvent')
        self.processFramesDoneEvent.wait()
        #logging.info('AudioStreamProcessor: done waiting for processFramesDoneEvent')

        outputIntArray = float2pcm(self.outputFrames.T.flatten())
        try:
            outputBuffer = np.getbuffer(outputIntArray)
        except:
            outputBuffer = outputIntArray.tobytes()

        self.processingTimes.append(tm.time() - startTime)

        return outputBuffer, self.paContinue
Пример #49
0
def add_dataarray(
    parent,
    array,
    name,
    ofmt="ascii",
    nbcomp=None,
    nbtuples=None,
    nbitemsbyrow=10,
):
    elt = add_dataarray_node(parent, name, array.dtype, ofmt, nbcomp, nbtuples)
    doc = elt.ownerDocument
    assert len(array.shape) == 1
    if ofmt == "ascii":
        if array.dtype.kind in ["i", "u"]:
            datafmt = "d"
        else:
            datafmt = ".10f"
        fmt = " ".join(["{:%s}" % datafmt] * nbitemsbyrow)
        i = -1
        for i in range(0, array.shape[0] // int(nbitemsbyrow)):
            elt.appendChild(
                doc.createTextNode(
                    fmt.format(*array[i * nbitemsbyrow:(i + 1) *
                                      nbitemsbyrow])))
        left = array[(i + 1) * nbitemsbyrow:]
        fmt = " ".join(["{:%s}" % datafmt] * len(left))
        elt.appendChild(doc.createTextNode(fmt.format(*left)))
    elif ofmt == "binary":
        elt.appendChild(doc.createTextNode(""))  # this is just to indent node
        nbytes = 8 + array.nbytes
        tmp = np.empty(nbytes, dtype=np.byte)
        tmp = np.require(tmp, requirements=["CONTIGUOUS", "OWNDATA"])
        if sys.version_info.major < 3:
            buffersize = np.empty(1, dtype=np.ulonglong)
            buffersize[0] = array.nbytes
            tmp[:8] = np.getbuffer(buffersize)
            tmp[8:] = np.getbuffer(array)
        else:
            buffersize = memoryview(tmp[:8]).cast("Q")
            buffersize[0] = array.nbytes
            datapart = memoryview(tmp[8:])
            datapart[:] = memoryview(array).cast("b")
        s = base64.b64encode(tmp).decode("ascii")
        elt.appendChild(doc.createTextNode(s))
    else:
        raise Exception("Unknown format !")
    return elt
Пример #50
0
def main(flip_v=False, alpha=128, device="/dev/spidev0.0"):
    # Create an array representing a 1280x720 image of
    # a cross through the center of the display. The shape of
    # the array must be of the form (height, width, color)
    a = np.zeros((240, 320, 3), dtype=np.uint8)
    lepton_buf = np.zeros((60, 80, 1), dtype=np.uint16)

    with picamera.PiCamera() as camera:
        camera.resolution = (320, 240)
        camera.framerate = 24
        camera.vflip = flip_v
        camera.hflip = True
        camera.start_preview()
        camera.crop = (0.25, 0.25, 0.5, 0.5)
        camera.fullscreen = True
        # Add the overlay directly into layer 3 with transparency;
        # we can omit the size parameter of add_overlay as the
        # size is the same as the camera's resolution
        o = camera.add_overlay(np.getbuffer(a),
                               size=(320, 240),
                               layer=3,
                               alpha=int(alpha),
                               crop=(0, 0, 80, 60),
                               vflip=flip_v,
                               hflip=True)
        time.sleep(2)
        try:
            time.sleep(0.2)  # give the overlay buffers a chance to initialize
            with Lepton(device) as l:
                last_nr = 0
                while True:
                    _, nr = l.capture(lepton_buf)
                    if nr == last_nr:
                        # no need to redo this frame
                        continue
                    last_nr = nr
                    cv2.normalize(lepton_buf, lepton_buf, 0, 65535,
                                  cv2.NORM_MINMAX)
                    np.right_shift(lepton_buf, 8, lepton_buf)
                    a[:lepton_buf.shape[0], :lepton_buf.
                      shape[1], :] = lepton_buf
                    o.update(np.getbuffer(a))
        except KeyboardInterrupt:
            print "hello"
            traceback.print_exc()
        finally:
            camera.remove_overlay(o)
Пример #51
0
	def load(self, gen):
		''' Put() into the database many (name, vector) pairs '''
		with self._txn(write=True) as txn:
			try:
				for name, value in gen:
					txn.put(name, numpy.getbuffer(value))
			except lmdb.BadValsizeError as e:
				print name, value.shape, value
Пример #52
0
 def _generateData(self):
     if self.typeDescriptor == 'ndarray':
         self.data = numpy.getbuffer(self.obj)
     elif self.typeDescriptor == 'pickle':
         self.data = pickle.dumps(self.obj, 2)
     else:
         raise SerializationError("Really wierd serialization error.")
     del self.obj
Пример #53
0
 def _generateData(self):
     if self.typeDescriptor == 'ndarray':
         self.data = numpy.getbuffer(self.obj)
     elif self.typeDescriptor == 'pickle':
         self.data = pickle.dumps(self.obj, 2)
     else:
         raise SerializationError("Really wierd serialization error.")
     del self.obj
Пример #54
0
 def put(self, data):
     # import ipdb; ipdb.set_trace()
     if data.size > 15000 and data.flags.c_contiguous:
         header = (data.dtype, data.shape)
         message = [pickle.dumps(header), np.getbuffer(data)]
         self.s.send_multipart(message, copy=False)
     else:
         self.s.send_pyobj(data)
Пример #55
0
def numpy_array_to_gst_buffer(frames, chunk_size, num_samples, sample_rate):
    from gst import Buffer
    """ gstreamer buffer to numpy array conversion """
    buf = Buffer(getbuffer(frames.astype("float32")))
    #Set its timestamp and duration
    buf.timestamp = gst.util_uint64_scale(num_samples, gst.SECOND, sample_rate)
    buf.duration = gst.util_uint64_scale(chunk_size, gst.SECOND, sample_rate)
    return buf
Пример #56
0
 def need_data(self,el,l):
     if self.pos >= self.arr.shape[0]:
         el.emit("end-of-stream")
     else:
         buf = gst.Buffer(numpy.getbuffer(self.arr,self.pos*self.fac,1024*self.fac))
         buf.timestamp = self.pos * self.per_sample
         buf.duration = int(1024*self.per_sample)
         el.emit("push-buffer", buf)
         self.pos += 1024
Пример #57
0
    def vol2string(self, label_chunk):
        import numpy
        data = numpy.getbuffer(label_chunk)
        x,y,z = label_chunk.shape

        # pack little endian 64-bit coordinate numbers
        coord_data = '<QQQ'
        coord_bin = struct.pack(coord_data, x, y, z)
        return str(coord_bin) + str(data)