Unity 口型驱动方式总结 之 与 Flask 服务联调

项目背景:unity嘴型驱动目前都是使用Oculus ,但是其对于中文的支持不是很友好。

        方案参考网站需要的BS节点优点缺点
Oculus oculuslipsync-unity官网官网15实际18

免费、使用者多

中文效果不好
Oculus + Vosk参考网站
 
a i u e o免费、个人开发者共享、
中文效果可以
(参考网站有视频效果)
需额外搭建音频转换服务
GitHub地址
训练好的模型
uLipSyncGitHub地址a i u e o免费、可校准实际使用效果没有
Oculus + Vosk好
效果视频
CRI LipSync官网a i u e o中文效果好收费贵、买断费用约11w
Unity3D Salsa LipSyncB站视频a i u e o\中文效果不好、收费49.55$
插件地址
微软AZure代码书写参考网站55个节点网站微表情、ARKit通用收费、且文档未维护到
Unity端效果并不理想
获取Viseme后如何操作
如何返回55节点(注意事项)
使用定制声音
定制声音精简版与pro区别
定制声音(工作台)
使用定制声音
ARkitAR kit face BS52个节点面捕\

参考了很多方案后觉得Unity + Flask 这套方案比较靠谱。

准备:

  1. 下载Vosk示例
  2. 找到Python示例
  3. 安装python 环境,我使用的是3.12.2
  4. 使用VScode打开D:\Vosk\python\example
  5. 安装Flask环境:pip install Flask
  6. 编写python 服务脚本,并监听Unity链接,解析数据
    from flask import Flask,request
    import io
    from vosk import Model, KaldiRecognizer
    import logging
    
    # 创建 Flask 应用程序实例
    app = Flask(__name__)
    
    # 定义路由和视图函数
    @app.route('/')
    def hello():
        return 'Hello, World!'
    
    @app.route('/api/unity_post_endpoint', methods=['POST'])
    def receive_audio_bytes():
        try:
            if 'audioData' in request.files:
                audio_file = request.files['audioData']
                audio_bytes = audio_file.read()
                json = process_data('flakOutJson',audio_bytes)
                # 保存音频字节数组到文件或其他处理
                with open('received_audio.wav', 'wb') as f:
                    f.write(audio_bytes)
            print(request.files)
            return json, 200
        except Exception as e :print(f"类型错误:{e}")
    
        
    def process_data(text_input: str, byte_input: bytes):
        model = Model("model")
        rec = KaldiRecognizer(model, 24000)
        rec.SetWords(True)
        rec.SetPartialWords(True)
        rec.AcceptWaveform(byte_input)
        output = rec.FinalResult()
        outputfilename = text_input+'.json'
        print(output)
        with open(outputfilename,mode = 'w',encoding = 'utf-8') as file_obj:
            file_obj.write(output)
        return output
    
    
    # 如果直接运行该文件,则启动 Flask 服务器
    if __name__ == '__main__':
        app.run(debug=True, port=6002)

  7. Unity端代码
    public IEnumerator SendAudioDataToServer(byte[] audioData, Action<bool, string, AudioClip> GetJsonDone)
    {
        string apiUrl = "http://localhost:6002/api/unity_post_endpoint";
    
        var form = new WWWForm();
        form.AddBinaryData("audioData", audioData, "audioFile.wav", "audio/wav");
    
        using (var request = UnityWebRequest.Post(apiUrl, form))
        {
            yield return request.SendWebRequest();
    
            if (request.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError($"Upload failed: {request.error}");
                GetJsonDone?.Invoke(false,request.error,null);
            }
            else
            {
                GetJsonDone?.Invoke(true, request.downloadHandler.text, WavUtility.ToAudioClip(audioData));
                
                string audioOutPutPath = Application.persistentDataPath + "/AzureSynthesizedAudio/outPut.wav";
                SaveAsWav(audioData,audioOutPutPath);
                Debug.Log($"保存完成  路径为 : {audioOutPutPath}");
            }
        }
    }

  8. python方法process_data是自己写的,就是把原来demo里通过bat文件执行的逻辑改成收到UnityWebRequest消息后执行,rec = KaldiRecognizer(model, 24000)这里的24000是音频采样率,从Unity端可以得到后从wwwform传过来也行,这里直接定义好了24000,所以使用的是定值
  9. 错误解决
    1. unity端报错InvalidOperationException: Insecure connection not allowed
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大只弱鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值