Connecting your phone camera to computer in python using IP Webcam

Scenario :-

1. Connecting your webcam with python.

2. Connecting your phone camera with python.

1. Connecting your webcam with python

Ensure that you have a camera connected to your device either webcam or inbuilt camera.

Following code is used to connect the device camera with the python

import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Display the resulting frame
    cv2.imshow('webcam',frame)
    # In order to quit the window, we need to press 'q'
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Here we are -




2. Connecting your phone camera with python.

Here first you need to download the IP Webcam application from the google playstore


IP Webcam App

Following code is used to connect the IP Webcam app with the python

import urllib.request
import cv2
import numpy as np

#Enter the ip address displayed in your app 
url = 'http://192.168.0.3:8080/shot.jpg'

while True:
    imgResp = urllib.request.urlopen(url)
    imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
    img = cv2.imdecode(imgNp, -1)

    # all the opencv processing is done here
    cv2.imshow('test', img)
    if ord('q') == cv2.waitKey(10):
        exit(0)

Open the IP Webcam app. Go to the start server option (Check figure 2 last option). Note the URL after starting the server and change the URL given in code with your URL.


Here we are -


SHARE

Shubu

Hi. I’I hope this post was helpful for you.

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment