使用 OpenCV Python 从 Android 智能手机捕获视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31478137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:04:30  来源:igfitidea点击:

Capturing Video from Android Smartphone using OpenCV Python

pythonopencvvideo

提问by Mohit

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.

我刚刚开始使用 Python 学习 OpenCV,第一个教程从使用内置笔记本电脑网络摄像头或外部网络摄像头捕获视频开始。就像它会发生的那样,我两者都没有。所以我想是否可以使用我的 Android 智能手机的相机,然后使用 IP 捕获该视频以进行进一步处理。

My Smartphone: Moto E

我的智能手机:Moto E

OS: Windows 7

操作系统:Windows 7

Language: Python

语言:Python

Android Application : IP Webcam

Android 应用程序:IP 网络摄像头

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.

我在网上广泛搜索,但找不到任何可行的解决方案,所以任何人都可以指导我如何使用 IP 网络摄像头从我的智能手机捕获视频。

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

很抱歉没有发布代码,因为我只是趋向于这个领域,所以我完全一无所知。

Thanks.

谢谢。

回答by goncalopp

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

由于各种原因,这可能比您预期的要困难得多。

The first would be bandwidth. A modest stream of rawvideo (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.

首先是带宽。适度的原始视频流(640x480 像素,每通道 8 位,每秒 30 帧)需要大约 200mbps 的带宽。虽然 USB (2) 可以轻松达到这些速度,但您将很难找到如此可靠的无线连接。

Now you may be thinking

现在你可能在想

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

为什么我可以在手机中观看 1080p 互联网视频而没有任何问题?

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.

几乎所有通过网络传输的视频都使用专门的算法进行压缩,例如 MPEG4、H.264 和 VP8。这些算法大大减少了传输视频所需的带宽。

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.

首先,为了实现视频数据量的大幅减少,视频压缩编码器)需要花费大量的处理能力来处理视频。您可能会发现您的手机没有足够的 CPU 能力(或专用硬件)来以适合您的任务的分辨率和帧速率对视频进行编码。

If you manage to solve that andfind 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).

如果您设法解决了这个问题找到了一个可以完成这项工作的应用程序,那么第二个问题是,为了在 OpenCV 中获取(编码的)视频数据,您需要对其进行解码!您可以找到现成的软件来解码视频文件,但要解码实时视频流,您需要对软件进行编程以执行解码(最好使用库或OpenCV 本身)。

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

在这一点上,你会诅咒并后悔你没有在网络摄像头上花 15 美元(但你会在这个过程中学到很多有趣的东西:)

回答by Peter Lunk

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

使用 urllib 和 numpy 将 Android“IP 网络摄像头”应用程序视频流导入 Python OpenCV;)

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

回答by Ibadullah Khan

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.

这个线程接缝很旧,但只想添加我的答案。所以这就是我能够在 python 3.5、OpenCV 3.2 和 android 应用程序“IP WEB CAM”中完成任务的方式。get 函数中的 url ( http://192.168.0.103:8080)是 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

回答by Kashinath Patekar

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

您可以简单地使用cv2VideoCapture方法,通过向它传递流媒体 url,如 IP 网络摄像头应用程序所示。这是示例代码:

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

注意:/video如果是 IP 网络摄像头应用程序,则需要 url的后缀。我通过在浏览器中检查原始 url 页面来解决这个问题。

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