備忘録

備忘録

WindowsでDarknetとYOLOv3を使う方法

Ⅰ. はじめに

タイトルの通り「WindowsでDarknetとYOLOv3を使う方法」です。

Ⅱ. やり方

1. Visual StudioC++の開発環境を整える

f:id:kagasu:20190508193924p:plain

2. Visual Studio 2015 (v140) 用のC++ビルドツールをインストールする


3. 英語をインストールする

vcpkg に「英語」が必要です

4. CUDAをインストールする

最新版をインストールする
https://developer.nvidia.com/cuda-downloads

5. cuDNN をダウンロードする

最新版をインストールする
NVIDIAのアカウントが必要です
https://developer.nvidia.com/rdp/cudnn-download

6. cuDNN をコピーする
copy cudnn\cuda\bin\cudnn64_7.dll "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\cudnn64_7.dll"
copy cudnn\cuda\include\cudnn.h "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include\cudnn.h"
copy cudnn\cuda\lib\x64\cudnn.lib "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cudnn.lib"
7. CMake をインストールする

https://cmake.org/

8. vcpkg をインストールする

https://kagasu.hatenablog.com/entry/2019/02/20/162907

9. Pthreads と OpenCV をインストールする

ビルド完了まで約1時間かかります(i7 8700Kの場合)

vcpkg install pthreads:x64-windows opencv[cuda]:x64-windows
10. Pathに以下ディレクトリを追加する
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin
C:\Program Files\CMake\bin
11. 環境変数を追加する
環境変数 変数値
VCPKG_ROOT C:\vcpkg
VCPKG_DEFAULT_TRIPLET x64-windows
CUDA_TOOLKIT_ROOT_DIR C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1
12. Darknet を clone する
git clone https://github.com/AlexeyAB/darknet
13. ビルドする
powershell build.ps1
14. YOLOv3 モデルをダウンロードする

https://pjreddie.com/media/files/yolov3.weights

15. 実行する
cd build_win_release/Release
darknet.exe detector test ../../cfg/coco.data ../../cfg/yolov3.cfg yolov3.weights -ext_output data/dog.jpg

実行結果

f:id:kagasu:20190508222033p:plain

WPFにマテリアルデザインを適応する方法

Ⅰ. はじめに

タイトルの通り「WPFマテリアルデザインを適応する方法」です。

Ⅱ. やり方

1. 必要なパッケージをインストールする
Install-Package MaterialDesignThemes
Install-Package MaterialDesignThemes.MahApps
2. App.xaml を変更する
<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <!-- MahApps -->
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />

      <!-- Material Design -->
      <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />

      <!-- Material Design: MahApps Compatibility -->
      <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.MahApps;component/Themes/MaterialDesignTheme.MahApps.Fonts.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.MahApps;component/Themes/MaterialDesignTheme.MahApps.Flyout.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <!-- MahApps Brushes -->
    <SolidColorBrush x:Key="HighlightBrush" Color="{DynamicResource Primary700}"/>
    <SolidColorBrush x:Key="AccentBaseColorBrush" Color="{DynamicResource Primary600}" />
    <SolidColorBrush x:Key="AccentColorBrush" Color="{DynamicResource Primary500}"/>
    <SolidColorBrush x:Key="AccentColorBrush2" Color="{DynamicResource Primary400}"/>
    <SolidColorBrush x:Key="AccentColorBrush3" Color="{DynamicResource Primary300}"/>
    <SolidColorBrush x:Key="AccentColorBrush4" Color="{DynamicResource Primary200}"/>
    <SolidColorBrush x:Key="WindowTitleColorBrush" Color="{DynamicResource Primary700}"/>
    <SolidColorBrush x:Key="AccentSelectedColorBrush" Color="{DynamicResource Primary500Foreground}"/>
    <LinearGradientBrush x:Key="ProgressBrush" EndPoint="0.001,0.5" StartPoint="1.002,0.5">
      <GradientStop Color="{DynamicResource Primary700}" Offset="0"/>
      <GradientStop Color="{DynamicResource Primary300}" Offset="1"/>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="CheckmarkFill" Color="{DynamicResource Primary500}"/>
    <SolidColorBrush x:Key="RightArrowFill" Color="{DynamicResource Primary500}"/>
    <SolidColorBrush x:Key="IdealForegroundColorBrush" Color="{DynamicResource Primary500Foreground}"/>
    <SolidColorBrush x:Key="IdealForegroundDisabledBrush" Color="{DynamicResource Primary500}" Opacity="0.4"/>
    <SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchBrush.Win10" Color="{DynamicResource Primary500}" />
    <SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchMouseOverBrush.Win10" Color="{DynamicResource Primary400}" />
    <SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorCheckedBrush.Win10" Color="{DynamicResource Primary500Foreground}" />
  </ResourceDictionary>
</Application.Resources>
3. MainWindow.xaml を変更する
<metro:MetroWindow
  ...
  xmlns:metro="http://metro.mahapps.com/winfx/xaml/controls"
  GlowBrush="{DynamicResource AccentColorBrush}"
  BorderThickness="1"
  Title="MainWindow" Height="200" Width="300">
  <Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/>
  </Grid>
</metro:MetroWindow>
4. MainWindow.xaml.cs を変更する
public partial class MainWindow : MetroWindow
{
  public MainWindow()
  {
    InitializeComponent();
  }
}

実行結果

