cvpilot-tool/apps/desktop/electron/core/alg.ts
2024-09-20 15:55:17 +08:00

52 lines
1.4 KiB
TypeScript

import log from "electron-log";
import { spawn } from "node:child_process";
import path from "node:path";
import { InferReq } from "./alg.type";
import axios from "axios";
export const ALGServerRoot = "http://127.0.0.1:5000/root";
export const getEntryPath = () => {
// 区分操作系统
return path.join(process.env.VITE_PUBLIC!, "main.exe");
};
export const startALGServer = () => {
const entryPath = getEntryPath();
const child_process = spawn(entryPath);
child_process.on("message", (data) => console.log(data));
child_process.stdout.on("data", (data) => log.info(data.toString()));
child_process.stderr.on("data", (data) => log.error(data.toString()));
};
/**
* 执行推理任务
*/
export const executeInferTask = (
task: InferReq,
onData: (data: string) => void
) => {
return new Promise((resolve, reject) => {
axios
.post(ALGServerRoot, task, {
responseType: "stream",
headers: { "Content-Type": "application/json" },
})
.then((response) => {
response.data.on("data", (chunk: Buffer) => {
const data = chunk.toString();
onData(data); // 实时处理数据
});
response.data.on("end", () => {
resolve(task); // 数据流结束
});
response.data.on("error", (err: Error) => {
reject(err);
});
})
.catch((error) => reject(error));
});
};