-
Notifications
You must be signed in to change notification settings - Fork 124
/
transfer.py
319 lines (271 loc) · 9.49 KB
/
transfer.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import argparse
import json
import os
import queue
import threading
import time
import uuid
from argparse import RawTextHelpFormatter
from datetime import datetime
from pathlib import Path
try:
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
except ImportError:
print(
"A dependency is missing. Please install: "
"https://pythonhosted.org/watchdog/installation.html"
)
exit(1)
try:
import requests
except ImportError:
print(
"A dependency is missing. Please install: "
"https://2.python-requests.org/en/master/user/install/#install"
)
exit(1)
try:
import jsonlines
except ImportError:
print(
"A dependency is missing. Please install: "
"https://jsonlines.readthedocs.io/en/latest/#installation"
)
exit(1)
_queue = queue.Queue(256) # type: ignore
##########################
# Command line arguments #
##########################
def parse_arguments():
parser = argparse.ArgumentParser(
description="""
Important! Before starting the image transfer:
1) Make sure to start the SDK.
2) Get your ParkPow API token from: https://app.parkpow.com/accounts/token (optional if using parkpow)
3) Get your PlateRecognizer API token from: https://app.platerecognizer.com/start/ (optional if using Cloud instead of local SDK)
Here is an example of how to call this script if using Local SDK:
python transfer.py --source /home/alpr/camera-images/ --archive /home/alpr/archived-images/ --alpr-api http://localhost:8080/v1/plate-reader/ --parkpow-token MY_TOKEN --cam-pos 2
Example of how to call this script is using the Cloud Api
python transfer.py --source /home/alpr/camera-images/ --archive /home/alpr/archived-images/ --alpr-api https://api.platerecognizer.com/v1/plate-reader/ --platerec-token MY_PLATEREC_TOKEN --parkpow-token MY_TOKEN --cam-pos 2
The path of each image must contain a directory with the camera name.
It is specified with the --cam-pos argument.
Once processed, images are moved to the archive directory.
If it the --api-url is not defined, the results will be saved in the output file --output-file
""",
formatter_class=RawTextHelpFormatter,
)
parser.add_argument(
"--source", help="Where camera images are saved.", type=str, required=True
)
parser.add_argument(
"--archive",
help="Where images are moved to archive after being processed.",
type=str,
required=True,
)
parser.add_argument(
"--parkpow-token", help="API token for ParkPow.", type=str, required=False
)
parser.add_argument(
"--platerec-token",
help="API token for PlateRecognizer.",
type=str,
required=False,
)
parser.add_argument(
"--cam-pos",
help="Position of the directory with camera name (.../4/3/2/1/image.jpg).\n"
"For example, with /home/export/parking/camera/july/image.jpg, set --cam-pos=2",
type=int,
required=True,
)
parser.add_argument(
"--workers", help="Number of worker threads.", type=int, default=2
)
parser.add_argument(
"--alpr-api",
help="URL of Cloud/SDK API.",
default="https://api.platerecognizer.com/v1/plate-reader",
)
parser.add_argument(
"--use-parkpow", help="Upload results to ParkPow", action="store_true"
)
parser.add_argument(
"--output-file", help="Json file with response", type=str, required=False
)
return parser.parse_args()
##################
# Process images #
##################
def image_transfer(src_path, args):
split = Path(src_path).parts
# make this better
if args.cam_pos >= len(split):
print("Image path does not match template. Call with -h to see help.")
return
filename = split[-1]
camera = split[-args.cam_pos - 1]
results = alpr(src_path, args)
if not results:
return
if not args.output_file:
payload = {
"results": json.dumps(results),
"camera": camera,
}
files = {"image": (filename, open(src_path, "rb"), "application/octet-stream")}
response = api_request(args, payload, files)
if not response:
return
else:
with jsonlines.open(args.output_file, mode="a") as json_file:
json_file.write(results)
response = results
# Move to archive
archive_dir = "{0}/{1}/{2:%Y}/{2:%m}/{2:%d}".format(
args.archive, camera, datetime.now()
)
destination = f"{archive_dir}/{uuid.uuid4()}={filename}"
try:
Path(archive_dir).mkdir(parents=True, exist_ok=True)
os.rename(src_path, destination)
except (PermissionError, OSError):
print("%s could not be moved to archive folder." % src_path)
return dict(dest=destination, response=response)
def alpr(path, args):
print("Sending %s" % path)
try:
if "localhost" in args.alpr_api:
time.sleep(1) # Wait for the whole image to arrive
with open(path, "rb") as fp:
response = requests.post(
args.alpr_api, files=dict(upload=fp), timeout=10
)
else:
time.sleep(1) # Wait for the whole image to arrive
filename = os.path.basename(path)
response = requests.post(
args.alpr_api,
files=dict(
upload=(filename, open(path, "rb"), "application/octet-stream")
),
headers={"Authorization": "Token " + args.platerec_token},
)
except requests.exceptions.Timeout:
print("SDK: Timeout")
return
except ConnectionError:
print("SDK: ConnectionError")
return
except PermissionError:
print("SDK: %s could not be read." % path)
return
except Exception as e:
print(e)
return
data = response.json()
# TODO: skip data if there is no change
if "results" not in data:
print(data)
return []
return data["results"]
def api_request(args, payload, files):
api_url = "https://app.parkpow.com/api/v1/log-vehicle"
headers = {"Authorization": f"Token {args.parkpow_token}"}
try:
response = requests.post(
api_url, data=payload, headers=headers, files=files, timeout=20
)
except ConnectionError:
print("ParkPow API: ConnectionError")
return
except requests.exceptions.Timeout:
print("ParkPow API: Timeout")
return
return response
###################
# File monitoring #
###################
def worker(args):
while True:
image_transfer(_queue.get(), args)
_queue.task_done()
class Handler(PatternMatchingEventHandler):
def on_created(self, event):
try:
_queue.put(event.src_path)
except queue.Full:
print("Queue is full. Skipping %s." % event.scr_path)
def main(args, debug=False):
if args.source in args.archive:
print("Archive argument should not be in source directory.")
return exit(1)
observer = Observer()
observer.schedule(
Handler(ignore_directories=True, patterns="*.jpg *.jpeg".split()),
args.source,
recursive=True,
)
observer.start()
for _ in range(args.workers):
t = threading.Thread(target=worker, args=(args,))
t.daemon = True
t.start()
print("Monitoring source directory.")
try:
while True:
time.sleep(1 if debug else 0.25)
if debug:
break
except KeyboardInterrupt:
pass
print("Closing...")
observer.stop()
observer.join()
_queue.join()
def validate_env(args):
messages = []
Path(args.archive).mkdir(parents=True, exist_ok=True)
if not Path(args.archive).exists():
messages.append("%s does not exist." % args.archive)
if not Path(args.source).exists():
messages.append("%s does not exist." % args.source)
if not args.use_parkpow and not args.output_file:
messages.append("Pass argument --use-parkpow or the argument --output-file")
if "http" not in args.alpr_api:
messages.append("--alpr-api is not a valid URL")
if "api.platerecognizer.com" in args.alpr_api and not args.platerec_token:
messages.append("Missing argument --platerec-token or SDK argument --alpr-api")
elif "api.platerecognizer.com" not in args.alpr_api:
try:
response = requests.get(args.alpr_api.rsplit("/v1", 1)[0], timeout=2)
except Exception:
response = None
if not response or response.status_code != 200:
messages.append(
"Make sure that the SDK is up and running (%s)." % args.alpr_api
)
if args.use_parkpow:
api_url = "https://app.parkpow.com/api/v1/log-vehicle"
try:
response = requests.get(
api_url.rsplit("/", 1)[0] + "/parking-list",
headers={"Authorization": f"Token {args.parkpow_token}"},
timeout=2,
)
except Exception:
response = None
if not response or response.status_code != 200:
messages.append(
response.json() if response else "Parkpow server could not be reached."
)
if len(messages) > 0:
print("Script initialization failed:")
print("\n".join(messages))
print("Exiting...")
exit(1)
if __name__ == "__main__":
args = parse_arguments()
validate_env(args)
main(args)