f:id:kagasu:20190508144634p:plain

.NET Coreで実行ファイル1つだけを出力する方法

2024/01/22 追記

FDDは未知の原因不明なエラーが発生する場合があります。
何か特別な理由が無い限りはSCDを推奨します。

Ⅰ. はじめに

タイトルの通り「.NET Coreで実行ファイル1つだけを出力する方法」です。

FDDはFramework-dependent deploymentsの事です。
SCDはSelf-contained deploymentsの事です。

Ⅱ. 環境

  • .NET Core 3.0以上必須

※.NET 5以上についてはこの記事で触れません

Ⅲ. やり方1(csprojを変更しない場合)

FDDの場合

dotnet publish -c Release -r win-x64 --self-contained false /p:PublishSingleFile=true

SCDの場合

dotnet publish -c Release -r win-x64 --self-contained true /p:PublishSingleFile=true

Ⅳ. やり方2(csprojを変更する場合)

FDDの場合

1. csprojを変更する
<PropertyGroup>
  <PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
2. ビルドする
dotnet publish -c Release -r win-x64 --self-contained false

SCDの場合

1. csprojを変更する
<PropertyGroup>
  <PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
2. ビルドする
dotnet publish -c Release -r win-x64 --self-contained true

その他

Q. ファイルサイズを小さくしたい

A. PublishTrimmed オプションを付けると不要なDLLを除くことが出来ます。
つまり、ファイルサイズが小さくなります。
※SCDでのみ有効です。※2019/07/05時点
※.NET Core 3.0 preview 6以上必須

dotnet publish -r <rid> /p:PublishTrimmed=true /p:PublishSingleFile=true

Q. 起動速度を高速化したい

A. PublishReadyToRun オプションを付けると「起動速度だけ」高速化出来ます。
PublishTrimmedと併用可能です。
※SCDでのみ有効です。※2019/07/05時点
※.NET Core 3.0 preview 6以上必須

dotnet publish -r <rid> /p:PublishTrimmed=true /p:PublishReadyToRun=true /p:PublishSingleFile=true

Node.jsからDLL(C++)を呼び出す方法

Ⅰ. はじめに

タイトルの通り「Node.jsからDLL(C++)を呼び出す方法」です。

Ⅱ. やり方

1. DLLを作成する

Source.cpp

#include <Windows.h>
#include <iostream>

void Hello1() {
  std::cout << "hello world" << std::endl;
}

void Hello2(char str[]) {
  std::cout << "hello " << str << std::endl;
}

void Hello3(wchar_t str[]) {
  std::wcout << "hello " << str << std::endl;
}

int Sum(int a, int b) {
  return a + b;
}

BOOL WINAPI DllMain(HINSTANCE hinstModule, DWORD dwReason, LPVOID lpvReserved) {
  if (dwReason == DLL_PROCESS_ATTACH) {
    DisableThreadLibraryCalls(hinstModule);
  }

  return TRUE;
}
2. ffi をインストールする
// Windowsのみ
// npm install --global --production windows-build-tools
npm install ffi
3.サンプルプログラムを書く

index.js

const ffi = require('ffi');

const lib = ffi.Library('c:\\test.dll', {
  'Hello1': ['void', []],
  'Hello2': ['void', ['string']],
  'Hello3': ['void', [ref.types.CString]],
  'Sum': ['int', ['int', 'int']]
});

lib.Hello1();
lib.Hello2('tanaka');
lib.Hello3(Buffer.from('tanaka\0', 'utf16le'));
let result = lib.Sum(1, 2);
console.log(result);

実行結果

$ node index.js
hello world
hello tanaka
hello tanaka
3

FAQ

Q1. Dynamic Linking Error と表示されます。

Node.js と DLLのアーキテクチャx86, x64等)を合わせる必要があります。

LinuxにNode.jsをインストールする方法

Ⅰ. はじめに

タイトルの通り「LinuxにNode.jsをインストールする方法」です。

Ⅱ. やり方(Ubuntu)

1. 以下のコマンドを実行する
// curl -sL https://deb.nodesource.com/setup_21.x | bash -
curl -sL https://deb.nodesource.com/setup_20.x | bash -
// curl -sL https://deb.nodesource.com/setup_19.x | bash -
// curl -sL https://deb.nodesource.com/setup_18.x | bash -
// curl -sL https://deb.nodesource.com/setup_17.x | bash -
// curl -sL https://deb.nodesource.com/setup_16.x | bash -
// curl -sL https://deb.nodesource.com/setup_15.x | bash -
// curl -sL https://deb.nodesource.com/setup_14.x | bash -
// curl -sL https://deb.nodesource.com/setup_13.x | bash -
// curl -sL https://deb.nodesource.com/setup_12.x | bash -
// curl -sL https://deb.nodesource.com/setup_10.x | bash -
// curl -sL https://deb.nodesource.com/setup_8.x | bash -
2. Node.js をインストールする
apt-get install -y nodejs

Ⅲ. やり方(CentOS)

1. 以下のコマンドを実行する
// curl -sL https://rpm.nodesource.com/setup_21.x | sudo bash -
curl -sL https://rpm.nodesource.com/setup_20.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_19.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_18.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_17.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_15.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_13.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_11.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -
// curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash -
2. Node.js をインストールする
yum install -y nodejs

実行結果

$ node --version
v18.16.0

$ npm --version
9.5.1