Skip to content
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

can I do real-time editing/filtering on the web stream? #50

Closed
YubinXie opened this issue Feb 24, 2019 · 16 comments
Closed

can I do real-time editing/filtering on the web stream? #50

YubinXie opened this issue Feb 24, 2019 · 16 comments

Comments

@YubinXie
Copy link

I was wondering if I can apply some filters/mask on the streaming video? This requires access to the frames in real-time. Is it possible to do this on this package?

I have tried to capture frame one by one using cv2: clean the previous one and display the next one. However, what I actually see is firstly the first image disappears and the output space of this cell also disappears; then the next image comes and increases the output space. Overall, you will the whole page is shaking up and down due to the clean and display step.

It would be great if I can do the modification on the streaming on this package! Thanks.

@YubinXie YubinXie changed the title can I do real-time editing/filtering on the video? can I do real-time editing/filtering on the web stream? Feb 24, 2019
@martinRenou
Copy link
Collaborator

I made an example using skimage instead of opencv, but I guess you can achieve something similar with opencv: https://github.com/QuantStack/quantstack-talks/blob/master/2018-11-14-PyParis-widgets/notebooks/6.ipywebrtc.ipynb

In the example I process images took from the chat room, but you can do the same taking images from a CameraStream or a VideoStream.

@martinRenou
Copy link
Collaborator

So basically the process is the following: Create your input stream (CameraStream for example) an create an ImageRecorder.

camera = CameraStream()
recorder = ImageRecorder(stream=camera)

Then you implement your image processing logic, taking recorder.image.value as input (it's a binary value). And you can use the Image widget (from ipywidgets) as output:

out = Image()

This way you can update the out.value in your image processing logic. And display the out widget under the cell.

@YubinXie
Copy link
Author

Great! Thanks!

@MetaDev
Copy link

MetaDev commented Jul 17, 2019

Thanks for providing this example, this set me up on the right path.
However, the loop is quite slow, the webcam image is lagging even without processing.
This is probably due to the back and fort conversion with PIL?:

im_in = PIL.Image.open(io.BytesIO(image_recorder.image.value))
im_array = np.array(im_in)[...,:3]
f = io.BytesIO()
im_out.save(f, format='png')

Is there any way around it?

@martinRenou
Copy link
Collaborator

If you do a quick profiling of your code, I suspect the slow part is the saving of the image.

@MetaDev
Copy link

MetaDev commented Jul 17, 2019

I commented out all code except,

im_in = PIL.Image.open(io.BytesIO(image_recorder.image.value))
im_array = np.array(im_in)[...,:3]

and it is still slow.
comparing webcamstream and imagestream of that webcam I see a very strong drop in FPS in the imagestream.

@martinRenou
Copy link
Collaborator

Oh I see, I'm not sure using an ImageStream is a good idea. Try using the ImageRecorder directly on the CameraStream

@MetaDev
Copy link

MetaDev commented Jul 17, 2019

I'm sorry I meant ImageRecorder with webcam as stream, maybe I should use videorecorder?

@martinRenou
Copy link
Collaborator

The VideoRecorder would be faster, and then you could try extracting each frame of the video using OpenCV or maybe PIL if it can.

@MetaDev
Copy link

MetaDev commented Jul 17, 2019 via email

@martinRenou
Copy link
Collaborator

Using the VideoRecorder you could maybe save the video to a file, then use OpenCV to load the file and extract the frames? You would need to look at OpenCV documentation for doing this.

@MetaDev
Copy link

MetaDev commented Jul 17, 2019 via email

@martinRenou
Copy link
Collaborator

than I am no longer processing real-time

Indeed, the VideoRecorder is for recording videos out of the stream. For now, the only way you can do it is with the ImageRecorder, even if it is slow...

There might be some room for improvement in terms of performances here.

@wolterlw
Copy link

Is it possible to create a VideoRecorder variant that acts anything like OpenCV's VideoCapture(0)
so that one can process the frames as a numpy array and then display them using 3rd-party tools, like bokeh for example?

@martinRenou
Copy link
Collaborator

My guess is that you can dump the video output of the VideoRecorder into a video file and use OpenCV's VideoCapture on this file?

@wolterlw
Copy link

@martinRenou no, there are much better ways of doing that without ipywebrtc.
So the use case I believe many people have is to make life demos that execute on remote servers but display results in a browser. In my particular case I perform hand pose estimation and it works much faster on a server, but it's quite complicated to stream video from the webcam there yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants