dtmilano / AndroidViewClient

Android ViewServer and ADB client
Apache License 2.0
1.63k stars 347 forks source link

Every N seconds trigger an image to be taken? #266

Closed Nestak2 closed 2 years ago

Nestak2 commented 5 years ago

I am performing a task at a research facility and I would like to know if I can automate it easily with AndoridViewClient/culebra. The pseudo code of the task is like this:

def main_python_function():
    loop(every N seconds do):
        change the light of a light source (I already can do this)
        take an image with a real, physical Android phone using AndoridViewClient/culebra

main_python_function()

Is this possible with AndoridViewClient? Also, is it possible for the images to be taken almost instantaneously (timing is very important because of synchronisation with the light source changes)? Pointing to a particular part in the documentation or a template is highly appreciated!

dtmilano commented 5 years ago

Not sure if you want to take a photo with the camera of a real device or want the screenshot of the device. Can you please give more details?

Nestak2 commented 5 years ago

@dtmilano Thanks for the reply! I want to take a photo with the camera of a real Android device. Let me know if I should specify more! So far I have been using instead of AndroidViewClient another app called DroidCam. DroidCam lets me connect a computer to the phone camera and turns the phone camera into a virtual webcam and then I can command the computer to take photos with the phone every N seconds. But the resolution of DroidCam is very limited, so I would like to use AndoridViewClient. I have a script written in Matlab for using DroidCam, that I can attach, if this would clarify things.

dtmilano commented 5 years ago

Using a culebra generated script which I later slightly changed

#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2013-2018  Diego Torres Milano
Created on 2019-06-14 by Culebra v15.8.0
                      __    __    __    __
                     /  \  /  \  /  \  /  \ 
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \ 
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
"""

import re
import sys
import os
import time

from com.dtmilano.android.viewclient import ViewClient

TAG = 'CULEBRA'

_s = 5
_v = '--verbose' in sys.argv

kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'debug': {}, 'startviewserver': True, 'compresseddump': True}
vc = ViewClient(device, serialno, **kwargs2)

# start camera from the shortcut:
#vc.dump(window=-1)
#vc.findViewWithContentDescriptionOrRaise(u'''Camera''').touch()
#vc.sleep(_s)

# switch cameras:
#vc.dump(window=-1)
#vc.findViewWithContentDescriptionOrRaise(u'''Switch to front camera''').touch()
#vc.sleep(_s)

vc.dump(window=-1)
take_photo = vc.findViewWithContentDescriptionOrRaise(u'''Take photo''')
start = time.time()
for n in range(10):
    take_photo.touch()
end = time.time()
print 'Elapsed time: {}'.format(end - start)

on a Pixel 1 I was able to take 10 photos in

Elapsed time: 7.57594203949
Nestak2 commented 5 years ago

Thanks so much for the answer, it looks really helpfull! I was away from work for 3 weeks and now I have to finalize another project, but I will comment back as soon as I get to test your script