How to access phone camera using python script - android

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.

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

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()

Related

send a photo from android device to server (treat it on matlab)

I am in trouble and need your help. I am currently working on a project which requires me to first pick a photo from gallery using an android device (with android studio), extract text and then recognize the text.
I have already achieved the process of extraction and recognizing through Matlab. Now my problem is, how can i transfer a photo which I picked from my android cell phone to MATLAB? How to send the results back to the phone after processing the image?
Please help. A code will be appreciated.
I think the best way to do so will be using a TCP/IP socket. I did a similar project once where I had to receive text from my RPi in MATLAB. Simply connect your Android device and the workstation with MATLAB to the same network and note the IP address of the server(the device on which you will initialize your Server. Preferably make your Android device as the server). Connect the client to the server and read/write the data. Here is an example of what I did. I initialized MATLAB as the client and my other device as Server.
t = tcpip('xxx.xxx.xxx.xx', yyyyy, 'NetworkRole', 'Client'); #Replace xxx.xxx.xxx.xx with IP address and yyyyy with the Port
#Change Client to Server if you want MATLAB to work as server
fopen(t)
dataMat = zeros(1, N);
while 1
data = fread(t); #Change fread to fwrite to transmit data
raw = char(data); ###############################
val = str2num(raw); #### This part converts the ###
val = val'; #### received ASCII values ####
l = length(val); #### to the original data #####
message = 0; ###############################
for i = 1:l
message = val(i)*(10^(l-i)) + message;
end
dataMat(count) = message;
message
end
I'm not sure how to send an image over TCP/IP, but I'm pretty sure it's possible. Click here for TCP/IP with MATLAB blog.
I haven't used Android studio so can't help with TCP/IP there, but if your app is fairly simple, you can create a TCP/IP application with MIT App Inventor(which I have worked on), so can help with that.
Cheers!

Stream video from Raspberry pi camera to Android app

I'm currently writing a project using Raspberry pi and mobile (Android).
I have problem to send data from Camera Rpi to Android App.
I'm using library Picamera on python: https://picamera.readthedocs.io/en/release-1.13/recipes1.html#recording-to-a-network-stream .
My actual code on Rpi looks something like this:
import socket
import time
import picamera
camera = picamera.PiCamera()
camera.resolution = (640, 480)
camera.framerate = 24
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8000))
server_socket.listen(0)
# Accept a single connection and make a file-like object out of it
connection = server_socket.accept()[0].makefile('wb')
try:
camera.start_recording(connection, format='h264')
camera.wait_recording(60)
camera.stop_recording()
finally:
connection.close()
server_socket.close()
To receive stream we can use: tcp/h264://x.x.x.x:8000 . It works on PC when I used vlc.
On Android I try use VideoView or ExoPlayer, but problem is with URI because, android can't parse tcp/h264 protocol.
When I try stream using vlc:
raspivid -o - -t 99999 |cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8000}' :demux=h264
It works on Android if I pass url with prefix http:// but is not from my program on python.
It seems to me that I have 2 ways.
On python use different way to stream video output.
Somehow handle protocol tcp/h264 (probably used socket and independently parse stream bytes to video). It is possible: https://github.com/ShawnBaker/RPiCameraViewer but i am looking for better (not low level) solution.
You can stream it from python easily, just use
import subprocess
subprocess.Popen("raspivid -o - -t 99999 |cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8000}' :demux=h264", shell=True)
This will launch it in a different thread, so it won't be blocking your program/

Python Bluetooth how to send a file to a phone

in my current project it is a requirement to send a file from a windows computer to an android device over bluetooth without anything on the phone other than it's standard state and of course a paired bluetooth connection. i've looked over pybluez and it seemed simple enough to send files between a client and server architecture (and in fact got it sending between my laptop and desktop rather quickly) but I cannot for the life of me find any way to get python to send a file from the computer to android once the connection is established; my attempts have been grabbing the bluetooth mac address like thing from the device like so
nearby_devices = bluetooth.discover_devices(
duration=8, lookup_names=True, flush_cache=True, lookup_class=False)
and then later trying to send the files like so
port = 1
for addr, name in nearby_devices:
bd_addr = addr
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("download-app")
sock.close()
Of course with the example script given by the pybluez documentation I can seamlessly send files between a client and a server but I am still stuck without a way to send a file to the selected android device (even if I specify it's address and know it's within range)
You're most of the way there...
As you know, you need something to talk to at the other end of your Bluetooth connection. You just need to replace your custom server with a well-known service (generally one of these options).
In my case, my phone supports the "OBEX Object Push" service, so I just need to connect to that and use a suitable client to talk the right protocol. Fortunately, the combination of PyOBEX and PyBluez does the trick here!
The following code (quickly patched together from PyOBEX and PyBluez samples) runs on my Windows 10, Python 2.7 installation and creates a simple text file on the phone.
from bluetooth import *
from PyOBEX.client import Client
import sys
addr = sys.argv[1]
print("Searching for OBEX service on %s" % addr)
service_matches = find_service(name=b'OBEX Object Push\x00', address = addr )
if len(service_matches) == 0:
print("Couldn't find the service.")
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("Connecting to \"%s\" on %s" % (name, host))
client = Client(host, port)
client.connect()
client.put("test.txt", "Hello world\n")
client.disconnect()
Looks like PyOBEX is a pretty minimal package, though, and isn't Python 3 compatible, so you may have a little porting to do if that's a requirement.
I haven't personally explored it but check out this blog -
http://recolog.blogspot.com/2013/07/transferring-files-via-bluetooth-using.html
The author uses the lightblue package as an API for the Obex protocol and send files over the connection. Now the lightblue package appears to be unmaintained. There are other packages like PyObex (which I could not import for whatever reason) which you could also explore as alternatives but lightblue seems to be the way to go.
I have made a Python 3 port of PyOBEX based on the PyOBEX code on bitbucket. I've tested so far only the client functionalities, but I expect the server to be working fine as well, since most of the compatibility issues with Python 3 were due to struct.pack/struct.unpack binary blobs appended to strings that should have all been tackled.

