Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 2,586 Bytes
096584a 955ce73 096584a 39166f8 096584a 39166f8 096584a 39166f8 096584a 39166f8 096584a 6a3d22f 096584a 39166f8 096584a 39166f8 096584a 6a3d22f 096584a 39166f8 096584a 39166f8 096584a 6a3d22f 096584a 39166f8 096584a 39166f8 096584a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import path from "node:path"
import { v4 as uuidv4 } from "uuid"
import tmpDir from "temp-dir"
import { RenderedScene, RenderRequest } from "../types.mts"
import { writeBase64ToFile } from "../utils/filesystem/writeBase64ToFile.mts"
import { segmentImage } from "../providers/image-segmentation/segmentImage.mts"
export async function renderImageSegmentation(
request: RenderRequest,
response: RenderedScene,
): Promise<RenderedScene> {
const actionnables = Array.isArray(request.actionnables) ? request.actionnables : []
if (actionnables.length > 0) {
// console.log("we have some actionnables:", actionnables)
const tmpImageFilePath = path.join(tmpDir, `${uuidv4()}.png`)
// console.log("beginning:", imageBase64.slice(0, 100))
await writeBase64ToFile(response.assetUrl, tmpImageFilePath)
// console.log("wrote the image to ", tmpImageFilePath)
if (!tmpImageFilePath) {
// console.error("failed to segment the image")
response.error = "failed to segment the image"
response.status = "error"
} else {
// console.log("got the first frame! segmenting..")
try {
const result = await segmentImage(tmpImageFilePath, actionnables, request.width, request.height)
response.maskUrl = result.maskUrl
response.segments = result.segments
// console.log(`it worked the first time! got ${response.segments.length} segments`)
} catch (err) {
// console.log("this takes too long :/ trying another server..")
try {
const result = await segmentImage(tmpImageFilePath, actionnables, request.width, request.height)
response.maskUrl = result.maskUrl
response.segments = result.segments
// console.log(`it worked the second time! got ${response.segments.length} segments`)
} catch (err) {
// console.log("trying one last time, on a 3rd server..")
try {
const result = await segmentImage(tmpImageFilePath, actionnables, request.width, request.height)
response.maskUrl = result.maskUrl
response.segments = result.segments
// console.log(`it worked the third time! got ${response.segments.length} segments`)
} catch (err) {
console.log("yeah, all servers are busy it seems.. aborting")
response.error = "all servers are busy"
response.status = "error"
}
}
}
}
} else {
// console.log("no actionnables: just returning the image, then")
}
return response
}
|