summaryrefslogtreecommitdiff
path: root/python/openvino/runtime/streaming/image_streaming_app/image_streaming_app.cpp
blob: bf4cb2bb8dcc71f21c3d035f7a94c5105eec3a60 (plain)
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
// Copyright 2023 Intel Corporation.
//
// This software and the related documents are Intel copyrighted materials,
// and your use of them is governed by the express license under which they
// were provided to you ("License"). Unless the License provides otherwise,
// you may not use, modify, copy, publish, distribute, disclose or transmit
// this software or the related documents without Intel's prior written
// permission.
//
// This software and the related documents are provided as is, with no express
// or implied warranties, other than those that are expressly stated in the
// License.

#include "image_streaming_app.h"
#include <signal.h>
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include <fcntl.h>
#include "raw_image.h"

int main(int numParams, char* paramValues[]) {
  ImageStreamingApp imageStreamingApp(numParams, paramValues);
  imageStreamingApp.Run();
  return 0;
}

volatile bool ImageStreamingApp::_shutdownEvent;

ImageStreamingApp::ImageStreamingApp(int numParams, char* paramValues[]) : _commandLine(numParams, paramValues) {
  std::string imagesFolder;
  if (_commandLine.GetOption("images_folder", imagesFolder))
    _imageFilesFolder = imagesFolder;
  else
    _imageFilesFolder = "./";

  std::string imageFile;
  if (_commandLine.GetOption("image", imageFile)) {
    _numToSend = 1;
    _imageFile = imageFile;
  }

  std::string nSendStr;
  if (_commandLine.GetOption("send", nSendStr)) _numToSend = std::strtoul(nSendStr.c_str(), 0, 0);

  std::string rateStr;
  if (_commandLine.GetOption("rate", rateStr)) _sendRate = std::strtoul(rateStr.c_str(), 0, 0);

  _dumpTransformedImages = _commandLine.HaveOption("dump");
  _runLayoutTransform = _commandLine.HaveOption("layout_transform");

  _ltConfiguration._width = GetUintOption("width", 224);
  _ltConfiguration._height = GetUintOption("height", 224);
  _ltConfiguration._cVector = GetUintOption("c_vector", 32);
  _ltConfiguration._blueVariance = GetFloatOption("blue_variance", 1.0f);
  _ltConfiguration._greenVariance = GetFloatOption("green_variance", 1.0f);
  _ltConfiguration._redVariance = GetFloatOption("red_variance", 1.0f);
  _ltConfiguration._blueShift = GetFloatOption("blue_shift", -103.94f);
  _ltConfiguration._greenShift = GetFloatOption("green_shift", -116.78f);
  _ltConfiguration._redShift = GetFloatOption("red_shift", -123.68f);

  signal(SIGINT, SigIntHandler);
}

void ImageStreamingApp::Run() {
  if (_commandLine.HaveOption("-help")) {
    std::cout << "Usage:\n";
    std::cout << " image_streaming_app [Options]\n";
    std::cout << "\nOptions:\n";
    std::cout << "-images_folder=folder     Location of bitmap files. Defaults to working folder.\n";
    std::cout << "-image=path               Location of a single bitmap file for single inference.\n";
    std::cout << "-send=n                   Number of images to stream. Default is 1 if -image is set, otherwise infinite.\n";
    std::cout << "-rate=n                   Rate to stream images, in Hz. n is an integer. Default is 30.\n";
    std::cout << "-width=n                  Image width in pixels, default = 224\n";
    std::cout << "-height=n                 Image height in pixels, default = 224\n";
    std::cout << "-c_vector=n               C vector size, default = 32\n";
    std::cout << "-blue_variance=n          Blue variance, default = 1.0\n";
    std::cout << "-green_variance=n         Green variance, default = 1.0\n";
    std::cout << "-red_variance=n           Red variance, default = 1.0\n";
    std::cout << "-blue_shift=n             Blue shift, default = -103.94\n";
    std::cout << "-green_shift=n            Green shift, default -116.78\n";
    std::cout << "-red_shift=n              Red shift, default = -123.68\n";
    return;
  }

  if (not ProgramLayoutTransform()) {
    return;
  }

  if (not LoadImageFiles(_dumpTransformedImages)) {
    return;
  }

  if (_dumpTransformedImages) {
    return;
  }

  if (not WaitForInferenceApp())
    return;

  // Start event signal thread
  auto sendImageEventThreadCB = [this]() { RunSendImageSignalThread(); };
  std::thread sendImageEventThread(sendImageEventThreadCB);
  uint32_t sentCount = 0;

  while (not _shutdownEvent) {
    // Wait for the send image event
    _sendNextImageEvent.Wait();

    if (not SendNextImage()) {
      _shutdownEvent = true;
      break;
    }
    sentCount++;

    if ((_numToSend > 0) and (sentCount >= _numToSend)) {
      _shutdownEvent = true;
      break;
    }
  }

  // Wait for signalling thread to finish
  sendImageEventThread.join();
}

