Ejemplo n.º 1
0
 def on_new_webcam_sample(self, appsink: GstApp.AppSink) -> Gst.FlowReturn:
     if appsink.is_eos():
         return Gst.FlowReturn.OK
     sample: Gst.Sample = appsink.try_pull_sample(0.5)
     buffer: Gst.Buffer = sample.get_buffer()
     caps: Gst.Caps = sample.get_caps()
     # This Pythonic usage is thank to python3-gst
     struct: Gst.Structure = caps[0]
     width = struct['width']
     height = struct['height']
     success, mapinfo = buffer.map(
         Gst.MapFlags.READ)  # type: bool, Gst.MapInfo
     if not success:
         logger.error('Failed to get mapinfo.')
         return Gst.FlowReturn.ERROR
     # In Gstreamer 1.18, Gst.MapInfo.data is memoryview instead of bytes
     imgdata = mapinfo.data.tobytes() if isinstance(
         mapinfo.data, memoryview) else mapinfo.data
     img = zbar.Image(width, height, 'Y800', imgdata)
     n = self.zbar_scanner.scan(img)
     logger.debug('Any QR code?: {}', n)
     if not n:
         return Gst.FlowReturn.OK
     # Found QR code in webcam screenshot
     logger.debug('Emulate pressing Pause button')
     self.btn_pause.set_active(True)
     self.display_result(img.symbols)
     return Gst.FlowReturn.OK
Ejemplo n.º 2
0
 def on_new_webcam_sample(self, appsink: GstApp.AppSink) -> Gst.FlowReturn:
     if appsink.is_eos():
         return Gst.FlowReturn.OK
     sample: Gst.Sample = appsink.try_pull_sample(0.5)
     buffer: Gst.Buffer = sample.get_buffer()
     caps: Gst.Caps = sample.get_caps()
     # This Pythonic usage is thank to python3-gst
     struct: Gst.Structure = caps[0]
     width = struct['width']
     height = struct['height']
     success, mapinfo = buffer.map(
         Gst.MapFlags.READ)  # type: bool, Gst.MapInfo
     if not success:
         logger.error('Failed to get mapinfo.')
         return Gst.FlowReturn.ERROR
     img = zbar.Image(width, height, 'Y800', mapinfo.data)
     n = self.zbar_scanner.scan(img)
     logger.debug('Any QR code?: {}', n)
     if not n:
         return Gst.FlowReturn.OK
     # Found QR code in webcam screenshot
     logger.debug('Emulate pressing Pause button')
     self.btn_pause.set_active(True)
     try:
         sym = next(iter(img.symbols))
     except StopIteration:
         logger.error(
             'Something wrong. Failed to extract symbol from zbar image!')
         return Gst.FlowReturn.ERROR
     logger.info('QR type: {}', sym.type)
     logger.info('Decoded string: {}', sym.data)
     self.raw_result_buffer.set_text(sym.data)
     return Gst.FlowReturn.OK
Ejemplo n.º 3
0
    def _on_buffer(self, sink: GstApp.AppSink,
                   data: typ.Any) -> Gst.FlowReturn:
        """Callback on 'new-sample' signal"""
        # Emit 'pull-sample' signal
        # https://lazka.github.io/pgi-docs/GstApp-1.0/classes/AppSink.html#GstApp.AppSink.signals.pull_sample

        sample = sink.emit("pull-sample")
        if isinstance(sample, Gst.Sample):
            self._queue.put(self._extract_buffer(sample))
            self._counter += 1

            return Gst.FlowReturn.OK

        self.log.error(
            "Error : Not expected buffer type: %s != %s. %s",
            type(sample),
            Gst.Sample,
            self,
        )
        return Gst.FlowReturn.ERROR