Use Your Android Devices as Cameras for OpenCV

November 04, 2022 · 2 min read

建议 Tips

您正在查看印刷版本的博客, 印刷版本的博客可能会缺少部分交互功能, 部分内容显示不全或过时. 如果您在查看该印刷版博客时遇到了任何问题, 欢迎来此链接查看在线版: https://www.kxxt.dev/blog/use-android-devices-as-cameras-in-opencv/

You are viewing a printed version of this blog. It may lack some interactive features and some content might now be fully displayed or outdated. If you encounter any problems while viewing this printed version of the blog, feel free to view the online version here: https://www.kxxt.dev/blog/use-android-devices-as-cameras-in-opencv/

Why?

  • Android devices are common and cheap. You probably have tons of old phones.
  • Within a normal wireless local area network, the latency is usually negligible for most use cases.
  • Why not? It’s a good opportunity to make use of your old phones.

How

First, you need an android app which acts as a webcam.

I find droidcam a good fit.

The free version allows you to make your android phone a web cam with a resolution of 640x480. But it will add a watermark on the top left corner of the picture.

Then you can use your phone as an input source for OpenCV.

example.py

import cv2 as cv
ip = input("IP address of your phone: ")
vid = cv.VideoCapture(f"http://{ip}:4747/video?640x480")
while True:
ret, frame = vid.read()
if not ret:
raise "Failed to read image!"
cv.imshow('image', frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv.destroyAllWindows()

Of course, you can connect multiple android devices to OpenCV. It’s just a matter of creating multiple cv.VideoCapture instances.

Drawbacks

I find if your android device locks its screen, you can no longer receive camera input. (Possibly due to Android 12 only allowing apps to use camera when the user is using them).

You can use an app like Wakey to circumvent this issue.

Alternatives

iriun is a good alternative to droidcam, but it only allows one connection at the same time, which means you can only use one android device as camera in your code at the same time.

But iriun does implement something that droidcam doesn’t provide. It generates a virtual camera device so that you can use it as a normal camera in OpenCV: cv.VideoCapture(ID_OF_IRIUN_VIRTUAL_CAMERA_DEVICE).