サンプルアプリケーションのAPI - DigitalMediaProfessionals/dv-sdk GitHub Wiki
ここではサンプルアプリケーションに共通のAPI を紹介します。 このAPI を使うことで、DNN モデルを使ったAI FPGA モジュール用アプリケーションを容易に実装できます。 具体的にはAPI は以下の機能を提供します。
- DNN への入力画像のカメラやファイルからの読み込み
- DNN 用の画像の前処理
- DNN の出力の描画とそのモニターへの出力
なお、上記の機能は一部を除き、DNN 用途以外でもご利用いただけます。
-
モニター出力・描画系
- ユーティリティAPI
- COverlayRGB (描画用クラス)
- 入力系
これらのAPI はcommon/include/util_draw.h
にて宣言されています。
ランダムにピクセルが設定された画像をモニターへ出力する例は次の通りです。
#include <iostream>
#include <cstdlib>
#include "util_draw.h"
using namespace std;
using namespace dmp;
using namespace util;
int main()
{
// 手順1:フレームバッファを初期化します。
if (!init_fb()) {
cerr << "Failed to init FrameBuffer" << endl;
return -1;
]
// 手順2:COverlayRGB クラスのインスタンスを作成して、必要なメモリを確保します。
uint32_t width = get_screen_width();
uint32_t height = get_screen_height();
uint32_t image_w = 256;
uint32_t image_h = 256
COverlayRGB overlay(width, height);
overlay.alloc_mem_overlay(image_w, image_h);
// 手順3:先ほど確保したメモリ(バッファ)に描画します。
// ここではランダムにピクセルを埋めます。
for (uint32_t x = 0; x < image_w; x++) {
for (uint32_t y = 0; y < image_h; y++) {
bool s = overlay.set_pixel(x, y, rand(), rand(), rand());
if (!s) goto fin;
}
}
fin:
// 手順4:手順3で描画したバッファをフレームバッファに反映します。
uint32_t left_top_x = 10;
uint32_t left_top_y = 10;
overlay.print_to_display(left_top_x, left_top_y);
// 手順5:フレームバッファはダブルバッファリングされているので、バッファを入れ替えて、描画結果をモニターに出力する。
swap_buffer();
// 手順6:終了処理をします。
shutdown();
}
関数 | 説明 |
---|---|
bool dmp::util::init_fb() |
フレームバッファを初期化します。 |
void dmp::util::shutdown() |
フレームバッファのリソースを解放します。 |
bool dmp::util::swap_buffer() |
ダブルバッファリングされたフレームバッファを切り替えます。 |
uint32_t dmp::util::get_screen_width() |
フレームバッファの幅をピクセル数で返します。 |
uint32_t dmp::util::get_screen_height() |
フレームバッファの高さをピクセル数で返します。 |
dmp::util::COverlayRGB
クラスは描画とモニター出力のためのクラスです。
alloc_mem_overlay()
メソッドで描画用のバッファを確保してから、各種描画用メソッドで描画した後で、print_to_display()
メソッドでモニター出力をします。(ただしこの後にswap_buffer()
関数でモニター用のバッファを切り替える必要があります。)
以下にメソッドの一覧を示します。詳細についてはソースコードのコメントをご覧ください。
メソッド | 説明 |
---|---|
COverlayRGB(uint32_t screen_width, uint32_t screen_height); |
コンストラクタです。スクリーンサイズを指定します。 |
void alloc_mem_overlay(uint32_t overlay_width, uint32_t overlay_height); |
オーバーレイバッファを確保します。 |
uint32_t get_overlay_width(); |
オーバーレイバッファの幅を返します。 |
uint32_t get_overlay_height(); |
オーバーレイバッファの高さを返します。 |
uint8_t* get_overlay_buf_ref(); |
オーバーレイバッファへのポインタを返します。 |
bool convert_to_overlay_pixel_format(uint32_t *imgview, uint32_t size_of_imgview); |
RGB 画像からデータを読み込んで、オーバーレイバッファへ書き込みます。 |
rbuf_type& get_ren_buf_ref(); |
描画用バッファへの参照を返します。 |
uint8_t* get_overlay_row_ptr_ref(uint32_t row); |
描画用バッファの特定の行へのポインタを返します。 |
bool set_pixel(uint32_t xpos, uint32_t ypos, uint8_t red, uint8_t green, uint8_t blue); |
ピクセルを描画します。 |
bool copy_overlay(COverlayRGB &src_overlay, uint32_t xpos, uint32_t ypos); |
別のオーバーレイを特定の箇所にコピーします。 |
void set_box_with_text(int32_t x0pos, int32_t y0pos, int32_t x1pos, int32_t y1pos, uint32_t color, string text); |
テキストとともに四角形を描画します。 |
void set_text(int32_t xpos, int32_t ypos, string text, uint32_t text_size, uint32_t color, double stroke_size = 1.5); |
テキストを描画します。 |
void set_text_with_font(string path_to_ttf, string text, double xpos, double ypos, uint32_t text_size, uint32_t color); |
指定のフォントでテキストを描画します。 |
void set_box(int32_t x0pos, int32_t y0pos, int32_t x1pos, int32_t y1pos, uint32_t color); |
四角形を描画します。 |
void set_line(int32_t x0pos, int32_t y0pos, int32_t x1pos, int32_t y1pos, uint32_t color); |
テキストのない線分を描画します。color はargb 形式です。 |
void blend_from(COverlayRGB &overlay, double alpha=0.5); |
ほかのオーバーレイとブレンドします。 |
bool load_ppm_img(string filename_wo_ext); |
ppm ファイルから画像を読み込み、描画します。 |
bool save_as_ppm_img(string filename_wo_ext); |
ppm ファイルに画像を書き出します。 |
void draw_progress_bar(int32_t xpos, int32_t ypos, uint32_t w, uint32_t h, uint32_t color, uint8_t prog_0_to_100); |
プログレスバーを描画します。 |
void print_to_display(uint32_t xpos, uint32_t ypos); |
ディスプレイにオーバーレイを描画します。 |
void delete_overlay(); |
オーバーレイバッファを削除します。 |
static void capture_screen(std::string filename, uint32_t screen_width, uint32_t screen_height); |
画面をキャプチャします。 |
void set_mouse_cursor(uint32_t m_color); |
マウスカーソルを描画します。 |
void set_background_to_color(uint32_t m_color); |
背景を塗りつぶします。 |
入力系のAPI はcommon/include/util_input.h
にて定義されています。
カメラを利用するにはopen_cam()
でカメラを開いてから、capture_cam()
で画像を取り込んでください。
カメラが不要になりましたらclose_cam()
でカメラを閉じてください。
int dmp::util::open_cam(int capture_w, int capture_h, int fps);
カメラを開きます。
- 引数
-
capture_w
カメラの入力画像の幅 -
capture_h
カメラの入力画像の高さ -
fps
カメラのFPS
-
- 返値
- 成功時には0 が、そうでないときは0 以外の値が返されます。
int dmp::util::close_cam();
カメラを閉じます。
int dmp::util::capture_cam(uint32_t* vImg, int capture_w, int capture_h,
int crop_xoffs, int crop_yoffs, int crop_w, int crop_h);
カメラから画像を取得します。
- 引数
-
vImg
画像のバッファ -
capture_w
画像の幅 -
capture_h
画像の高さ -
crop_xoffs
クロップする際のx オフセット -
crop_yoffs
クロップする際のy オフセット -
crop_w
クロップの幅 -
crop_h
クロップの高さ
-
- 返値
- 成功時には0 が、そうでないときは0 以外の値が返されます。
void get_jpeg_image_names(const std::string &input_image_path, std::vector<std::string> &image_names);
jpeg ファイルを再帰的に検索して、パスを取得します。
なおこの関数はutil_input.h
でなくdemo_common.h
で宣言されています。
- 引数
-
input_image_path
検索対象ディレクトリ -
image_names
発見されたjpeg ファイルのパスの一覧を格納するバッファ
-
int dmp::util::decode_jpg_file(const std::string &imgFileName, uint32_t *imgView,
int img_width, int img_height);
JPEG ファイルをRGB データにデコードします。
- 引数
-
imgFileName
JPEG ファイルのパス -
imgView
RGB データがかかれるバッファ -
img_width
画像の幅 -
img_height
画像の高さ
-
- 返値
- 成功時には0 が、そうでないときは0 以外の値が返されます。
void dmp::util::preproc_image(uint32_t *__restrict imgView, __fp16 *__restrict imgProc,
int img_width, int img_height, float r_offs, float g_offs,
float b_offs, float sf, bool transpose, bool is_bgr = true);
void dmp::util::preproc_image(uint32_t *__restrict imgView, __fp16 *__restrict imgProc,
int inimg_width, int inimg_height, int outimg_width,
int outimg_height, float r_offs, float g_offs, float b_offs,
float sf, bool transpose, bool is_bgr = true);
RGB 画像をネットワークの入力画像へと変換します。
- 引数
-
imgView
RGB 画像です。 -
imgProc
ネットワークの入力画像のバッファです。 -
inimg_width
RGB 画像の幅です。 -
inimg_height
RGB 画像の高さです。 -
outimg_width
ネットワークの入力画像の幅です。 -
outimg_height
ネットワークの入力画像の高さです。 -
r_offs
R チャネルのオフセットです。ネットワークの入力画像でのR チャネルの値は、RGB 画像のR チャネルの値にr_offs
を足した値となります。 -
g_offs
G チャネルのオフセットです。ネットワークの入力画像でのG チャネルの値は、RGB 画像のG チャネルの値にg_offs
を足した値となります。 -
b_offs
B チャネルのオフセットです。ネットワークの入力画像でのB チャネルの値は、RGB 画像のB チャネルの値にb_offs
を足した値となります。 -
sf
スケール係数です。各チャネルの値では、*_offs
引数が適用されたあとに、sf
引数の値がかけられ、最終的な値が決まります。 -
transpose
true
ならば画像を転置します。SDK では入力画像が転置されていることが前提のため、true
をご指定ください。 -
is_bgr
true
ならばネットワークの入力画像をRGB ではなく、BGR に変換します。
-
void handle_keyboard_input(int &exit_code, bool &pause);
stdin
へのキーボード入力を取得して、exit_code
やpause
を設定します。
ESC キーが押されたときはexit_code
が0 に設定されます。
SPACE キーが押されたときはpause
が!pause
に設定されます。
このAPI はアプリケーション・リポジトリのcommon/include/demo_common.h
にて宣言されています。