Integrate Your Android Phone Camera with IP Webcam and OpenCV Using Python

Arthur Wellten
3 min readAug 31, 2023

--

IP CAM App and Python
Running the IP Cam app on Android

Frequently, situations arise demanding our involvement in Computer Vision or Image Processing tasks that surpass the capabilities of relying solely on our PC’s webcam. Or perhaps, there’s an idle old Android smartphone waiting to be put to good use. These instances might stem from a thirst for superior camera quality, the necessity to craft image processing applications leveraging an Android camera as the prime conduit, or even the ingenious repurposing of your aging Android device into a vigilant security camera.

This article delves into the procedure within a Windows and Android environment, both interconnected on the same network. It’s crucial to acknowledge that while we’re exploring this setup, the foundational code principles retain their uniformity across different operating systems.

SETTING THINGS UP

  1. Begin by downloading and installing the IP Webcam application onto your Android mobile phone.
  2. Next, ensure that your PC and phone are seamlessly connected to the same network.
  3. Launch the IP Webcam application on your phone. Locate and tap the “Start Server” option, typically at the interface's lower section. This action will initiate the camera functionality on your phone.
  4. On your phone’s screen, a URL will be displayed. Take this URL and input it into your Python code.
  5. Now, we will get the image data from the URL using the request module and convert this to an image frame using NumPy, and finally, start using our Android camera as a webcam in Python.

THE CODE

Behold, the code I’ve crafted for your enjoyment and experimentation. It may not be adorned with extravagance, but rest assured, it operates seamlessly, casting its charming spell upon the task at hand.

Code by Art Wellten

Congratulations! You’ve embarked on a fantastic journey that marks just the beginning. From here, the path opens up to grander and more sophisticated projects. Picture yourself connecting an array of devices, whether it’s for bolstering security or diving into the realm of AI applications. The exhilarating possibilities await — enjoy every step of the way!

Just want to copy and paste the code? Here you go:

# Import libraries
import cv2
import numpy as np
import imutils
import requests

# Replace the below URL with your own IP provided by the IP WEBCAM APP.
# Make sure to add "/shot.jpg" at last.
url = "http://YOUR_IP_WEBCAM_APP_IP_HERE:8080/shot.jpg"

# While loop to continuously fetching data from the Url
while True:
img_resp = requests.get(url)
img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
img = cv2.imdecode(img_arr, -1)
img = imutils.resize(img, width=800, height=600)
cv2.imshow("Android Cam", img)

# Press Esc key to exit
if cv2.waitKey(1) == 27:
break

cv2.destroyAllWindows()

Have fun!

By Art. Wellten
www.methark.com

--

--

Arthur Wellten

Senior Business and Technology Consultant, Researcher and Scientist.