File size: 1,930 Bytes
096584a
 
 
 
 
 
 
 
 
 
 
6a3d22f
096584a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a3d22f
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
import { v4 as uuidv4 } from "uuid"

import { RenderedScene, RenderRequest } from "../types.mts"
import { downloadFileToTmp } from "../utils/downloadFileToTmp.mts"
import { getFirstVideoFrame } from "../utils/getFirstVideoFrame.mts"
import { segmentImage } from "../utils/segmentImage.mts"

export async function renderVideoSegmentation(
  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)
    if (request.segmentation === "firstframe") {
      console.log("going to grab the first frame")
      const tmpVideoFilePath = await downloadFileToTmp(response.assetUrl, `${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")
        response.error = "failed to segment the image"
        response.status = "error"
      } else {
        console.log("got the first frame! segmenting..")
        const result = await segmentImage(firstFrameFilePath, actionnables, request.width, request.height)
        response.maskUrl = result.maskUrl
        response.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()
      }
      */
    }
  }

  return response
}