对接语音识别api

This commit is contained in:
yueliuli 2026-05-06 15:00:30 +08:00
parent 5755e15d9d
commit 48826d52ef
3 changed files with 105 additions and 16 deletions

View File

@ -20,3 +20,47 @@ export async function runApi(funcName: string, params: Record<string, any>) {
return 'error' return 'error'
} }
} }
export async function runApiAudio(funcName: string, method: string = 'GET', params: Record<string, any>) {
const baseUrl = 'http://127.0.0.1:5000'
try {
if (method === 'GET') {
const url = new URL(`${baseUrl}/api/${funcName}`)
Object.entries(params).forEach(([key, value]) => {
url.searchParams.append(key, String(value))
})
const response = await fetch(url.toString(), {
method: 'GET',
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const result = await response.json()
return result.data
}
else {
const response = await fetch(`${baseUrl}/api/${funcName}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(params),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const result = await response.json()
return result.data
}
}
catch (error) {
console.error('接口调用失败:', error)
return 'error'
}
}

View File

@ -3,7 +3,7 @@ import type { FormInstance, FormRules } from 'element-plus'
import { onMounted, reactive, ref } from 'vue' import { onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import { runApi } from '~/composables/api' import { runApi, runApiAudio } from '~/composables/api'
const router = useRouter() const router = useRouter()
@ -73,6 +73,11 @@ async function runCheck(formEl: FormInstance | undefined) {
if (valid) { if (valid) {
isRunningCheck.value = true isRunningCheck.value = true
startTimer() startTimer()
if (ruleForm.function.includes('runAudioRecognition')) {
await runApiAudio('recognize', 'POST', {
path: vidPaths.value[vidPaths.value.indexOf(ruleForm.vidPath)],
})
}
await runApi('/run', { await runApi('/run', {
vid_file: vidPaths.value[vidPaths.value.indexOf(ruleForm.vidPath)], vid_file: vidPaths.value[vidPaths.value.indexOf(ruleForm.vidPath)],
run_sam3: ruleForm.function.includes('runSam3'), run_sam3: ruleForm.function.includes('runSam3'),

View File

@ -2,7 +2,20 @@
import { computed, onMounted, ref } from 'vue' import { computed, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import { runApi } from '~/composables/api' import { runApi, runApiAudio } from '~/composables/api'
interface Sentence {
begin_time: number
duration: number
end_time: number
speaker: string
text: string
}
interface AudioResult {
sentences: Sentence[]
total_sentences: number
}
const 隐患等级字典: Record<number, string> = { const 隐患等级字典: Record<number, string> = {
0: '一般隐患', 0: '一般隐患',
@ -187,20 +200,6 @@ function getData() {
整改建议: obj.recommend || '', 整改建议: obj.recommend || '',
} }
}) })
//
data.value.对话列表 = [
[0, '00:00', '客户', '当前对话仅为测试数据'],
[3, '00:03', '客户', '你好'],
[5.2, '00:05', '专家', '你好,有什么我可以帮助你的吗?'],
[8, '00:08', '客户', '我想知道我的隐患等级'],
[10.5, '00:10', '专家', '你的隐患等级是一般隐患'],
[12.3, '00:12', '客户', '我需要一个整改建议'],
[20.1, '00:20', '专家', '好的,我会尽快给你一个整改建议'],
[16.5, '00:16', '客户', '我需要一个整改建议'],
[18.2, '00:28', '专家', '好的,我会尽快给你一个整改建议'],
[18.2, '00:30', '专家', '好的,我会尽快给你一个整改建议'],
]
} }
// //
@ -235,6 +234,40 @@ function handleJumpToTimePoint(seconds: number) {
videoEl.pause() videoEl.pause()
} }
function getAudioRecData(res: any) {
if (!res) {
console.error('音频识别结果为空')
return
}
let sentences = res.sentences
if (!sentences && res.data) {
sentences = res.data.sentences
}
if (!sentences) {
console.error('未找到 sentences 字段,完整响应:', res)
return
}
const conversationList: [number, string, string, string][] = []
sentences.forEach((sentence: Sentence, index: number) => {
const mins = Math.floor(sentence.begin_time / 60)
const secs = Math.floor(sentence.begin_time % 60)
const timeStr = `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
conversationList.push([
index,
timeStr,
sentence.speaker,
sentence.text,
])
})
data.value.对话列表 = conversationList
}
// //
// label: // label:
// key: // key:
@ -314,6 +347,13 @@ onMounted(() => {
handleJumpToHazard(0) handleJumpToHazard(0)
} }
}) })
runApiAudio('result', 'GET', {
// path: vidFile,
path: 'VID_20251104_085655_024.AVI',
}).then((res) => {
getAudioRecData(res as AudioResult)
})
} }
}) })
</script> </script>