Repository navigation
Expand file tree
/
Copy pathInferenceSession.cpp
More file actions
135 lines (120 loc) · 4.53 KB
/
Copy pathInferenceSession.cpp
File metadata and controls
135 lines (120 loc) · 4.53 KB
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
/* Inference Session
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/Exceptions.h"
#include "CommonFramework/Tools/VideoStream.h"
#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h"
#include "CommonTools/InferenceCallbacks/AudioInferenceCallback.h"
#include "CommonTools/InferencePivots/VisualInferencePivot.h"
#include "CommonTools/InferencePivots/AudioInferencePivot.h"
#include "InferenceSession.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
InferenceSession::InferenceSession(
Cancellable& scope, VideoStream& stream,
const std::vector<PeriodicInferenceCallback>& callbacks,
std::chrono::milliseconds default_video_period,
std::chrono::milliseconds default_audio_period
)
: m_scope(scope)
, m_stream(stream)
, m_overlays(stream.overlay())
, m_triggered(nullptr)
{
WallClock start_time = current_time();
try{
for (size_t c = 0; c < callbacks.size(); c++){
const PeriodicInferenceCallback& callback = callbacks[c];
if (callback.callback == nullptr){
continue;
}
if (!m_map.emplace(callback.callback, c).second){
throw InternalProgramError(&stream.logger(), PA_CURRENT_FUNCTION, "Attempted to add the same callback twice.");
}
switch (callback.callback->type()){
case InferenceType::EXTERNAL:{
ExternalInferenceCallback& stopper = static_cast<ExternalInferenceCallback&>(*callback.callback);
stopper.add_cancel_listener(*this);
break;
}
case InferenceType::VISUAL:{
VisualInferenceCallback& visual_callback = static_cast<VisualInferenceCallback&>(*callback.callback);
stream.video_inference_pivot().add_callback(
scope, &m_triggered,
visual_callback,
callback.period > std::chrono::milliseconds(0) ? callback.period : default_video_period,
start_time
);
visual_callback.make_overlays(m_overlays);
break;
}
case InferenceType::AUDIO:
stream.audio_inference_pivot().add_callback(
scope, &m_triggered,
static_cast<AudioInferenceCallback&>(*callback.callback),
callback.period > std::chrono::milliseconds(0) ? callback.period : default_audio_period,
start_time
);
break;
}
}
}catch (...){
clear();
throw;
}
}
InferenceSession::~InferenceSession(){
clear();
}
InferenceCallback* InferenceSession::triggered_ptr() const{
return m_triggered.load(std::memory_order_acquire);
}
int InferenceSession::triggered_index() const{
auto iter = m_map.find(m_triggered.load(std::memory_order_acquire));
if (iter != m_map.end()){
return (int)iter->second;
}
return -1;
}
void InferenceSession::clear() noexcept{
const double DIVIDER = (double)(std::chrono::milliseconds(1) / std::chrono::microseconds(1));
const char* UNITS = " ms";
for (auto& item : m_map){
switch (item.first->type()){
case InferenceType::EXTERNAL:{
Cancellable& stopper = dynamic_cast<Cancellable&>(*item.first);
stopper.remove_cancel_listener(*this);
}
case InferenceType::VISUAL:{
StatAccumulatorI32 stats = m_stream.video_inference_pivot().remove_callback(
static_cast<VisualInferenceCallback&>(*item.first)
);
try{
stats.log(m_stream.logger(), item.first->label(), UNITS, DIVIDER);
}catch (...){}
break;
}
case InferenceType::AUDIO:{
StatAccumulatorI32 stats = m_stream.audio_inference_pivot().remove_callback(
static_cast<AudioInferenceCallback&>(*item.first)
);
try{
stats.log(m_stream.logger(), item.first->label(), UNITS, DIVIDER);
}catch (...){}
break;
}
}
}
}
void InferenceSession::on_cancellable_cancel(
Cancellable& cancellable,
std::exception_ptr reason
){
m_triggered.store(&static_cast<ExternalInferenceCallback&>(cancellable), std::memory_order_release);
m_scope.cancel(std::move(reason));
}
}