-
Notifications
You must be signed in to change notification settings - Fork 6
/
testthermal.py
124 lines (100 loc) · 3.45 KB
/
testthermal.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
import argparse
import json
import logging
import sys
import processing
from processing import thermal
from pathlib import Path
def init_logging():
"""Set up logging for use by various classifier pipeline scripts.
Logs will go to stderr.
"""
fmt = "%(levelname)7s %(message)s"
logging.basicConfig(
stream=sys.stderr, level=logging.DEBUG, format=fmt, datefmt="%Y-%m-%d %H:%M:%S"
)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"source",
help='a CPTV file to process, or a folder name, or "all" for all files within subdirectories of source folder.',
)
args = parser.parse_args()
return args
def main():
init_logging()
conf = processing.Config.load()
args = parse_args()
recording_meta = {
"filename": args.source,
"id": "testrecid",
"jobKey": "test job key",
}
api = TestAPI()
thermal.track(conf, recording_meta, api, 10, logging)
thermal.classify(conf, recording_meta, api, logging)
class TestAPI:
id_ = 0
ALGORITHM = 1
TRUNCATE_OVER = 100
def new_id(self):
TestAPI.id_ += 1
return TestAPI.id_
def report_failed(self, rec_id, job_key):
logging.warn("TestAPI Recording %s failed".rec_id)
def report_done(self, recording, newKey=None, newMimeType=None, metadata=None):
if not metadata:
metadata = {}
if newMimeType:
metadata["fileMimeType"] = newMimeType
params = {
"jobKey": recording["jobKey"],
"id": recording["id"],
"success": True,
"complete": True,
"result": json.dumps({"fieldUpdates": metadata}),
}
if newKey:
params["newProcessedFileKey"] = newKey
logging.debug("TestAPI report_done %s", str(params)[: TestAPI.TRUNCATE_OVER])
def tag_recording(self, recording, label, metadata):
tag = metadata.copy()
tag["automatic"] = True
# Convert "false positive" to API representation.
if not "event" in metadata:
tag["event"] = "just wandering about"
tag["animal"] = label
data = {"recordingId": recording["id"], "tag": json.dumps(tag)}
logging.debug("TestAPI tag_recording %s", str(data)[: TestAPI.TRUNCATE_OVER])
def get_algorithm_id(self, algorithm):
post_data = {"algorithm": json.dumps(algorithm)}
logging.debug(
"TestAPI get_algorithm_id %s", str(post_data)[: TestAPI.TRUNCATE_OVER]
)
return TestAPI.ALGORITHM
def add_track(self, recording, track, algorithm_id):
post_data = {"data": json.dumps(track), "algorithmId": algorithm_id}
track_id = self.new_id()
logging.debug(
"TestAPI add_track (%s) %s",
track_id,
str(post_data)[: TestAPI.TRUNCATE_OVER],
)
return track_id
def add_track_tag(self, recording, track_id, prediction, data=""):
url = "/{}/tracks/{}/tags".format(recording["id"], track_id)
post_data = {
"what": prediction["tag"],
"confidence": prediction["confidence"],
"data": json.dumps(data),
}
track_tag_id = self.new_id()
logging.debug(
"TestAPI add_track_tag (%s) %s, %s",
track_tag_id,
url,
str(post_data)[: TestAPI.TRUNCATE_OVER],
)
return track_tag_id
if __name__ == "__main__":
main()