PyAutoGUI - No Such File or Directory Error - pyautogui

Anyone have any luck on this ? I am tyring to recreate this [tutorial code][1] :
>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('calc7key.png')
>>> button7location
(1416, 562, 50, 41)
>>> button7x, button7y = pyautogui.center(button7location)
>>> button7x, button7y
(1441, 582)
>>> pyautogui.click(button7x, button7y)
But I get the following error [at line 2 of the code above] FileNotFoundError: [Errno 2] No such file or directory: 'calc7key.PNG' when I have such file stored in File "C:\Users*******\Desktop\Python\lib\site-packages\pyscreeze__init__.py", line 165, in _locateAll_python
needleFileObj = open(needleImage, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'calc7key.PNG' ..anyone have any luck using this. essentially I am trying to screenshot a minimized tab to be opened by pyautogui (like lets say the google chrome icon at the bottom of my screen)

Solved : Had to change to the directory it was in
>>> import os
>>> os.chdir (r'C:\******')
>>>button7location = pyautogui.locateOnScreen('calc7key.png')

for me worked installing open cv: pip install opencv-python
(https://pypi.org/project/opencv-python/)
showing recommendation to install openCV

Related

error while using nltk.post_tag

I have been trying to use nltk.pos_tag in my code but I face an error when I do so. I have already downloaded Penn treebank and max_ent_treebank_pos. But the error persists. here is my code :
import nltk
from nltk import tag
from nltk import*
a = "Alan Shearer is the first player to score over a hundred Premier League goals."
a_sentences = nltk.sent_tokenize(a)
a_words = [nltk.word_tokenize(sentence) for sentence in a_sentences]
a_pos = [nltk.pos_tag(sentence) for sentence in a_words]
print(a_pos)
and this is the error I get :
"Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
print (nltk.pos_tag(text))
File "C:\Python34\lib\site-packages\nltk\tag\__init__.py", line 110, in pos_tag
tagger = PerceptronTagger()
File "C:\Python34\lib\site-packages\nltk\tag\perceptron.py", line 140, in __init__
AP_MODEL_LOC = 'file:'+str(find('taggers/averaged_perceptron_tagger/'+PICKLE))
File "C:\Python34\lib\site-packages\nltk\data.py", line 641, in find
raise LookupError(resource_not_found)
LookupError:
Resource 'taggers/averaged_perceptron_tagger/averaged_perceptron
_tagger.pickle' not found. Please use the NLTK Downloader to
obtain the resource: >>> nltk.download()
Searched in:
- 'C:\\Users\\T01142/nltk_data'
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- 'C:\\Python34\\nltk_data'
- 'C:\\Python34\\lib\\nltk_data'
- 'C:\\Users\\T01142\\AppData\\Roaming\\nltk_data'
Call this from python:
nltk.download('averaged_perceptron_tagger')
Had the same problem in a Flask server. nltk used a different path when in server config, so I recurred to adding nltk.data.path.append("/home/yourusername/whateverpath/") inside of the server code right before the pos_tag call
Note there is some replication of this question
How to config nltk data directory from code?
nltk doesn't add $NLTK_DATA to search path?
POS tagging with NLTK. Can't locate averaged_perceptron_tagger
To resolve this error run following command on python prompt:
import nltk
nltk.download('averaged_perceptron_tagger')

Selenium + Xvfb concurrency issues

test.py
#!/usr/bin/env python2
import os
from pyvirtualdisplay import Display
from selenium import webdriver
import sys
display = Display(visible=0, size=(800, 600))
display.start()
try:
capabilities = webdriver.common.desired_capabilities.DesiredCapabilities.CHROME
browser = webdriver.Chrome('/opt/chrome-driver/chromedriver', desired_capabilities=capabilities)
try:
browser.get('http://example.com')
browser.execute_script('return 1 + 1;')
finally:
browser.quit()
finally:
display.stop()
print 'Done'
And then run
seq 1 20 | parallel -j 5 ./test.py
I get a random number of errors like this:
Traceback (most recent call last):
File "./test.py", line 15, in <module>
browser.execute_script('return 1 + 1;')
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute_script
{'script': script, 'args':converted_args})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
(Session info: chrome=50.0.2661.86)
(Driver info: chromedriver=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a),platform=Linux 3.19.0-59-generic x86_64)
If I remove the concurrency (parallel -j 1), it works.
If I remove Xvfb, it works.
What does this error mean, and how can I fix it (without giving up concurrency or virtual displays)?
Looks like starting xvfb has concurrency issues when starting. xvfb-run unreliable when multiple instances invoked in parallel
So I can fix this by requiring that xfvb sessions are started serially.
#!/usr/bin/env python2
import fcntl
import os
from pyvirtualdisplay import Display
from selenium import webdriver
import sys
import xvfbwrapper
display = None
browser = None
try:
with open('/tmp/xvfb.lock', 'w') as lock:
fcntl.lockf(lock, fcntl.LOCK_EX)
display = Display(visible=0, size=(800, 600))
display.start()
capabilities = webdriver.common.desired_capabilities.DesiredCapabilities.CHROME
browser = webdriver.Chrome('/opt/chrome-driver/chromedriver', desired_capabilities=capabilities)
browser.get('http://example.com')
browser.execute_script('return 1 + 1;')
finally:
if browser:
browser.quit()
if display:
display.stop()
print 'Done'
As long as everybody uses /tmp/xvfb.lock to control starting a Xvfb session starting, this works.
P.S. I don't think the selenium driver starting needed to be serialized too, but I did run into a transient problem that I think was fixed by this.

