Menu

How to spawn separate zbar thread in python?

Help
Max
2011-02-07
2015-10-21
  • Max

    Max - 2011-02-07

    Hello folks!  Is it possible to prevent zbar from blocking main thread in the code below?

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #!/usr/bin/env python
    import threading
    import zbar
    import time
    class ScanThread(threading.Thread):
        def run(self):
            self.proc = zbar.Processor()
            self.proc.parse_config('enable')
            self.proc.visible = False
            self.proc.set_data_handler(self.process_data)
            self.proc.init('/dev/video0')
            self.proc.active = True
            self.proc.user_wait()
        def process_data(self, proc, image, closure):
            for symbol in image:
                if not symbol.count:
                    print symbol.data
    if __name__ == "__main__":
        scanner = ScanThread()
        scanner.start()
        try:
            while True:
                print "main thread"
                time.sleep(1)
        except KeyboardInterrupt as k:
            scanner.proc.active = False
            scanner.join()
    
     
  • spadix

    spadix - 2011-02-08

    > Is it possible to prevent zbar from blocking main thread

    The extension does not currently handle the GIL correctly (ie, at all).

    Note that the Processor already runs in its own thread, which is running once you set the Processor active.  The data_handler is called from that thread, so you may be able to get the effect you're looking for (…although it's still inviting trouble).

    This bug is actually not hard to fix - are you interested in testing a new version if I fix it right now?  In any case, you should definitely open a bug tracker…

    Thanks for pointing this out!

     
  • Max

    Max - 2011-02-11

    I opened an issue #3177905.

    BTW, regarding python UI responsiveness - is it possible to configure Processor to use lower resolution? By default it grabs maximum possible resolution and eats too much CPU. I can reduce video dimensions via command line but didn't figured out respective python method.

     
  • Max

    Max - 2011-02-11

    And yes, I'm willing to help in testing. I'm able to apply patches or pull from mercurial and compile the code.

     
  • spadix

    spadix - 2011-02-13

    > maximum possible resolution and eats too much CPU

    You can also use the density configurations to avoid low frame rates without sacrificing as much resolution in the scan direction.

    > I can reduce video dimensions via command line but didn't figured out respective python method.

    I just pushed support for this interface to hg (rev 13a17b5b13f7).  It would be great to hear back about this as well.

    processor.request_size = (w, h)
    

    Thanks for your feedback!

     
  • Max

    Max - 2011-02-13

    New function works as expected.  Smaller video stream resolution combined with density configuration dropped CPU usage from 100% to mere 12-15%.  Thank you!

     
  • Oneiro

    Oneiro - 2015-10-21

    hello excuse me.
    why i use processor.request_size <this funtion.
    then it told me : AttributeError: 'Zbar_Processor' object has no attribute ' request_size'

     

Log in to post a comment.