maartenbreddels / ipywebrtc

WebRTC for Jupyter notebook/lab
MIT License
243 stars 40 forks source link

With Context for MediaStream Classes #73

Open pushkarnimkar opened 4 years ago

pushkarnimkar commented 4 years ago

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)
    ...
maartenbreddels commented 4 years ago

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

pushkarnimkar commented 4 years ago

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.