From 48826d52ef65c2fec5070d71d535cb620f8bc0fc Mon Sep 17 00:00:00 2001 From: yueliuli <1628111725@qq.com> Date: Wed, 6 May 2026 15:00:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E8=AF=AD=E9=9F=B3=E8=AF=86?= =?UTF-8?q?=E5=88=ABapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/composables/api.ts | 44 ++++++++++++++ src/pages/index.vue | 7 ++- src/pages/nav/HazardCheckResult/index.vue | 70 ++++++++++++++++++----- 3 files changed, 105 insertions(+), 16 deletions(-) diff --git a/src/composables/api.ts b/src/composables/api.ts index 3464878..580eace 100644 --- a/src/composables/api.ts +++ b/src/composables/api.ts @@ -20,3 +20,47 @@ export async function runApi(funcName: string, params: Record) { return 'error' } } + +export async function runApiAudio(funcName: string, method: string = 'GET', params: Record) { + 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' + } +} diff --git a/src/pages/index.vue b/src/pages/index.vue index e81c0e4..8cd0e40 100644 --- a/src/pages/index.vue +++ b/src/pages/index.vue @@ -3,7 +3,7 @@ import type { FormInstance, FormRules } from 'element-plus' import { onMounted, reactive, ref } from 'vue' import { useRouter } from 'vue-router' -import { runApi } from '~/composables/api' +import { runApi, runApiAudio } from '~/composables/api' const router = useRouter() @@ -73,6 +73,11 @@ async function runCheck(formEl: FormInstance | undefined) { if (valid) { isRunningCheck.value = true startTimer() + if (ruleForm.function.includes('runAudioRecognition')) { + await runApiAudio('recognize', 'POST', { + path: vidPaths.value[vidPaths.value.indexOf(ruleForm.vidPath)], + }) + } await runApi('/run', { vid_file: vidPaths.value[vidPaths.value.indexOf(ruleForm.vidPath)], run_sam3: ruleForm.function.includes('runSam3'), diff --git a/src/pages/nav/HazardCheckResult/index.vue b/src/pages/nav/HazardCheckResult/index.vue index 045472c..480d1b9 100644 --- a/src/pages/nav/HazardCheckResult/index.vue +++ b/src/pages/nav/HazardCheckResult/index.vue @@ -2,7 +2,20 @@ import { computed, onMounted, ref } from 'vue' 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 = { 0: '一般隐患', @@ -187,20 +200,6 @@ function getData() { 整改建议: 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() } +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: 显示名称 // key: 数据字段名 @@ -314,6 +347,13 @@ onMounted(() => { handleJumpToHazard(0) } }) + + runApiAudio('result', 'GET', { + // path: vidFile, + path: 'VID_20251104_085655_024.AVI', + }).then((res) => { + getAudioRecData(res as AudioResult) + }) } })