3030OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3131OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232"""
33+
3334import collections
3435import cv2
3536import numpy
3637from PIL .Image import Image
3738from typing import Union , Tuple , Optional , Generator , Any
3839
39- RUNNING_CV_2 = cv2 .__version__ [0 ] < '3'
40+ RUNNING_CV_2 = cv2 .__version__ [0 ] < "3"
4041
41- Box = collections .namedtuple (' Box' , ' left top width height' )
42+ Box = collections .namedtuple (" Box" , " left top width height" )
4243
4344if RUNNING_CV_2 :
4445 LOAD_COLOR = cv2 .CV_LOAD_IMAGE_COLOR
4849 LOAD_GRAYSCALE = cv2 .IMREAD_GRAYSCALE
4950
5051
51- def _load_cv2 (img : Union [Image , numpy .ndarray , str ], grayscale : bool = False ) -> numpy .ndarray :
52+ def _load_cv2 (
53+ img : Union [Image , numpy .ndarray , str ], grayscale : bool = False
54+ ) -> numpy .ndarray :
5255 """
5356 TODO
5457 """
@@ -68,23 +71,25 @@ def _load_cv2(img: Union[Image, numpy.ndarray, str], grayscale: bool = False) ->
6871 else :
6972 img_cv = cv2 .imread (img , LOAD_COLOR )
7073 if img_cv is None :
71- raise IOError ("Failed to read %s because file is missing, "
72- "has improper permissions, or is an "
73- "unsupported or invalid format" % img )
74+ raise IOError (
75+ "Failed to read %s because file is missing, "
76+ "has improper permissions, or is an "
77+ "unsupported or invalid format" % img
78+ )
7479 elif isinstance (img , numpy .ndarray ):
7580 # don't try to convert an already-gray image to gray
7681 if grayscale and len (img .shape ) == 3 : # and img.shape[2] == 3:
7782 img_cv = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
7883 else :
7984 img_cv = img
80- elif hasattr (img , ' convert' ):
85+ elif hasattr (img , " convert" ):
8186 # assume its a PIL.Image, convert to cv format
82- img_array = numpy .array (img .convert (' RGB' ))
87+ img_array = numpy .array (img .convert (" RGB" ))
8388 img_cv = img_array [:, :, ::- 1 ].copy () # -1 does RGB -> BGR
8489 if grayscale :
8590 img_cv = cv2 .cvtColor (img_cv , cv2 .COLOR_BGR2GRAY )
8691 else :
87- raise TypeError (' expected an image filename, OpenCV numpy array, or PIL image' )
92+ raise TypeError (" expected an image filename, OpenCV numpy array, or PIL image" )
8893 return img_cv
8994
9095
@@ -95,7 +100,7 @@ def locate_all_opencv(
95100 limit : int = 10000 ,
96101 region : Optional [Tuple [int , int , int , int ]] = None ,
97102 step : int = 1 ,
98- confidence : float = 0.999
103+ confidence : float = 0.999 ,
99104) -> Generator [Box , Any , None ]:
100105 """
101106 TODO - rewrite this
@@ -116,15 +121,19 @@ def locate_all_opencv(
116121
117122 if region :
118123 haystack_image = haystack_image [
119- region [1 ]:region [1 ] + region [3 ],
120- region [0 ]:region [0 ] + region [2 ]
121- ]
124+ region [1 ] : region [1 ] + region [3 ], region [0 ] : region [0 ] + region [2 ]
125+ ]
122126 else :
123- region = (0 , 0 ) # full image; these values used in the yield statement
124- if (haystack_image .shape [0 ] < needle_image .shape [0 ] or
125- haystack_image .shape [1 ] < needle_image .shape [1 ]):
127+ region = (0 , 0 , 0 , 0 ) # full image; these values used in the yield statement
128+
129+ if (
130+ haystack_image .shape [0 ] < needle_image .shape [0 ]
131+ or haystack_image .shape [1 ] < needle_image .shape [1 ]
132+ ):
126133 # avoid semi-cryptic OpenCV error below if bad size
127- raise ValueError ('needle dimension(s) exceed the haystack image or region dimensions' )
134+ raise ValueError (
135+ "needle dimension(s) exceed the haystack image or region dimensions"
136+ )
128137
129138 if step == 2 :
130139 confidence *= 0.95
@@ -147,6 +156,8 @@ def locate_all_opencv(
147156 matchy = matches [0 ] * step + region [1 ]
148157
149158 # Order results before sending back
150- ordered = sorted (zip (matchx , matchy ), key = lambda p : result [p [1 ]][p [0 ]], reverse = True )
159+ ordered = sorted (
160+ zip (matchx , matchy ), key = lambda p : result [p [1 ]][p [0 ]], reverse = True
161+ )
151162 for x , y in ordered :
152163 yield Box (x , y , needle_width , needle_height )
0 commit comments