Page 1 of 1

VLC Get Frame For OpenCV

Posted: 26 Sep 2019 14:21
by anilargede
Hi there,

I am using libVLC with python in my Raspberry Pi 3 B and getting RTSP stream. I also want to process every frame in stream then save processed frames as video file. I worked with OpenCV before. So I know how to process frames and how to save as video file. Bu I don't know how to get frames from VLC for process.

Code: Select all

rtsp_server = 'rtsp://[IP_OF_CAMERA]/...' i = vlc.Instance("--vout=dummy --sout-mux-caching=<>") player = i.media_player_new() player.set_mrl(rtsp_server) player = vlc.MediaPlayer(rtsp_server) player.play()
This is how I am getting stream from RTSP. I found something like my problem in bottom of https://stackoverflow.com/questions/50591068/how-to-get-the-frame-from-the-vlc-media-player/505%2096151 page. But I can't understand how to do that. For now, I save frames with "take snapshot" and I read them with OpenCV then try to save as video.

Code: Select all

while True: player.video_take_snapshot(0, '/home/pi/Desktop/snapshot1.png', 0, 0) frame = cv2.imread('/home/pi/Desktop/snapshot1.png', 0) if frame is None: cap.release() print("NO FRAME") break cap.write(frame) cv2.imshow('Camera1', frame) if (cv2.waitKey(10) & 0xFF) == 27: #esc key ends process cap.release() break cv2.destroyAllWindows() player.stop()
That is rest of my code. In this case, I got almost 10 second delay in stream.
I tried to get RTSP stream with OpenCV but I couldn't. I tried to get frames from VLC with callbacks or functions but I couldn't that either.
Do you know anything how to get frame for OpenCV? Or have any idea?

Thanks

Re: VLC Get Frame For OpenCV

Posted: 27 Sep 2019 13:44
by chubinou
Hi,

You should use the video_set_callbacks [1] method to retrieve decoded video buffers directly from VLC. You may refer to [2] for a sample use in python

[1] https://www.olivieraubert.net/vlc/python-ctypes/doc/vlc-pysrc.html#MediaPlayer.video_set_callbacks
[2] https://github.com/oaubert/python-vlc/issues/17#issuecomment-277476196

Re: VLC Get Frame For OpenCV

Posted: 27 Sep 2019 15:37
by anilargede
Hi @chubinou,

It worked
Thanks a lot for help
I am appreciative

Re: VLC Get Frame For OpenCV

Posted: 15 Oct 2020 09:23
by MayoG
Hello @anilargede , can you please share your code, I am having exactly the same problem that you had. Thank you :)