• Home
  • Computer Vision
  • Neural Network
  • Forums
  • About
  • Contact
  • Privacy Policy
  • Disclaimer

The Codacus

The Coding Abacus

  • Home
  • About
  • Contact
  • Privacy Policy
  • Disclaimer
You are here: Home / Computer Vision / How to use IP Webcam with opencv as a wireless camera

How to use IP Webcam with opencv as a wireless camera

Hi guys, If you are interested in creating robots, or embedded systems like me then you must be wondering if there is an way to use your android phone as an wireless camera (IP Webcam ) for your opencv code embedded in a SBC like Raspberry Pi,

You might have already know about IP Webcam apps for smart phones, but for those who don’t know yet, its an application which streams live images from your mobile camera over local wifi network, and you can see it through web browser.

Okay so now that we all are on same page, lets talk about how we can grab that image streaming on local network in to opencv.

Prerequisites

  • IP webCam (from here)
  • Android Phone

Setting up IP Webcam

After installing IP webcam in your phone, open it up and you will see an IP address in the screen with a port number like below.IP Webcam with OpenCV

Note that down somewhere, now type that in your browser. it will open up the IP webcam dashboard, which will look like below IP Webcam with OpenCV

Now we need to get the url to get image frames from this dashboard. To do that go to javascript option and we will see the live camera feed from the phone into the browser. just Right click and select “Copy Image Address” 

IP Webcam with OpenCV

Now paste that link somewhere like a notepad we will be needing that in our code

Lets Code

So now we are done with the setup all we need to do now is write a python code to grab a frame from the url that we noted down in our last step and display that in an infinite loop

lets import the libraries

import urllib
import cv2
import numpy as np

so imported the cv2 for opencv and numpy for matrix manipulation and urllib for opening url and reading values from urls

now we need to read the image from the url that we noted down earlier

url='http://192.168.0.103:8080/shot.jpg'

now its time to construct the loop

while True:
    imgResp=urllib.urlopen(url)
    imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
    img=cv2.imdecode(imgNp,-1)
    cv2.imshow('test',img)
    if ord('q')==cv2.waitKey(10):
        exit(0)

So what just happened up there

Inside the while loop, we are opening the url using “urllib.urlopen(url)” this will give us an response in a variable “imgResp”,

imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
In this line we are reading the values from the url response and converting it to a byte array and ultimately converting it to a numpy array and stored it in a numpy variable imgNp. This is our image. in encoded formate.

img=cv2.imdecode(imgNp,-1)
In this line we decode the encoded image and store it in another variable called img. and thats out final image. Now after that we can do out image processing in that image or what ever we want and then finally display that image we an imshow-waitKey combination

cv2.imshow('test',img)
if ord('q')==cv2.waitKey(10):
    exit(0)

 

Finally all put together

import urllib
import cv2
import numpy as np

url='http://192.168.0.103:8080/shot.jpg'

while True:
    imgResp=urllib.urlopen(url)
    imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
    img=cv2.imdecode(imgNp,-1)

    # all the opencv processing is done here
    cv2.imshow('test',img)
    if ord('q')==cv2.waitKey(10):
        exit(0)

 

Complete IP Webcam  OpenCV Video Tutorial

Comments

  1. galo celly alvarado says

    December 6, 2017 at 3:29 am

    Con la misma aplicación sin tener que usar un arreglo (transmisión en vivo):
    import cv2
    import urllib
    import numpy as np
    stream=urllib.urlopen(‘http://192.168.0.102:8080/video’)
    bytes=”
    while True:
    bytes+=stream.read(10000)
    a = bytes.find(‘xffxd8’)
    b = bytes.find(‘xffxd9’)
    if a!=-1 and b!=-1:
    jpg = bytes[a:b+2]
    bytes= bytes[b+2:]
    i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
    cv2.imshow(‘i’,i)
    if cv2.waitKey(1) ==27:
    exit(0)

    Reply
  2. kiran says

    March 15, 2018 at 10:58 am

    Sir amazing Turorial. I have one query , How to set resolution for frame.if we are using cap = cv2.VideoCapture(0) so we can write cap.set(3,640) and cap.set(4,480) but here how to set it??

    Reply
  3. Shashi Kiran says

    April 18, 2018 at 10:59 am

    For Python3 users :

    Replace ‘urllib’, with ‘urllib.request’

    Reply
  4. Stefano Silvestré Di Stefano says

    May 8, 2018 at 2:50 am

    Hi. I love this a lot. how can i configure the ip webcam with use with face recognition?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Lets Build An Image Classifier Using Tensorflow
  • Prepare Real life Data Set To Train Your Tensorflow Model
  • TensorBoard Tutorial, Visualize Your Networks Graphically
  • TensorFlow Tutorial Ground Zero | How To Start
  • Gesture Recognition Virtual Mouse Using OpenCV

Categories

  • Computer Vision
    • Face Recognition
    • Object Recognition
  • Machine Learning
    • Neural Network

Archives

  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • March 2017
  • February 2017
  • January 2017

Recent Comments

  • Python-OpenCV人脸识别之数据集生成 - 造壳 on Face Recognition – OpenCV Python | Dataset Generator
  • Python-OpenCV训练一个人脸识别器 - 造壳 on Face Recognition OpenCV – Training A Face Recognizer
  • Deep Learning Tutorial in Python #3 – Tensorflow Operations Part 1 | Quantum Code on TensorFlow Tutorial Ground Zero | How To Start
  • Python-OpenCV训练一个人脸识别器 - 奇奇问答 on Face Recognition OpenCV – Training A Face Recognizer
  • Vangipuram Sainath on Build Neural Network From Scratch in Python (no libraries)

Copyright © 2021 · Magazine Pro Theme on Genesis Framework · WordPress · Log in