Search code examples
python-3.xvideo-streamingraspberry-pi3

Raspberry Pi 3 b+ Video Streaming to Python GUI


I have PC A & PC B. PC A is Raspberry Pi 3 b+, streaming video while open netcat listening port 8090 using this code:

  • /opt/vc/bin/raspivid -t 0 -w 1024 -h 600 -hf -ih -fps 60 -o - | nc -k -l 8090

PC B uses MPlayer to connect to PC A Port 8090 and open up the Video Stream, output is really awesome with low latency, the command:

  • mplayer -fps 200 -demuxer h264es ffmpeg://tcp://192.168.1.3:8090

But the thing is:

  • I want to pipe those output into Python GUI (maybe using Tkinter lib, or any if you guys can recommend me).
  • I tried to execute MPlayer inside my code, and ofc i'm so stupid because it will open up a new MPlayer windows playing the video-stream, but not attach to my GUI :(

-> 1, Is there any Python lib or code can help me catch the streaming video and pipe it into GUI ? So i can add some info on my GUI while display the Video on background.

-> 2, Is there anyway to use the Mplayer as embed output inside my GUI? And then I can editing my GUI as I want and the video stream is playing on the background.


Solution

  • After a day, I've figured out the Solution for this.

    For anyone who looking Solution, this will be your write-ups:

    1. PC B (Client Side, which is my DebianOS Laptop): You create these 2 files:
    • mkFIFO264.sh

            #!/bin/bash
            if [ -p fifo264 ]
            then
              rm fifo264
            fi
            mkfifo fifo264
            nc -l -v -p <port> > fifo264
      
    • getStreamData.py

            import numpy as np
            import cv2
            cap = cv2.VideoCapture('fifo264')
            while(cap.isOpened()):
                ret, frame = cap.read()
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                cv2.imshow('< DroneName >',gray)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            cap.release()
            cv2.destroyAllWindows()
      
    1. PC A (Raspberry Pi 3 Stream Server): You create a .sh file with this code:

           #!/bin/bash
           raspivid -t 0 -w <video_width> -h <video_height> -hf -ih -fps 60 -o - | nc <PC-B_IP> <port>