Skip to content

Commit

Permalink
fix: remove more than 10 img api identify
Browse files Browse the repository at this point in the history
  • Loading branch information
iFwu committed Nov 29, 2024
1 parent 009f71b commit a534496
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
35 changes: 34 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</template>

<script setup lang="ts">
import { computed, watch } from 'vue';
import { computed, watch, onMounted, onUnmounted } from 'vue';
import { useOpenCV } from './composables/useOpenCV';
import { useChessEngine } from './composables/useChessEngine';
Expand Down Expand Up @@ -106,6 +106,39 @@ const handleImageUpload = async (img: HTMLImageElement) => {
await processUploadedImage(img);
console.log('[App] 图片处理完成');
};
// 处理剪贴板粘贴
const handlePaste = async (e: ClipboardEvent) => {
const items = e.clipboardData?.items;
if (!items) return;
for (const item of items) {
if (item.type.indexOf('image') === -1) continue;
const blob = item.getAsFile();
if (!blob) continue;
// 转换为图片元素
const img = new Image();
img.onload = () => {
handleImageUpload(img);
};
img.src = URL.createObjectURL(blob);
// 阻止默认粘贴行为
e.preventDefault();
break;
}
};
// 添加和移除事件监听
onMounted(() => {
document.addEventListener('paste', handlePaste);
});
onUnmounted(() => {
document.removeEventListener('paste', handlePaste);
});
</script>

<style scoped>
Expand Down
52 changes: 24 additions & 28 deletions src/composables/useImageProcessing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,44 +62,40 @@ async function resizeAndConvertImage(
async function getStepFunAnalysis(
pieces: { cell: ImageData }[]
): Promise<string[][]> {
// 如果未识别棋子超过10个,直接报错
if (pieces.length > 10) {
throw new Error('未识别棋子过多(超过10个),请检查图片质量或重新截图');
}

// 处理所有图片
const processedImages = await Promise.all(
pieces.map((piece) => resizeAndConvertImage(piece.cell))
);

// 分批处理,每批最多10张图片
const results: string[][] = [];
for (let i = 0; i < processedImages.length; i += 10) {
const batch = processedImages.slice(i, i + 10);
const batchFormData = new FormData();
batch.forEach((blob) => batchFormData.append('image', blob));

const response = await fetch(
'https://workers.nicesprite.com/api/stepfun/',
{
method: 'POST',
body: batchFormData,
}
);

if (!response.ok) {
throw new Error('StepFun API 请求失败');
}
// 构建请求数据
const formData = new FormData();
processedImages.forEach((blob) => formData.append('image', blob));

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

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

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

return results;
const results = data.choices[0].message.content
.split(/,|,/)
.map((s: string) => s.trim())
.filter(Boolean);

return [results];
}

export function useImageProcessing(
Expand Down

0 comments on commit a534496

Please sign in to comment.