-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsysmatch.py
177 lines (145 loc) · 4.99 KB
/
dsysmatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
import urllib.parse as url
import requests
import json
class DsysMatch:
def __init__(self, server_address: str, server_port: int = 80):
"""
A simple wrapper over the dsys/match Web API.
Parameters
----------
server_address : str
The server IP address, and scheme.
Example: http://35.185.109.219
server_port : int = 80
The port on which dsys/match is listening.
Returns
-------
None
TODO
----
- Add type & range guards
"""
self.server_address = server_address.strip(
'/') + ':' + str(server_port) + '/'
def Add(self, image_path: str, file_path: str, metadata: str = None):
"""
Adds an image signature to the database.
Parameters
----------
image_path : str
The location, on disk for the image.
filepath : str
The path of the image signature in the database.
If the path exists it will be overwritten with the
new signature.
metadata : str = None
A JSON formatted str object featuring metadata to
attach to the image.
Returns
-------
dict
"""
image_binary = open(image_path, 'rb')
payload = {
'image': ('image', image_binary),
'filepath': ('', file_path),
}
if (metadata != None):
payload['metadata'] = ('', metadata)
request_address = url.urljoin(self.server_address, '/add')
return json.loads(requests.post(request_address, files=payload).text)
def Delete(self, file_path: str):
"""
Deletes an image signature from the database.
Parameters
----------
filepath : str
The path of the image signature in the database.
Returns
-------
dict
"""
payload = {
'filepath': file_path,
}
request_address = url.urljoin(self.server_address, '/delete')
return json.loads(requests.delete(request_address, data=payload).text)
def Search(self, image_path: str, all_orientations: bool = True):
"""
Searches for a similar image in the database.
Scores range from 0 to 100, with 100 being a perfect match.
Parameters
----------
image_path : str
The location, on disk for the image.
all_orientations : bool = True
Whether or not to search for similar 90 degree rotations of the image.
Returns
-------
dict
"""
image_binary = open(image_path, 'rb')
payload = {
'image': ('image', image_binary),
'all_orientations': ('', all_orientations),
}
request_address = url.urljoin(self.server_address, '/search')
return json.loads(requests.post(request_address, files=payload).text)
def Compare(self, image1_path: str, image2_path: str):
"""
Compares two images, returning a score for their similarity.
Scores range from 0 to 100, with 100 being a perfect match.
Parameters
----------
image1_path : str
The location, on disk for image1.
image2_path : str
The location, on disk for image2.
Returns
-------
dict
"""
image1_binary = open(image1_path, 'rb')
image2_binary = open(image2_path, 'rb')
payload = {
'image1': ('image1', image1_binary),
'image2': ('image2', image2_binary),
}
request_address = url.urljoin(self.server_address, '/compare')
return json.loads(requests.post(request_address, files=payload).text)
def Count(self):
"""
Count the number of image signatures in the database.
"""
request_address = url.urljoin(self.server_address, '/count')
return json.loads(requests.get(request_address).text)
def List(self, offset: int = 0, limit: int = 20):
"""
Lists the file paths for the image signatures in the database.
Parameters
----------
offset : int = 0
The location in the database to begin listing image paths.
limit : int = 20
The number of image paths to retrieve.
Returns
-------
dict
"""
params = {
'offset': offset,
'limit': limit
}
request_address = url.urljoin(self.server_address, '/list')
return json.loads(requests.get(request_address, params=params).text)
def Ping(self):
"""
Check for the health of the server.
"""
request_address = url.urljoin(self.server_address, '/ping')
return json.loads(requests.get(request_address).text)
if __name__ == '__main__':
raise SystemExit(
"Please import this module into a python script and use it from there..."
)