简体   繁体   中英

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.

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

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 :)

You can simply use cv2 's VideoCapture method, by passing it the streaming url, as shown on the IP Webcam app. Here is sample code:

Note: the /video suffix to the url is required in case of IP Webcam app. I had figured this out by inspecting the original url page in browser.

import cv2
url = "http://192.168.43.1:8080" # Your url might be different, check the app
vs = cv2.VideoCapture(url+"/video")

while True:
    ret, frame = vs.read()
    if not ret:
        continue
    # Processing of image and other stuff here
    cv2.imshow('Frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

This thread seams to be old but just want to add my answer. So this is how I was able to achieve the task in python 3.5, OpenCV 3.2 and android app "IP WEB CAM". The url ( http://192.168.0.103:8080 ) in the get function is the streaming address provided by ip web cam app.

import requests
import numpy as np
import cv2
while True:
    img_res = requests.get("http://192.168.0.103:8080/shot.jpg")
    img_arr = np.array(bytearray(img_res.content), dtype = np.uint8)
    img = cv2.imdecode(img_arr,-1)

    cv2.imshow('frame', img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Using IP webcam and OpenCV in 2020

import requests
import cv2
import numpy as np


URL = "http://192.168.68.126:8080/shot.jpg"
while True:
    img_resp = requests.get(URL)
    img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
    img = cv2.imdecode(img_arr, -1)
    cv2.imshow('IPWebcam', img)
    height, width, channels = img.shape
    print(height, width, channels)

    if cv2.waitKey(1) == 27:
        break

And here if you need to capture video stream

import requests
import cv2
import numpy as np


URL = "http://192.168.68.126:8080/video"
cam = cv2.VideoCapture(URL)

while True:
    check, img = cam.read()
    cv2.imshow('IPWebcam', img)
    height, width, channels = img.shape
    print(height, width, channels)
    if cv2.waitKey(1) == 27:
      break

download Droidcam. It can be used by wifi and after that in cv2.VideoCapture(n) where n can be 1 or 2 for me its 2 you can use mobile cam in python open_cv2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM