ipywebrtc icon indicating copy to clipboard operation
ipywebrtc copied to clipboard

With Context for MediaStream Classes

Open pushkarnimkar opened this issue 5 years ago • 2 comments
trafficstars

Creating MediaStream classes using a with context can be useful as it ensures that media devices will be closed at the end of context.

Example syntax:

constraints = {
    "facing_mode": "user",
    "audio": False,
    "video": {
        "width": 640,
        "height": 480
    }
}

with CameraStream(constraints=constraints) as camera:
    recorder = ImageRecorder(stream=camera)
    ...

pushkarnimkar avatar Feb 21 '20 18:02 pushkarnimkar

I like it, would you want to make a pull request?

maartenbreddels avatar Feb 21 '20 18:02 maartenbreddels

Sure! I'll need some help though as I've never worked on internals of any widget before.

The use-case I was looking for is somewhat like this: I should be able to take multiple images from webcam (as training set) till my model builds enough confidence on the subject (for facial recognition). I was not actually looking for UI element of the widget in this case. So, I actually needed to take multiple images from webcam without any button click. I faced some issue when trying to do that. I had to eventually switch to button-based system.

For example, following snippet:

for _ in range(10):
    recorder.recording = True
    time.sleep(0.5)

    recorder.recording = False
    time.sleep(0.5)

    print(hash(recorder.image.value))

Prints the same hash which corresponds to previous state of the widget (initially it prints out zero as hash(b'') is 0.

Please tell me if this is not the correct way of taking multiple images in a loop. If this is a problem, I'll like to help in fixing this too.

pushkarnimkar avatar Feb 24 '20 02:02 pushkarnimkar