How to easily stream webcam video over wifi with Raspberry Pi

 · 5 mins read

Hi friends! Today’s tutorial is Part 01 of the Raspberry Pi learning series. You will learn how to transmit video that from OpenCV library over the wifi. This video stream can be received on any mobile device connected to wifi and can open up an HTML webpage. Interestingly, in this tutorial, we do not require Flask or Django. All we need are two libraries; 1) cv2 and 2) pyshine.


1) cv2 You can easily install opencv as cv2 by following the tutorial here:

2) pyshine pyshine is required to enable the streaming features in this tutorial. So you can use the latest version 0.0.9 as:

pip3 install pyshine==0.0.9

Please note that you can easily observe the current IP address of the Raspberry Pi. Although the code below will work on any PC connected to the same wifi router, we recommend using RPi for its numerous advantages in daily life applications.

We will use raspberry pi zero board in this tutorial, but theoretically, you can use any other boards as long as they have the Python3 installed and access wifi. The wifi standard used in RPi zero is IEEE 802.11n, which relatively supports a longer distance of about 200 m in Line of sight theoretically. However, the range also depends on antenna characteristics and, of course, the transmission power. We will cover these details in upcoming tutorials so let us get back to the streaming part.

For this purpose, we need a code as shown below, which will use the docstring of an HTML page and pass it to the streaming server through pyshine library. The HTML page will get the mjpeg stream plays it in an inline fashion.

From our previous tutorials, you may already have known the idea to get the IP address of your device. Let’s say your IP address is, then the following code is all we need to observe the video stream in another device’s webbrowser.

# Part 01 using opencv access webcam and transmit the video in HTML
import cv2
import  pyshine as ps #  pip3 install pyshine==0.0.9
<title>PyShine Live Streaming</title>

<center><h1> PyShine Live Streaming using OpenCV </h1></center>
<center><img src="stream.mjpg" width='640' height='480' autoplay playsinline></center>
def main():
    StreamProps = ps.StreamProps
    address = ('',9000) # Enter your IP address 
        capture = cv2.VideoCapture(0)
        server = ps.Streamer(address,StreamProps)
        print('Server started at','http://'+address[0]+':'+str(address[1]))
    except KeyboardInterrupt:
if __name__=='__main__':

Copy this code to a file and paste that file in the Raspberry pi that already has camera enabled. To run the code:


After that you will see these messages in terminal window:

Warning! PortAudio library not found
Warning! No module named 'matplotlib'
Warning! No module named 'keras'
Server started at

Ignore the warnings and copy the address and paste it to the browser of your pc or mobile device that is on the same wifi network. And there you go, now you can view in the browser what the camera of your RPi is providing to you.

But wait a minute, what if you don’t have a wifi router and you want to connect RPi in Ad Hoc mode. The whole idea is to configure RPI’s wifi interface in Adhoc mode, so that it can be connected to a PC/Mobile device without any wifi router. Yes, in this mode we can enjoy First Person View (FPV) capabilities of RPi. By simply writing an auto run script in Python that will be called once the RPi is ever booted up, we can run the above code in it. In this way, you can also enjoy point to point FPV link, where the video from RPi is transmitted straight to your mobile device. The RPi zero is only 9g in weight, which means you can use it with drones as well. The frame size is kept 320x240 to reduce the latency, which is essential for FPV based vehicles/drones. To configure the Raspberry pi in Adhoc mode please follow our upcoming tutorial here

Have a nice day!