LCOV - code coverage report
Current view: top level - src/extensions/core - webcamCapture.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 98.8 % 81 80
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { t } from '@/i18n'
       2              : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
       3              : import { useToast } from '@/components/ui/toast/toastStore'
       4              : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
       5              : import { uploadTempFile } from '@/services/uploadTempFile'
       6              : 
       7              : import { app } from '@/scripts/app'
       8              : import type { ComfyExtension } from '@/types/comfy'
       9              : 
      10         2355 : const webcamReady = new WeakMap<LGraphNode, Promise<HTMLVideoElement>>()
      11              : 
      12         2355 : export const webcamCaptureExtension: ComfyExtension = {
      13              :   name: 'Comfy.WebcamCapture',
      14              :   getCustomWidgets() {
      15         2355 :     return {
      16              :       WEBCAM(node, inputName) {
      17           12 :         let resolveVideo: (video: HTMLVideoElement) => void = () => undefined
      18           12 :         webcamReady.set(
      19              :           node,
      20              :           new Promise((resolve) => (resolveVideo = resolve))
      21              :         )
      22              : 
      23           12 :         const container = document.createElement('div')
      24           12 :         container.style.background = 'rgba(0,0,0,0.25)'
      25           12 :         container.style.textAlign = 'center'
      26              : 
      27           12 :         const video = document.createElement('video')
      28           12 :         video.style.height = video.style.width = '100%'
      29              : 
      30           12 :         const loadVideo = async () => {
      31           12 :           try {
      32           12 :             const stream = await navigator.mediaDevices.getUserMedia({
      33              :               video: true,
      34              :               audio: false
      35              :             })
      36            9 :             container.replaceChildren(video)
      37              : 
      38            9 :             setTimeout(() => resolveVideo(video), 500) // Fallback as loadedmetadata doesnt fire sometimes?
      39            9 :             video.addEventListener(
      40              :               'loadedmetadata',
      41              :               () => resolveVideo(video),
      42              :               false
      43              :             )
      44            9 :             video.srcObject = stream
      45            9 :             await video.play()
      46              :           } catch (error) {
      47            2 :             const label = document.createElement('div')
      48            2 :             label.style.color = 'red'
      49            2 :             label.style.overflow = 'auto'
      50            2 :             label.style.maxHeight = '100%'
      51            2 :             label.style.whiteSpace = 'pre-wrap'
      52              : 
      53            2 :             const message =
      54              :               error instanceof Error ? error.message : String(error)
      55            2 :             if (window.isSecureContext) {
      56            1 :               label.textContent =
      57              :                 'Unable to load webcam, please ensure access is granted:\n' +
      58              :                 message
      59              :             } else {
      60            1 :               label.textContent =
      61              :                 'Unable to load webcam. A secure context is required, if you are not accessing ComfyUI on localhost (127.0.0.1) you will have to enable TLS (https)\n\n' +
      62              :                 message
      63              :             }
      64              : 
      65            2 :             container.replaceChildren(label)
      66              :           }
      67              :         }
      68              : 
      69           12 :         void loadVideo()
      70              : 
      71           12 :         return { widget: node.addDOMWidget(inputName, 'WEBCAM', container) }
      72              :       }
      73              :     }
      74              :   },
      75              :   nodeCreated(node: LGraphNode) {
      76        23115 :     if ((node.type, node.constructor.comfyClass !== 'WebcamCapture')) return
      77              : 
      78           12 :     let video: HTMLVideoElement | undefined
      79        23115 :     const camera = node.widgets?.find((w) => w.name === 'image')
      80        23115 :     const w = node.widgets?.find((w) => w.name === 'width')
      81        23115 :     const h = node.widgets?.find((w) => w.name === 'height')
      82        23115 :     const captureOnQueue = node.widgets?.find(
      83              :       (w) => w.name === 'capture_on_queue'
      84              :     )
      85        23115 :     if (!camera || !w || !h || !captureOnQueue) return
      86              : 
      87           12 :     const canvas = document.createElement('canvas')
      88           12 :     const nodeOutputStore = useNodeOutputStore()
      89              : 
      90           12 :     const capture = () => {
      91            7 :       if (
      92              :         !video ||
      93              :         typeof w.value !== 'number' ||
      94              :         typeof h.value !== 'number'
      95              :       ) {
      96            0 :         return
      97              :       }
      98            7 :       canvas.width = w.value
      99            7 :       canvas.height = h.value
     100            7 :       const ctx = canvas.getContext('2d')
     101            7 :       if (!ctx) return
     102            7 :       ctx.drawImage(video, 0, 0, w.value, h.value)
     103            7 :       const data = canvas.toDataURL('image/png')
     104              : 
     105            7 :       const img = new Image()
     106            7 :       img.onload = () => {
     107            7 :         node.imgs = [img]
     108            7 :         nodeOutputStore.setNodePreviewsByNodeId(node.id, [data])
     109            7 :         app.canvas.setDirty(true)
     110              :       }
     111            7 :       img.src = data
     112              :     }
     113              : 
     114           12 :     const btn = node.addWidget(
     115              :       'button',
     116              :       'waiting for camera...',
     117              :       'capture',
     118              :       capture,
     119              :       {}
     120              :     )
     121           12 :     btn.disabled = true
     122           12 :     btn.serializeValue = () => undefined
     123              : 
     124           12 :     camera.serializeValue = async () => {
     125            4 :       if (captureOnQueue.value) {
     126            2 :         capture()
     127            2 :       } else if (!node.imgs?.length) {
     128            1 :         useToast().warning(t('toastMessages.noWebcamImageCaptured'))
     129            1 :         throw new Error('No webcam image captured')
     130              :       }
     131              : 
     132              :       // Upload image to temp storage
     133            3 :       const blob = await new Promise<Blob>((resolve, reject) =>
     134              :         canvas.toBlob((blob) =>
     135              :           blob ? resolve(blob) : reject(new Error('Failed to capture webcam'))
     136              :         )
     137              :       )
     138            3 :       const name = `${+new Date()}.png`
     139            3 :       const file = new File([blob], name)
     140            3 :       const upload = await uploadTempFile(file, 'webcam')
     141            3 :       if (!upload.ok)
     142            1 :         throw new Error(`Error uploading camera image: ${upload.reason}`)
     143            2 :       const data = upload.file
     144            4 :       const serverName = data.name || name
     145              :       const subfolder = data.subfolder || 'webcam'
     146              :       const type = data.type || 'temp'
     147            4 :       return `${subfolder}/${serverName} [${type}]`
     148              :     }
     149              : 
     150           12 :     const ready = webcamReady.get(node)
     151          108 :     if (!ready) return
     152           12 :     void ready.then((v) => {
     153            9 :       video = v
     154              :       // If width isn't specified then use video output resolution
     155            9 :       if (typeof w.value !== 'number' || !w.value) {
     156            8 :         w.value = video.videoWidth || 640
     157            8 :         h.value = video.videoHeight || 480
     158              :       }
     159            9 :       btn.disabled = false
     160            9 :       btn.label = t('g.capture')
     161              :     })
     162              :   }
     163              : }
        

Generated by: LCOV version 2.0-1