1.2.6_Path的使用

move & line

mPath.moveTo(100, 70); //移动
mPath.lineTo(140, 800);
//等同于上一行代码效果,相当于 (140-100,800-70)
//mPath.rLineTo(40, 730);
mPath.lineTo(250, 600);
mPath.close(); //是否曲线是否闭合  

在这里插入图片描述

添加子图形addXXX

//添加弧形
mPath.addArc(200, 200, 400, 400, -225, 225);
//CW表示顺时针绘制,CCW表示逆时针绘制
mPath.addRect(500, 500, 900, 900, Path.Direction.CW);
//添加一个圆
mPath.addCircle(700, 700, 200, Path.Direction.CW);
//添加一个椭圆
mPath.addOval(100,100, 600, 400, Path.Direction.CCW);

在这里插入图片描述

追加图形

//xxxTo 画线
//mPath.addArc(200, 200, 400, 400, -225, 225);
//mPath.arcTo(400, 200, 600, 400, -180, 225, false);  

在这里插入图片描述

forceMoveTo

forceMoveTo,true,绘制时移动起点,false,绘制时连接最后一个点与圆弧起点

forceMoveTo为false的情况

mPath.moveTo(0, 0);
mPath.lineTo(100, 100);
//forceMoveTo,true,绘制时移动起点,false,绘制时连接最后一个点与圆弧起点
mPath.arcTo(400, 200, 600, 400, 0, 270, false);  

在这里插入图片描述

forceMoveTo为true的情况

mPath.moveTo(0, 0);
mPath.lineTo(100, 100);
//forceMoveTo,true,绘制时移动起点,false,绘制时连接最后一个点与圆弧起点
mPath.arcTo(400, 200, 600, 400, 0, 270, true);

在这里插入图片描述

添加一个路径

mPath.moveTo(100, 70);
mPath.lineTo(140, 180);
mPath.lineTo(250, 330);
mPath.lineTo(400, 630);
mPath.lineTo(100, 830);

Path newPath = new Path();
newPath.moveTo(100, 1000);
newPath.lineTo(600, 1300);
newPath.lineTo(400, 1700);
mPath.addPath(newPath);  

在这里插入图片描述

添加圆角矩形

//添加圆角矩形, CW顺时针,CCW逆时针
RectF rectF5 = new RectF(200, 800, 700, 1200);
mPath.addRoundRect(rectF5, 20, 20, Path.Direction.CCW);  

在这里插入图片描述

画二阶贝塞尔曲线

   mPath.moveTo(300, 500);
   mPath.quadTo(500, 100, 800, 500);
   //参数表示相对位置,等同于上面一行代码
   //mPath.rQuadTo(200, -400, 500, 0);

在这里插入图片描述

画三阶贝塞尔曲线

mPath.moveTo(300, 500);
mPath.cubicTo(500, 100, 600, 1200, 800, 500);
//参数表示相对位置,等同于上面一行代码
//mPath.rCubicTo(200, -400, 300, 700, 500, 0);  

在这里插入图片描述

以上都需要添加一行代码

以上都需要添加一行代码,进行Path的绘制

