Dynamic webcam control using (py)ephem

Webcam control using Python

What you need is the pygame module, which can be found in the wheezy repo. The installation is straightforward:

$ sudo apt-get install python-pygame

This module provides some basic functionality for webcams and is capable to grab jpg images. It doesn’t support much of the webcam control settings, except for brightness and image orientation. Therefore it is necessary to use a system tool, such as uvcdynctrl in order to change exposure, gain and brightness. To be able to call uvcdynctrl within python, the os module must be imported.

Change webcam settings according to time of day

To dynamically change the exposure settings according to time of day and seasons, I use the ephem module in order to calculate the time for sunrise and sunset. It is not present in the wheezy repo, therefore it must be either installed manually or using pip.

$ pip install pyephem

For calculation of sunrise/sunset, you need to know your geographical position (latitude and longitude). It doesn’t need to be very precise. You may also set your position using the ephem.city() function (consult the pyephem tutorial how to do that).

To let the script decide between day, night and twilight settings, it checks whether the next sunset or sunrise will be closer in time. Twenty minutes before and after sunrise/sunset the twilight preset will be used.

# Import some modules
import os
import pygame
import datetime
import time
import ephem
import pygame.camera

# Set location, latitude and longitude for sunset/sunrise calculation
Berlin=ephem.Observer()
Berlin.lat='52'
Berlin.long='13'
Sun=ephem.Sun()
Sun.compute()

# Calculate next/previous sunset, sunrise and retrieve system time
n_rising = ephem.localtime(Berlin.next_rising(Sun))
p_rising = ephem.localtime(Berlin.previous_rising(Sun))
n_setting = ephem.localtime(Berlin.next_setting(Sun))
p_setting = ephem.localtime(Berlin.previous_setting(Sun))
now = datetime.datetime.fromtimestamp(time.time())

# Calculate time from now until next/previous sunrise/sunset in minutes
delta_n_rising = round(abs(datetime.timedelta.total_seconds(now - n_rising))/60)
delta_p_rising = round(abs(datetime.timedelta.total_seconds(now - p_rising))/60)
delta_n_setting = round(abs(datetime.timedelta.total_seconds(now - n_setting))/60)
delta_p_setting = round(abs(datetime.timedelta.total_seconds(now - p_setting))/60)

# Enable for debugging of time formats
#print(now)
#print(rising)
#print(setting)

# Initialize webcam resolution
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()

# Adjust gain and brightness according daytime
if delta_n_rising < 20 or delta_n_setting < 55 or delta_p_rising < 55 or delta_p_setting < 20:
    # Setings for TWILIGHT
    os.system('sudo uvcdynctrl --set="Exposure, Auto" 3')
    os.system('sudo uvcdynctrl --set="Exposure, Auto Priority" 1')
    os.system('sudo uvcdynctrl --set="White Balance Temperature, Auto" 1')
    os.system('sudo uvcdynctrl --set=Sharpness 128')
    #os.system('sudo uvcdynctrl --set="Exposure (Absolute)" 128')
    os.system('sudo uvcdynctrl --set=Brightness 75')
    os.system('sudo uvcdynctrl --set=Contrast 45')
    os.system('sudo uvcdynctrl --set=Saturation 30')
    #os.system('sudo uvcdynctrl --set=Gain 255')
    os.system('sudo uvcdynctrl --set="Backlight Compensation" 0')
 elif  n_rising > n_setting:
    # Settings for DAYTIME
    os.system('sudo uvcdynctrl --set="Exposure, Auto" 1')
    os.system('sudo uvcdynctrl --set="Exposure, Auto Priority" 0')
    os.system('sudo uvcdynctrl --set="White Balance Temperature, Auto" 1')
    os.system('sudo uvcdynctrl --set=Sharpness 128')
    os.system('sudo uvcdynctrl --set="Exposure (Absolute)" 7')
    os.system('sudo uvcdynctrl --set=Brightness 95')
    os.system('sudo uvcdynctrl --set=Contrast 45')
    os.system('sudo uvcdynctrl --set=Saturation 30')
    os.system('sudo uvcdynctrl --set=Gain 160')
    os.system('sudo uvcdynctrl --set="Backlight Compensation" 1')
elif n_setting > n_rising:
    # Setings for NIGHT
    os.system('sudo uvcdynctrl --set="Exposure, Auto" 1')
    os.system('sudo uvcdynctrl --set="Exposure, Auto Priority" 0')
    os.system('sudo uvcdynctrl --set="White Balance Temperature, Auto" 1')
    os.system('sudo uvcdynctrl --set=Sharpness 128')
    os.system('sudo uvcdynctrl --set="Exposure (Absolute)" 2047')
    os.system('sudo uvcdynctrl --set=Brightness 75')
    os.system('sudo uvcdynctrl --set=Contrast 25')
    os.system('sudo uvcdynctrl --set=Saturation 15')
    os.system('sudo uvcdynctrl --set=Gain 255')
    os.system('sudo uvcdynctrl --set="Backlight Compensation" 0')

# Enable for debugging of webcam settings
#controls = cam.get_controls()
#print(controls)

# Grab frame from webcam and store as jpg image, we do not take the first three frames
img = cam.get_image()
img = cam.get_image()
img = cam.get_image()
img = cam.get_image()
pygame.image.save(img,"/var/www/data/snaps/snap.jpg")

# Stop camera
cam.stop()

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments