User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Photo Booth Help

Tue Feb 04, 2020 7:08 pm

I have this code for my photo booth that I used 2 1/2 yrs ago. My Micro SD card went in my Pi 3B. Got latest Raspbian installed. When I put the code where it needs to be & fire it up in Thonny, I get errors. I was wondering if somebody could check it out & see what the problem is. It worked flawlessly before. Now, errors. It's set up to use either a keyboard or a button to start taking pictures. Thanks in advance.




Code: Select all

# Raspberry Pi - Photo Booth
#
# Copyright (c) 2014, John Croucher - www.jcroucher.com
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, 
# are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice, this 
# list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright notice, this 
# list of conditions and the following disclaimer in the documentation and/or other 
# materials provided with the distribution.
# 
# 3. Neither the name of the copyright holder nor the names of its contributors may 
# be used to endorse or promote products derived from this software without specific 
# prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
# THE POSSIBILITY OF SUCH DAMAGE.


import RPi.GPIO as GPIO
import time
from threading import Thread
import picamera
import PIL
from PIL import Image
# import cups
import os
import sys
import pygame
import random

slideshowRunning = True
basewidth = 177 # Used for merging the photos onto one
printPhoto = False
imgPath = './images/tmp'
input_state = True


# Push button for starting the photo sequence
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Display surface
pygame.init()

pygame.mouse.set_visible(0)

w = pygame.display.Info().current_w
h = pygame.display.Info().current_h

screenSize = (w, h)

screen = pygame.display.set_mode(screenSize, pygame.FULLSCREEN) # Full screen the display with no window


# Used for loading a random photo for the slideshow
def random_file(dir):
	files = [os.path.join(path, filename)
		for path, dirs, files in os.walk(dir)
		for filename in files]

	return random.choice(files)


def displayImage(file):
	screen.fill((0,0,0))
	img = pygame.image.load(file) 
	img = pygame.transform.scale(img,(w,h)) # Make the image full screen
	screen.blit(img,(0,0))
	pygame.display.flip() # update the display


# Display a random image
def slideshow():
	while True:

		if slideshowRunning == True:  
			checkEvents()
			randomFile = random_file('./images/')       
			displayImage(randomFile)
			time.sleep(2) # pause 

# Handle events like keypress
def checkEvents():
	global input_state
	for event in pygame.event.get():
		# Shutdown the application if quit event or escape key is pressed
		if event.type == pygame.QUIT or ( event.type is pygame.KEYDOWN and event.key == pygame.K_ESCAPE ):
			slideshowRunning = False
			pygame.quit()
			sys.exit()

		if event.type is pygame.KEYDOWN and event.key == pygame.K_f: # Switch the display mode between full screen and windowed
			if screen.get_flags() & pygame.FULLSCREEN:
				pygame.display.set_mode(screenSize)
			else:
				pygame.display.set_mode(screenSize,pygame.FULLSCREEN)
		if event.type is pygame.KEYDOWN and event.key == pygame.K_p:
			input_state = False

# On screen text message
def displayStatus(status):
	screen.fill((0,0,0))
   
	font = pygame.font.SysFont("monospace",24)
	text = font.render(status,True,(255,255,255))

	# Display in the center of the screen
	textrect = text.get_rect()
	textrect.centerx = screen.get_rect().centerx
	textrect.centery = screen.get_rect().centery
	screen.blit(text,textrect)
	pygame.display.flip() # update the display
   
def displayCnt(cnt):
	screen.fill((0,0,0))
	
	font = pygame.font.SysFont("monospace",200)
	text = font.render(cnt,True,(255,255,255))

	# Display in the center of the screen
	textrect = text.get_rect()
	textrect.centerx = screen.get_rect().centerx
	textrect.centery = screen.get_rect().centery

	screen.blit(text,textrect)
	
	pygame.display.flip() # update the display
		
   

