-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Capturing CameraStream raises error "No data, did you record anything?" #54
Comments
Also, the I have an example here https://github.com/QuantStack/quantstack-talks/blob/master/2018-11-14-PyParis-widgets/notebooks/6.ipywebrtc.ipynb that does approximately what you want to achieve. The important part of the code is this one: out = Image()
stop_process = False
def process_image(_):
if stop_process:
return
im_in = PIL.Image.open(io.BytesIO(image_recorder.image.value))
im_array = np.array(im_in)[...,:3]
im_array_edges = adapt_rgb(each_channel)(sobel)(im_array)
im_array_edges = ((1-im_array_edges) * 255).astype(np.uint8)
im_array_edges[im_array_edges < 250] = 0
im_out = PIL.Image.fromarray(im_array_edges)
f = io.BytesIO()
im_out.save(f, format='png')
out.value = f.getvalue()
image_recorder.recording = True
image_recorder.image.observe(process_image, names=['value'])
HBox([out, image_recorder]) I observe the value of the recorder, everytime it changes it means that we took a new snapshot and |
I suppose you would need something like: recorder = ImageRecorder(stream=camera)
i = 0
def save_html(_):
if i > 100:
return
recorder.save("webcam/" + str(i) + ".png")
i = i + 1
recorder.recording = True
recorder.image.observe(save_html, names=['value'])
recorder.recording = True |
Thanks for the clarification on how the library works :-) But something still seems to be missing here as the callback does not seems to be triggered. I tried the example notebook you mentioned above and it seems to working (in your example the user needs to click on the capture button in order to start capturing, but even that does not work for me): Tested on latest Firefox and Chrome on Windows. from ipywebrtc import CameraStream, ImageRecorder
from ipywidgets import Output, VBox
camera = CameraStream.facing_user(audio=False)
camera recorder = ImageRecorder(stream=camera)
recorder out = Output()
numImage = 0
numImages = 100
def savePicture(_):
if i >= numImages:
return
out.append_stdout("Image " + numImage + " captured \n")
recorder.save("webcam/" + str(i) + ".png")
numImage = numImage + 1
recorder.recording = True
recorder.image.observe(savePicture, names=['value'])
VBox([recorder, out]) |
Is it the full code? if i >= numImages: should be if numImage >= numImages: |
I feel so silly 🤦♂ But unfortunately, it does not seem to solve the issue :-( Even commenting out the non-relevant parts does not seem to fix it: out = Output()
numImage = 0
numImages = 100
def savePicture(_):
#if numImage >= numImages:
# return
#out.append_stdout("Image " + numImage + " captured \n")
recorder.save("webcam/" + str(i) + ".png")
#numImage = numImage + 1
recorder.recording = True
recorder.image.observe(savePicture, names=['value'])
VBox([recorder, out]) |
You also have this line: recorder.save("webcam/" + str(i) + ".png") That should be recorder.save("webcam/" + str(numImage) + ".png") |
The annoying thing is that if there is an error in the callback you won't see it... And it fails silently |
If you try something like: def savePicture(_):
with out:
if i >= numImages:
return
out.append_stdout("Image " + numImage + " captured \n")
recorder.save("webcam/" + str(i) + ".png")
numImage = numImage + 1
recorder.recording = True You should see errors in the Output widget I think |
Very nice, now I see what's going wrong: the variable scope does not seem to inherit as I expected.
Code: out = Output()
numImage = 0
numImages = 100
def savePicture(_):
with out:
if numImage >= numImages:
return
out.append_stdout("Image " + numImage + " captured \n")
recorder.save("webcam/" + str(numImage) + ".png")
numImage = numImage + 1
recorder.recording = True
recorder.image.observe(savePicture, names=['value'])
VBox([recorder, out]) |
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
[...] |
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]) |
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)
Second code block (which should capture 100 shots from the camera):
Executing the last block, the following error appears:
What am I missing here?
The text was updated successfully, but these errors were encountered: