PySide2で音声を録音・文字起こしをする【PySide2,Python】

PySide2でリアルタイムで文字起こし,音声の録音をやってみたいと思います

実行結果

実行すると録音の音声ファイル recorded.wavとその録音音声内容のrecordedText.txtが保存されます f:id:ayousanz:20200904104754p:plain

音声の認識が良くないのか,自分の発音が良くないのか...

正確には録音できない時があります f:id:ayousanz:20200904104908p:plain

準備

今回必要なものをconda でinstallします

音声を扱うためのライブラリ

conda install -c anaconda pyaudio

録音をするためにライブラリ

conda install -c conda-forge speechrecognition

Code

import speech_recognition as sr


def main():
    r = sr.Recognizer()

    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)

        print("Please say something")

        audio = r.listen(source)

        print("Recognizing Now .... ")

        # recognize speech using google

        try:
            print("You have said \n" + r.recognize_google(audio))
            print("Audio Recorded Successfully \n ")


        except Exception as e:
            print("Error :  " + str(e))

        # write audio and text

        with open("recorded.wav", "wb") as f:
            f.write(audio.get_wav_data())

        with open("recordedText.txt", "w") as f:
            s = str(r.recognize_google(audio))
            f.write(s)

if __name__ == "__main__":
    main()

参考サイト

codeloop.org

Siv3Dでドロップした画像をグレイスケール化する【C++,Visual Studio】

前回の続きです

前回の記事

ayousanz.hatenadiary.jp

実行結果

f:id:ayousanz:20200903145323p:plain

操作?画面

f:id:ayousanz:20200903145348g:plain

グレイスケールにする

siv3d.github.io

画像をドロップして表示する

siv3d.github.io

Code

# include <Siv3D.hpp>
using namespace std;

void Main()
{
    Texture textureInput;
    Texture textureOutput;

    double scale = 1.0;

    Array<Rect> detectedFaces;
    const PixelShader ps(U"example/shader/2d/grayscale" SIV3D_SELECT_SHADER(U".hlsl", U".frag"),
        { { U"PSConstants2D", 0 } });

    if (!ps)
    {
        throw Error(U"Failed to load a shader file");
    }

    while (System::Update())
    {
         //ファイルがドロップされた
        if (DragDrop::HasNewFilePaths())
        {
            // ファイルを画像として読み込めた
            if (const Image image{ DragDrop::GetDroppedFilePaths().front().path })
            {
                // 画像の拡大縮小率
                scale = static_cast<double>(textureInput.width()) / image.width()/2;

                // 画面のサイズに合うように画像を拡大縮小
                textureInput = textureInput = Texture(image.fitted(Scene::Size()));
                textureOutput = textureOutput = Texture(image.fitted(Scene::Size()));
                
            }
        }
        if (KeyA.down()) {
            Print << U"拡大率:" << scale;
        }

        if (textureInput)
        {
            //textureOutput.draw(100,100);
            Rect(0, 0, 400, 400)(textureInput).draw();
            ScopedCustomShader2D shader(ps);
            //textureOutput.draw(400,400);
            Rect(400, 0, 400, 400)(textureOutput).draw();
        }
    }
}

Siv3Dを試してみた【C++,Visual Studio】

twitterで以下のようなものが流れていたので自分でもやってみました (サンプルコードを動かしただけなので,特に何もしてません) (随時追加予定)

インストール等

siv3d.github.io

こちらを参考にすればうまくいくと思います

実行画面

f:id:ayousanz:20200903123127p:plain

ボタンを押したときにマウスのある所にアイコンを移動させる

実際の動き

f:id:ayousanz:20200903124854g:plain

Code

// 猫のテクスチャを用意
    const Texture cat(Emoji(U"🐈"));
    const Texture temp(Emoji(U"🐶"));

    // 猫の座標
    Vec2 catPos(640, 450);
    Vec2 dogPos(353, 242);

    while (System::Update())
    {
        // テキストを画面の中心に描く
        font(U"Hello, Siv3D!🐣").drawAt(Scene::Center(), Palette::Black);

        // 大きさをアニメーションさせて猫を表示する
        cat.resized(100 + Periodic::Sine0_1(1s) * 20).drawAt(catPos);
        temp.resized(100 + Periodic::Sine0_1(1s) * 20).drawAt(dogPos);

        // マウスカーソルに追従する半透明の赤い円を描く
        Circle(Cursor::Pos(), 40).draw(ColorF(1, 0, 0, 0.5));

        // [A] キーが押されたら
        if (KeyA.down())
        {
            // Hello とデバッグ表示する
            Print << U"Hello!";
        }

        // ボタンが押されたら
        if (SimpleGUI::Button(U"Move the cat", Vec2(600, 20)))
        {
            // 猫の座標を画面内のランダムな位置に移動する
            catPos = RandomVec2(Scene::Rect());
        }
        if (KeyB.down()) {
            cout << "hello" << endl;
            
            dogPos = Cursor::Pos();
        }
    }

Sive3Dからツイートする

ツイート画面

Code

# include <Siv3D.hpp>

void Main()
{
    const String text = U"Hello, Siv3D! #Siv3D";

    while (System::Update())
    {
        if (SimpleGUI::Button(U"Tweet", Vec2(20, 20)))
        {
            // text をつぶやくツイート投稿画面を開く
            Twitter::OpenTweetWindow(text);
        }
    }
}

引用元 siv3d.github.io

アニメーションを任意のタイミングで開始する【Unity】

今回は敵からプレイヤーに向かって火の玉を発射するものを作成してみました 火の玉がせいせいされたあとにプレイヤーに当たってからアニメーションを開始したかったのでメモついでに記録します

成果

f:id:ka1357amnbpdr:20200902184042g:plain

ゲーム公開

unityroom.com

実装方法

  1. 生成した後にAnimatorからアニメーションの開始をoffにします
gameobject.GetComponent<Animator>().enabled = false;

2.  任意のタイミングでアニメーションを開始します ここでは,プレイヤーに当たったときにアニメーションを開始しています

_animator.enabled = true;
_animator.Play("Base Layer.explosion",0,0);

Scripts

生成するscript(敵にアタッチ)

private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            Debug.Log("プレイヤーが範囲に入った");
            isInPlayer = true;
            // var t = Instantiate(explosionPrefab, explosionPrefab.transform.position,explosionPrefab.transform.rotation);
            Observable.Interval(TimeSpan.FromSeconds(1f)).Where(_ => isInPlayer).Subscribe(_ =>
            {
                var t = Instantiate(explosionPrefab, parent.transform.position,explosionPrefab.transform.rotation);
                // t.GetComponent<Animator>().enabled = false;
                t.GetComponent<Rigidbody2D>().velocity = other.gameObject.transform.position;
            }).AddTo(this);
        }
    }

火の玉にアタッチ

private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            Debug.Log("アニメーションスタート");
            _animator.enabled = true;
            _animator.Play("Base Layer.explosion",0,0);
        }
    }

参考サイト

docs.unity3d.com

qiita.com

Riderのtermial をPowerShell Coreに変更する【Rider,Powshell】

Riderを使っているとterminalでlsが使えないのがちょっと嫌になってくるので,terminalをへんこうしました 

PowerShell Coreのインストール

rh-kimata.hatenablog.com

Riderのインストール

pleiades.io

Riderのターミナルの変更方法

pleiades.io

設定画面から 以下のようにします Power shell 6の実行ファイル名は pwsh.exe だそうです f:id:ka1357amnbpdr:20200828141612p:plain

UnityでVRを実行しても動かなくなったときに見る項目【Oculus Rift S,Quest】

なぜか急にVRの開発をやっていてUnity editor上で実行しても映らなくなってしまってびっくりしたのでメモとして残しておきます ほぼ自分のメモ用になります

原因

原因としてかんがえられるのは,Unity 2018 -> 2019にupgradeしたことによるXR plug-inの扱いが変わったからだと思っています

解決方法

framesynthesis.jp

こちらのサイトを参考に以下のことを試してみる

Windows > Package を開いて、XR Plugin Management パッケージをインストールする

  1. Edit > Project Settings > XR Plug-in Management を開いて「Oculus」にチェックを入れる(これによって Oculus XR Plugin パッケージがインストールされる)
  2. そのまま XR Plug-in Management のタブを PC, Mac and Linux Standalone settings に切り替えて、そちらでも「Oculus」にチェックを入れる(これでエディタで VR モードで再生できるようになります)
  3. 再度 Window > Package を開き、Oculus XR Plugin パッケージの項目を左の矢印で開いて 1.4.0 に更新する(Oculus Integration 18.0 以降で必要になります。Unity 2019.4.7f1 以降ではデフォルトで 1.4.0 がインストールされます)