Skip to content

Commit

Permalink
首次上传
Browse files Browse the repository at this point in the history
  • Loading branch information
waityousea committed Apr 21, 2023
1 parent 9414423 commit c14a864
Show file tree
Hide file tree
Showing 144 changed files with 52,653 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 hawkey

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
43 changes: 43 additions & 0 deletions WebSocket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## 消息格式

通讯地址:`ws://127.0.0.1:8800/th`

### 发送音频

```
# 发送给api的message数据是str类型
{
"Topic": "Unreal",
"Data": {
"Key": "audio",
"Value": "C:\samples\sample-1.mp3",
"Time": 10,
"Type": "interact"
}
}
```

| 参数 | 描述 | 类型 | 是否必须 |
| ---------- | ---------------- | ----- | -------- |
| Data.Value | 音频文件绝对路径 | str ||
| Data.Time | 音频时长 (秒) | float ||
| Data.Type | 发言类型 | str ||

接口测试场景:Postman

![image-20230420105751124](D:\coding\NeRF\xuniren\ky\img\image-20230420105751124.png)

### 返回视频

```json
# 返回数据的格式,Json
{
'video': 'data:video/mp4;base64,xxx'
}
```

| 参数 | 描述 | 类型 | 是否必须 |
| ----- | ---------------------------------------------------------- | ---- | -------- |
| video | base64编码的视频流,前段接收时需要采用base64对视频进行解码 | str ||
| | | | |

18 changes: 18 additions & 0 deletions activation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import torch
from torch.autograd import Function
from torch.cuda.amp import custom_bwd, custom_fwd

class _trunc_exp(Function):
@staticmethod
@custom_fwd(cast_inputs=torch.float32) # cast to float32
def forward(ctx, x):
ctx.save_for_backward(x)
return torch.exp(x)

@staticmethod
@custom_bwd
def backward(ctx, g):
x = ctx.saved_tensors[0]
return g * torch.exp(x.clamp(-15, 15))

trunc_exp = _trunc_exp.apply
95 changes: 95 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# server.py
from flask import Flask, request, jsonify
from flask_sockets import Sockets
import base64
import time
import json
import gevent
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
from tools import audio_pre_process, video_pre_process, generate_video,audio_process
import os
import re
import numpy as np

import shutil
app = Flask(__name__)
sockets = Sockets(app)
video_list = []





def send_information(path, ws):

print('传输信息开始!')
#path = video_list[0]
''''''
with open(path, 'rb') as f:
video_data = base64.b64encode(f.read()).decode()

data = {
'video': 'data:video/mp4;base64,%s' % video_data,
}
json_data = json.dumps(data)

ws.send(json_data)



@sockets.route('/th')
def echo_socket(ws):
# 获取WebSocket对象
#ws = request.environ.get('wsgi.websocket')
# 如果没有获取到,返回错误信息
if not ws:
print('未建立连接!')
return 'Please use WebSocket'
# 否则,循环接收和发送消息
else:
print('建立连接!')
while True:
message = ws.receive()




if len(message)==0:

return '输入信息为空'
else:

message=message.replace("\\","/")
print('message:', message,type(message))
message = eval(message)
aud_dir = message["Data"]["Value"]
basedir = ""
for i in aud_dir.split("/"):
basedir = os.path.join(basedir,i)
basedir = basedir.replace(":",":\\")
num = 1
new_path = r'./data/audio/aud_%d.wav'%num #新路径
old_path = basedir
shutil.copy(old_path, new_path)
audio_path = 'data/audio/aud_%d.wav' % num
audio_process(audio_path)
audio_path_eo = 'data/audio/aud_%d_eo.npy' % num
video_path = 'data/video/results/ngp_%d.mp4' % num
output_path = 'data/video/results/output_%d.mp4' % num
generate_video(audio_path, audio_path_eo, video_path, output_path)
video_list.append(output_path)
send_information(output_path, ws)




if __name__ == '__main__':

audio_pre_process()
video_pre_process()

server = pywsgi.WSGIServer(('127.0.0.1', 8800), app, handler_class=WebSocketHandler)
server.serve_forever()


Binary file added data/audio/aud_1.wav
Binary file not shown.
Binary file added data/audio/aud_1_eo.npy
Binary file not shown.
Loading

0 comments on commit c14a864

Please sign in to comment.