# Merge all photos onto one ready for printing
def combineImages():
	displayStatus('Please wait. Processing Images')
         
	# Do the merging
	blankImage = Image.open('blank.jpg')

	image1 = Image.open(imgPath + '/image1.jpg')      
	image1 = image1.resize((177,140),PIL.Image.ANTIALIAS)
	blankImage.paste(image1, (0,4))

	image2 = Image.open(imgPath + '/image2.jpg')      
	image2 = image2.resize((177,140),PIL.Image.ANTIALIAS)
	blankImage.paste(image2, (0,144))

	image3 = Image.open(imgPath + '/image3.jpg')      
	image3 = image3.resize((177,140),PIL.Image.ANTIALIAS)
	blankImage.paste(image3, (177,4))

	image4 = Image.open(imgPath + '/image4.jpg')      
	image4 = image4.resize((177,140),PIL.Image.ANTIALIAS)
	blankImage.paste(image4, (177,144))

	blankImage.save(imgPath + '/combined.jpg', 'JPEG', subsampling=0, quality=100)


# Print the photo
def printPhoto():
	global input_state
	if printPhoto == True:
		displayStatus('Printing')

		conn = cups.Connection()
		printers = conn.getPrinters()
		printer_name = printers.keys()[0]
		conn.printFile(printer_name, imgPath + '/combined.jpg',"TITLE",{})

		time.sleep(2)

# Thread for the slideshow
t = Thread(target=slideshow)
t.start()

with picamera.PiCamera() as camera:
	camera.sensor_mode=2
	camera.preview_alpha=100
	camera.resolution = (3280, 2464)

	while True:
      
		checkEvents() # Needed to check for keypresses and close signals

		# Putton press to start the photo sequence
		if not GPIO.input(17):
			input_state = False
			
		if input_state == False:
   
			# Stop the slideshow
			slideshowRunning = False

			# Start the camera preview
			camera.start_preview()

			# Make the destination path for the photos
			if not os.path.exists(imgPath):
				os.mkdir(imgPath)

			# Loop through the 4 photo taking sequences
			for pNum in range (1,5):
				camera.annotate_text = 'Photo ' + str(pNum) + ' of 4'
				# time.sleep(1)

				for countDown in range (4,0,-1):
					displayCnt(str(countDown))
					pygame.display.flip() # update the display
					# camera.annotate_text = str(countDown)
					time.sleep(1)
				displayCnt("")
				camera.annotate_text = ''
				camera.capture( imgPath + '/image' + str(pNum) + '.jpg')
				time.sleep(1)
         
			# Stop the camera preview so we can return to the pygame surface
			camera.stop_preview()
			combineImages()
			printPhoto()
			displayImage( imgPath + '/combined.jpg' ) # Display a preview of the combined image
			time.sleep(5)

			# Move the temp files to a new dir based on the current timestamp so they can be retrieved later
			os.rename(imgPath, './images/' + str(int(time.time())))

			# Restart the slideshow
			slideshowRunning = True

			input_state = True   
         

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Tue Feb 04, 2020 7:18 pm

Here is the error when I run the Photo Booth

Python 3.7.3 (/usr/bin/python3)
>>> %Run PiPhotoBooth2.py
pygame 1.9.4.post1
Hello from the pygame community. https://www.pygame.org/contribute.html
mmal: mmal_vc_port_enable: failed to enable port vc.ril.image_encode:out:0(JPEG): ENOMEM
mmal: mmal_port_enable: failed to enable port vc.ril.image_encode:out:0(JPEG)(0x20b7630) (ENOMEM)

Backend terminated or disconnected.Fatal Python error: Segmentation fault

Thread 0x71eb3460 (most recent call first):
File "/home/pi/PiPhotoBooth2.py", line 90 in slideshow
File "/usr/lib/python3.7/threading.py", line 865 in run
File "/usr/lib/python3.7/threading.py", line 917 in _bootstrap_inner
File "/usr/lib/python3.7/threading.py", line 885 in _bootstrap

Current thread 0x76f47ad0 (most recent call first):
File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 1331 in _get_framesize
File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 1325 in __repr__
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 864 in export_value
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 880 in export_variables
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 927 in _export_stack
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 1027 in _prepare_user_exception
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 1218 in wrapper
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 1259 in execute_source
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 815 in _execute_source
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 801 in _execute_file
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 403 in _cmd_Run
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 204 in handle_command
File "/usr/lib/python3/dist-packages/thonny/backend.py", line 146 in mainloop
File "/usr/lib/python3/dist-packages/thonny/backend_launcher.py", line 87 in <module> Use 'Stop/Restart' to restart.