Image to OCR processing using PySpark

I am trying to extract text from an image (using pytesser->Tesseract ocr engine) from pyspark, I have uploaded my image file in hdfs and trying to read from spark.
this is my code
>>> import sys
>>> sys.path.append("/home/dsuser/Downloads/pytesser")
>>> from PIL import Image
>>> from pytesser import *
>>> image_file = 'hdfs://localhost:9000/image/image6.jpg'
>>> rdd = sc.binaryFiles(image_file)
>>> img = Image.open(rdd.first())
>>> text = image_to_string(img)
>>> print("=====output=======\n")
>>> print(text)
while running, spark able to load the image file from hdfs, but i am getting below error while calling the below code
>>> im = Image.open(rdd.first())
Traceback (most recent call last): File "", line 1, in
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line
2000, in open
prefix = fp.read(16) AttributeError: 'tuple' object has no attribute 'read'
not sure about the error, I need help here to convert the image to OCR

Running Stanford POS tagger in NLTK leads to "not a valid Win32 application" on Windows

I am trying to use stanford POS tagger in NLTK by the following code:
import nltk
from nltk.tag.stanford import POSTagger
st = POSTagger('E:\Assistant\models\english-bidirectional-distsim.tagger',
'E:\Assistant\stanford-postagger.jar')
st.tag('What is the airspeed of an unladen swallow?'.split())
and here is the output:
Traceback (most recent call last):
File "E:\J2EE\eclipse\WSNLP\nlp\src\tagger.py", line 5, in <module>
st.tag('What is the airspeed of an unladen swallow?'.split())
File "C:\Python34\lib\site-packages\nltk\tag\stanford.py", line 59, in tag
return self.tag_sents([tokens])[0]
File "C:\Python34\lib\site-packages\nltk\tag\stanford.py", line 81, in tag_sents
stdout=PIPE, stderr=PIPE)
File "C:\Python34\lib\site-packages\nltk\internals.py", line 153, in java
p = subprocess.Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr)
File "C:\Python34\lib\subprocess.py", line 858, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1111, in _execute_child
startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
P.S. My java home is set and I have no problem with my java installation. Can someone explain what this error is talking about? It is not informative for me. Thanks in advance.
Looks like your Java installation is botched or missing.
It worked after a lot of trial and error:
It seems that NLTK Internal cannot find the java binary automatically on windows, so we need to identify it as follows:
import os
import nltk
from nltk.tag.stanford import POSTagger
os.environ['JAVA_HOME'] = r'C:\Program Files\Java\jre6\bin'
st = POSTagger('E:\stanford-postagger-2014-10-26\models\english-left3words-distsim.tagger',
'E:\stanford-postagger-2014-10-26\stanford-postagger.jar')
st.tag(nltk.word_tokenize('What is the airspeed of an unladen swallow?'))
As one of the gurus said to me: "don't forget to add "r" while working with "\" in strings."

Loading CPP functions into Python using Ctypes

I have a question to Ctypes and do not know what I do wrong. And yes, I am a newbie for Python and searched the other posts in here. So any advice is appreciated.
What do I want to do :
I simply want to load the FXCM C++ APP fundtions into Python 3.3 so I can call them for connecting to their server.
As it seems Ctypes seems to be the best tool. So a simple code in Python :
import os
dirlist = os.listdir('ForexConnectAPIx64/bin')
from pprint import pprint
pprint(dirlist)
from ctypes import *
myDll = cdll.LoadLibrary ("ForexConnectAPIx64/bin/ForexConnect.dll")
gives a result :
Traceback (most recent call File "C:\Users\scaberia3\Python_Projects \FTC\ListDir_Test.py", line 20, in <module>
myDll = cdll.LoadLibrary ("ForexConnectAPIx64/bin/ForexConnect.dll")
File "C:\Python33\lib\ctypes\__init__.py", line 431, in LoadLibrary
return self._dlltype(name)
File "C:\Python33\lib\ctypes\__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] Das angegebene Modul wurde nicht gefunden (Module not found)
['ForexConnect.dll',
'fxmsg.dll',
'gsexpat.dll',
'gslibeay32.dll',
'gsssleay32.dll',
'gstool2.dll',
'gszlib.dll',
'java',
'log4cplus.dll',
'msvcp80.dll',
'msvcr80.dll',
'net',
'pdas.dll']
Means the path is correct ForextConnect.dll is present and I might do some very simple wrong, but have no clue what.
You can either use Dependency Walker to figure out the correct sequence to manually load the DLLs, or simply add the directory to the system search path:
dllpath = os.path.abspath('ForexConnectAPIx64/bin')
os.environ['PATH'] += os.pathsep + dllpath
myDLL = CDLL('ForexConnect.dll')