Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenALPR not work with PyQt

I tried to build a GUI app with PyQt and openalpr, but there are an issue with my code. A simple example:

from openalpr import Alpr
from PyQt4 import QtCore, QtGui

class AnalizePlate(object):

    def __init__(self):

        self.alpr = None
        try:
            self.alpr = Alpr("eu", "/etc/openalpr/openalpr.conf", "/usr/share/openalpr/runtime_data")
            if not self.alpr.is_loaded():
                print("Error loading OpenALPR")
        except:
            print "Error"


    def proccess(self):
        self.alpr.set_top_n(7)
        self.alpr.set_default_region("md")

        results = self.alpr.recognize_file("/tmp/1487428945.14.jpg")
        print results

a = AnalizePlate()
a.proccess()

Above code works like a charm, but if GUI is involved, strange behavior occurs.

from openalpr import Alpr
from PyQt4 import QtCore, QtGui

class AnalizePlate(object):

    def __init__(self):

        self.alpr = None
        try:
            self.alpr = Alpr("eu", "/etc/openalpr/openalpr.conf", "/usr/share/openalpr/runtime_data")
            if not self.alpr.is_loaded():
                print("Error loading OpenALPR")
        except:
            print "Error"


    def proccess(self):
        self.alpr.set_top_n(7)
        self.alpr.set_default_region("md")

        results = self.alpr.recognize_file("/tmp/1487428945.14.jpg")
        print results

class Window(QtGui.QWidget):

    def __init__(self):
        super(Window, self).__init__()
        self.resize(1198, 651)
        self.analize = AnalizePlate()
        self.analize.proccess()
        QtCore.QMetaObject.connectSlotsByName(self)


if __name__ == "__main__":
    import sys
    import sip
    app = QtGui.QApplication(sys.argv)
    window = Window()

    window.show()
    sys.exit(app.exec_())

It's a basic example, but error is still here. Tried to implement openalpr code directly to Window class with no luck. So basicly, if there is no gui, code works. Using openALPR version 2.2.4 and PyQT4. Also, checked the image, it's there. The same apply when recognize_array() is used instead of recognize file. Error I got is:

OpenCV Error: Assertion failed (scaleFactor > 1 && image.depth() == CV_8U) in detectMultiScale, file /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/objdetect/src/cascadedetect.cpp, line 1081 Caught exception in OpenALPR recognize: /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/objdetect/src/cascadedetect.cpp:1081: error: (-215) scaleFactor > 1 && image.depth() == CV_8U in function detectMultiScale

Traceback (most recent call last): File "analize.py", line 39, in window = Window() File "analize.py", line 31, in init self.analize.proccess() File "analize.py", line 22, in proccess results = self.alpr.recognize_file("/tmp/1487428945.14.jpg") File "/usr/lib/python2.7/dist-packages/openalpr/openalpr.py", line 132, in recognize_file response_obj = json.loads(json_data) File "/usr/lib/python2.7/json/init.py", line 339, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 1 column 122 (char 121)

like image 955
Aleksandar Avatar asked Feb 19 '17 08:02

Aleksandar


3 Answers

It may be problem of DPI Awareness. you may need to have a look at High DPI support in Qt on http://doc.qt.io/qt-5/highdpi.html. Also if you are willing to update Qt to 5.6 this may help https://stackoverflow.com/a/36058813/2135548

like image 181
Mandar Avatar answered Nov 13 '22 09:11

Mandar


I guess signature for new application app = QtGui.QApplication(sys.argv) is missing argc. Signature for new application is QApplication(int & argc, char ** argv) see here

like image 1
SACn Avatar answered Nov 13 '22 11:11

SACn


We found that with Python multiprocessing when process A launched process B, OpenCV would fail if it was imported in A and called from B, whether or not B imported it again. I'm not familiar with QT but if the window is running in a separate process the same thing may be happening (OpenCV being a dependency of OpenALPR).

If it is, then you'll probably find that leaving AnalizePlate[1] in a separate source file with the import openalpr at the top and referencing it from your window code with no import openalpr at the top will work fine.

[1] I think you mean Analyze(US)/Analyse(BR/AU). Analize means something else :-)

like image 1
Robin Hilliard Avatar answered Nov 13 '22 09:11

Robin Hilliard