VLC Get Frame For OpenCV

This forum is about all development around libVLC.
anilargede
New Cone
New Cone
Posts: 2
Joined: 26 Sep 2019 13:38

VLC Get Frame For OpenCV

Postby anilargede » 26 Sep 2019 14:21

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

chubinou
Developer
Developer
Posts: 521
Joined: 23 Jul 2015 15:19

Re: VLC Get Frame For OpenCV

Postby chubinou » 27 Sep 2019 13:44

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

anilargede
New Cone
New Cone
Posts: 2
Joined: 26 Sep 2019 13:38

Re: VLC Get Frame For OpenCV

Postby anilargede » 27 Sep 2019 15:37

Hi @chubinou,

It worked
Thanks a lot for help
I am appreciative

MayoG
New Cone
New Cone
Posts: 1
Joined: 15 Oct 2020 09:12

Re: VLC Get Frame For OpenCV

Postby MayoG » 15 Oct 2020 09:23

Hello @anilargede , can you please share your code, I am having exactly the same problem that you had. Thank you :)


Return to “Development around libVLC”

Who is online

Users browsing this forum: No registered users and 20 guests