0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pyautoGUIでopencvを入れたらエラーになる

Last updated at Posted at 2020-09-16

概要

pythonでRPAを行うために、
pyautoguiを使っているとき、
許容誤差や、グレースケール認識などを行いたいときがあります。
その時にopencvを入れる必要が出てきます。

ところが、結局使えませんでした。
エラー内容と回避策を載せておきます。

エラー内容

pos = pyautogui.locateCenterOnScreen(GetImage(imgname), region=inputregion)
このコードが、これまで動いていたのに、急に動かなくなりました。
pip opencv-pythonして、
pos = pyautogui.locateCenterOnScreen(GetImage(imgname), region=inputregion, grayscale=True)
と書き換えてグレースケールに対応させようとしました。

output
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
================================== FAILURES ===================================
    pos = pyautogui.locateCenterOnScreen(GetImage(imgname), region=inputregion,grayscale=True)
C:\tools\miniconda3\envs\rpa2\lib\site-packages\pyautogui\__init__.py:175: in wrapper
    return wrappedFunction(*args, **kwargs)
C:\tools\miniconda3\envs\rpa2\lib\site-packages\pyautogui\__init__.py:207: in locateCenterOnScreen
    return pyscreeze.locateCenterOnScreen(*args, **kwargs)
C:\tools\miniconda3\envs\rpa2\lib\site-packages\pyscreeze\__init__.py:400: in locateCenterOnScreen
    coords = locateOnScreen(image, **kwargs)
C:\tools\miniconda3\envs\rpa2\lib\site-packages\pyscreeze\__init__.py:360: in locateOnScreen
    retVal = locate(image, screenshotIm, **kwargs)
C:\tools\miniconda3\envs\rpa2\lib\site-packages\pyscreeze\__init__.py:340: in locate
    points = tuple(locateAll(needleImage, haystackImage, **kwargs))
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
needleImage = array([[31, 31, 31, ..., 31, 31, 31],
       [31, 31, 31, ..., 31, 31, 31],
       [31, 31, 31, ..., 31, 31, 31],
    ...31, 31, 31, ..., 31, 31, 31],
       [31, 31, 31, ..., 31, 31, 31],
       [31, 31, 31, ..., 31, 31, 31]], dtype=uint8)
haystackImage = array([[60, 60, 60, ..., 60, 60, 60],
       [60, 60, 60, ..., 60, 60, 60],
       [60, 60, 60, ..., 60, 60, 60],
    ...16, 16, 16, ..., 16, 16, 16],
       [16, 16, 16, ..., 16, 16, 16],
       [16, 16, 16, ..., 16, 16, 16]], dtype=uint8)
grayscale = True, limit = 1, region = (0, 945.0, 240.0, 135.0), step = 1
confidence = 0.999
    def _locateAll_opencv(needleImage, haystackImage, grayscale=None, limit=10000, region=None, step=1,
                          confidence=0.999):
        """
        TODO - rewrite this
            faster but more memory-intensive than pure python
            step 2 skips every other row and column = ~3x faster but prone to miss;
                to compensate, the algorithm automatically reduces the confidence
                threshold by 5% (which helps but will not avoid all misses).
            limitations:
              - OpenCV 3.x & python 3.x not tested
              - RGBA images are treated as RBG (ignores alpha channel)
        """
        if grayscale is None:
            grayscale = GRAYSCALE_DEFAULT

        confidence = float(confidence)

        needleImage = _load_cv2(needleImage, grayscale)
        needleHeight, needleWidth = needleImage.shape[:2]
        haystackImage = _load_cv2(haystackImage, grayscale)

        if region:
>           haystackImage = haystackImage[region[1]:region[1]+region[3],
                                          region[0]:region[0]+region[2]]
E           TypeError: slice indices must be integers or None or have an __index__ method
C:\tools\miniconda3\envs\rpa2\lib\site-packages\pyscreeze\__init__.py:202: TypeError
=========================== short test summary info ===========================
FAILED test_iqctrl.py::test_iqcreate - TypeError: slice indices must be integ...
============================== 1 failed in 2.03s ==============================

環境

使用しているpythonモジュールの一覧です。

conda install python
pip install pyautogui
pip install rope yapf black autopep8 pylint pytest
pip install tqdm
pip install pyperclip
pip install pyodbc
pip install cx_Oracle

ここにopencv-pythonを追加しました。

OSはWindows10です。

結論

_locateAll_opencv関数がpython3.xではテストされておらず、発展途上とのこと。
「配列をスライスするインデックスは整数にしなきゃダメだよ」などのエラーが出ていますが、
pyautoguiモジュールの中の話なので関与できませんでした。

回避策

現状は2.Xで我慢するか、opencvをあきらめるかどちらか。
どちらも受け入れられない場合にはグレースケールと曖昧画像検索をあきらめる必要がありそう。

_locateAll_opencv関数そのものを書き換えられる人は、Githubにプルリクしていただけると助かります。

関係資料

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?