bool ImageStreamingApp::LoadImageFiles(bool dumpLayoutTransform) {
  if (not _imageFile.empty()) {
    std::filesystem::path filePath(_imageFile);
    std::string extension = filePath.extension();
    std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
    if ((extension == ".bmp") or (extension == ".raw") or (extension == ".lt")) {
      auto spRawImage = std::make_shared<RawImage>(filePath, _runLayoutTransform, _ltConfiguration);
      if (spRawImage->IsValid()) {
        _images.push_back(spRawImage);

        if (dumpLayoutTransform and _runLayoutTransform) {
          spRawImage->DumpLayoutTransform();
        }
      } else {
        std::cout << "Unsupported image: " << filePath << '\n';
      }
    }
  } else {
    for (const auto& entry : std::filesystem::directory_iterator(_imageFilesFolder)) {
      std::string filename = entry.path();
      std::filesystem::path filePath(filename);
      std::string extension = filePath.extension();
      std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
      if ((extension == ".bmp") or (extension == ".raw") or (extension == ".lt")) {
        auto spRawImage = std::make_shared<RawImage>(filePath, _runLayoutTransform, _ltConfiguration);
        _images.push_back(spRawImage);

        if (dumpLayoutTransform and _runLayoutTransform) {
          spRawImage->DumpLayoutTransform();
        }

        // Don't load any more than we need to send
        if (_images.size() == _numToSend) {
          break;
        }
      }
    }
  }

  std::cout << "Loaded " << _images.size() << " image";
  if (_images.size() > 1) {
    std::cout << "s";
  }
  std::cout << '\n';
  return not _images.empty();
}

bool ImageStreamingApp::OpenMsgDmaStream() {
  if (_msgDmaStream) {
    return true;
  }

  constexpr const char* msgdmaFilename = "/dev/msgdma_stream0";
  _msgDmaStream = ::fopen(msgdmaFilename, "w+");
  if (_msgDmaStream == NULL) {
    std::cout << "Failed to open" << '\n';
    return false;
  }

  // Turn off output buffering
  setvbuf(_msgDmaStream, NULL, _IONBF, 0);

  return true;
}

void ImageStreamingApp::CloseMsgDmaStream() {
  if (_msgDmaStream) {
    ::fclose(_msgDmaStream);
    _msgDmaStream = nullptr;
  }
}

bool ImageStreamingApp::SendNextImage() {
  size_t nImages = _images.size();
  if (nImages == 0) {
    return false;
  }

  if (not _msgDmaStream) {
    if (not OpenMsgDmaStream()) {
      return false;
    }
  }

  std::shared_ptr<RawImage> uploadImage = _images[_nextImageIndex];

  // Move to next index for next time
  _nextImageIndex = (_nextImageIndex + 1) % nImages;
  _sentCount++;

  char* pBuffer = reinterpret_cast<char*>(uploadImage->GetData());
  size_t bufferSize = uploadImage->GetSize();

  std::cout << _sentCount << " Send image " << uploadImage->Filename() << " size = " << bufferSize;

  size_t nWritten = ::fwrite(pBuffer, 1, bufferSize, _msgDmaStream);
  bool ok = (nWritten == bufferSize);
  if (ok) {
    std::cout << '\n';
  } else {
    std::cout << " failed\n";
  }

  return ok;
}

void ImageStreamingApp::RunSendImageSignalThread() {
  int64_t microSeconds = 1000000 / _sendRate;
  if (_sendRate == 59) {
    microSeconds = 16683;  // 59.94 Hz
  }

  while (not _shutdownEvent) {
    std::this_thread::sleep_for(std::chrono::microseconds(microSeconds));
    _sendNextImageEvent.Set();
  }
}

bool ImageStreamingApp::ProgramLayoutTransform() {
  auto spLayoutTransform = ILayoutTransform::Create();
  spLayoutTransform->SetConfiguration(_ltConfiguration);
  return true;
}

uint32_t ImageStreamingApp::GetUintOption(const char* optionName, uint32_t defaultValue) {
  std::string optionValue;
  if (_commandLine.GetOption(optionName, optionValue)) {
    return std::strtoul(optionValue.c_str(), nullptr, 0);
  } else {
    return defaultValue;
  }
}

float ImageStreamingApp::GetFloatOption(const char* optionName, float defaultValue) {
  std::string optionValue;
  if (_commandLine.GetOption(optionName, optionValue)) {
    return std::strtof(optionValue.c_str(), nullptr);
  } else {
    return defaultValue;
  }
}

void ImageStreamingApp::SigIntHandler(int) {
  std::cout << "\nShutting down application\n";
  _shutdownEvent = true;
}

bool ImageStreamingApp::WaitForInferenceApp() {
  bool isReady = false;
  bool firstTime = true;
  sem_t* pSemaphore = ::sem_open("/CoreDLA_ready_for_streaming", O_CREAT, 0644, 0);
  if (!pSemaphore) {
    return isReady;
  }

  while (not _shutdownEvent) {
    // Don't use a wait timeout because we need to break
    // if the user presses Ctrl+C
    timespec waitTimeout = {};
    int r = ::sem_timedwait(pSemaphore, &waitTimeout);
    if (r == 0) {
      isReady = true;
      ::sem_post(pSemaphore);
      break;
    }

    if (firstTime) {
      firstTime = false;
      std::cout << "Waiting for streaming_inference_app to become ready." << std::endl;
    }

    std::this_thread::sleep_for(std::chrono::milliseconds(100));
  }

  ::sem_close(pSemaphore);

  return isReady;
}