User avatar
B.Goode
Posts: 10890
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Photo Booth Help

Tue Feb 04, 2020 7:47 pm

If it is fully developed previously working code, why do you need to run it under the control of an Editor/IDE?

Maybe it is Thonny - which may not have been available in 2014 when the code was written - that is getting in the way of the graphical output?

What happens if you simply invoke the script from the shell (command line) prompt -

Code: Select all

python3  PiPhotoBooth2.py 

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Tue Feb 04, 2020 7:57 pm

Gave it a try.
Traceback (most recent call last):
File "PiPhotoBooth2.py", line 33, in <module>
import RPi, GPIO as GPIO
ModuleNotFoundError: No module named "RPi"

I have no idea what that means. I have little no zero experience with this.

gordon77
Posts: 5375
Joined: Sun Aug 05, 2012 3:12 pm

Re: Photo Booth Help

Tue Feb 04, 2020 8:00 pm

import RPi.GPIO as GPIO

. Not a ,

User avatar
B.Goode
Posts: 10890
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Photo Booth Help

Tue Feb 04, 2020 8:01 pm

It should be RPi.GPIO

That is what the code you posted earlier says...

What has changed, and why?

User avatar
neilgl
Posts: 2605
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Photo Booth Help

Tue Feb 04, 2020 8:02 pm

Ah it is

Code: Select all

import RPi.GPIO as GPIO
, you can copy and paste this.

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Tue Feb 04, 2020 8:10 pm

I checked the code & that's what it says

import RPi.GPIO as GPIO

I have no idea as to why it's getting changed.

User avatar
B.Goode
Posts: 10890
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Photo Booth Help

Tue Feb 04, 2020 8:15 pm

RottieGirl wrote:
Tue Feb 04, 2020 8:10 pm
I checked the code & that's what it says

import RPi.GPIO as GPIO

I have no idea as to why it's getting changed.

That code will not create the error you have reported. Are there multiple versions of this script with similar names that might be confusing you?

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Tue Feb 04, 2020 8:31 pm

That was a typing error. My bad. Fingers don't want to work sometimes. I'm on my desktop, with my RPi beside me.

Traceback (most recent call last):
File "PiPhotoBooth2.py", line 33, in <module>
import RPi.GPIO as GPIO
ModuleNotFoundError: No module named 'RPi'

User avatar
B.Goode
Posts: 10890
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Photo Booth Help

Tue Feb 04, 2020 8:38 pm

What Operating System is running on your RPi board, and when was it installed?

Because the RPi.GPIO library has been a standard part of the Raspbian Operating System for years.

Starting afresh with an up to date Operating System might be more productive than trying to patch up something ancient...

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Tue Feb 04, 2020 8:40 pm

Raspbian installed on Dec 24, 2019. Whatever version it was at the time. I have done an update since then.

gordon77
Posts: 5375
Joined: Sun Aug 05, 2012 3:12 pm

Re: Photo Booth Help

Tue Feb 04, 2020 8:52 pm

Have you saved a script called RPi.py ?

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Tue Feb 04, 2020 9:01 pm

No. They are, both the same, PiPhotoBooth2.py and PiPhotoBooth, Button and Keyboard.py

User avatar
B.Goode
Posts: 10890
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Photo Booth Help

Tue Feb 04, 2020 9:11 pm

I thought we were helping you debug a single script posted in full in the opening post of the thread?

Please explain the full situation.

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Tue Feb 04, 2020 9:18 pm

Both are the same. Identical. They're named so I knew what was what. I copied from PiPhotoBooth2 one. I'm trying to get to work, like it did previously. So, I'm currently working with the PiPhotoBooth2 on my RPi. If it can be fixed, that would be wonderful. I can go about renaming it after.

User avatar
B.Goode
Posts: 10890
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Photo Booth Help

Wed Feb 05, 2020 1:40 pm

On a test system based on an Rpi3B+ board running the September 2019 release of Raspbian Buster the python script published in the first post in this thread runs under both the Python and Python3 interpreters.

