Python Forum
Idea of timelapse with automatic settings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Idea of timelapse with automatic settings
#1
Hey Guys,

i new to programing and have my first project in mind :)

Rasberry pi with light sensor, that automaticly changes my camera settings to a optimal exposure, captures a image and saves it at a hard drive connected to the pi.

First of all, is there something like that allready ? i dont want to reinvent the weel.

my setup hardware setup so far:
Rasberry Pi 3
ADAFRUIT TSL2561 connected via i2c https://www.adafruit.com/product/439
Sony A5100 connected via USB
3 TB USB harddrive for storing the images.

Software:
Rasbian light

gphoto2
gphoto2 --capture-image-and-download --filename "images/%Y_%m_%d_%H_%M_%S.arw"  # capture image rename it and store it...

# these 3 settings can be used to change the exposure :)

gphoto2 --set-config iso=X #values Choice: 2 100 to Choice: 14 1600
gphoto2 --set-config f-Number=X  #values 2.8 to 22
gphoto2 --set-config shutterspeed=X    #values  30s: 300/10  25s: 250/10 1/200s: 1/200 and so on
Pyhton script (thanks ryker1990) that i modified to output the mesured lux:
import smbus
import time

# Get I2C bus
bus = smbus.SMBus(1)

# TSL2561 address, 0x39(57)
# Select control register, 0x00(00) with command register, 0x80(128)
#		0x03(03)	Power ON mode
bus.write_byte_data(0x39, 0x00 | 0x80, 0x03)
# TSL2561 address, 0x39(57)
# Select timing register, 0x01(01) with command register, 0x80(128)
#		0x02(02)	Nominal integration time = 402ms
bus.write_byte_data(0x39, 0x01 | 0x80, 0x02)

time.sleep(0.5)

# Read data back from 0x0C(12) with command register, 0x80(128), 2 bytes
# ch0 LSB, ch0 MSB
data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)

# Read data back from 0x0E(14) with command register, 0x80(128), 2 bytes
# ch1 LSB, ch1 MSB
data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)

# Convert the data
ch0 = data[1] * 256 + data[0]
ch1 = data1[1] * 256 + data1[0]

# Output data to screen

print "%d lux" %(ch0 - ch1)
To do (what i think Huh hahaha)
Make a "list" of all camera settings possible from dark to bright.
Measure several brightness levels and manualy adjust the camera settings to that brightness.
Extrapolate the rest?

than write a script:

every 30 minutes:
read lux measurement
look up the correct setting in the list
set the camera to that settings
(wait: needs some time)
capture image, rename it, save it on the harddrive
write a logfile that it worked
start again :D

im a total noob, do you think its possible like that?
what would you do differently ?
any thoughts appreciated :)

Hans Tongue
Reply
#2
Hardware is set up, pi is runnig :)
[Image: img_20180524_200049-kopie-jpg.242]

gphoto2 works :)

Now i need some help with variables, i cant not make it work :/

# Camera setting script

import smbus
import time
from subprocess import call

# Get I2C bus
bus = smbus.SMBus(1)

# TSL2561 address, 0x39(57)
# Select control register, 0x00(00) with command register, 0x80(128)
#		0x03(03)	Power ON mode
bus.write_byte_data(0x39, 0x00 | 0x80, 0x03)
# TSL2561 address, 0x39(57)
# Select timing register, 0x01(01) with command register, 0x80(128)
#		0x02(02)	Nominal integration time = 402ms
bus.write_byte_data(0x39, 0x01 | 0x80, 0x02)

time.sleep(0.5)

# Read data back from 0x0C(12) with command register, 0x80(128), 2 bytes
# ch0 LSB, ch0 MSB
data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)

# Read data back from 0x0E(14) with command register, 0x80(128), 2 bytes
# ch1 LSB, ch1 MSB
data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)

# Convert the data
ch0 = data[1] * 256 + data[0]
ch1 = data1[1] * 256 + data1[0]
luxvis = (ch0 - ch1)

# Output data to screen
print "Sensor reading: %d lux" %(luxvis)

# Decide what preset to use

if luxvis <= 90:
    fiso= 1
    g_iso = 100
    g_f = 2.8
elif luxvis <= 120:
    fiso = 2
    g_iso = 200
    g_f = 10.0 
elif luxvis<= 300:
    fiso = 3
    g_iso = 100
    g_f = 2.8
elif luxvis <= 4000:
    fiso = 4
    g_iso = 1600
    g_f = 8.0
else:
    fiso= 5
    g_iso = 400
    g_f = 4.0

# Output preset to screen
print "Use iso and F setting number: %d" %(fiso)
print "iso %d" %(g_iso)
print "f   %d" %(g_f)

# Set iso and wait
call(["gphoto2","--set-config","iso=%d"]) %(g_iso)
print "setting iso and wait...."

time.sleep(6)
print "iso set!"

# set F and wait
call(["gphoto2","--set-config","F-Number=%d"]) %(g_f)
print "setting f and wait...."

time.sleep(6)
print "f set!"

# capture image, rename it and save it
print "Capturing Image"
call (["gphoto2","--capture-image-and-download","--filename","/media/pi/6CD8-2DB4/images/%Y_%m_%d_%H_%M_%S.arw"])
i have a problem getting g_iso and g_f values into the call :/
g_iso = 100
call(["gphoto2","--set-config","iso=%d"]) %(g_iso)
does not work.
call(["gphoto2","--set-config","iso=100"])
does.

also for g_f i need floating point value so i tryed to use %f

any hints? :) Thanks!
Reply
#3
g_iso = 100
call(["gphoto2","--set-config","iso=%d" % g_iso])
The format command (either as "text with %% %f" % 4.5 or "text with {{} {:f}".format(4.5)) only works with string (and bytes in python3)

If for example you want to write your g_f with 2 decimal places you can use:
    print "%.2f" % g_f
    call(["gphoto2","--set-config","F-Number=%.2f" % g_f])
And for the last line:
import os
from datetime import datetime
name = datetime.now().strftime("%Y_%m_%d_%H_%M_%S.arw")
call (["gphoto2","--capture-image-and-download","--filename", os.path.join("/media/pi/6CD8-2DB4/images", name)])
One side note, you are using python 2.7 for a new project... I rather recommend to use python3 except if there is no other option. The syntax is more uniform, easier to learn and to work with raw binary data is nicer, once you get used to the bytes type.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 273 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  Save settings frohr 3 1,493 May-14-2022, 12:38 PM
Last Post: frohr
  Matplotlib - automatic update frohr 1 1,071 Mar-29-2022, 07:05 PM
Last Post: deanhystad
  automatic create folders Mr_Kool 4 1,717 Dec-21-2021, 04:38 PM
Last Post: BashBedlam
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,726 May-18-2021, 06:42 PM
Last Post: Skaperen
  PyCharm - 'unable to save settings' Taguero 1 4,185 Jun-12-2020, 06:18 PM
Last Post: Larz60+
  Automatic registering python to registry kozaizsvemira 1 2,142 Oct-22-2019, 11:23 AM
Last Post: kozaizsvemira
  TimeLapse Help - ImportError: No module named 'sh' evvvonder 4 4,333 Jun-28-2019, 10:52 PM
Last Post: evvvonder
  Automatic redefining Hassediagram 5 3,217 Feb-25-2019, 01:36 PM
Last Post: Hassediagram
  [Automation] Mouse Settings masch1na 1 3,524 Apr-10-2018, 12:49 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020