Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing Video from Android Smartphone using OpenCV Python

I have just started learning OpenCV using Python and the first tutorial starts with capturing video using either in built laptop webcam or external webcam. And as it would happen, I have neither. So I thought if it would be possible to use Camera of my Android Smartphone and then capture that video using IP for further processing.

My Smartphone: Moto E

OS: Windows 7

Language: Python

Android Application : IP Webcam

I have searched the net extensively but am unable to find any working solution, so can anyone please guide me on how to capture the video from my smartphone using IP Webcam.

Sorry for posting no code as I am just trending into this field so am completely clueless.

Thanks.

like image 373
Mohit Avatar asked Jul 17 '15 14:07

Mohit


People also ask

Can OpenCV access mobile camera?

Download and install IP Webcam application on your mobile phone. Then make sure your PC and Phone both are connected to the same network. Open your IP Webcam application on your both, click “Start Server” (usually found at the bottom). This will open a camera on your Phone.

How can I use my phone as a webcam for Python?

First, install the OpenCV library in Python; pip install opencv-python. Download and install the IP Webcam application on your smartphones. After installing the IP Webcam app, make sure your phone and PC are connected to the same network. Run the app on your phone and click Start Server.

Which function in OpenCV is used to capture videos?

OpenCV provides the VideoCature() function which is used to work with the Camera. We can do the following task: Read video, display video, and save video. Capture from the camera and display it.


2 Answers

Android 'IP Webcam' App video stream import to Python OpenCV using urllib and numpy;)

import urllib
import cv2
import numpy as np
import time

# Replace the URL with your own IPwebcam shot.jpg IP:port
url='http://192.168.2.35:8080/shot.jpg'

while True:

    # Use urllib to get the image and convert into a cv2 usable format
    imgResp=urllib.urlopen(url)
    imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
    img=cv2.imdecode(imgNp,-1)

    # put the image on screen
    cv2.imshow('IPWebcam',img)

    #To give the processor some less stress
    #time.sleep(0.1) 

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
like image 114
Peter Lunk Avatar answered Sep 22 '22 15:09

Peter Lunk


This is probably much harder than what you're expecting, for a variety of reasons.

The first would be bandwidth. A modest stream of raw video (640x480 pixels, 8 bits per channel, 30 frames per second), requires a bandwidth on the order of 200mbps. While USB (2) easily reaches these speeds, you'll be hard pressed to find a wireless connection that does so reliably.

Now you may be thinking

How come I can watch 1080p internet videos in my phone with no problem whatsoever then?

Virtually all videos transmitted over a network are compressed using specialized algorithms, such as MPEG4, H.264 and VP8. These algorithms vastly reduce the bandwidth needed to transmit video.

Great! Then I can just compress the video from my phone live and stream it to my computer

Not so fast! There's two main problems with that.

The first is that, in order to achieve such a drastic reduction in amount of video data, video compressors (encoders) need to spend a lot of processing power crunching the video. You'll probably find that your phone doesn't have enough CPU power (or dedicated hardware) to encode video at a resolution and frame rate usable for your task.

If you manage to solve that and find a app that does the job, the second problem is that, in order to get the (encoded) video data in OpenCV, you'll need to decode it! You can find readily available software to decode video files, but for decoding a live stream of video, you'll need to program your software to perform the decoding (preferably using a library or OpenCV itself).

At this point, you'll be cursing and regretting you didn't spend the $15 on a webcam (but you'll have learned a lot of interesting stuff in the process :)

like image 44
loopbackbee Avatar answered Sep 23 '22 15:09

loopbackbee