繁体   English   中英

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

[英]Capturing Video from Android Smartphone using OpenCV Python

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

我的智能手机:Moto E

操作系统:Windows 7

语言:Python

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

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

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

谢谢。

使用 urllib 和 numpy 将 Android 'IP Webcam' App 视频流导入 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

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

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

现在你可能在想

为什么我可以毫无问题地在手机上观看 1080p 网络视频?

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

伟大的! 然后我可以将手机中的视频实时压缩并将其流式传输到我的计算机

没那么快! 这样做有两个主要问题。

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

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

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

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

注意:如果是 IP 网络摄像头应用程序,则 URL 的/video后缀是必需的。 我通过检查浏览器中的原始 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

该线程接缝很旧,但只想添加我的答案。 所以这就是我在 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

2020 年使用 IP 网络摄像头和 OpenCV

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

在这里,如果您需要捕获视频流

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

下载 Droidcam。 它可以被wifi使用,之后在 cv2.VideoCapture(n) 中 n 可以是 1 或 2 对我来说它的 2 你可以在 python open_cv2 中使用移动摄像头

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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