Skip to content

Commit a534496

Browse files
committed
fix: remove more than 10 img api identify
1 parent 009f71b commit a534496

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed

src/App.vue

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
</template>
4545

4646
<script setup lang="ts">
47-
import { computed, watch } from 'vue';
47+
import { computed, watch, onMounted, onUnmounted } from 'vue';
4848
4949
import { useOpenCV } from './composables/useOpenCV';
5050
import { useChessEngine } from './composables/useChessEngine';
@@ -106,6 +106,39 @@ const handleImageUpload = async (img: HTMLImageElement) => {
106106
await processUploadedImage(img);
107107
console.log('[App] 图片处理完成');
108108
};
109+
110+
// 处理剪贴板粘贴
111+
const handlePaste = async (e: ClipboardEvent) => {
112+
const items = e.clipboardData?.items;
113+
if (!items) return;
114+
115+
for (const item of items) {
116+
if (item.type.indexOf('image') === -1) continue;
117+
118+
const blob = item.getAsFile();
119+
if (!blob) continue;
120+
121+
// 转换为图片元素
122+
const img = new Image();
123+
img.onload = () => {
124+
handleImageUpload(img);
125+
};
126+
img.src = URL.createObjectURL(blob);
127+
128+
// 阻止默认粘贴行为
129+
e.preventDefault();
130+
break;
131+
}
132+
};
133+
134+
// 添加和移除事件监听
135+
onMounted(() => {
136+
document.addEventListener('paste', handlePaste);
137+
});
138+
139+
onUnmounted(() => {
140+
document.removeEventListener('paste', handlePaste);
141+
});
109142
</script>
110143

111144
<style scoped>

src/composables/useImageProcessing.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,44 +62,40 @@ async function resizeAndConvertImage(
6262
async function getStepFunAnalysis(
6363
pieces: { cell: ImageData }[]
6464
): Promise<string[][]> {
65+
// 如果未识别棋子超过10个,直接报错
66+
if (pieces.length > 10) {
67+
throw new Error('未识别棋子过多(超过10个),请检查图片质量或重新截图');
68+
}
69+
6570
// 处理所有图片
6671
const processedImages = await Promise.all(
6772
pieces.map((piece) => resizeAndConvertImage(piece.cell))
6873
);
6974

70-
// 分批处理,每批最多10张图片
71-
const results: string[][] = [];
72-
for (let i = 0; i < processedImages.length; i += 10) {
73-
const batch = processedImages.slice(i, i + 10);
74-
const batchFormData = new FormData();
75-
batch.forEach((blob) => batchFormData.append('image', blob));
76-
77-
const response = await fetch(
78-
'https://workers.nicesprite.com/api/stepfun/',
79-
{
80-
method: 'POST',
81-
body: batchFormData,
82-
}
83-
);
84-
85-
if (!response.ok) {
86-
throw new Error('StepFun API 请求失败');
87-
}
75+
// 构建请求数据
76+
const formData = new FormData();
77+
processedImages.forEach((blob) => formData.append('image', blob));
8878

89-
const data = await response.json();
90-
if (!data.choices?.[0]?.message?.content) {
91-
throw new Error('StepFun API 返回格式错误');
92-
}
79+
const response = await fetch('https://workers.nicesprite.com/api/stepfun/', {
80+
method: 'POST',
81+
body: formData,
82+
});
9383

94-
const batchResults = data.choices[0].message.content
95-
.split(/|,/)
96-
.map((s: string) => s.trim())
97-
.filter(Boolean);
84+
if (!response.ok) {
85+
throw new Error('StepFun API 请求失败');
86+
}
9887

99-
results.push(batchResults);
88+
const data = await response.json();
89+
if (!data.choices?.[0]?.message?.content) {
90+
throw new Error('StepFun API 返回格式错误');
10091
}
10192

102-
return results;
93+
const results = data.choices[0].message.content
94+
.split(/|,/)
95+
.map((s: string) => s.trim())
96+
.filter(Boolean);
97+
98+
return [results];
10399
}
104100

105101
export function useImageProcessing(

0 commit comments

Comments
 (0)