TypeError:参数'%s'的预期Ptr <cv :: UMat>

时间:2019-12-22 13:47:31

标签: python-3.x numpy opencv python-imaging-library pywin32

我做了一个跟踪窗口坐标的类。 想法是捕获应用程序窗口,即使该窗口移动了位置。 我使用的坐标表示(x,y,width,height)(260,183,1653,1266)。

以下代码可以解决问题:

from utils.coordinator import Coordinator
from PIL import ImageGrab
import numpy as np
import win32gui
import cv2

while True:
    coords = Coordinator("AnotherApplication")

    img = ImageGrab.grab(
        bbox=(
            coords.application["position"]["x"], 
            coords.application["position"]["y"], 
            coords.application["size"]["width"], 
            coords.application["size"]["height"]
        ))
    img_np = np.array(img)
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)

    cv2.imshow("frame", frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    print(coords.application)
    print((coords.application["position"]["x"], coords.application["position"]["y"], coords.application["size"]["width"], coords.application["size"]["height"]))

请注意,您必须按一个键才能完成循环。应用程序本身运行 没有明显的问题。经过几次成功的屏幕捕获图像后,出现以下异常:

{'pid': 8783626, 'title': 'AnotherApplication', 'position': {'x': -21, 'y': 22}, 'size': {'width': 1653, 'height': 1266}}
(-21, 22, 1653, 1266)
{'pid': 8783626, 'title': 'AnotherApplication', 'position': {'x': 758, 'y': 144}, 'size': {'width': 1653, 'height': 1258}}
(758, 144, 1653, 1258)
{'pid': 8783626, 'title': 'AnotherApplication', 'position': {'x': 758, 'y': 144}, 'size': {'width': 1653, 'height': 1258}}
(758, 144, 1653, 1258)
{'pid': 8783626, 'title': 'AnotherApplication', 'position': {'x': 758, 'y': 144}, 'size': {'width': 1653, 'height': 1258}}
(758, 144, 1653, 1258)
Traceback (most recent call last):
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "P:\Python\Python37\Lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "P:\Python\Python37\Lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "P:\Python\Python37\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Private\wooper/main.py", line 12, in <module>
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
TypeError: Expected Ptr<cv::UMat> for argument '%s'

在中午休息时,我发现numpy数组似乎被扭曲了。 我认为最好检查数据img_np以便进行验证 如果实际有数据。

if img_np:
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)

img_np似乎是真实的,但是单击img_np会引发以下异常

Traceback (most recent call last):
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 606, in trace_dispatch
    self.do_wait_suspend(thread, frame, event, arg)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 97, in do_wait_suspend
    self._args[0].do_wait_suspend(*args, **kwargs)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1616, in do_wait_suspend
    keep_suspended = self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread, frames_tracker)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1644, in _do_wait_suspend
    self.process_internal_commands()
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1350, in process_internal_commands
    int_cmd.do_it(self)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 502, in do_it
    self.method(dbg, *self.args, **self.kwargs)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 675, in internal_get_variable_json
    for child_var in variable.get_children_variables(fmt=fmt):
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_suspended_frames.py", line 114, in get_children_variables
    dct = resolver.get_dictionary(self.value)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd_plugins\extensions\types\pydevd_plugin_numpy_types.py", line 80, in get_dictionary
    ret['[0:%s] ' % (len(obj))] = list(obj[0:MAX_ITEMS_TO_HANDLE])
IndexError: too many indices for array
Traceback (most recent call last):
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "P:\Python\Python37\Lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "P:\Python\Python37\Lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "P:\Python\Python37\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Private\wooper/main.py", line 19, in <module>
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
  File "D:\Private\wooper/main.py", line 19, in <module>
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 606, in trace_dispatch
    self.do_wait_suspend(thread, frame, event, arg)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 97, in do_wait_suspend
    self._args[0].do_wait_suspend(*args, **kwargs)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1616, in do_wait_suspend
    keep_suspended = self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread, frames_tracker)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1644, in _do_wait_suspend
    self.process_internal_commands()
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1350, in process_internal_commands
    int_cmd.do_it(self)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 502, in do_it
    self.method(dbg, *self.args, **self.kwargs)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 675, in internal_get_variable_json
    for child_var in variable.get_children_variables(fmt=fmt):
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_suspended_frames.py", line 114, in get_children_variables
    dct = resolver.get_dictionary(self.value)
  File "c:\Users\boomer\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd_plugins\extensions\types\pydevd_plugin_numpy_types.py", line 80, in get_dictionary
    ret['[0:%s] ' % (len(obj))] = list(obj[0:MAX_ITEMS_TO_HANDLE])
IndexError: too many indices for array

我可能会偏离轨道,但是引发此异常的原因是什么?可能是我的屏幕分辨率有问题吗?

0 个答案:

没有答案