I made a simple motion detector program in using python 3.7 and opencv, is there a way to access my phone's camera using python and stream the video to my laptop using bluetooth or mobile hotspot so I can process the data on my laptop? I'm basically just using my phone as a detachable camera.
2 Answers
You can do this using IP Webcam android application.
Steps -
- Install the application in your android phone.
- Connect your Laptop and Phone in a local network (you can use mobile hotspot).
- Start application and select Start Server option, the application will start capturing video and show you IP addresses.
- Use this IP address to read the video feed using the following python code.
- Process the video using OpenCV.
Python code -
import urllib
import cv2
import numpy as np
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = 'Your URL'
while True:
imgResp = urllib3.urlopen(url)
imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
img = cv2.imdecode(imgNp, -1)
cv2.imshow('temp',cv2.resize(img,(600,400)))
q = cv2.waitKey(1)
if q == ord("q"):
break;
cv2.destroyAllWindows()
You can find the android application here - IP Webcam
And this video will explain better - How to use with OpenCV
-
I had to use urllib3 instead of urllib because
urllib.urlopen(url)
does not work anymore, but other that that It's working fine. Can you explain what the ssl is for? I tried removing it and it still works... Nov 4, 2018 at 5:40 -
ssl stands for secure sockets layer, which provides access to transport layer security encryption and peer authentication network facilities for network sockets. Here, no certificates will be requied from the other side of the socket connection. If a certificate is received from the other end, no attempt to validate it is made. Nov 4, 2018 at 14:42
Use IP Webcam android application. url is given by ip webcam and at the end I have added video for video streaming or you can url = 'http://192.168.137.138:8080/shot.jpg' inside for loop before cap.read()
This works for me flawlessly with 1280 x 720 resolution NOTE your url ip will change but add video in the last
import cv2
import numpy as np`
url = 'http://192.168.137.138:8080/video'
cap = cv2.VideoCapture(url)
while(True):
ret, frame = cap.read()
if frame is not None:
cv2.imshow('frame',frame)
q = cv2.waitKey(1)
if q == ord("q"):
break
cv2.destroyAllWindows()
-
This is certainly the easiest way to read a video stream from IP webcam using opencv Jan 26, 2020 at 15:55