Monday, October 11, 2010

Crash recorder using android scripting and python

I have been working on a demonstrator script to determine the
feasibility of using an android phone as a vehicle crash recording
system.  I have been using SL4A and python (see the attached file) but
the final program would need integration into the Google sat nav app.
Due to the slow speed at which sensor events are recovered from the
event buffer I can't get much further with it at the moment.

# Crash recorder, John Karwatzki September 2010
# When the android phone is used in sat nav mode the
# camera will be facing the front of the vehicle.
# This python script is the first attempt at using the
# z force sensor to detect a crash. The camera provides
# video recording before, during and after the crash
# It will be developed further in the future
import android
import shutil
import os
import time
droid = android.Android()
crash = False
g = 12 # sensitivity value for g-force
# We need to use an available 'rw' directory in ram to
# avoid excessive writing to the sd card
s = 'app-cache/'
# (It would be preferable for SL4A to provide a temporary
# directory for scripting use)
# temporary video files (2 required for continuous recording)
v1 = 'vid1.mp4'
v2 = 'vid2.mp4'
# crash event files
v3 = 'before.mp4'
v4 = 'crash.mp4'
v5 = 'after.mp4'
# take initial setup video
droid.recorderCaptureVideo(s + v2, 3, True)
droid.recorderStop()  # required when recorder is restarted
os.chmod(s + v2, 0777) # allow rw access
v = 0 # swap recordings from one file to the other
# We will need a better way to stop the script in the
# final version. At the moment just shake the phone.
while crash is False:
  if v is 0:
    vid = s + v1
    v = 1
    vid = s + v2
    v = 0
  # Take 10 second video
  #  We need to disable output to the screen in the final version
  droid.startSensing(5) # Put sensor data into the event buffer
  droid.recorderCaptureVideo(vid, 10, True)
  os.chmod(vid, 0777)
  # check the event buffer
  e = droid.receiveEvent()
  for count in range(1, 10):  # Ignore the first 10 (no data)
    e = droid.receiveEvent()
  while e.result is not None:
    e = droid.receiveEvent() # We don't need all these
    e = droid.receiveEvent() # but we have to pull them
    e = droid.receiveEvent() # out regardless. This takes
    e = droid.receiveEvent() # 4 to 5 seconds for 300
      # events which is too slow for real time!
    if e.result is not None: # Check for end of event buffer
      z = e.result['data']['zforce'] # Check for crash
      if z > g:
        crash = True
# take video after crash
droid.recorderCaptureVideo(s + v5, 5, True)
os.chmod(s + v5, 0777)
if v is 0:
  os.rename(s + v2, s + v4) # Rename videos to Before, During
  os.rename(s + v1, s + v3) # and After
  os.rename(s + v1, s + v4)
  os.rename(s + v2, s + v3)
# Save videos on sd card (or Flickr.upload if it
# becomes available on SL4A)
shutil.copyfile(s + v3, '/sdcard/' + v3)
shutil.copyfile(s + v4, '/sdcard/' + v4)
shutil.copyfile(s + v5, '/sdcard/' + v5)

1 comment:

  1. Hi JohnK!

    startSensing is deprecated. Have a other simular?