运行带有来自maven项目的pytesseract库的python脚本时出现问题



我想从我的Java Maven项目中运行一个python脚本。简单的python脚本运行良好,没有任何问题。但是,如果我尝试一些复杂的东西,比如使用OpenCV和pytesseract库来执行一些与图像识别相关的任务,代码就不起作用。

如果我只是从终端运行python脚本,它就可以正常工作(我已经安装了所有必需的库(但是从Java Maven项目调用相同的脚本时,它不起作用。

这是代码:

String path = "/Users/team/Desktop/my.py";
ProcessBuilder pb = new ProcessBuilder("python3","/Users/team/Desktop/my.py").inheritIO();
Process p = pb.start();
p.waitFor();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = bfr.readLine()) != null) {
System.out.println(line);
}

它正在抛出以下错误:

Traceback (most recent call last):
File "/Users/team/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 252, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'tesseract'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/team/Desktop/my.py", line 73, in <module>
result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))
File "/Users/team/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 413, in image_to_string
return {
File "/Users/team/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 416, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "/Users/team/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 284, in run_and_get_output
run_tesseract(**kwargs)
File "/Users/team/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 256, in run_tesseract
raise TesseractNotFoundError()

有人能帮我弄清楚吗?我觉得这个问题是因为某种PATH,但不确定如何解决!

您介意尝试使用-Djava.library.path=吗?这样java进程就知道在哪里可以找到tesseract库。

最新更新