Stream audio commands from Android to Ubuntu and back via Bluetooth

I have a college assignment to build Android app that communicates with Ubuntu (or any other Linux distribution), and streams audio via microphone and speakers both on PC and phone. Switching the direction of communication should be done on Android and script for listening on Bluetooth port on PC should be written in Python or some other lightweight language. It does not have to be full-duplex, only single-duplex.
Is the answer in the BluetoothA2dp Android profile or is there something else?
I'm common with making simple Android apps.
Thanks a lot!
Not sure if you still need the answer, but I am working on something similar.
Basically working with python on windows platform to record streaming audio from microphone of laptop then process the sound for ANC [ automatic noise cancellation ] and pass it through band-pass filter then output the audio stream to a Bluetooth device.
I would like to ultimately port this to smartphone, but for now prototyping with Python as that's lot easier.
While I am still early stage on the project, here are two piceses that may be helpful,
1) Stream Audio from microphone to speakers using sounddevice
Record external Audio and play back
Refer to soundaudio module installation details from here
http://python-sounddevice.readthedocs.org/en/0.3.1/
import sounddevice as sd
duration = 5 # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2, dtype='float64')
print "Recording Audio"
sd.wait()
print "Audio recording complete , Play Audio"
sd.play(myrecording, fs)
sd.wait()
print "Play Audio Complete"
2)Communicate to bluetooth
Refer to details from here:
https://people.csail.mit.edu/albert/bluez-intro/c212.html
import bluetooth
target_name = "My Phone"
target_address = None
nearby_devices = bluetooth.discover_devices()
for bdaddr in nearby_devices:
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print "found target bluetooth device with address ", target_address
else:
print "could not find target bluetooth device nearby"
I know I am simply quoting examples from these sites, You may refer to these sites to gain more insight.
Once I have a working prototype I will try to post it here in future.

sl4a, problems with reading (bluetooth)

I am trying to write a script, which will connect a device (radiomodem), that have a bluetooth with my Nexus 7 (Android 4.4)
The task is to send a command via bluetooth, and then get an answer from radiomodem.
After sending a command I don't get an answer from device (or I get it, but cannot read bluetooth buffer), and my script stops while reading. It doesn't send me any mistakes, just stops.
I've tried to send commands from Nexus to PC, and I've seen them in virtual COM on PC.
I've tried to send from PC to Nexus, and from radiomodem to Nexus long lines and read them. it was fine, too.
But writing-reading doesn't work. What I'm doing wrong?
Here is my code:
import sl4a
import time
droid = sl4a.Android()
uuid = '00001101-0000-1000-8000-00805F9B34FB'
adr = '6B:E2:00:DA:18:01'
droid.toggleBluetoothState(True) # connection is always successful
droid.bluetoothConnect(uuid,adr)
time.sleep(2)
i = 0
while i < 3:
res = droid.dialogGetInput().result
res = res + '\r'
droid.bluetoothWrite(res)
time.sleep(0.6) # here I've tried different timeouts
ans = droid.bluetoothRead(4096).result
if ans is None:
print('no answer')
else:
w = str(ans)
droid.dialogCreateAlert("+", w)
droid.dialogSetPositiveButtonText('OK')
i += 1
Half a year ago I did a small app with PY4A and I had trouble with connecting to my Bluetooth heart rate monitor from a galaxy S2. To solve the connection issue's I switched to pybluez. Using the Bluetooth device worked from there. See the working example that helped me here.
http://cuu508.wordpress.com/2011/02/21/hxm-t-display-heart-rate-from-zephyrs-hxm/
I hope this helps.
Regards.

Categories

Resources