简体   繁体   English

如何使用python脚本访问手机摄像头

[英]How to access phone camera using python script

I made a simple motion detector program in using python 3.7 and opencv, is there a way to access my phone's camera using python and stream the video to my laptop using bluetooth or mobile hotspot so I can process the data on my laptop?我使用python 3.7和opencv制作了一个简单的运动检测器程序,有没有办法使用python访问我手机的相机并使用蓝牙或移动热点将视频传输到我的笔记本电脑,以便我可以在我的笔记本电脑上处理数据? I'm basically just using my phone as a detachable camera.我基本上只是将我的手机用作可拆卸的相机。

Use IP Webcam android application.使用 IP 网络摄像头 android 应用程序。 url is given by ip webcam and at the end I have added video for video streaming or you can url = ' http://192.168.137.138:8080/shot.jpg ' inside for loop before cap.read() url 由 ip 网络摄像头给出,最后我添加了用于视频流的视频,或者您可以在 cap.read() 之前在 for 循环中 url = ' http://192.168.137.138:8080/shot.jpg '

This works for me flawlessly with 1280 x 720 resolution NOTE your url ip will change but add video in the last这对我来说完美无缺,分辨率为 1280 x 720 注意你的 url ip 会改变,但在最后添加视频

import cv2 
import numpy as np`
url = 'http://192.168.137.138:8080/video'
cap = cv2.VideoCapture(url)
while(True):
    ret, frame = cap.read()
    if frame is not None:
        cv2.imshow('frame',frame)
    q = cv2.waitKey(1)
    if q == ord("q"):
        break
cv2.destroyAllWindows()

You can do this using IP Webcam android application.您可以使用 IP 网络摄像头 android 应用程序执行此操作。

Steps -脚步 -

  1. Install the application in your android phone.在您的 android 手机中安装该应用程序。
  2. Connect your Laptop and Phone in a local network (you can use mobile hotspot).在本地网络中连接您的笔记本电脑和手机(您可以使用移动热点)。
  3. Start application and select Start Server option, the application will start capturing video and show you IP addresses.启动应用程序并选择启动服务器选项,应用程序将开始捕获视频并向您显示 IP 地址。
  4. Use this IP address to read the video feed using the following python code.使用此 IP 地址使用以下 python 代码读取视频源。
  5. Process the video using OpenCV.使用 OpenCV 处理视频。

Python code - Python代码 -

import urllib
import cv2
import numpy as np
import ssl

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = 'Your URL'

while True:
    imgResp = urllib3.urlopen(url)
    imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
    img = cv2.imdecode(imgNp, -1)
    cv2.imshow('temp',cv2.resize(img,(600,400)))
    q = cv2.waitKey(1)
    if q == ord("q"):
        break;

cv2.destroyAllWindows()

You can find the android application here - IP Webcam您可以在此处找到 android 应用程序 - IP 网络摄像头

And this video will explain better - How to use with OpenCV这个视频会更好地解释 -如何使用 OpenCV

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

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