DEV Community

parmarjatin4911@gmail.com
parmarjatin4911@gmail.com

Posted on

πŸ“Έ Building a Live Streaming Viewer with Python and OpenCV πŸ“Έ

Ever wanted to keep an eye on your surroundings using a webcam or IP camera? Look no further! In this exciting technical blog, we'll walk you through the process of creating a live-streaming viewer using Python and OpenCV. Get ready to witness the world through the lens of code! πŸŒπŸ”

πŸ”Ή Essential Libraries
Before we dive in, let's gather our tools:
pip install request && pip install request && pip install opencv-python && pip install imutils

First things first! We'll set up the URL to your camera's live feed. Make sure to append /shot.jpg to the URL. πŸ“ΉπŸ”—

Ever thought of turning your Android phone into a webcam? Get ready to dive into the world of live streaming as we guide you through the process of setting up IP Webcam. πŸ“ΉπŸ“±

πŸ”Ή Install IP Webcam App

  1. Launch Google Play Store on your Android phone.

  2. Search for "IP Webcam" by Pavel Khlebovich and install the app.

  3. Once installed, open the app.

πŸ”Ή Configure IP Webcam

The app presents you with configuration options. Customize to your liking:

  • Video Orientation: Choose portrait or landscape mode.

  • Video Resolution: Select your preferred quality.

  • Audio: Enable/disable audio streaming.

  • Authentication: Secure access with a username and password.

  • Web Server Options: Configure server settings.

Remember the IP address and port number displayed. This is your webcam's URL.

πŸ”Ή Start Webcam Server

Scroll down and tap "Start server." A message confirms the server is running.

πŸ”Ή Access Webcam Stream from Computer

  1. Open a web browser on your computer.

  2. Enter the IP address and port number in the address bar (e.g., http://192.168.1.23:8080).

  3. Explore options and enjoy the live video stream.

  4. Adjust camera settings, capture images, or record videos.

πŸ”Ή Access Webcam Stream Using Code (Optional)

For the tech-savvy:

  1. Modify the Python code you've got.

  2. Replace url with your phone's webcam stream URL (e.g., url = "http://192.168.1.23:8080/video").

  3. Run the code to display the live stream on your computer.

url = "http://192.168.1.23:8080/shot.jpg"

πŸ”Ή The Live Stream Loop

Here's where the magic happens! Our loop continuously fetches and displays images from the camera.

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, height=1800, width=1000)
cv2.imshow("Live Stream Viewer", img)

Press Enter to Exit

if cv2.waitKey(1) == 13:
break
cv2.destroyAllWindows()

This is the main loop of the code:

  1. The loop runs indefinitely (while True:) to continuously fetch images from the specified URL.

  2. requests.get(url) sends an HTTP GET request to the URL and retrieves the image data.

  3. np.array(bytearray(img_resp.content), dtype=np.uint8) converts the image data into a NumPy array of bytes.

  4. cv2.imdecode(img_arr, -1) decodes the image bytes using OpenCV and stores it in the img variable.

  5. imutils.resize(img, height=1800, width=1000) resizes the image to a height of 1800 pixels and a width of 1000 pixels using the imutils library.

  6. cv2.imshow("Android_cam", img) displays the resized image in a window with the title "Android_cam".

  7. The loop continues until the Enter key (key code 13) is pressed. When the key is pressed, the loop breaks and the OpenCV windows are closed using cv2.destroyAllWindows().

πŸ”Ή Real-World Application

Imagine turning your computer into a live-streaming hub! Monitor your home security camera or keep tabs on your furry friend's adventures. With this code, you can create a custom live-streaming viewer tailored to your needs. 🐢🏠

This blog is just the beginning. Explore our GitHub repository for complete code examples and unleash your creativity! πŸ’‘πŸš€

πŸ”— Stay Tuned for More!

We're excited to empower you with the skills to build your very own live-streaming viewer. Keep an eye on our LinkedIn page for the full step-by-step guide, complete with interactive examples and engaging visuals.

πŸ“£ Like, Share, and Comment if you're excited to embark on this coding journey with us. Let's bring the world to your screen, one frame at a time! πŸ“£

Python #OpenCV #LiveStreaming #Webcam #CodeMagic #LinkedInLearning

Top comments (0)