I had to make the following changes to get the script to do anything 'useful'.

1. Change the camera resolution, as I have only a Version 1 camera module;

2. 'Manually' create the ./images subdirectory;

3. 'Manually' create a 'blank.jpg' backdrop file.


The script seems to wait for a button press and then takes a sequence of 4 images after a 4 second countdown for each. Those images are then composited as a 2x2 sub-picture in the corner of the screen.


In summary: the script you published 'works'. There is something yet to be understood about your particular installation that is stopping it from being run correctly. Maybe install a fresh version of the Raspbian Operating System on a spare microSD card and start again from scratch?


And as an aside: the script also runs under the Thonny IDE.

hippy
Posts: 8884
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Photo Booth Help

Wed Feb 05, 2020 2:49 pm

RottieGirl wrote:
Tue Feb 04, 2020 7:18 pm
Here is the error when I run the Photo Booth

mmal: mmal_port_enable: failed to enable port vc.ril.image_encode:out:0(JPEG)(0x20b7630) (ENOMEM)

Backend terminated or disconnected.Fatal Python error: Segmentation fault
ENOMEM is an "out of memory" error. There's not enough memory available to do something and everything comes crashing down.

I have a strong feeling of deja vu that we've been through this before.

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Wed Feb 05, 2020 4:19 pm

I think what I'm going to do is get Raspbian put onto another micro sd card for my RPi 2B & give it a whirl. I'm wondering if it overheated a couple of times too many. :|

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Thu Feb 06, 2020 5:10 pm

I took the micro sd card out of my Pi 3B & put it into my Pi 2B. Fired it up. Got the same error. I'm going to have to reload the o.s. onto the micro sd card & start again.

FlirtingWithDisaster
Posts: 5
Joined: Fri Mar 30, 2018 8:36 pm

Re: Photo Booth Help

Thu Feb 06, 2020 5:57 pm

Is this the error you're referring to when running "python3 PiPhotoBooth2.py"?
Traceback (most recent call last):
File "PiPhotoBooth2.py", line 33, in <module>
import RPi.GPIO as GPIO
ModuleNotFoundError: No module named 'RPi'
Is python3-rpi.gpio installed? Please run:

Code: Select all

dpkg -s python3-rpi.gpio | grep Status
If it is not installed, then install it with:

Code: Select all

sudo apt update
sudo apt install python3-rpi.gpio

hippy
Posts: 8884
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Photo Booth Help

Thu Feb 06, 2020 6:11 pm

Or is it the ENOMEM error as in your second post ?

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Thu Feb 06, 2020 6:29 pm

Both :?
I'm updating. Status says rpi.gpio is installed

User avatar
RottieGirl
Posts: 108
Joined: Sun Mar 05, 2017 3:36 pm
Location: Manitoba, Canada

Re: Photo Booth Help

Wed Mar 18, 2020 6:37 pm

It's been awhile, but I'm finally back trying to figure out my photo booth. I figured it was os issues, so I installed the version of Raspbian that I had in it when I did the photo booth. The memory error is now gone. There still is an issue that's causing it to freeze up. Here is the error that I'm getting.

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
self.run()
File "/usr/lib/python3.4/threading.py", line 868, in run
self._target(*self._args, **self._kwargs)
File "/home/pi/PiPhotoBooth, Button and Keyboard.py", line 93, in slideshow
displayImage(randomFile)
File "/home/pi/PiPhotoBooth, Button and Keyboard.py", line 80, in displayImage
img = pygame.image.load(file)
pygame.error: Unsupported image format

I have no idea what this means. Image being used is a jpg file

hippy
Posts: 8884
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Photo Booth Help

Wed Mar 18, 2020 7:09 pm

You can put a print(file) command before line 80 to confirm which file it is trying to show, and then you can use the 'file' command at the command line to provide some information about that file, for example -

Code: Select all

pi@Pi3B:~ $ file agent.jpg
agent.jpg: JPEG image data, JFIF standard 1.01, aspect ratio,
density 1x1, segment length 16, baseline, precision 8, 1920x1080,
components 3
If that doesn't reveal enough information as to why PyGame is having problems loading it there may be other tools which can provide more information on the image file.

Return to “Python”