修复 ocr 错误
This commit is contained in:
@@ -34,6 +34,13 @@ export interface QrCodeOcrResult {
|
||||
raw_text: string
|
||||
}
|
||||
|
||||
export interface OCRJobRecord {
|
||||
paddle_job_id: string
|
||||
status: 'submitted' | 'running' | 'done' | 'failed'
|
||||
result: QrCodeOcrResult | null
|
||||
error: string
|
||||
}
|
||||
|
||||
export interface QrCodeListQuery {
|
||||
status?: QrCodeStatus
|
||||
keyword?: string
|
||||
@@ -121,6 +128,28 @@ export async function recognizeQrCodeGroupName(file: File) {
|
||||
return data.data
|
||||
}
|
||||
|
||||
export async function submitOCRJob(file: File) {
|
||||
const form = new FormData()
|
||||
form.append('file', file)
|
||||
const { data } = await apiClient.post<ApiResponse<{ job_id: string }>>(
|
||||
'/admin/chats/qrcodes/ocr-group-name/async',
|
||||
form,
|
||||
{
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
silent: true,
|
||||
}
|
||||
)
|
||||
return data.data
|
||||
}
|
||||
|
||||
export async function getOCRJobResult(jobId: string) {
|
||||
const { data } = await apiClient.get<ApiResponse<OCRJobRecord>>(
|
||||
`/admin/chats/qrcodes/ocr-group-name/async/${jobId}`,
|
||||
{ silent: true }
|
||||
)
|
||||
return data.data
|
||||
}
|
||||
|
||||
export async function updateQrCode(id: number, payload: UpdateQrCodePayload) {
|
||||
const { data } = await apiClient.patch<ApiResponse<ChatQrCode>>(
|
||||
`/admin/chats/qrcodes/${id}`,
|
||||
|
||||
@@ -17,7 +17,8 @@ import {
|
||||
deleteQrCode,
|
||||
fetchQrCodeStats,
|
||||
fetchQrCodes,
|
||||
recognizeQrCodeGroupName,
|
||||
getOCRJobResult,
|
||||
submitOCRJob,
|
||||
updateQrCode,
|
||||
type ChatQrCode,
|
||||
type CreateQrCodePayload,
|
||||
@@ -230,8 +231,19 @@ function parseGroupNameFromOcrText(text: string) {
|
||||
}
|
||||
|
||||
async function recognizeGroupName(file: File) {
|
||||
const result = await recognizeQrCodeGroupName(file)
|
||||
return result.group_name || parseGroupNameFromOcrText(result.raw_text || '')
|
||||
const { job_id } = await submitOCRJob(file)
|
||||
const delays = [1000, 2000, 3000, 3000, 3000]
|
||||
for (let i = 0; i < delays.length; i++) {
|
||||
await new Promise(resolve => setTimeout(resolve, delays[i]))
|
||||
const record = await getOCRJobResult(job_id)
|
||||
if (record.status === 'done' && record.result) {
|
||||
return record.result.group_name || parseGroupNameFromOcrText(record.result.raw_text || '')
|
||||
}
|
||||
if (record.status === 'failed') {
|
||||
throw new Error(record.error || 'OCR 识别失败')
|
||||
}
|
||||
}
|
||||
throw new Error('OCR 识别超时')
|
||||
}
|
||||
|
||||
// 上传:逐个上传图片拿 URL
|
||||
|
||||
Reference in New Issue
Block a user