User:MHohmann/RPiCam
Jump to navigation
Jump to search
The following python script allows taking one picture per second on a Raspberry Pi with camera module. I tested it with Raspbian, PiCamera and gpsd must be installed.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import picamera
import gps
import math
import time
from datetime import datetime
from dateutil import parser
def wait():
global cam, gpsd
while True:
report = gpsd.next()
print report
# Wait for position information.
if report['class'] == 'TPV':
# Set orientation to normal landscape.
cam.exif_tags['IFD0.Orientation'] = '1'
# Set picture date and time to GPS values.
now = parser.parse(report.get('time', datetime.now().isoformat()))
print now.strftime('%s')
cam.exif_tags['EXIF.DateTimeOriginal'] = now.strftime('%Y:%m:%d %H:%M:%S')
# Set altitude to GPS value.
alt = report.get('alt', 0.0)
print alt
cam.exif_tags['GPS.GPSAltitudeRef'] = '0' if alt > 0 else '1'
aalt = math.fabs(alt)
cam.exif_tags['GPS.GPSAltitude'] = '%d/100' % int(100 * aalt)
# Convert speed from m/s to km/h and set tag.
speed = report.get('speed', 0.0)
print speed
cam.exif_tags['GPS.GPSSpeedRef'] = 'K'
cam.exif_tags['GPS.GPSSpeed'] = '%d/1000' % int(3600 * speed)
# Set direction of motion and direction along which the picture is taken (assuming frontal view).
track = report.get('track', 0.0)
print track
cam.exif_tags['GPS.GPSTrackRef'] = 'T'
cam.exif_tags['GPS.GPSTrack'] = '%d/10' % int(10 * track)
cam.exif_tags['GPS.GPSImgDirectionRef'] = 'T'
cam.exif_tags['GPS.GPSImgDirection'] = '%d/10' % int(10 * track)
# Set GPS latitude.
lat = report.get('lat', 0.0)
print lat
cam.exif_tags['GPS.GPSLatitudeRef'] = 'N' if lat > 0 else 'S'
alat = math.fabs(lat)
dlat = int(alat)
mlat = int(60 * (alat - dlat))
slat = int(6000 * (60 * (alat - dlat) - mlat))
cam.exif_tags['GPS.GPSLatitude'] = '%d/1,%d/1,%d/100' % (dlat, mlat, slat)
# Set GPS longitude.
lon = report.get('lon', 0.0)
print lon
cam.exif_tags['GPS.GPSLongitudeRef'] = 'E' if lon > 0 else 'W'
alon = math.fabs(lon)
dlon = int(alon)
mlon = int(60 * (alon - dlon))
slon = int(6000 * (60 * (alon - dlon) - mlon))
cam.exif_tags['GPS.GPSLongitude'] = '%d/1,%d/1,%d/100' % (dlon, mlon, slon)
# Provide next image file name.
yield '/media/data/pictures/' + now.strftime('%s') + '.jpg'
# Initialize camera.
cam = picamera.PiCamera()
# Flip image if camera is mounted 180° rotated.
cam.vflip = True
cam.hflip = True
# Set maximum image resolution.
cam.resolution = (2592, 1944)
# Wait for camera to settle.
time.sleep(2)
# Connect to gpsd.
gpsd = gps.gps(mode=gps.WATCH_ENABLE)
try:
# Start taking pictures.
cam.capture_sequence(wait(), burst=True)
except(KeyboardInterrupt, SystemExit):
print "Exit.\n"
finally:
# Close camera.
cam.close()