18
18
19
19
from ..mobilecommand import MobileCommand as Command
20
20
21
+ Base64Payload = Union [str , bytes ]
22
+
21
23
22
24
class ImagesComparison (CanExecuteCommands ):
23
- def match_images_features (self , base64_image1 : bytes , base64_image2 : bytes , ** opts : Any ) -> Dict [str , Any ]:
25
+ def match_images_features (self , base64_image1 : Base64Payload , base64_image2 : Base64Payload , ** opts : Any ) -> Dict [str , Any ]:
24
26
"""Performs images matching by features.
25
27
26
28
Read
@@ -66,11 +68,16 @@ def match_images_features(self, base64_image1: bytes, base64_image2: bytes, **op
66
68
rect2 (dict): The bounding rect for the `points2` array or a zero rect if not enough matching points
67
69
were found. The rect is represented by a dictionary with 'x', 'y', 'width' and 'height' keys
68
70
"""
69
- options = {'mode' : 'matchFeatures' , 'firstImage' : base64_image1 , 'secondImage' : base64_image2 , 'options' : opts }
71
+ options = {
72
+ 'mode' : 'matchFeatures' ,
73
+ 'firstImage' : _adjust_image_payload (base64_image1 ),
74
+ 'secondImage' : _adjust_image_payload (base64_image2 ),
75
+ 'options' : opts ,
76
+ }
70
77
return self .execute (Command .COMPARE_IMAGES , options )['value' ]
71
78
72
79
def find_image_occurrence (
73
- self , base64_full_image : bytes , base64_partial_image : bytes , ** opts : Any
80
+ self , base64_full_image : Base64Payload , base64_partial_image : Base64Payload , ** opts : Any
74
81
) -> Dict [str , Union [bytes , Dict ]]:
75
82
"""Performs images matching by template to find possible occurrence of the partial image
76
83
in the full image.
@@ -97,13 +104,15 @@ def find_image_occurrence(
97
104
"""
98
105
options = {
99
106
'mode' : 'matchTemplate' ,
100
- 'firstImage' : base64_full_image ,
101
- 'secondImage' : base64_partial_image ,
107
+ 'firstImage' : _adjust_image_payload ( base64_full_image ) ,
108
+ 'secondImage' : _adjust_image_payload ( base64_partial_image ) ,
102
109
'options' : opts ,
103
110
}
104
111
return self .execute (Command .COMPARE_IMAGES , options )['value' ]
105
112
106
- def get_images_similarity (self , base64_image1 : bytes , base64_image2 : bytes , ** opts : Any ) -> Dict [str , Union [bytes , Dict ]]:
113
+ def get_images_similarity (
114
+ self , base64_image1 : Base64Payload , base64_image2 : Base64Payload , ** opts : Any
115
+ ) -> Dict [str , Union [bytes , Dict ]]:
107
116
"""Performs images matching to calculate the similarity score between them.
108
117
109
118
The flow there is similar to the one used in
@@ -125,8 +134,17 @@ def get_images_similarity(self, base64_image1: bytes, base64_image2: bytes, **op
125
134
score (float): The similarity score as a float number in range [0.0, 1.0].
126
135
1.0 is the highest score (means both images are totally equal).
127
136
"""
128
- options = {'mode' : 'getSimilarity' , 'firstImage' : base64_image1 , 'secondImage' : base64_image2 , 'options' : opts }
137
+ options = {
138
+ 'mode' : 'getSimilarity' ,
139
+ 'firstImage' : _adjust_image_payload (base64_image1 ),
140
+ 'secondImage' : _adjust_image_payload (base64_image2 ),
141
+ 'options' : opts ,
142
+ }
129
143
return self .execute (Command .COMPARE_IMAGES , options )['value' ]
130
144
131
145
def _add_commands (self ) -> None :
132
146
self .command_executor .add_command (Command .COMPARE_IMAGES , 'POST' , '/session/$sessionId/appium/compare_images' )
147
+
148
+
149
+ def _adjust_image_payload (payload : Base64Payload ) -> str :
150
+ return payload if isinstance (payload , str ) else payload .decode ('utf-8' )
0 commit comments