B
ˆê]¢@ ã @ s d dl Z d dlmZmZ d dlZd dlZd dlZd dlZd dlZd dl m
Z
d dlZd dlm
Z
d dlZd dlZd dlmZ d dlmZmZmZ d dlmZ d dlmZmZ yd d lmZmZ d
ZW n eefk
rà dZY nX d dlZd dlZd dl Z d dl!m!Z! d d
l"m#Z# e#ƒ Z"e$e"j%ƒ d*dd„Z&e"j'd
fdd„Z(d+dd„Z)d,dd„Z*d-dd„Z+d.dd„Z,d/d d!„Z-d0d"d#„Z.d1d&d'„Z/d2d(d)„Z0dS )3é N)ÚKeyÚAttr)ÚMP3)Úsleep)Ústrftime)ÚImageÚ ImageDrawÚ ImageFont)Úlistdir)ÚisfileÚjoin)ÚLEDÚButtonFT)Údatetime)Ú
Configurationc C sL t d¡}| d¡ | rd}nd}| d¡ tjd| dd}| d ¡ dS )
Nzcerebro_utils.control_displayz&Entered the control_display method ...zvcgencmd display_power 1zvcgencmd display_power 0z#Calling the display control now ...z%sT)ÚshellzDisplay control now complete!)ÚloggingÚ getLoggerÚinfoÚ
subprocessÚcall)Zenable_displayZcontrol_display_loggerZdisplay_cmdÚstatus© r úV/Users/sacholla/WorkDocs/My Projects/codecommit/project-cerebro/py_client/rpi_utils.pyÚcontrol_display\ s
r c C sj t d¡}| d¡ |r&| d¡ dS | d¡ d}d}dtj }tjd ||| |f dd
}| d¡ dS )Nzcerebro_utils.show_imagesz$Entered the switch_images method ...z'In Debug mode, so no showing of images!Tz,And now, starting up the slideshow again ...z../scripts/shutoff-display.shz!../scripts/start-picture-frame.shz%s/picframe.start.logz%s && %s "%s" > %s 2>&1 &)r z$Switching of images is now complete!)r r r ÚconfigÚ__CEREBRO_LOGS_DIR__r r )Ú media_dirZ
debug_modeZshow_images_loggerZslideshow_stop_utilZslideshow_utilZ
slideshow_logr r r r Úshow_imagesn s
r ÚJoannaúsample textú
speech.mp3c C s t d¡}| d¡ t ¡ d¡}| d¡ | ¡ }t|ƒ}|dkr†t|d ƒ}d |d |… ¡}d ||d d … ¡} d || f }
nd
| d }
dt
j|f }tj
|¡r¶td
ƒ nVtdƒ |j| dd|
d}| d¡ t|dƒ}
|
|d ¡ ¡ |
¡ | d| ¡ |S )Nzcerebro_utils.generate_audioz7Starting the generate_audio method in cerebro_utils ...Zpollyz*Now, going to synthesize_speech with pollyé
é ú é z7%s%szzz%s/%sz2
File already exists locally, not calling Polly..z&
File does not exist, making it now. Zmp3Zssml)ZVoiceIdZOutputFormatZTextTypeÚTextz@Next, call to polly completed - now to stream the audio file ...ÚwbZAudioStreamz-Finally, audio file is written and ready @ %s)r r r Úboto3ZSessionÚclientÚsplitÚlenÚintr r Z__CEREBRO_AUDIO_DIR__ÚosÚpathr ÚprintZsynthesize_speechÚopenÚwriteÚreadÚclose)Zvoice_idZspeech_textÚfilenameZgenerate_audio_loggerZpolly_clientZ
speech_tokensZ token_cntZ mid_tokenZpart_oneZpart_twoZspeech_text_with_bufferÚ file_pathÚresponseÚfiler r r Úgenerate_audio† s6
r8 Ú c C s¤ t d¡}| d¡ | d| ¡ | s4| d¡ dS |r@t|ƒ d}tjd|| f dd }|rt| d
| ¡ t|ƒ tj | ¡
¡ }|dkr–t | ¡ | d¡ dS )
Nzcerebro_utils.play_audioz3Starting the play_audio method in cerebro_utils ...zFile_path provided : %szNo file_path provided!Fzsudo mpg321z%s %sT)r zSleeping for %d secsz
speech.mp3zdone here. audio played ?)r r r Úerrorr r r r- r. ÚbasenameÚstripÚremove)r5 Z
delay_secsZdelay_to_startZplay_audio_loggerZmp3_utilr r4 r r r Ú
play_audio s*
r> c C sŒ t d¡}| d¡ t d¡}| d| ¡ t| dƒ}|jd| ¡ idgd}W d Q R X | d ¡ | |¡ d
|kr„| d¡ dS |d
S )
Nz(selfie_with_filters.get_facial_landmarksz&In the get_facial_landmarks method ...Zrekognitionz%Running Detect Faces on the image: %sÚrbÚBytesZALL)r Z
Attributesz#Completed the detect_faces API callZFaceDetailszNo Faces found!F) r r r r( r) r0 Zdetect_facesr2 r: )Ú
image_pathZget_facial_landmarks_loggerr) Úimager6 r r r Úget_facial_landmarksÜ s
"
rC ú&../assets/printing/reinvent_bkgrnd.jpgú#../assets/printing/reinventLogo.pngc C s* t d¡}| d¡ |s&| d¡ dS | s8| d¡ dS |sJ| d¡ dS | d¡ t |¡}t | ¡}t |¡}| |j¡ | |j¡ | |j¡ |jd d |jd
d f}| |tj¡ | d¡}|jd d |jd
d f} | | tj¡ t
d
ƒt
dƒf}
| |
¡ | |
tj¡}| ¡ }t
|jd d ƒt
|jd
d ƒf}
| |
¡ |
||
¡ t dd¡}t |¡}d}|
d d |
d
d f}|j|||dd |
d |
d
d f}| |¡ |
|||¡ t dd¡}t |¡}d}|
d d |
d
d f}|j|||dd dtj }| |¡ |S )Nzrpi_utils.generate_print_imagezIn generate_print_image ...zNo background image providedFzNo image selectedzNo logo providedzAll images provided correctly!r g ì?r% r" g Ð?g @@g t@gìQ¸…ë¡?gš™™™™™¹?Z
Arial_Boldé0 zCerebro: A Connected Photoboothéi éP )éÿ rI r )ÚfontÚfillÚVerdanaé zNov 16 2019 12:30:05 (PST)i é( )rI rI rI z%s/print_image.jpg)r r r r: r r0 ÚsizeÚ thumbnailÚ ANTIALIASÚrotater, ÚresizeÚcopyÚpaster Útruetyper ZDrawÚtextr Ú__CEREBRO_MEDIA_DIR__Úsave)Úimage_selectedZbkgrnd_imageZ
logo_imageZgenerate_print_image_loggerÚimage1Úimage2Z imageLogoZselfie_sizeZimage2_rotatedZ logo_sizeZbackground_sizeZbackground_imageZim3_copyZ image_boxZfntÚdZtitle_stringZ title_boxZlogo_boxZfnt2Úd2Zgenerated_print_image_pathr r r Úgenerate_print_imageð s\
$
r_ c C s¶ t d¡}| d¡ t| d}| d| ¡ |} t ¡ }| ¡ }d}x2|D ]*}| ||| d ¡ tj| ¡ krJ|}qJW | d|| f ¡ | d| ¡ |
|| d i ¡ | d
¡ d S )Nzrpi_utils.print_imagez!Attempting to run a print job ...)rZ zGenerated Print Image: %sr9 z
device-uriz8The Printer Chosen: %s , The Image Chosen is in path: %sz'Triggering the print job now for %s ...r$ zCompleted the print job!)r r r r_ ÚcupsÚ
ConnectionZgetPrintersr Z__PRINTER_TYPE__ÚlowerZ printFile)ÚfileNameZprint_image_loggerZgenerated_imageÚconnZprintersZprinter_nameÚprinterr r r Úprint_image6 s$
rf c C sÌ t d¡}| d¡ t d¡}| d¡ i }| |d<