Spaces:
Sleeping
Sleeping
Commit ·
3a2977d
1
Parent(s): 1958836
update: export from starry-refactor 2026-02-20 17:38
Browse files
backend/omr-service/src/routes/predictor.ts
CHANGED
|
@@ -10,6 +10,25 @@ interface PredictorBody {
|
|
| 10 |
images: string[];
|
| 11 |
}
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
export default async function predictorRoutes(fastify: FastifyInstance) {
|
| 14 |
// Layout predictor - returns detection results
|
| 15 |
fastify.post<{ Body: PredictorBody }>('/predict/layout', async (request, reply) => {
|
|
@@ -25,10 +44,7 @@ export default async function predictorRoutes(fastify: FastifyInstance) {
|
|
| 25 |
|
| 26 |
return {
|
| 27 |
code: 0,
|
| 28 |
-
data: results.map((result) => (
|
| 29 |
-
...result,
|
| 30 |
-
image: result.image ? `data:image/png;base64,${Buffer.from(result.image).toString('base64')}` : undefined,
|
| 31 |
-
})),
|
| 32 |
};
|
| 33 |
});
|
| 34 |
|
|
@@ -46,10 +62,7 @@ export default async function predictorRoutes(fastify: FastifyInstance) {
|
|
| 46 |
|
| 47 |
return {
|
| 48 |
code: 0,
|
| 49 |
-
data: results.map((result) => (
|
| 50 |
-
...result,
|
| 51 |
-
image: result.image ? `data:image/png;base64,${Buffer.from(result.image).toString('base64')}` : undefined,
|
| 52 |
-
})),
|
| 53 |
};
|
| 54 |
});
|
| 55 |
|
|
@@ -67,10 +80,7 @@ export default async function predictorRoutes(fastify: FastifyInstance) {
|
|
| 67 |
|
| 68 |
return {
|
| 69 |
code: 0,
|
| 70 |
-
data: results.map((result) => (
|
| 71 |
-
...result,
|
| 72 |
-
image: result.image ? `data:image/png;base64,${Buffer.from(result.image).toString('base64')}` : undefined,
|
| 73 |
-
})),
|
| 74 |
};
|
| 75 |
});
|
| 76 |
|
|
|
|
| 10 |
images: string[];
|
| 11 |
}
|
| 12 |
|
| 13 |
+
/** Recursively convert Buffer objects in 'image' fields to base64 data URLs. */
|
| 14 |
+
function convertBufferImages(obj: any): any {
|
| 15 |
+
if (obj == null || typeof obj !== 'object') return obj;
|
| 16 |
+
if (Buffer.isBuffer(obj)) return obj;
|
| 17 |
+
if (Array.isArray(obj)) return obj.map(convertBufferImages);
|
| 18 |
+
|
| 19 |
+
const result: any = {};
|
| 20 |
+
for (const [key, value] of Object.entries(obj)) {
|
| 21 |
+
if (key === 'image' && Buffer.isBuffer(value)) {
|
| 22 |
+
result[key] = `data:image/png;base64,${Buffer.from(value).toString('base64')}`;
|
| 23 |
+
} else if (typeof value === 'object' && value !== null) {
|
| 24 |
+
result[key] = convertBufferImages(value);
|
| 25 |
+
} else {
|
| 26 |
+
result[key] = value;
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
return result;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
export default async function predictorRoutes(fastify: FastifyInstance) {
|
| 33 |
// Layout predictor - returns detection results
|
| 34 |
fastify.post<{ Body: PredictorBody }>('/predict/layout', async (request, reply) => {
|
|
|
|
| 44 |
|
| 45 |
return {
|
| 46 |
code: 0,
|
| 47 |
+
data: results.map((result) => convertBufferImages(result)),
|
|
|
|
|
|
|
|
|
|
| 48 |
};
|
| 49 |
});
|
| 50 |
|
|
|
|
| 62 |
|
| 63 |
return {
|
| 64 |
code: 0,
|
| 65 |
+
data: results.map((result) => convertBufferImages(result)),
|
|
|
|
|
|
|
|
|
|
| 66 |
};
|
| 67 |
});
|
| 68 |
|
|
|
|
| 80 |
|
| 81 |
return {
|
| 82 |
code: 0,
|
| 83 |
+
data: results.map((result) => convertBufferImages(result)),
|
|
|
|
|
|
|
|
|
|
| 84 |
};
|
| 85 |
});
|
| 86 |
|