Spaces:
Sleeping
Sleeping
File size: 5,223 Bytes
3f5beef 60ec54d 3f5beef 0cb8033 f650aa5 5bb52cc f650aa5 3f5beef 5bb52cc f650aa5 3f5beef 5bb52cc ecac4b3 5bb52cc ecac4b3 5bb52cc 024c0c2 14d197f 5bb52cc dd28a36 5bb52cc f650aa5 3f5beef 5bb52cc dc94506 3f5beef 5bb52cc 3f5beef c76f83a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import os
from pathlib import Path
from tempfile import mkstemp, mkdtemp
import gradio as gr
from moviepy.editor import VideoFileClip, AudioFileClip
import shlex
import datetime
import subprocess
import time
import tempfile
import shutil
import os
import wave
end_time=1000
allowed_medias = [".mp3", ".wav", ".ogg"]
def is_used(file_name):
try:
vHandle = file.CreateFile(file_name, file.GENERIC_READ, 0, None, file.OPEN_EXISTING, file.FILE_ATTRIBUTE_NORMAL, None)
return int(vHandle) == file.INVALID_HANDLE_VALUE
except:
return True
finally:
try: file.CloseHandle(vHandle)
except: pass
def execute_command(cmdstring, cwd=None, timeout=None, shell=False):
"""执行一个SHELL命令
封装了subprocess的Popen方法, 支持超时判断,支持读取stdout和stderr
参数:
cwd: 运行命令时更改路径,如果被设定,子进程会直接先更改当前路径到cwd
timeout: 超时时间,秒,支持小数,精度0.1秒
shell: 是否通过shell运行
Returns: return_code
Raises: Exception: 执行超时
"""
global end_time
if shell:
cmdstring_list = cmdstring
else:
cmdstring_list = shlex.split(cmdstring)
if timeout:
end_time = datetime.datetime.now() + datetime.timedelta(seconds=timeout)
# 没有指定标准输出和错误输出的管道,因此会打印到屏幕上;
sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE, shell=shell, bufsize=4096)
# subprocess.poll()方法:检查子进程是否结束了,如果结束了,设定并返回码,放在subprocess.returncode变量中
while sub.poll() is None:
time.sleep(0.1)
if timeout:
if end_time <= datetime.datetime.now():
raise Exception("Timeout:%s" % cmdstring)
return str(sub.returncode)
def update(file,dclass):
#if dclass == "":
# raise gr.Error("请输入舞蹈类型.")
#if dappear == "":
# raise gr.Error("请输入人物外观.")
file_path = Path(file.name)
info = {}
info["size"] = os.path.getsize(file_path)
info["name"] = file_path.name
print(file_path.name)
file_extension = file_path.suffix
info["type"] = "audio"
audio = AudioFileClip(file.name)
info["duration"] = audio.duration
info["audio_channels"] = audio.nchannels
filename = file_path.name
print('##########################')
print(filename)
if info["size"] > 100000000:
raise gr.Error(
"Please make sure all files are less than 100MB in size."
)
audio.close()
###
###
temp_dir = tempfile.mkdtemp()
video_temp_dir=tempfile.mkdtemp()
shutil.copy(file_path,temp_dir)
for root,dirs,files in os.walk("./songs"):
for file1 in files:
path=os.path.join(root,file1)
shutil.copy(Path(path),temp_dir)
filenewname=filename.replace(".mp3","").replace(".wav","")
newdir=video_temp_dir.replace("/tmp/","")
execute_command("chmod +x ./feature_extraction/audio_feature_extraction_test.sh")
execute_command("chmod +x ./feature_extraction/script_to_list_filenames")
execute_command("chmod +x ./script_generate.sh")
print(execute_command("./feature_extraction/audio_feature_extraction_test.sh "+temp_dir+"/"))
print(execute_command("./script_generate.sh transflower_expmap_old "+filenewname+" "+newdir+" --generate_bvh --generate_video --data_dir="+temp_dir+"/ --seed=seed1 --max_length=400"))
print("./script_generate.sh transflower_expmap_old "+filenewname+" "+newdir+" --generate_bvh --generate_video --data_dir="+temp_dir+"/ --seed=seed1 --max_length=400")
print("###################newfie")
print(filenewname)
videopath=video_temp_dir+"/transflower_expmap_old/videos/"+filenewname+".mp4_music.mp4"
print(videopath)
return videopath
# coding=utf-8
with gr.Blocks() as demo:
gr.Markdown(
"""
# <span style="margin-right: 0.3rem;">🏞</span>智能编舞系统
队名:Three Dancers
成员:蒙贤辉 侯金言 毛忠昊
推荐上传WAV格式文件
""",
elem_id="header",
)
with gr.Row():
with gr.Column():
user_file = gr.File(
file_count="single", label="音频文件", keep_filename=True,
file_types=allowed_medias
)
dclass=gr.Dropdown(["流行", "民族", "混合"], label="舞蹈类型", info="")
#dappear=gr.Dropdown(["舞蹈服", "休闲服", "混合"], label="人物外观", info="")
#number = gr.Slider(minimum=-0, maximum=20, value=1, step=1,
# interactive=True, label="人物数量")
btn = gr.Button("Run", label="Run")
with gr.Column():
generated_video = gr.Video(
interactive=False, label="舞蹈视频", include_audio=True
)
# generated_command = gr.Markdown()
btn.click(
fn=update,
inputs=[user_file, dclass],
outputs=[generated_video]
)
if __name__ == "__main__":
demo.launch()
|