1
1
from pathlib import Path
2
2
from pooch import retrieve
3
+ from urllib import request
3
4
5
+ import hashlib
4
6
import socket
5
7
6
8
12
14
}
13
15
14
16
URL_HASH = {
15
- 'id_gender' : 'https://gitlab.com/alexdesiqueira/mothra-models/-/raw/main/models/id_gender/.SHA256SUM_ONLINE-id_gender' ,
16
- 'id_position' : 'https://gitlab.com/alexdesiqueira/mothra-models/-/raw/main/models/id_position/.SHA256SUM_ONLINE-id_position' ,
17
- 'segmentation' : 'https://gitlab.com/alexdesiqueira/mothra-models/-/raw/main/models/segmentation/.SHA256SUM_ONLINE-segmentation'
18
- }
19
-
20
- LOCAL_HASH = {
21
- 'id_gender' : Path ('./models/SHA256SUM-id_gender' ),
22
- 'id_position' : Path ('./models/SHA256SUM-id_position' ),
23
- 'segmentation' : Path ('./models/SHA256SUM-segmentation' )
17
+ 'id_gender' : 'https://gitlab.com/alexdesiqueira/mothra-models/-/raw/main/models/id_gender/SHA256SUM-id_gender' ,
18
+ 'id_position' : 'https://gitlab.com/alexdesiqueira/mothra-models/-/raw/main/models/id_position/SHA256SUM-id_position' ,
19
+ 'segmentation' : 'https://gitlab.com/alexdesiqueira/mothra-models/-/raw/main/models/segmentation/SHA256SUM-segmentation'
24
20
}
25
21
26
22
@@ -39,39 +35,8 @@ def _get_model_info(weights):
39
35
URL of the file for the latest model.
40
36
url_hash : str
41
37
URL of the hash file for the latest model.
42
- local_hash : pathlib.Path
43
- Path of the local hash file.
44
- """
45
- return (URL_MODEL .get (weights .stem ), URL_HASH .get (weights .stem ),
46
- LOCAL_HASH .get (weights .stem ))
47
-
48
-
49
- def _check_hashes (weights ):
50
- """Helping function. Downloads hashes for `weights` if they are not
51
- present.
52
-
53
- Parameters
54
- ----------
55
- weights : str or pathlib.Path
56
- Path of the file containing weights.
57
-
58
- Returns
59
- -------
60
- None
61
38
"""
62
- _ , url_hash , local_hash = _get_model_info (weights )
63
-
64
- if not local_hash .is_file ():
65
- download_hash_from_url (url_hash = url_hash , filename = local_hash )
66
-
67
- # creating filename to save url_hash.
68
- filename = local_hash .parent / Path (url_hash ).name
69
-
70
- if not filename .is_file ():
71
- download_hash_from_url (url_hash = url_hash , filename = filename )
72
-
73
-
74
- return None
39
+ return (URL_MODEL .get (weights .stem ), URL_HASH .get (weights .stem ))
75
40
76
41
77
42
def download_weights (weights ):
@@ -86,9 +51,7 @@ def download_weights(weights):
86
51
-------
87
52
None
88
53
"""
89
- # check if hashes are in disk, then get info from the model.
90
- _check_hashes (weights )
91
- _ , url_hash , local_hash = _get_model_info (weights )
54
+ _ , url_hash = _get_model_info (weights )
92
55
93
56
# check if weights is in its folder. If not, download the file.
94
57
if not weights .is_file ():
@@ -97,34 +60,15 @@ def download_weights(weights):
97
60
# file exists: check if we have the last version; download if not.
98
61
else :
99
62
if has_internet ():
100
- local_hash_val = read_hash_local (filename = local_hash )
101
- url_hash_val = read_hash_from_url (path = local_hash .parent ,
102
- url_hash = url_hash )
63
+ local_hash_val = read_hash_local (weights )
64
+ url_hash_val = read_hash_from_url (url_hash )
103
65
if local_hash_val != url_hash_val :
104
66
print ('New training data available. Downloading...' )
105
67
fetch_data (weights )
106
68
107
69
return None
108
70
109
71
110
- def download_hash_from_url (url_hash , filename ):
111
- """Downloads hash from `url_hash`.
112
-
113
- Parameters
114
- ----------
115
- url_hash : str
116
- URL of the SHA256 hash.
117
- filename : str
118
- Filename to save the SHA256 hash locally.
119
-
120
- Returns
121
- -------
122
- None
123
- """
124
- retrieve (url = url_hash , known_hash = None , fname = filename , path = '.' )
125
- return None
126
-
127
-
128
72
def fetch_data (weights ):
129
73
"""Downloads and checks the hash of `weights`, according to its filename.
130
74
@@ -137,15 +81,11 @@ def fetch_data(weights):
137
81
-------
138
82
None
139
83
"""
140
- url_model , url_hash , local_hash = _get_model_info (weights )
84
+ url_model , url_hash = _get_model_info (weights )
141
85
142
- # creating filename to save url_hash.
143
- filename = local_hash .parent / Path (url_hash ).name
144
-
145
- download_hash_from_url (url_hash = url_hash , filename = filename )
146
- local_hash_val = read_hash_local (local_hash )
86
+ url_hash_val = read_hash_from_url (url_hash )
147
87
retrieve (url = url_model ,
148
- known_hash = f'sha256:{ local_hash_val } ' ,
88
+ known_hash = f'sha256:{ url_hash_val } ' ,
149
89
fname = weights ,
150
90
path = '.' )
151
91
@@ -166,40 +106,44 @@ def has_internet():
166
106
return socket .gethostbyname (socket .gethostname ()) != '127.0.0.1'
167
107
168
108
169
- def read_hash_local (filename ):
170
- """Reads local SHA256 hash file .
109
+ def read_hash_local (weights ):
110
+ """Reads local SHA256 hash from weights .
171
111
172
112
Parameters
173
113
----------
174
- filename : pathlib.Path
175
- Path of the hash file.
114
+ weights : str or pathlib.Path
115
+ Path of the file containing weights .
176
116
177
117
Returns
178
118
-------
179
- local_hash : str
180
- SHA256 hash.
119
+ local_hash : str or None
120
+ SHA256 hash of weights file .
181
121
182
122
Notes
183
123
-----
184
124
Returns None if file is not found.
185
125
"""
126
+ BUFFER_SIZE = 65536
127
+ sha256 = hashlib .sha256 ()
128
+
186
129
try :
187
- with open (filename , 'r' ) as file_hash :
188
- hashes = [line for line in file_hash ]
189
- # expecting only one hash, and not interested in the filename:
190
- local_hash , _ = hashes [0 ].split ()
130
+ with open (weights , 'rb' ) as file_weights :
131
+ while True :
132
+ data = file_weights .read (BUFFER_SIZE )
133
+ if not data :
134
+ break
135
+ sha256 .update (data )
136
+ local_hash = sha256 .hexdigest ()
191
137
except FileNotFoundError :
192
138
local_hash = None
193
139
return local_hash
194
140
195
141
196
- def read_hash_from_url (path , url_hash ):
197
- """Downloads and returns the SHA256 hash online for the file in `url_hash`.
142
+ def read_hash_from_url (url_hash ):
143
+ """Returns the SHA256 hash online for the file in `url_hash`.
198
144
199
145
Parameters
200
146
----------
201
- path : str
202
- Where to look for the hash file.
203
147
url_hash : str
204
148
URL of the hash file for the latest model.
205
149
@@ -208,14 +152,14 @@ def read_hash_from_url(path, url_hash):
208
152
online_hash : str
209
153
SHA256 hash for the file in `url_hash`.
210
154
"""
211
- filename = Path ( url_hash ). name
212
- latest_hash = Path ( f' { path } / { filename } ' )
155
+ user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
156
+ headers = { 'User-Agent' : user_agent ,}
213
157
214
- download_hash_from_url (url_hash = url_hash , filename = filename )
215
- with open ( latest_hash , 'r' ) as file_hash :
216
- hashes = [ line for line in file_hash ]
158
+ aux_req = request . Request (url_hash , None , headers )
159
+ response = request . urlopen ( aux_req )
160
+ hashes = response . read ()
217
161
218
162
# expecting only one hash, and not interested in the filename:
219
- online_hash , _ = hashes [ 0 ] .split ()
163
+ online_hash , _ = hashes . decode ( 'ascii' ) .split ()
220
164
221
165
return online_hash
0 commit comments