import pytesser3
import Image
a = Image.open("C:\\Users\\dell\\Desktop\\test.png")
b = pytesser3.image_to_string(a)
print(b)
I tried to make a simple OCR program but whenever I run it I get the following error. I have even uninstalled pytesser3 and reinstalled it but nothing has changed. What am I doing wrong?
Traceback (most recent call last):
File "C:/Users/dell/Desktop/crossword.py", line 4, in <module>
b = pytesser3.image_to_string(a)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site- packages\pytesser3\__init__.py", line 30, in image_to_string
call_tesseract(scratch_image_name, scratch_text_name_root)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site- packages\pytesser3\__init__.py", line 20, in call_tesseract
proc = subprocess.Popen(args)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified:
Here is the test.png:
https://i.stack.imgur.com/NBjbc.png
import pytesser3
import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages\\pytesseract'
a = Image.open("C:\\Users\\dell\\Desktop\\test.png")
b = pytesseract.image_to_string(a)
print(b)
I did what you said but now I've got a different error:
Traceback (most recent call last):
File "C:/Users/dell/Desktop/crossword.py", line 6, in <module>
b = pytesseract.image_to_string(a)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
config=config)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access denied.
You will have to declare the pytesseract path:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-
OCR/tesseract'
open "init.py" file in pytesser3 module. The path would be like "c:\....\Python\site-packages\pytesser3" or inside "c:\....\Python\Lib\site-packages\pytesser3".
And modify the variable value for "tesseract_exe_name = " to your pytesseract path which would be like "C:\....\Python\Lib\site-packages\pytesseract" or "c:\....\Python\site-packages\pytesseract"
It will resolve the issue.
Related
Python 3.9, Pycharm
Am trying to run this code to use the live webcam to take a screenshot, than process that screenshot and identify any text in the screenshot
Code I have put in:
import cv2
from PilLite import Image
import pytesseract
camera=cv2.VideoCapture(0)
def NIC_tesseract():
path_to_tesseract=r"Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytesseract"
pytesseract.pytesseract.tesseract_cmd=path_to_tesseract
#Imagepath='test1.jpg'
pytesseract.tesseract_cmd=path_to_tesseract
text=print(pytesseract.image_to_string(Image.open('test1.jpg'),lang="eng"))
print(text[:-1])
while True:
_,PicturePhoto=camera.read()
cv2.imshow('Text Detection',PicturePhoto)
if cv2.waitKey(30)& 0xFF==ord('s'):
cv2.imwrite('test1.jpg',PicturePhoto)
break
camera.release()
cv2.destroyAllWindows()
NIC_tesseract()
Error Coming Up:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytesseract/pytesseract.py", line 254, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 947, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1819, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytesseract'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/NicAveray/PycharmProjects/FacialRecognition/Trial 2.py", line 25, in
NIC_tesseract()
File "/Users/NicAveray/PycharmProjects/FacialRecognition/Trial 2.py", line 13, in NIC_tesseract
text = pytesseract.image_to_string(PicturePhoto, lang="eng")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytesseract/pytesseract.py", line 416, in image_to_string
return {
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytesseract/pytesseract.py", line 419, in
Output.STRING: lambda: run_and_get_output(*args),
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytesseract/pytesseract.py", line 286, in run_and_get_output
run_tesseract(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytesseract/pytesseract.py", line 258, in run_tesseract
raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytesseract is not installed or it's not in your PATH. See README file for more information.
You assign 'Image' the return from camera.read() while at the same time it is a function imported from PilLite. I think the return from camera.read() is actually a numpy array, which explains the error message. Change the variable name of the return I would suggest.
I'm not sure why you added a print in the line of the OCR. However, please try passing the full path directly.
text = pytesseract.image_to_string(r"D:/.../test1.jpg", lang="eng")
I'm just worried that the time taken to write the image and reading it is too much, so you should read the frames directly by passing your variable "PicturePhoto" to tesseract like this:
text = pytesseract.image_to_string(PicturePhoto, lang="eng")
While trying to use an mp3 file in a python environment I get this error, my file directories are all properly spelt.
Note: the directories are formatted so that within a project folder there is the main code, and 2 sub-folders one for the non-altered mp3 files ("targets"), and one for the altered mp3 files ("altered_targets").
I have already tried to switch AudioSegment.from_file("filename","mp3") with no luck.
from pydub.playback import play
from pydub import AudioSegment
import sys
import os
sys.path.append('/usr/local/lib/python3.7/dist-packages/ffmpeg')
def adjust_volume(file_name, adjustment):
song = AudioSegment.from_mp3(file_name)
new_song = song + adjustment
new_title = "./alter_targets/%s" % (file_name)
new_song.export(new_title, format='mp3')
for target in os.listdir("./targets"):
adjust_volume("./targets/"+target, 10)
This code snippet should slightly increase the volume of the mp3 file within "targets", then save the new mp3 file into the "altered_targets" file.
Instead I get "OSError: [Errno 2] No such file or directory"
Traceback (most recent call last):
File "volume_alter.py", line 19, in <module>
adjust_volume("./targets/"+target, 10)
File "volume_alter.py", line 10, in adjust_volume
song = AudioSegment.from_mp3(file_name)
File "/usr/local/lib/python2.7/dist-packages/pydub/audio_segment.py", line 716, in from_mp3
return cls.from_file(file, 'mp3', parameters=parameters)
File "/usr/local/lib/python2.7/dist-packages/pydub/audio_segment.py", line 665, in from_file
info = mediainfo_json(orig_file)
File "/usr/local/lib/python2.7/dist-packages/pydub/utils.py", line 263, in mediainfo_json
res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I'm trying to get osm-bundler to work, but I run into this problem:
Traceback (most recent call last):
File "RunBundler.py", line 9, in <module>
manager.preparePhotos()
File "/Users/PeterE/Downloads/osm-bundler/osmbundler/__init__.py", line 121, in preparePhotos
self._preparePhoto(photoInfo)
File "/Users/PeterE/Downloads/osm-bundler/osmbundler/__init__.py", line 176, in _preparePhoto
self.extractFeatures(photo)
File "/Users/PeterE/Downloads/osm-bundler/osmbundler/__init__.py", line 244, in extractFeatures
self.featureExtractor.extract(photo, self.photoDict[photo])
File "/Users/PeterE/Downloads/osm-bundler/osmbundler/features/siftvlfeat.py", line 16, in extract
subprocess.call([self.executable, "%s.jpg.pgm" % photo, "-o", "%s.key" % photo])
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 523, in call
return Popen(*popenargs, **kwargs).wait()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
This is caused when I run this command:
python RunBundler.py --photos="/Users/PeterE/Desktop/osmbundlertest"
I can put in any relevant lines from the Python scripts, but I have no idea which one it is now. The output before the error is:
Working directory created: /var/folders/rq/5mypbx554jgflr6c8097zltc0000gn/T/osm-bundler-CXSOEi
BundlerMatching executable path: /Users/PeterE/Downloads/osm-bundler/software/bundler/bin/KeyMatchFull
Sift executable path: /Users/PeterE/Downloads/osm-bundler/software/vlfeat/bin/glx/sift
Processing photo 'IMG_0055.JPG':
Entry for the camera 'Apple', 'iPod touch' does not exist in the camera database
Can't estimate focal length in pixels for the photo '/Users/PeterE/Desktop/osmbundlertest/IMG_0055.JPG'
Copy of the photo has been scaled down to 1200x896
Extracting features with the SIFT method from VLFeat library...
I've search a while and still can not figure it out...
Here's part of my code that went wrong.
import subprocess as sp
import os
cmd_args = []
cmd_args.append('start ')
cmd_args.append('/wait ')
cmd_args.append(os.path.join(dirpath,filename))
print(cmd_args)
child = sp.Popen(cmd_args)
And the command prompt through out this.
['start ', '/wait ', 'C:\\Users\\xxx\\Desktop\\directory\\myexecutable.EXE']
Traceback (most recent call last):
File "InstallALL.py", line 89, in <module>
child = sp.Popen(cmd_args)
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child startupinfo)
FileNotFoundError: [WinError 2]
It looks like the filepath is wrong with 2 backslashes.
I know if I do
print(os.path.join(dirpath,filename))
It'll return
C:\Users\xxx\Desktop\directory\myexecutable.EXE
I'm sure the file is there.
How can I debug this?
This is happening because Popen is trying to find the file start instead of the file you want to run.
For example, using notepad.exe:
>>> import subprocess
>>> subprocess.Popen(['C:\\Windows\\System32\\notepad.exe', '/A', 'randomfile.txt']) # '/A' is a command line option
<subprocess.Popen object at 0x03970810>
This works fine. But if I put the path at the end of the list:
>>> subprocess.Popen(['/A', 'randomfile.txt', 'C:\\Windows\\System32\\notepad.exe'])
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
subprocess.Popen(['/A', 'randomfile.txt', 'C:\\Windows\\System32\\notepad.exe'])
File "C:\python35\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\python35\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Use this instead:
import subprocess as sp
import os
cmd_args = []
cmd_args.append(os.path.join(dirpath,filename))
cmd_args.append('start ')
cmd_args.append('/wait ')
print(cmd_args)
child = sp.Popen(cmd_args)
You might need to swap cmd_args.append('start ') and cmd_args.append('/wait ') around too depending on which order they are meant to be in.
I faced the same problem and just to add a note about Popen: As argument Popen takes a list of strings for non-shell calls and only a string for shell calls.
Details listed here: WinError 2 The system cannot find the file specified (Python)
how can I open an java file in python?, i've search over the net and found this:
import os.path, subprocess
from subprocess import STDOUT, PIPE
def compile_java (java_file):
subprocess.check_call(['javac', java_file])
def execute_java (java_file):
cmd=['java', java_file]
proc=subprocess.Popen(cmd, stdout = PIPE, stderr = STDOUT)
input = subprocess.Popen(cmd, stdin = PIPE)
print(proc.stdout.read())
compile_java("CsMain.java")
execute_java("CsMain")
but then I got this error:
Traceback (most recent call last):
File "C:\Python33\lib\subprocess.py", line 1106, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\casestudy\opener.py", line 13, in <module>
compile_java("CsMain.java")
File "C:\casestudy\opener.py", line 5, in compile_java
subprocess.check_call(['javac', java_file])
File "C:\Python33\lib\subprocess.py", line 539, in check_call
retcode = call(*popenargs, **kwargs)
File "C:\Python33\lib\subprocess.py", line 520, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Python33\lib\subprocess.py", line 820, in __init__
restore_signals, start_new_session)
File "C:\Python33\lib\subprocess.py", line 1112, in _execute_child
raise WindowsError(*e.args)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>>
the python file and java file is in the same folder, and I am using Python 3.3.2, how can I resolve this? or do you guys have another way on doing this?, any answer is appreciated thanks!
I think it isn't recognizing the javac command. Try manually running the command and if javac isn't a recognized command, register it in your PATH variable and try again.
Or you could just try typing the full pathname to the Java directory for javac and java.
you need to add path to your java file name. like this:
compile_java("C:\\path\to\this\CsMain.java")