尝试在python中运行OpenALPR时出错



我很好地安装了OpenALPR本身,并且我能够在终端中运行它来获得这个结果:

C:Userszebsu>"C:\OpenALPR\Agent\bin\alpr.exe" "C:\plate.jpg"
plate0: 3 results
State ID: us-oh (97% confidence)
- PZ65BYV    confidence: 94.5181     pattern_match: 0
- P265BYV    confidence: 81.1941     pattern_match: 0
- P65BYV     confidence: 81.1336     pattern_match: 0

然而,我随后遵循了PyPI上的说明(https://pypi.org/project/openalpr/#description)以安装带有pip install openalpr的openalpr-python绑定。但是,当我运行以下代码时,他们建议使用python 3.8.6 x64:

import json
from openalpr import Alpr
alpr = Alpr("us", "C:/OpenALPR/Agent/etc/openalpr/openalpr.conf", "C:/OpenALPR/Agent/usr/share/openalpr/configruntime_data")
if not alpr.is_loaded():
print("Error loading OpenALPR")
sys.exit(1)
results = alpr.recognize_file("C:/image.jpg")
print(json.dumps(results, indent=4))
alpr.unload()

我得到以下错误:

Traceback (most recent call last):
File "C:UserszebsuAppDataLocalProgramsPythonPython38libsite-packagesopenalpropenalpr.py", line 70, in __init__
self._openalprpy_lib = ctypes.cdll.LoadLibrary("libopenalpr.dll")
File "C:UserszebsuAppDataLocalProgramsPythonPython38libctypes__init__.py", line 451, in LoadLibrary
return self._dlltype(name)
File "C:UserszebsuAppDataLocalProgramsPythonPython38libctypes__init__.py", line 373, in __init__
self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'libopenalpr.dll' (or one of its dependencies). Try using the full path with constructor syntax.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UserszebsuOneDrivecompuuter scienceworkLPRLPR_test.py", line 4, in <module>
alpr = Alpr("us", "C:/OpenALPR/Agent/etc/openalpr/openalpr.conf", "C:/OpenALPR/Agent/usr/share/openalpr/configruntime_data")
File "C:UserszebsuAppDataLocalProgramsPythonPython38libsite-packagesopenalpropenalpr.py", line 80, in __init__
raise nex
OSError: Unable to locate the OpenALPR library. Please make sure that OpenALPR is properly installed on your system and that the libraries are in the appropriate paths.

如果我用python 3.6.8 x32:运行代码,就会出现以下错误

Traceback (most recent call last):
File "C:UserszebsuAppDataLocalProgramsPythonPython36-32libsite-packagesopenalpropenalpr.py", line 70, in __init__
self._openalprpy_lib = ctypes.cdll.LoadLibrary("libopenalpr.dll")
File "C:UserszebsuAppDataLocalProgramsPythonPython36-32libctypes__init__.py", line 426, in LoadLibrary
return self._dlltype(name)
File "C:UserszebsuAppDataLocalProgramsPythonPython36-32libctypes__init__.py", line 348, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UserszebsuOneDrivecompuuter scienceworkLPRLPR_test.py", line 4, in <module>
alpr = Alpr("us", "C:/OpenALPR/Agent/etc/openalpr/openalpr.conf", "C:/OpenALPR/Agent/usr/share/openalpr/configruntime_data")
File "C:UserszebsuAppDataLocalProgramsPythonPython36-32libsite-packagesopenalpropenalpr.py", line 80, in __init__
raise nex
OSError: Unable to locate the OpenALPR library. Please make sure that OpenALPR is properly installed on your system and that the libraries are in the appropriate paths.

我搜索了所有的互联网论坛寻找答案,但大多数提交的内容都是几年前openalpr绑定可以用pip安装,并且必须从github安装之前的内容。有人有什么建议吗?

我最终在另一个线程上从问题的答案中找到了解决方案,该线程出现了类似的错误,但使用了不同的库:FileNotFoundError:找不到模块';libvlc.dll';。问题是该程序无法找到所需的dll文件,因此需要将dll文件所在的目录添加到python代码中的操作系统中。对我来说,这意味着将这些行添加到我的代码顶部:

import os
os.add_dll_directory("C:/OpenALPR/Agent/bin")

这一变化意味着代码完全按照预期运行

最新更新