-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathsample_cpp_multithreading.cpp
More file actions
293 lines (243 loc) · 9.45 KB
/
sample_cpp_multithreading.cpp
File metadata and controls
293 lines (243 loc) · 9.45 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
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
/**
* Created by Jingyu Yan
* @date 2025-07-13
*
* Multi-threading example: One thread performs face tracking and stores tokens,
* another thread monitors tokens and extracts face features.
*/
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <chrono>
#include <memory>
#include <inspireface.h>
// Thread-safe token storage using C++ containers
class ThreadSafeTokenStorage {
private:
std::vector<HFFaceBasicToken> tokens_;
mutable std::mutex mutex_; // Make mutex mutable for const member functions
std::condition_variable cv_;
std::atomic<bool> stop_flag_{false};
public:
// Add a token to the storage
void addToken(const HFFaceBasicToken& token) {
std::lock_guard<std::mutex> lock(mutex_);
// Copy token data
HInt32 tokenSize;
HFGetFaceBasicTokenSize(&tokenSize);
char* tokenBuffer = new char[tokenSize];
HFCopyFaceBasicToken(token, tokenBuffer, tokenSize);
// Create copied token
HFFaceBasicToken copiedToken;
copiedToken.size = tokenSize;
copiedToken.data = tokenBuffer;
tokens_.push_back(copiedToken);
cv_.notify_one();
}
// Get the last token and remove it
bool getLastToken(HFFaceBasicToken& token) {
std::unique_lock<std::mutex> lock(mutex_);
if (tokens_.empty()) {
return false;
}
token = tokens_.back();
tokens_.pop_back();
return true;
}
// Wait for a token with timeout
bool waitForToken(HFFaceBasicToken& token, int timeout_ms = 1000) {
std::unique_lock<std::mutex> lock(mutex_);
if (cv_.wait_for(lock, std::chrono::milliseconds(timeout_ms),
[this] { return !tokens_.empty() || stop_flag_.load(); })) {
if (!tokens_.empty()) {
token = tokens_.back();
tokens_.pop_back();
return true;
}
}
return false;
}
// Check if there are tokens available
bool hasTokens() const {
std::lock_guard<std::mutex> lock(mutex_);
return !tokens_.empty();
}
// Get token count
size_t getTokenCount() const {
std::lock_guard<std::mutex> lock(mutex_);
return tokens_.size();
}
// Stop the threads
void stop() {
stop_flag_.store(true);
cv_.notify_all();
}
// Check if should stop
bool shouldStop() const {
return stop_flag_.load();
}
// Cleanup allocated memory
void cleanup() {
std::lock_guard<std::mutex> lock(mutex_);
for (auto& token : tokens_) {
if (token.data != nullptr) {
delete[] static_cast<char*>(token.data);
}
}
tokens_.clear();
}
~ThreadSafeTokenStorage() {
cleanup();
}
};
// Face tracking thread function
void faceTrackingThread(ThreadSafeTokenStorage& tokenStorage,
HFSession session,
const std::string& imagePath,
int maxFrames = 100) {
std::cout << "Face tracking thread started" << std::endl;
// Load image using C API
HFImageBitmap imageBitmap = {0};
HResult ret = HFCreateImageBitmapFromFilePath(imagePath.c_str(), 3, &imageBitmap);
if (ret != HSUCCEED) {
std::cerr << "Failed to create image bitmap: " << ret << std::endl;
return;
}
// Create image stream
HFImageStream stream;
ret = HFCreateImageStreamFromImageBitmap(imageBitmap, HF_CAMERA_ROTATION_0, &stream);
if (ret != HSUCCEED) {
std::cerr << "Failed to create image stream: " << ret << std::endl;
HFReleaseImageBitmap(imageBitmap);
return;
}
int frameCount = 0;
while (!tokenStorage.shouldStop() && frameCount < maxFrames) {
// Perform face detection and tracking using C API
HFMultipleFaceData multipleFaceData = {0};
ret = HFExecuteFaceTrack(session, stream, &multipleFaceData);
if (ret == HSUCCEED && multipleFaceData.detectedNum > 0) {
// Add tokens to storage
for (int i = 0; i < multipleFaceData.detectedNum; i++) {
tokenStorage.addToken(multipleFaceData.tokens[i]);
std::cout << "Frame " << frameCount << ": Added token for face "
<< multipleFaceData.trackIds[i] << std::endl;
}
}
frameCount++;
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Simulate frame processing
}
// Cleanup
HFReleaseImageStream(stream);
HFReleaseImageBitmap(imageBitmap);
std::cout << "Face tracking thread finished after " << frameCount << " frames" << std::endl;
}
// Face feature extraction thread function
void featureExtractionThread(ThreadSafeTokenStorage& tokenStorage,
HFSession session,
const std::string& imagePath) {
std::cout << "Feature extraction thread started" << std::endl;
// Load image using C API (same as tracking thread)
HFImageBitmap imageBitmap = {0};
HResult ret = HFCreateImageBitmapFromFilePath(imagePath.c_str(), 3, &imageBitmap);
if (ret != HSUCCEED) {
std::cerr << "Failed to create image bitmap: " << ret << std::endl;
return;
}
// Create image stream
HFImageStream stream;
ret = HFCreateImageStreamFromImageBitmap(imageBitmap, HF_CAMERA_ROTATION_0, &stream);
if (ret != HSUCCEED) {
std::cerr << "Failed to create image stream: " << ret << std::endl;
HFReleaseImageBitmap(imageBitmap);
return;
}
int extractedCount = 0;
while (!tokenStorage.shouldStop()) {
HFFaceBasicToken token;
// Wait for token with timeout
if (tokenStorage.waitForToken(token, 2000)) {
// Extract face feature using C API
HFFaceFeature feature;
ret = HFCreateFaceFeature(&feature);
if (ret == HSUCCEED) {
ret = HFFaceFeatureExtractTo(session, stream, token, feature);
if (ret == HSUCCEED) {
extractedCount++;
std::cout << "Extracted feature " << extractedCount
<< " (feature size: " << feature.size << ")" << std::endl;
// Print first few feature values as example
std::cout << "Feature values: ";
for (int i = 0; i < std::min(5, feature.size); i++) {
std::cout << feature.data[i] << " ";
}
std::cout << "..." << std::endl;
} else {
std::cerr << "Feature extraction failed with error: " << ret << std::endl;
}
HFReleaseFaceFeature(&feature);
}
// Clean up token memory
if (token.data != nullptr) {
delete[] static_cast<char*>(token.data);
}
} else {
std::cout << "No tokens available, waiting..." << std::endl;
}
}
// Cleanup
HFReleaseImageStream(stream);
HFReleaseImageBitmap(imageBitmap);
std::cout << "Feature extraction thread finished, extracted "
<< extractedCount << " features" << std::endl;
}
int main(int argc, char** argv) {
if (argc != 3) {
std::cout << "Usage: " << argv[0] << " <model_path> <image_path>" << std::endl;
return -1;
}
std::string modelPath = argv[1];
std::string imagePath = argv[2];
// Initialize InspireFace using C API
HResult ret = HFLaunchInspireFace(modelPath.c_str());
if (ret != HSUCCEED) {
std::cerr << "Failed to launch InspireFace: " << ret << std::endl;
return -1;
}
// Create session using C API
HOption option = HF_ENABLE_FACE_RECOGNITION;
HFSession session;
ret = HFCreateInspireFaceSessionOptional(option, HF_DETECT_MODE_LIGHT_TRACK, 10, -1, -1, &session);
if (ret != HSUCCEED) {
std::cerr << "Failed to create session: " << ret << std::endl;
return -1;
}
// Set session parameters using C API
HFSessionSetTrackPreviewSize(session, 640);
// Create thread-safe token storage
ThreadSafeTokenStorage tokenStorage;
// Start face tracking thread
std::thread trackingThread(faceTrackingThread, std::ref(tokenStorage), session, imagePath, 50);
// Start feature extraction thread
std::thread extractionThread(featureExtractionThread, std::ref(tokenStorage), session, imagePath);
// Main thread: monitor and print statistics
std::cout << "Main thread: Monitoring token storage..." << std::endl;
for (int i = 0; i < 30; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
size_t tokenCount = tokenStorage.getTokenCount();
std::cout << "Token storage status: " << tokenCount << " tokens available" << std::endl;
}
// Stop threads
std::cout << "Stopping threads..." << std::endl;
tokenStorage.stop();
// Wait for threads to finish
trackingThread.join();
extractionThread.join();
// Cleanup using C API
HFReleaseInspireFaceSession(session);
std::cout << "All threads finished successfully" << std::endl;
return 0;
}