question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Capturing CameraStream raises error "No data, did you record anything?"

See original GitHub issue

I am trying to capture 100 snapshots programatically from my webcam but somehow, the ImageRecorder does not record any image. However, when capturing the images using the dedicated widget button, it works like charm.

This is my code:

First code block (which simply shows the user the webcam input)

from ipywebrtc import CameraStream, ImageRecorder
from time import sleep

camera = CameraStream.facing_user(audio=False)
camera

Second code block (which should capture 100 shots from the camera):

recorder = ImageRecorder(stream=camera)
recorder.recording = True

for i in range(1,100):
    sleep(0.1)
    recorder.save("webcam/" + str(i) + ".png")

Executing the last block, the following error appears:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-77bec100e9f4> in <module>
      4 for i in range(1,100):
      5     sleep(0.1)
----> 6     recorder.save("webcam/" + str(i) + ".png")

c:\program files (x86)\microsoft visual studio\shared\python37_64\lib\site-packages\ipywebrtc\webrtc.py in save(self, filename)
    397             filename += '.' + self.format
    398         if len(self.image.value) == 0:
--> 399             raise ValueError('No data, did you record anything?')
    400         with open(filename, 'wb') as f:
    401             f.write(self.image.value)

ValueError: No data, did you record anything?

What am I missing here?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
frostieDEcommented, Jun 12, 2019

It finally works 🎉 In case someone wants to do the same as I want, this is the code:

from ipywebrtc import CameraStream, ImageRecorder
from ipywidgets import Output, VBox

camera = CameraStream.facing_user(audio=False)
camera
recorder = ImageRecorder(stream=camera)
out = Output()

numImage = 0
numImages = 100

def savePicture(_):
    with out:
        global numImage
        global numImages
        if numImage >= numImages:
            return

        out.append_stdout("Image " + str(numImage) + " captured \n")
        recorder.save("webcam/" + str(numImage) + ".png")
        numImage = numImage + 1
        recorder.recording = True
    
recorder.image.observe(savePicture, names=['value'])
VBox([recorder, out])
0reactions
martinRenoucommented, Jun 12, 2019

I’m not sure I get why…

This could make it work, but I don’t see why it is needed:

def savePicture(_):
    with out:
        global numImage
        global numImages

        [...]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Capture video from camera/file with OpenCV in Python
In Python, you can read, capture, and display video files and camera stream using the VideoCapture class with OpenCV.
Read more >
4. Advanced Recipes — Picamera 1.13 Documentation
Capturing to a numpy array¶. Since 1.11, picamera can capture directly to any object which supports Python's buffer protocol (including numpy's ndarray )....
Read more >
Freely Accessing Your Own Nest HD Camera Stream - Den
A twist on a project I worked on a couple of years ago, that makes it easier to get the Nest HD camera...
Read more >
How to Access a Webcam and Record Video with JavaScript
Learn how to access a webcam and record video using JavaScript. ... Download Code ‍ h... ... Your browser can 't play...
Read more >
MediaCapture Class (Windows.Media.Capture)
To capture video, the image encoding profile can be set to Auto or you need to specify an encoding profile that matches the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found