Access the android camera to Python using OpenCV

Abhishek Mishra
2 min readMay 11, 2021

This blog is very useful for those who are considering creating image processing applications that will use an Android camera as a carrier. I'll use Python 3.6 on a window, but don't worry about my other OS friends, the process will be the same too. Without waiting any longer, let's begin the tutorial.

Before you get started, install the following libraries:

  • OpenCV-contrib-python
  • NumPy
  • urllib

To install using pip, just type the following command on command prompt (cmd):

pip install urllib
pip install numpy
pip install opencv-contrib-python

The next step is to download and install the IP Webcam application on your mobile phones. This application will be used to make communication between your android phone and computer system with the help of a URL.

After installing the app make, gives all permissions are required and sure that your phone and PC are connected to the same network. Run the app on your phone and click on “Start server”.

After this setup, the camera of your android phone will open up with an IP address at the bottom. Like this:-

Now it turns to start coding for access the camera using python:-

from urllib.request import urlopen
import cv2
import numpy as np
#use the URL shown in your IP Webcam interface, replace it then run the code
url="http://192.168.43.1:8080/shot.jpg"#paste url here
# this url gives current frame of image
# frame of images is called video
while True:
motion=0#for motion
imgResp=urlopen(url)
imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
img=cv2.imdecode(imgNp,-1)
cv2.imshow("img1",img)
q=cv2.waitKey(1)
if (q==ord("q")):
break
cv2.destroyAllWindows()

After a few moments, a cv window will show. To close the window just press the “q” key (You can use any key as per your requirement).

This is essentially how to connect Android phones to your Python application. The next steps are in your hands. This can include real-time image classification, image segmentation, object detection, and facial recognition.

--

--

Abhishek Mishra

System Engineer-TCS | COMPUTER SCIENCE ENGINEER | I love Python 🐍🐍😀