Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 3,182 Bytes
ef22617 75aad1e 6f310ff ef22617 6f310ff ef22617 6f310ff ef22617 75aad1e ef22617 |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import { v4 as uuidv4 } from "uuid"
import { ImageSegment, RenderedScene, RenderRequest } from "../types.mts"
import { downloadFileToTmp } from "../utils/downloadFileToTmp.mts"
import { generateSeed } from "../utils/generateSeed.mts"
import { getValidNumber } from "../utils/getValidNumber.mts"
import { generateVideo } from "./generateVideo.mts"
import { getFirstVideoFrame } from "../utils/getFirstVideoFrame.mts"
import { segmentImage } from "../utils/segmentImage.mts"
export async function renderVideoScene(scene: RenderRequest): Promise<RenderedScene> {
let url = ""
let error = ""
const width = 576
const height = 320
const params = {
seed: getValidNumber(scene.seed, 0, 2147483647, generateSeed()),
nbFrames: getValidNumber(scene.nbFrames, 8, 24, 16), // 2 seconds by default
nbSteps: getValidNumber(scene.nbSteps, 1, 50, 10), // use 10 by default to go fast, but not too sloppy
}
try {
url = await generateVideo(scene.prompt, params)
// console.log("successfull generation")
error = ""
if (!url?.length) {
throw new Error(`url for the generated image is empty`)
}
} catch (err) {
console.error(`failed to render the scene.. but let's try again!`)
try {
url = await generateVideo(scene.prompt, params)
// console.log("successfull generation")
error = ""
if (!url?.length) {
throw new Error(`url for the generated image is empty`)
}
} catch (err) {
console.error(`it failed the second time ${err}`)
error = `failed to render scene: ${err}`
}
}
// TODO add segmentation here
const actionnables = Array.isArray(scene.actionnables) ? scene.actionnables : []
let mask = ""
let segments: ImageSegment[] = []
if (actionnables.length > 0) {
console.log("we have some actionnables:", actionnables)
if (scene.segmentation === "firstframe") {
console.log("going to grab the first frame")
const tmpVideoFilePath = await downloadFileToTmp(url, `${uuidv4()}`)
console.log("downloaded the first frame to ", tmpVideoFilePath)
const firstFrameFilePath = await getFirstVideoFrame(tmpVideoFilePath)
console.log("downloaded the first frame to ", firstFrameFilePath)
if (!firstFrameFilePath) {
console.error("failed to get the image")
error = "failed to segment the image"
} else {
console.log("got the first frame! segmenting..")
const result = await segmentImage(firstFrameFilePath, actionnables, width, height)
mask = result.pngInBase64
segments = result.segments
// console.log("success!", { segments })
}
/*
const jpgBase64 = await getFirstVideoFrame(tmpVideoFileName)
if (!jpgBase64) {
console.error("failed to get the image")
error = "failed to segment the image"
} else {
console.log(`got the first frame (${jpgBase64.length})`)
console.log("TODO: call segmentImage with the base64 image")
await segmentImage()
}
*/
}
}
error = ""
return {
assetUrl: url,
error,
maskBase64: mask,
segments
} as RenderedScene
} |