#!/usr/bin/env python
# coding=utf-8
# trigger espeak colophon by covering the camera with a finger
# import dependencies
# sudo pip install PiCamera[array]
# sudo pip install aplay
import imutils
from imutils . video import VideoStream
from io import BytesIO
from PIL import Image
import time , sys
from time import sleep
import cv2
import datetime
from datetime import datetime
from subprocess import call
# check open cv version
print ( cv2 . __version__ )
# choose video source from camera pi
vs = VideoStream ( usePiCamera = True ) . start ( )
# let camera warm up
sleep ( 2.0 )
image = vs . read ( )
count = 0
success = True
buffer = [ ]
playing = False
while success :
# save live frame as JPEG file
cv2 . imwrite ( " check_frame.jpg " , image )
image = vs . read ( )
# open the frame
img = Image . open ( " check_frame.jpg " )
# resize the frame
img2 = img . resize ( ( 1 , 1 ) )
# get the color of pixel
color = img2 . getpixel ( ( 0 , 0 ) )
# print the color of pixel
print ( ' # {:02x} {:02x} {:02x} ' . format ( * color ) )
# create a sum of the 3 parts that constitute the value
sum = color [ 0 ] + color [ 1 ] + color [ 2 ]
# this allows the color not to be complete black, but also dark shades, by raising the sum value, we can increase the sensability
if sum < 10 :
buffer . append ( True )
else :
# the values are set to be datetime.now to prevent the triggering of the colophon while all the values are False
buffer . append ( datetime . now ( ) )
# create an array with all the values that we got, when the array is bigger than 300 values, start erasing being the first one first to go
if len ( buffer ) > 300 :
buffer . pop ( 0 )
print ( buffer )
# For calling the colophon.wav some conditions must be met.
# By the order of:
# all the values must be the same
# we need to have at least 200 values, this prevents it from starting when there is just 1 value
if ( ( len ( set ( buffer ) ) == 1 ) & ( len ( buffer ) > 200 ) & ( playing == False ) ) :
print ( " All elements in list are same " )
call ( [ " aplay " , " /home/pi/colophon/colophon.wav " ] )
# to record the espeak sentence into a .wav file ->
# -> espeak "sentence goes here" -ven+whisper -s 150 --stdout > colophon.wav
# espeak 'Iris Version 0.5 Contributors: Gill Baldwin, Simon Browne, Tancredi Di Giovanni, Paloma García, Rita Graça, Artemis Gryllaki, Pedro Sá Couto, Biyi Wen, Bohye Woo, Silvio Lorusso, Aymeric Mansoux, André Castro, Steve Rushton, Michael Murtaugh, Leslie Robbins. Produced and published by the Experimental Publishing (XPUB) program of the Piet Zwart Institute, Rotterdam, December 2018. A collaboration between the Research Department of Het Nieuwe Instituut and XPUB.
playing = True
else :
print ( " All elements in list are not same " )