Anish U
Anish U

Reputation: 61

How do I use my DroidCam video feed (via USB) in Python for image processing using OpenCV?

So I am trying to use my Android phone as a webcam for image processing in Python using OpenCV. I have setup the client on my PC and am able to stream videos from my phone(via USB) without any problem. But the problem is that when I try to use this feed in my code, it gives me an error.

Here is what I am trying to execute.

import numpy as np    
import cv2    
cap = cv2.VideoCapture(0)     
ret, frame = cap.read()            
cap.release()    
cv2.destroyAllWindows() 

So in the above code I am getting the following error:

[ WARN:0] videoio(MSMF): can't grab frame. Error: -2147483638

When I tried to print 'ret' it printed 'False'. I don't know how to fix this error. Please note that I'm trying to stream via USB cable. Thank you.

Upvotes: 4

Views: 7753

Answers (4)

user12579252
user12579252

Reputation: 21

Just Try this, If you want to connect via DriodCam, befor of it, you should be connected.

cap = cv2.VideoCapture(1)

Upvotes: 1

Amirhossein
Amirhossein

Reputation: 1

If you want to connect using wifi, it's not hard.

You just need to make a connection between your PC and your mobile using DroidCam, then write your code. But set cv2.VideoCapture(0 or 1). It will use your mobile camera in OpenCV using a DroidCam connection. DroidCam must be started.

It works well for me.

Upvotes: 0

Anish U
Anish U

Reputation: 61

I tried all the methods that I could but nothing worked for me. Luckily, I found an alternative solution.

For those of you who want to use an Android phone as a webcam (via USB) for image processing can download another app from the playstore called IP Webcam. It's free and doesn't require a PC client.

Connect your Android phone to your PC and then turn on 'USB Tethering' on your Android device.

Open the app and navigate to the bottom and click on 'Start server' to start streaming to your PC.

When the stream starts, two IP addresses will be displayed on the bottom of the screen of your phone. To access the stream on your PC, use any one of the two IP addresses followed by "/video".

The final address should look something like:

http://100.71.43.213:8080/video

Note that, USB Tethering must be on to stream the video.

Upvotes: 2

Borsuk
Borsuk

Reputation: 11

DroidCam is seen as IP camera and if it's connected via USB you can use 127.0.0.1(or localhost) as an IP. Try this:

import numpy as np
import cv2

cap = cv2.VideoCapture('http://localhost:4747/mjpegfeed')

while(True):
    ret, frame = cap.read()

    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

If it is not working try this tutorial. Tab "Connect via USB (advanced)". https://www.dev47apps.com/droidcam/connect/

Upvotes: 1

Related Questions