Stream Mobile Camera Feed to Python with Open CV

Open CV has been known for it’s powerful abilities to display and manipulate pre-recorded videos — but how can we utilize it to do the…

Stream Mobile Camera Feed to Python with Open CV
stream video from phone to code thumbnail graphic

Open CV has been known for it’s powerful abilities to display and manipulate pre-recorded videos — but how can we utilize it to do the same with live camera feed from our mobile phone?
In this short article, I’ll show you exactly how to do it with the help of Ip Webcam and Python!

Table of Content

Step 1: install Ip Webcam on mobile phone
Step 2: render mobile camera stream in browser
Step 3: display mobile camera feed in Python
Step 4: add video filters and transformations

Are you more of a video person?

checkout the same tutorial on YouTube if you’re not a big fan of reading:

Install Ip Webcam

On your Android mobile phone, navigate to the Play Store and search for “ip webcam” (please see screenshots below).
Install this app, open it, scroll to the very bottom and press on “start server”.
* Please make sure you’re phone is connected to the same WIFI network as your computer before you do so!

How to install Ip Webcam — Screenshots

Once our server has started streaming, Ip Webcam will present us with an IPv4 address that we can access from our computer (also, please see screenshot below).

IPv4 address of Ip Webcam server — screenshot

Render Live Streaming Video in Browser

Once we obtained the IPv4 address of our Ip Webcam server — we can move on with accessing this video stream on our computer.
We will type the ip address we obtained in our browser and select “Browser” for video rendering.
Then, we will manually add a “/video” extension to the URL, so that our Ip address will look similar to: “http://192.168.1.67:8080/video
It will display the full screen version of our video feed and we can then utilize this URL inside our Python code!

obtain the URL of our live video stream

Display Mobile Camera Feed in Python

We will begin with a blank Python file, and we will include the following lines of code to display the mobile camera feed from Ip Webcam:import cv2capture = cv2.VideoCapture(“http://192.168.1.67:8080/video")while(True):
  ret, frame = capture.read()
  cv2.imshow(‘livestream’, frame)if cv2.waitKey(1) == ord(“q”):
     breakcapture.release()
cv2.destroyAllWindows()

So what did we do in the code above?

  • cv2.VideoCapture : opens whichever video we specify inside the round brackets. This could be a pre-recorder video, sequence of images or a live streaming video as in our case. The same command fits all!
    * Please remember to replace the IPv4 address in the code example with your own individual address (yours will be slightly different from mine)
  • capture.read() : decodes the video and returns it frame by frame.
    This function unpacks into 2 variables — the first one (ret) represents a Boolean value indicating whether there exists a video frame or not.
    The second variable (frame) represents a single image in the sequence of images that we call a “video”.
  • cv2.imshow() : displays an image (frame) inside a window object.
    first argument (“livestream”) is the name of the window, second argument is the image to be displayed.
  • cv2.waitKey(1) : listens to a keypress event, 1 ms at a time.
  • ord(“q”) : represents the “Q” key on our keyboard, equivalent to chr(113) which is using numeric key code values instead of Unicode ones.
  • capture.release() : frees the resources we used in our VideoCapture. Very handy if you’re looking to display a new video as soon as closing the previous one!
  • cv2.destroyAllWindows() : collapses the video capture window.

Why did we use an a While loop?

Since a video is a sequence of images, and cv2.imshow() can only display one image at a time — the best way to broadcast videos is with a while loop!
Please note, that this is possible due to the exist character we usually include in the end of the while loop (such as “q” in the example above).
Without this character or another creative break statement — we might get stuck in an infinite loop. Therefore — it is very important!

Add Video Filters and Transformations

Open CV is widely known for it’s video manipulation abilities. For example, If we want to convert our video stream to greyscale and mirror it horizontally — we will include 2 extra lines of code:import cv2capture = cv2.VideoCapture(“http://192.168.1.67:8080/video")while(True):
  ret, frame = capture.read()
 
  #transformations
  gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
  mirror = cv2.flip(gray, 1)cv2.imshow(‘livestream’, mirror)if cv2.waitKey(1) == ord(“q”):
breakcapture.release()
cv2.destroyAllWindows()

cv2.cvtColor() : applies colour filters on the VideoCapture.
In the example above we have converted RGB to Grayscale, but there are many other handy colour space conversions available on this page (from the Open CV documentation).

cv2.flip() : is used to mirror the video, where the second argument (1) represents the flip code.
- Flip code (0) will result in vertical mirroring.
- Flip code (1) will result in horizontal mirroring.
- Flip code (-1) will mirror video both horizontally and vertically.

For other geometric transformations, please have a look at this page from the Open CV documentation.

Conclusion

Using the examples above, we’re not just displaying live camera feed from our mobile phone on our computer — but we can also manipulate the video as much as we’d like. Starting from colour filters, passing through linear and geometric transformations and even drawing shapes, including text and photos! (we’ll keep these examples for the next article)

With Open Cv — the sky is the limit! and once we know how to get our mobile phone camera involved — just imagine how many cool things we can build with this powerful combination!

Find me on social media:

Say Hi on YouTube:
https://www.youtube.com/PythonSimplified
Checkout my Github:
https://github.com/MariyaSha
Connect with me on LinkedIn:
https://www.linkedin.com/in/mariyasha888/
Follow me on Instagram:
https://instagram.com/mariyasha888