DesignSpark Electrical Logo
  • Brought to you by
  • RS Components
  • Allied Electronics
Andrew Back

March 13, 2017 10:34

A Desktop Raspberry Pi Selfie Camera

A fun project that uses a Pi 3 plus Touch Screen display, Camera and SenseHAT.

In this post we take a look at an easy to complete project that makes use of four official Raspberry Pi products, to create a desktop “selfie” camera. One of the really great things about this is that it does not require any soldering or custom electronics. Furthermore, with great software support for the hardware used, the software application is also super simple.

Hardware

The following components were used:

  • Raspberry Pi 3 Model B
  • Raspberry Pi Touch Screen
  • Raspberry Pi Camera
  • SenseHAT
  • Power supply
  • 32GB Micro SD card

Plus a custom laser cut and folded acrylic case. Obviously, if you don’t have access to a laser cutter and acrylic folder, alternative enclosures could be utilised.

O/S and dependencies

Raspbian Jessie Lite was written out to a Micro SD card, following which the host name was changed to something more unique than simply raspberrypi, by editing the root filesystem etc/hostname and etc/hosts files. That way it would be easier to locate on the network.

The openssh-server is now disabled by default and because I prefer to log in via SSH, this was enabled by simply creating an empty file called ssh on the boot partition.

The Touch Screen was technically mounted upside down and so to rotate the display to the correct orientation, it was necessary to edit config.txt — also on the boot partition — and add the line:

lcd_rotate=2

Next the Micro SD card was inserted into the Raspberry Pi and power applied. Following which, as is customary and highly advisable, the O/S was updated by logging in and entering:

$ sudo apt get update
$ sudo apt-get dist-upgrade

Note that since we had changed the host name to selfiecam, it was possible to log in to the Pi from another Linux computer using:

$ ssh pi@selfiecam.local

Since we wanted to use the SenseHAT, Camera and framebuffer, some useful Python software was also installed via:

$ sudo apt-get install sense-hat python-picamera python-pygame

Finally, it was necessary to enable the Camera and reboot, which can be done via raspi-config:

$ sudo raspi-config

Software

First we need to import some libraries, which includes those mentioned above.

import os
import time
import pygame
from picamera import PiCamera
from sense_hat import SenseHat

We need to initialise Pygame, which will be used to display images to the framebuffer (display).

pygame.init()
screen = pygame.display.set_mode([800,480])

Next we set up the camera and since it’s physical position is horizontally flipped, we flip it back via software.

camera = PiCamera()
camera.resolution = (800, 480)
camera.hflip = True

Following which the SenseHAT is configured.

sense = SenseHat()
sense.set_rotation(0)

Next we define a function that will take a file as an argument and then use Pygame to paint the screen black, before displaying the image it contains.

def displayImage(file):
   image = pygame.image.load(file)
   imagerect = image.get_rect()

   screen.fill([0, 0, 0])
   screen.blit(image, imagerect)
   pygame.display.flip()

Then we define a function which enables video preview, displays a 3-2-1-0 countdown on the SenseHAT LED matrix, before capturing a still, disabling preview, displaying the image and finally clearing the LED matrix. The image filename contains the date and time.

def getSelfie():
   # Start camera video preview
   camera.start_preview()
   # Commence countdown
   sense.show_letter("3", text_colour=[255, 0, 0])
   time.sleep(1)
   sense.show_letter("2", text_colour=[255, 255, 0])
   time.sleep(1)
   sense.show_letter("1", text_colour=[0, 255, 0])
   time.sleep(1)
   sense.show_letter("0", text_colour=[0, 0, 255])
   # Capture image and stop preview
   filename = time.strftime("/selfies/%Y-%m-%d-%H-%M-%S.jpg")
   camera.capture(filename)
   camera.stop_preview()
   # Display image and clear SenseHAT display
   displayImage(filename)
   sense.clear()

Note that the numbers displayed on the LED matrix are different colours: 3 = red, 2 = yellow, 1 = green and 0 = blue.

So far we have just imported libraries and defined functions. Next we use the displayImage() function to update the Touch Screen display with a welcome graphic upon start-up.

displayImage("/usr/local/share/images/Welcome.png")

Finally, we enter the main loop of the program. In this we wait for an event from the SenseHAT joystick. If this was depressed, we wait until it is released and call the getSelfie() function. If it was held to the left we initiate shutdown. We simply ignore any other actions.

while True:
   # Wait for and process joystick events
   event = sense.stick.wait_for_event(emptybuffer=True)
   if event.direction == "middle" and event.action == "released":
      getSelfie()
   elif event.direction == "left" and event.action == "held":
      os.system("/sbin/shutdown -h now")
   else:
      pass

Running

The /etc/rc.local file is run at start-up and a line was added to this that disables screen blanking, followed by a second which launches the selfie camera application.

sudo sh -c "TERM=linux setterm -blank 0 >/dev/tty0"
/usr/local/bin/selfie.py &

Upon rebooting we were then presented with the welcome graphic. On pressing the joystick a live video preview was displayed and the countdown commences.

At zero a still image is captured, video preview stopped and the image displayed to the screen.

And the final result could be copied off the SD card.

 

 Simple! :o)

Andrew Back

If you would like to have a go at making this project yourself the Python script and acrylic case design files can be found on GitHub.

Andrew Back

Open source (hardware and software!) advocate, Treasurer and Director of the Free and Open Source Silicon Foundation, organiser of Wuthering Bytes technology festival and founder of the Open Source Hardware User Group.

Comments

Harry-13

March 20, 2017 08:47

Pi Day Competition

Wosch

March 16, 2017 10:07

Pi Day Competition