canvas.drawPath(mPath, mPaint);
执行文件: import os import torch from funasr import AutoModel from pydub import AudioSegment def convert_to_wav(input_path, output_dir="temp_wav"): """将任意音频格式转换为16kHz单声道WAV格式""" os.makedirs(output_dir, exist_ok=True) filename = os.path.basename(input_path) wav_path = os.path.join(output_dir, f"{os.path.splitext(filename)[0]}.wav") try: audio = AudioSegment.from_file(input_path) audio = audio.set_frame_rate(16000).set_channels(1) audio.export(wav_path, format="wav") return wav_path except Exception as e: raise RuntimeError(f"音频转换失败: {str(e)}") def check_model_files(model_path): """检查模型目录是否包含必要文件""" required_files = ["config.yaml", "model.pt", "tokens.json"] missing_files = [] for file in required_files: file_path = os.path.join(model_path, file) if not os.path.exists(file_path): missing_files.append(file) if missing_files: raise FileNotFoundError(f"模型文件缺失: {', '.join(missing_files)}") else: print("模型文件完整") def asr_with_diarization(model, audio_path): """执行带说话人分离的语音识别""" try: # 进行推理 res = model.generate( input=audio_path, batch_size_s=300, # 长音频批处理大小 param_dict={"cache": dict()}, # 启用缓存加速 hotword='' # 可选的触发词 ) # 解析结果 results = [] for item in res[0]["value"]: speaker = item.get("spk", "SPK0") text = item["text"] results.append(f"{speaker}: {text}") return "\n".join(results) except Exception as e: raise RuntimeError(f"语音识别失败: {str(e)}") def main(audio_path): if not audio_path.lower().endswith('.wav'): print("检测到非WAV格式,正在进行转换...") audio_path = convert_to_wav(audio_path) print(f"已转换为WAV: {audio_path}") print("正在加载模型...") # 主模型路径 model_dir = "D:/models/ASR-models/iic/speech_paraformer-large-vad-punc-spk_asr_nat-zh-cn" # 验证模型文件 print("验证模型文件完整性...") check_model_files(model_dir) # 加载主模型(已集成所有功能) model = AutoModel( model=model_dir, model_revision="v2.0.4", # 指定模型版本 device="cuda" if torch.cuda.is_available() else "cpu", disable_update=True # 禁止检查更新 ) print("模型加载成功!") print("开始语音识别...") result = asr_with_diarization(model, audio_path) print("\n识别结果:") print(result) return result if __name__ == "__main__": input_audio = "D:/python/语音情感分析/实际录音/测试/通话录音@10086(10086)_20250620100304.mp3" main(input_audio) 出现错误: 检测到非WAV格式,正在进行转换... 已转换为WAV: temp_wav\通话录音@10086(10086)_20250620100304.wav 正在加载模型... 验证模型文件完整性... 模型文件完整 funasr version: 1.2.6. 模型加载成功! 开始语音识别... rtf_avg: 0.122: 100%|████████████████████████████████████████████████████████████████████| 1/1 [00:05<00:00, 5.36s/it] --------------------------------------------------------------------------- KeyError Traceback (most recent call last) Input In [6], in asr_with_diarization(model, audio_path) 47 results = [] ---> 48 for item in res[0]["value"]: 49 speaker = item.get("spk", "SPK0") KeyError: 'value' During handling of the above exception, another exception occurred: RuntimeError Traceback (most recent call last) Input In [6], in <module> 89 if __name__ == "__main__": 90 input_audio = "D:/python/语音情感分析/实际录音/测试/通话录音@10086(10086)_20250620100304.mp3" ---> 91 main(input_audio) Input In [6], in main(audio_path) 80 print("模型加载成功!") 82 print("开始语音识别...") ---> 83 result = asr_with_diarization(model, audio_path) 85 print("\n识别结果:") 86 print(result) Input In [6], in asr_with_diarization(model, audio_path) 53 return "\n".join(results) 54 except Exception as e: ---> 55 raise RuntimeError(f"语音识别失败: {str(e)}") RuntimeError: 语音识别失败: 'value'
最新发布
08-06
运行代码: import os import torch from funasr import AutoModel from pydub import AudioSegment def convert_to_wav(input_path, output_dir="temp_wav"): """将任意音频格式转换为16kHz单声道WAV格式""" os.makedirs(output_dir, exist_ok=True) filename = os.path.basename(input_path) wav_path = os.path.join(output_dir, f"{os.path.splitext(filename)[0]}.wav") try: audio = AudioSegment.from_file(input_path) audio = audio.set_frame_rate(16000).set_channels(1) audio.export(wav_path, format="wav") return wav_path except Exception as e: raise RuntimeError(f"音频转换失败: {str(e)}") def asr_with_diarization(model, audio_path): """执行带说话人分离的语音识别""" try: # 进行推理 res = model.generate( input=audio_path, batch_size_s=300, # 长音频批处理大小 param_dict={"cache": dict()}, # 启用缓存加速 hotword='' # 可选的触发词 ) # 解析结果 results = [] for item in res[0]["value"]: speaker = item.get("spk", "SPK0") text = item["text"] results.append(f"{speaker}: {text}") return "\n".join(results) except Exception as e: raise RuntimeError(f"语音识别失败: {str(e)}") def main(audio_path): """主处理函数""" # 检查并转换音频格式 if not audio_path.lower().endswith('.wav'): print("检测到非WAV格式,正在进行转换...") audio_path = convert_to_wav(audio_path) print(f"已转换为WAV: {audio_path}") # 设置模型路径 model_dir = "D:/models/ASR-models/iic/speech_paraformer-large-vad-punc-spk_asr_nat-zh-cn" # 离线加载模型(使用本地模型路径) print("正在加载模型...") model = AutoModel( model=model_dir, model_revision="v2.0.4", vad_model="D:/models/ASR-models/iic/speech_fsmn_vad_zh-cn-16k-common-pytorch", punc_model="D:/models/ASR-models/iic/punc_ct-transformer_zh-cn-common-vocab272727-pytorch", spk_model="D:/models/ASR-models/iic/speech_campplus_sv_zh-cn_16k-common", device="cuda" if torch.cuda.is_available() else "cpu" ) # 执行语音识别 print("开始语音识别...") result = asr_with_diarization(model, audio_path) # 输出结果 print("\n识别结果:") print(result) return result if __name__ == "__main__": # 使用示例 input_audio = "D:/python/语音情感分析/实际录音/测试/通话录音@10086(10086)_20250620100304.mp3" main(input_audio) 输出提示: 检测到非WAV格式,正在进行转换... 已转换为WAV: temp_wav\通话录音@10086(10086)_20250620100304.wav 正在加载模型... funasr version: 1.2.6. Check update of funasr, and it would cost few times. You may disable it by set `disable_update=True` in AutoModel Download: D:/models/ASR-models/iic/speech_fsmn_vad_zh-cn-16k-common-pytorch failed!: Invalid repo_id: model, must be of format namespace/name --------------------------------------------------------------------------- AssertionError Traceback (most recent call last) Input In [1], in <module> 72 if __name__ == "__main__": 73 # 使用示例 74 input_audio = "D:/python/语音情感分析/实际录音/测试/通话录音@10086(10086)_20250620100304.mp3" ---> 75 main(input_audio) Input In [1], in main(audio_path) 52 # 离线加载模型(使用本地模型路径) 53 print("正在加载模型...") ---> 54 model = AutoModel( 55 model=model_dir, 56 model_revision="v2.0.4", 57 vad_model="D:/models/ASR-models/iic/speech_fsmn_vad_zh-cn-16k-common-pytorch", 58 punc_model="D:/models/ASR-models/iic/punc_ct-transformer_zh-cn-common-vocab272727-pytorch", 59 spk_model="D:/models/ASR-models/iic/speech_campplus_sv_zh-cn_16k-common", 60 device="cuda" if torch.cuda.is_available() else "cpu" 61 ) 63 # 执行语音识别 64 print("开始语音识别...") File d:\python\lib\site-packages\funasr\auto\auto_model.py:135, in AutoModel.__init__(self, **kwargs) 133 vad_kwargs["model_revision"] = kwargs.get("vad_model_revision", "master") 134 vad_kwargs["device"] = kwargs["device"] --> 135 vad_model, vad_kwargs = self.build_model(**vad_kwargs) 137 # if punc_model is not None, build punc model else None 138 punc_model = kwargs.get("punc_model", None) File d:\python\lib\site-packages\funasr\auto\auto_model.py:261, in AutoModel.build_model(**kwargs) 259 # build model 260 model_class = tables.model_classes.get(kwargs["model"]) --> 261 assert model_class is not None, f'{kwargs["model"]} is not registered' 262 model_conf = {} 263 deep_update(model_conf, kwargs.get("model_conf", {})) AssertionError: D:/models/ASR-models/iic/speech_fsmn_vad_zh-cn-16k-common-pytorch is not registered
08-06
运行以下代码时: import os import torch from funasr import AutoModel from pydub import AudioSegment 音频格式转换函数 def convert_to_wav(input_path, output_dir=“temp_wav”): “”“将任意音频格式转换为16kHz单声道WAV格式”“” os.makedirs(output_dir, exist_ok=True) filename = os.path.basename(input_path) wav_path = os.path.join(output_dir, f"{os.path.splitext(filename)[0]}.wav") try: audio = AudioSegment.from_file(input_path) audio = audio.set_frame_rate(16000).set_channels(1) audio.export(wav_path, format=“wav”) return wav_path except Exception as e: raise RuntimeError(f"音频转换失败: {str(e)}") 2. 语音识别与说话人分离 def asr_with_diarization(model, audio_path): “”“执行带说话人分离的语音识别”“” try: 进行推理 res = model.generate( input=audio_path, batch_size_s=300, # 长音频批处理大小 param_dict={“cache”: dict()}, # 启用缓存加速 hotword=‘’ # 可选的触发词 ) 解析结果 results = [] for item in res[0][“value”]: speaker = item.get(“spk”, “SPK0”) text = item[“text”] results.append(f"{speaker}: {text}“) return “\n”.join(results) except Exception as e: raise RuntimeError(f"语音识别失败: {str(e)}”) 主函数 def main(audio_path): 检查并转换音频格式 if not audio_path.lower().endswith(‘.wav’): print(“检测到非WAV格式,正在进行转换…”) audio_path = convert_to_wav(audio_path) print(f"已转换为WAV: {audio_path}") 设置模型路径 model_dir = “D:/models/ASR-models/iic/speech_paraformer-large-vad-punc-spk_asr_nat-zh-cn” # 离线加载模型 print(“正在加载模型…”) model = AutoModel( model=model_dir, model_revision=“v1.2.4”, # 使用与模型匹配的版本 vad_model=“fsmn-vad”, # 语音活动检测组件 punc_model=“ct-punc-c”, # 标点恢复组件 spk_model=“cam++”, # 说话人分离组件 device=“cuda” if torch.cuda.is_available() else “cpu” ) # 执行语音识别 print(“开始语音识别…”) result = asr_with_diarization(model, audio_path) # 输出结果 print(“\n识别结果:”) print(result) return result if name == “main”: 使用示例 input_audio = “D:/python/语音情感分析/实际录音/测试/通话录音@10086(10086)_20250620100304.mp3” # 替换为你的音频文件路径 main(input_audio) 输出以下结果,分析问题及如何解决: 检测到非WAV格式,正在进行转换… 已转换为WAV: temp_wav\通话录音@10086(10086)_20250620100304.wav 正在加载模型… funasr version: 1.2.6. Check update of funasr, and it would cost few times. You may disable it by set disable_update=True in AutoModel You are using the latest version of funasr-1.2.6 Downloading Model from https://www.modelscope.cn to directory: C:\Users\Lenovo.cache\modelscope\hub\models\iic\speech_fsmn_vad_zh-cn-16k-common-pytorch Downloading Model from https://www.modelscope.cn to directory: C:\Users\Lenovo.cache\modelscope\hub\models\iic\punc_ct-transformer_zh-cn-common-vocab272727-pytorch Downloading Model from https://www.modelscope.cn to directory: C:\Users\Lenovo.cache\modelscope\hub\models\iic\speech_campplus_sv_zh-cn_16k-common Detect model requirements, begin to install it: C:\Users\Lenovo.cache\modelscope\hub\models\iic\speech_campplus_sv_zh-cn_16k-common\requirements.txt fail to install model requirements! error ERROR: Exception: Traceback (most recent call last): File “D:\python\lib\site-packages\pip_vendor\urllib3\response.py”, line 438, in _error_catcher yield File “D:\python\lib\site-packages\pip_vendor\urllib3\response.py”, line 561, in read data = self._fp_read(amt) if not fp_closed else b"" File “D:\python\lib\site-packages\pip_vendor\urllib3\response.py”, line 527, in _fp_read return self._fp.read(amt) if amt is not None else self._fp.read() File “D:\python\lib\site-packages\pip_vendor\cachecontrol\filewrapper.py”, line 98, in read data: bytes = self.__fp.read(amt) File “D:\python\lib\http\client.py”, line 454, in read n = self.readinto(b) File “D:\python\lib\http\client.py”, line 498, in readinto n = self.fp.readinto(b) File “D:\python\lib\socket.py”, line 669, in readinto return self._sock.recv_into(b) File “D:\python\lib\ssl.py”, line 1241, in recv_into return self.read(nbytes, buffer) File “D:\python\lib\ssl.py”, line 1099, in read return self._sslobj.read(len, buffer) socket.timeout: The read operation timed out During handling of the above exception, another exception occurred: Traceback (most recent call last): File “D:\python\lib\site-packages\pip_internal\cli\base_command.py”, line 106, in _run_wrapper status = _inner_run() File “D:\python\lib\site-packages\pip_internal\cli\base_command.py”, line 97, in _inner_run return self.run(options, args) File “D:\python\lib\site-packages\pip_internal\cli\req_command.py”, line 67, in wrapper return func(self, options, args) File “D:\python\lib\site-packages\pip_internal\commands\install.py”, line 386, in run requirement_set = resolver.resolve( File “D:\python\lib\site-packages\pip_internal\resolution\resolvelib\resolver.py”, line 179, in resolve self.factory.preparer.prepare_linked_requirements_more(reqs) File “D:\python\lib\site-packages\pip_internal\operations\prepare.py”, line 554, in prepare_linked_requirements_more self._complete_partial_requirements( File “D:\python\lib\site-packages\pip_internal\operations\prepare.py”, line 469, in _complete_partial_requirements for link, (filepath, _) in batch_download: File “D:\python\lib\site-packages\pip_internal\network\download.py”, line 184, in call for chunk in chunks: File “D:\python\lib\site-packages\pip_internal\cli\progress_bars.py”, line 55, in _rich_progress_bar for chunk in iterable: File “D:\python\lib\site-packages\pip_internal\network\utils.py”, line 65, in response_chunks for chunk in response.raw.stream( File “D:\python\lib\site-packages\pip_vendor\urllib3\response.py”, line 622, in stream data = self.read(amt=amt, decode_content=decode_content) File “D:\python\lib\site-packages\pip_vendor\urllib3\response.py”, line 587, in read raise IncompleteRead(self._fp_bytes_read, self.length_remaining) File “D:\python\lib\contextlib.py”, line 131, in exit self.gen.throw(type, value, traceback) File “D:\python\lib\site-packages\pip_vendor\urllib3\response.py”, line 443, in _error_catcher raise ReadTimeoutError(self._pool, None, “Read timed out.”) pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host=‘files.pythonhosted.org’, port=443): Read timed out. NameError Traceback (most recent call last) Input In [1], in 75 if name == “main”: 76 # 使用示例 77 input_audio = “D:/python/语音情感分析/实际录音/测试/通话录音@10086(10086)_20250620100304.mp3” # 替换为你的音频文件路径 —> 78 main(input_audio) Input In [1], in main(audio_path) 55 # 离线加载模型 56 print(“正在加载模型…”) —> 57 model = AutoModel( 58 model=model_dir, 59 model_revision=“v1.2.4”, # 使用与模型匹配的版本 60 vad_model=“fsmn-vad”, # 语音活动检测组件 61 punc_model=“ct-punc-c”, # 标点恢复组件 62 spk_model=“cam++”, # 说话人分离组件 63 device=“cuda” if torch.cuda.is_available() else “cpu” 64 ) 66 # 执行语音识别 67 print(“开始语音识别…”) File d:\python\lib\site-packages\funasr\auto\auto_model.py:159, in AutoModel.init(self, **kwargs) 157 spk_kwargs[“device”] = kwargs[“device”] 158 spk_model, spk_kwargs = self.build_model(**spk_kwargs) –> 159 self.cb_model = ClusterBackend(**cb_kwargs).to(kwargs[“device”]) 160 spk_mode = kwargs.get(“spk_mode”, “punc_segment”) 161 if spk_mode not in [“default”, “vad_segment”, “punc_segment”]: NameError: name ‘ClusterBackend’ is not defined
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

氦客

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

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

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

打赏作者

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

抵扣说明:

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

余额充值