I wanted to run a python script from my Java Maven project. Simple python scripts works fine without any issues.
But if i try something complicated like using OpenCV and pytesseract libraries to do some image recognition related tasks the code doesn't work.
The python script works fine if i just run it from the terminal (i have installed all the required libraries)
But the same script when called from the Java Maven project, it doesn't work.
Here is the code:
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);
}
It is throwing below error:
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()
Could someone please help me figure this out please? I feel like the issue is because of some sort of PATH, but not sure how to fix it!
Would you mind trying using -Djava.library.path= so the java process knows where to locate the tesseract library.
Related
I am having a simple code which has an image called "try.png" and I want to convert it from Image to Text using pytesseract but I am having some issues with the code.
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd=r'tesseract-ocr-setup-4.00.00dev.exe'
img = cv2.imread('try.png')
img= cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
print(pytesseract.image_to_string(img))
But it's giving me an error.
Traceback (most recent call last):
File "C:/Users/user 1/PycharmProjects/JARVIS/try.py", line 6, in <module>
print(pytesseract.image_to_string(img))
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pytesseract\pytesseract.py", line 356, in image_to_string
return {
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pytesseract\pytesseract.py", line 359, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pytesseract\pytesseract.py", line 270, in run_and_get_output
run_tesseract(**kwargs)
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pytesseract\pytesseract.py", line 241, in run_tesseract
raise e
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pytesseract\pytesseract.py", line 238, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
OSError: [WinError 740] The requested operation requires elevation
Process finished with exit code 1
Any idea on how to over come this error
tesseract-ocr-setup-4.00.00dev.exe sounds like a setup exe and not the tesseract itself. Check if you have actually installed tesseract and if not launch the exe to install.
For Windows download the latest version from here:
https://github.com/UB-Mannheim/tesseract/wiki
If you still get OSError: [WinError 740] The requested operation requires elevation try changing the tesseract executable to run as admin: Right Click tesseract.exe -> Properties -> Compability -> Check Run this program as an administrator -> OK .
I guess you have not installed tesseract on your system. Run tesseract-ocr-setup-4.00.00dev.exe to install it and make a note of the location where it is installed ($tesseractLocation)
If getting the same error while installing, try running it with admin access.
And Replace
pytesseract.pytesseract.tesseract_cmd=r'tesseract-ocr-setup-4.00.00dev.exe'
with
pytesseract.pytesseract.tesseract_cmd=r'$tesseractLocation'
This will resolve the issue. In the script, you have to mention the installation directory and not the setup file.
Try Uncheck "Run this program as an administrator"
TRY: Right Click tesseract.exe -> Properties -> Compability -> Uncheck Run this program as an administrator -> apply.
I have a Raspberry PI and I want to automatically open a website at boot time.
So I made an auto start config to run a python (v.3.5.3) script on boot.
The python script looks like this:
import os
import subprocess
subprocess.call([/home/pi/Desktop/Shortcut Final.desktop])
But I get the error:
Traceback (most recent call last): File "/home/pi/myscript.py", line 3
subprocess.call([/home/pi/Desktop/Shortcut Final.desktop])
^ SyntaxError: invalid syntax
I got my instructions from a guide that used windows file location, so I think the error is that my file location dont start with a directory letter like C:, but I dont think raspbian use the same system.
I have not found any other guides that explain how to accomplish the same thing in simple terms (as i only know very basic python coding)
I have looked into the open() command, but it seemed complicated.
Any tips on how to accomplish my goal/solve the error?
After putting the file location in quotes the syntax error disappears and i encounter a new error:
Traceback (most recent call last):
File "/home/pi/myscript.py", line 5, in <module>
subprocess.call(["/home/pi/Desktop/Shortcut Final.desktop"])
File "/usr/lib/python3.5/subprocess.py", line 247, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.5/subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1282, in _execute_child
raise child_exception_type(errno_num, err_msg)
PermissionError: [Errno 13] Permission denied
I need to create a python script that runs a file gams (myfile.gms).
My file is in a folder F:\Otim\correct, so I was using a part of a code that I saw in this forum. I made:
import subprocess
subprocess.check_call(["F:\Otim\correct\myfile.gms"])
and I get an error:
Runtime error
Traceback (most recent call last):
File "<string>", line 80, in <module>
File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 506, in check_call retcode = call(*popenargs, **kwargs)
File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 493, in call return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 679, in __init__errread, errwrite)
File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 193] %1 is not a valid Win32 application
Can someone help me please? Thanks
I've never used gams but after a quick look at www.gams.com it sounds like myfile.gms is not the file to be executed but the input file to be given as argument to the gams executable.
So you should try something like :
import subprocess
subprocess.check_call([r"C:\path\to\programs\GAMS.exe",r"F:\Otim\correct\myfile.gms"])
Notice : there is a GAMS python API which could give you a more portable solution.
I am working with WARC Tools, a python library for working with WARC files. I've installed everything and it all seems to work except for the last command. While I suspect many of you may not have worked with this tool, perhaps you can make sense of the error message for me.
When I run this command:
python /path/filesdump.py filtered.warc as per the documentation, I get this error message immediately:
html/811cac8c-7430-403b-96a4-7d77137b0d46.html
Traceback (most recent call last):
File "/users/ianmilligan1/desktop/warc/warc-tools-mandel/filesdump.py", line 63, in <module>
sys.exit(main(sys.argv))
File "/users/ianmilligan1/desktop/warc/warc-tools-mandel/filesdump.py", line 34, in main
dump_archive(fh,name)
File "/users/ianmilligan1/desktop/warc/warc-tools-mandel/filesdump.py", line 57, in dump_archive
txt = record.filedump(content=True)
File "/Users/ianmilligan1/Desktop/WARC/warc-tools-mandel/warctools/record.py", line 140, in filedump
p = Popen(['lynx', '-dump', '-stdin', '-nomargins', '-unique_urls', '-width=120'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
What's going on here? Where would you suggest I begin looking?
You need to install lynx, a command-line web browser that WARC Tools seems to require. That'll solve it.
What's going on is that warctools/record.py is calling lynx on line 140, and Python's subprocess library isn't finding the lynx executable so it throws an exception. They really ought to wrap the exception and provide a clearer message... Anyways. Try that and see if it fixes it.
I am trying to build adblockplus from this link. When I issue the command:
python build.py build
I end up getting the following error message. I tried with and without sudo to no avail.
Traceback (most recent call last):
File "build.py", line 10, in <module>
buildtools.build.processArgs('.', sys.argv)
File "/home/machine/projects/weird/buildtools/build.py", line 352, in processArgs
commands[command](baseDir, scriptName, opts, args, type)
File "/home/machine/projects/weird/buildtools/build.py", line 39, in __call__
return self._handler(baseDir, scriptName, opts, args, type)
File "/home/machine/projects/weird/buildtools/build.py", line 166, in runBuild
limitMetadata=limitMetadata)
File "/home/machine/projects/weird/buildtools/packager.py", line 274, in createBuild
buildNum = getBuildNum(baseDir)
File "/home/machine/projects/weird/buildtools/packager.py", line 80, in getBuildNum
(result, dummy) = subprocess.Popen(['hg', 'id', '-n'], stdout=subprocess.PIPE).communicate()
File "/usr/lib/python2.6/subprocess.py", line 623, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Am I missing a package? Any hint on what the problem could be will be much appreciated.
The stack trace makes it clear that the issue is caused by the following line:
(result, dummy) = subprocess.Popen(['hg', 'id', '-n'], stdout=subprocess.PIPE).communicate()
If you look at the subprocess package documentation, this line is trying to run the hg id -n command (get numerical Mercurial revision). Apparently, the Mercurial command line tool isn't present on your system (a possibility that this build script didn't consider) so it fails.
Disclaimer: I happen to be the one who wrote this script and I fixed this bug now. Mercurial isn't essential for the build, the revision number is pretty much only necessary to determine the output file name.