def updateImg( self, strMessage, strFilenameImg = None, strLegend = None ): "add an image and a sentence to a blog" "strFilenameImg: if None, take a picture using camera" "strLegend: if different of None or strLegend, add a legend below the image" if( strFilenameImg == None ): print( "INF: Blog.updateImg: Taking picture...\n" ); camToolBox = naoqitools.myGetProxy( "ALVisionToolbox" ); camToolBox.halfPress(); strFilenameImg = "/home/nao/" + filetools.getFilenameFromTime(); camToolBox.post.takePictureRegularly( 5.0, strFilenameImg, True, "jpg", 2 ); # 2=> VGA time.sleep( 2.2 ); # 1 second is not sufficient ! camToolBox.stopTPR( strFilenameImg, "jpg" ); strFilenameImg = strFilenameImg + '.jpg'; # coz takePictureRegularly add '.jpg' automatically print( "INF: Blog.updateImg: Taking picture, file goes to '%s'\n" % strFilenameImg ); strExt = os.path.basename( strFilenameImg ); strExt = strExt.split( '.' ); strExt = strExt.pop(); strFilename = filetools.getFilenameFromTime() + '.' + strExt; strDest = self.strImgFolderAbs + strFilename; filetools.copyFile( strFilenameImg, strDest ); strTxt = "<table cellpadding=2><tr><td>%s<br></td></tr><tr><td><IMG SRC='%s' HEIGHT=192></td></tr>" % ( strMessage, self.strImgFolderLocal + strFilename ); if( strLegend != None and strLegend != "" ): strTxt += "<tr><td><font size=-2><center>- %s -</center></font></td></tr>" % strLegend; strTxt += "</table>"; self.updateBlog( strTxt );
def updateImg(self, strMessage, strFilenameImg=None, strLegend=None): "add an image and a sentence to a blog" "strFilenameImg: if None, take a picture using camera" "strLegend: if different of None or strLegend, add a legend below the image" if (strFilenameImg == None): print("INF: Blog.updateImg: Taking picture...\n") camToolBox = naoqitools.myGetProxy("ALVisionToolbox") camToolBox.halfPress() strFilenameImg = "/home/nao/" + filetools.getFilenameFromTime() camToolBox.post.takePictureRegularly(5.0, strFilenameImg, True, "jpg", 2) # 2=> VGA time.sleep(2.2) # 1 second is not sufficient ! camToolBox.stopTPR(strFilenameImg, "jpg") strFilenameImg = strFilenameImg + '.jpg' # coz takePictureRegularly add '.jpg' automatically print("INF: Blog.updateImg: Taking picture, file goes to '%s'\n" % strFilenameImg) strExt = os.path.basename(strFilenameImg) strExt = strExt.split('.') strExt = strExt.pop() strFilename = filetools.getFilenameFromTime() + '.' + strExt strDest = self.strImgFolderAbs + strFilename filetools.copyFile(strFilenameImg, strDest) strTxt = "<table cellpadding=2><tr><td>%s<br></td></tr><tr><td><IMG SRC='%s' HEIGHT=192></td></tr>" % ( strMessage, self.strImgFolderLocal + strFilename) if (strLegend != None and strLegend != ""): strTxt += "<tr><td><font size=-2><center>- %s -</center></font></td></tr>" % strLegend strTxt += "</table>" self.updateBlog(strTxt)
def sayAndCacheAndLight( strTextToSay, bJustPrepare = False, bStoreToNonVolatilePath = False, nEyesColor = 0, nUseLang = -1, strUseVoice = None ): "say a cached text with light animation" "nEyesColor: 0: white, 1: blue, 2: green; 3: red, 4: romeo" "nUseLang: if different of -1: speak with a specific languages (useful, when text are already generated: doesn't need to swap languages for nothing!" "strUseVoice: if different of None or default: use specific voice" "return the length of the text in seconds, or None if impossible" print( "sayAndCacheAndLight( '%s', bJustPrepare: %s, bStoreToNonVolatilePath: %s, nEyesColor: %s, nUseLang: %s )" % ( strTextToSay, str( bJustPrepare ), str( bStoreToNonVolatilePath ), str( nEyesColor ), str( nUseLang ) ) ); if( not config.bPrecomputeText ): print( "sayAndCacheAndLight: disabled by configuration: bPrecomputeText is false" ); if( bJustPrepare ): return None; # do nothing tts = naoqitools.myGetProxy( "ALTextToSpeech" ); tts.say( strTextToSay ); return None; global global_mutexSayAndCache; while( global_mutexSayAndCache.testandset() == False ): print( "sayAndCacheAndLight: locked, waiting" ); time.sleep( 0.1 ); if( strUseVoice == "default" ): strUseVoice = None; rLength = sayAndCache_internal( strTextToSay, bJustPrepare = True, bStoreToNonVolatilePath = bStoreToNonVolatilePath, bDirectPlay = False, nUseLang = nUseLang, bWaitEnd = True, bCalledFromSayAndCacheFromLight = True, strUseVoice = strUseVoice ); # we store it to disk, only if we must do it if( rLength == None ): print( "INF: sayAndCacheAndLight('%s'): sayAndCache_internal returned None" % str( strTextToSay ) ); global_mutexSayAndCache.unlock(); return; # this two lines are done too in sayAndCache... strTextToSay = assumeTextHasDefaultSettings( strTextToSay, nUseLang ); szFilename = sayAndCache_getFilename( strTextToSay, nUseLang, strUseVoice = strUseVoice ); szPathVolatile = pathtools.getVolatilePath() + "generatedvoices" + pathtools.getDirectorySeparator(); rSampleLenSec = 0.05; # szPathFilenamePeak = szPathVolatile + szFilename + ("_%5.3f.egy" % rSampleLenSec); szPathFilenamePeak = szFilename + ("_%5.3f.egy" % rSampleLenSec); szPathFilenamePeakCache = pathtools.getCachePath() + "generatedvoices" + pathtools.getDirectorySeparator() + szPathFilenamePeak; szPathFilenamePeak = szPathVolatile + szPathFilenamePeak; anLedsColorSequency = []; aBufFile = ""; bFileGenerated = False; if( not filetools.isFileExists( szPathFilenamePeak ) ): if( filetools.isFileExists( szPathFilenamePeakCache ) ): filetools.copyFile( szPathFilenamePeakCache, szPathFilenamePeak ); if( not filetools.isFileExists( szPathFilenamePeak ) ): # generate peak file timeBegin = time.time(); print( "sayAndCacheAndLight: generating peak light - begin\n" ); szPathFilename = szPathVolatile + szFilename + ".raw"; anLedsColorSequency = []; try: une = naoqitools.myGetProxy( 'UsageNoiseExtractor' ); anLedsColorSequency = une.analyseSpeakSound( szPathFilename, int( rSampleLenSec * 1000 ), False ); except BaseException, err: print( "ERR: sayAndCacheAndLight( '%s' ): err: %s" % ( strTextToSay, str( err ) ) ); print( "ERR: sayAndCacheAndLight => trying old cpp version" ); anLedsColorSequency = sound.analyseSpeakSound( szPathFilename, rSampleLenSec * 1000 ); print( "sayAndCacheAndLight: analyseSpeakSound - end - time: %fs\n" % float( time.time() - timeBegin ) ); # print( "anLedsColorSequency: %d samples: %s\n" % ( len( anLedsColorSequency ), str( anLedsColorSequency ) ) ); print( "Writing file with %d peak samples (time: %d)\n" % ( len( anLedsColorSequency ), int( time.time() ) ) ); # struct.pack_into( "f"*len( anLedsColorSequency ), aBufFile, anLedsColorSequency[:] ); for peakValue in anLedsColorSequency: aBufFile += struct.pack( "f", peakValue ); try: file = open( szPathFilenamePeak, "wb" ); file.write( aBufFile ); except RuntimeError, err: print( "ERR: sayAndCacheAndLight( '%s' ): err: %s" % ( strTextToSay, str( err ) ) );
os.mkdir( szPathVolatile ); except BaseException, err: pass # if( not szFilename.isalnum() ): # the underscore is not an alphanumeric, but is valid there # debug.debug( "WRN: sayAndCache: some chars are not alphanumeric in filename '%s'" % szFilename ); szPathFilename = szPathVolatile + szFilename + ".raw"; bGenerate = not filetools.isFileExists( szPathFilename ); if( bGenerate ): szAlternatePathFilename = pathtools.getCachePath() + "generatedvoices" + pathtools.getDirectorySeparator() + szFilename + ".raw"; # look in a non volatile path if( filetools.isFileExists( szAlternatePathFilename ) ): debug.debug( "sayAndCache: get static precomputed text for '%s'" % ( strTextToSay ) ); filetools.copyFile( szAlternatePathFilename, szPathFilename ); bGenerate = not filetools.isFileExists( szPathFilename ); #update this variable # if alternate # if bGenerate if( bGenerate ): # generate it! debug.debug( "sayAndCache: generating '%s' to file '%s'" % ( strTextToSay, szPathFilename ) ); sayAndCache_InformProcess(); timeBegin = time.time(); tts = naoqitools.myGetProxy( "ALTextToSpeech" ); if( nUseLang != -1 ): # change the language to the wanted one setSpeakLanguage( nUseLang ); if( len( strTextToSay ) > 150 and ( not bJustPrepare or bCalledFromSayAndCacheFromLight ) ):
def sayAndCacheAndLight(strTextToSay, bJustPrepare=False, bStoreToNonVolatilePath=False, nEyesColor=0, nUseLang=-1, strUseVoice=None): "say a cached text with light animation" "nEyesColor: 0: white, 1: blue, 2: green; 3: red, 4: romeo" "nUseLang: if different of -1: speak with a specific languages (useful, when text are already generated: doesn't need to swap languages for nothing!" "strUseVoice: if different of None or default: use specific voice" "return the length of the text in seconds, or None if impossible" print( "sayAndCacheAndLight( '%s', bJustPrepare: %s, bStoreToNonVolatilePath: %s, nEyesColor: %s, nUseLang: %s )" % (strTextToSay, str(bJustPrepare), str(bStoreToNonVolatilePath), str(nEyesColor), str(nUseLang))) if (not config.bPrecomputeText): print( "sayAndCacheAndLight: disabled by configuration: bPrecomputeText is false" ) if (bJustPrepare): return None # do nothing tts = naoqitools.myGetProxy("ALTextToSpeech") tts.say(strTextToSay) return None global global_mutexSayAndCache while (global_mutexSayAndCache.testandset() == False): print("sayAndCacheAndLight: locked, waiting") time.sleep(0.1) if (strUseVoice == "default"): strUseVoice = None rLength = sayAndCache_internal( strTextToSay, bJustPrepare=True, bStoreToNonVolatilePath=bStoreToNonVolatilePath, bDirectPlay=False, nUseLang=nUseLang, bWaitEnd=True, bCalledFromSayAndCacheFromLight=True, strUseVoice=strUseVoice) # we store it to disk, only if we must do it if (rLength == None): print( "INF: sayAndCacheAndLight('%s'): sayAndCache_internal returned None" % str(strTextToSay)) global_mutexSayAndCache.unlock() return # this two lines are done too in sayAndCache... strTextToSay = assumeTextHasDefaultSettings(strTextToSay, nUseLang) szFilename = sayAndCache_getFilename(strTextToSay, nUseLang, strUseVoice=strUseVoice) szPathVolatile = pathtools.getVolatilePath( ) + "generatedvoices" + pathtools.getDirectorySeparator() rSampleLenSec = 0.05 # szPathFilenamePeak = szPathVolatile + szFilename + ("_%5.3f.egy" % rSampleLenSec); szPathFilenamePeak = szFilename + ("_%5.3f.egy" % rSampleLenSec) szPathFilenamePeakCache = pathtools.getCachePath( ) + "generatedvoices" + pathtools.getDirectorySeparator( ) + szPathFilenamePeak szPathFilenamePeak = szPathVolatile + szPathFilenamePeak anLedsColorSequency = [] aBufFile = "" bFileGenerated = False if (not filetools.isFileExists(szPathFilenamePeak)): if (filetools.isFileExists(szPathFilenamePeakCache)): filetools.copyFile(szPathFilenamePeakCache, szPathFilenamePeak) if (not filetools.isFileExists(szPathFilenamePeak)): # generate peak file timeBegin = time.time() print("sayAndCacheAndLight: generating peak light - begin\n") szPathFilename = szPathVolatile + szFilename + ".raw" anLedsColorSequency = [] try: une = naoqitools.myGetProxy('UsageNoiseExtractor') anLedsColorSequency = une.analyseSpeakSound( szPathFilename, int(rSampleLenSec * 1000), False) except BaseException, err: print("ERR: sayAndCacheAndLight( '%s' ): err: %s" % (strTextToSay, str(err))) print("ERR: sayAndCacheAndLight => trying old cpp version") anLedsColorSequency = sound.analyseSpeakSound( szPathFilename, rSampleLenSec * 1000) print("sayAndCacheAndLight: analyseSpeakSound - end - time: %fs\n" % float(time.time() - timeBegin)) # print( "anLedsColorSequency: %d samples: %s\n" % ( len( anLedsColorSequency ), str( anLedsColorSequency ) ) ); print("Writing file with %d peak samples (time: %d)\n" % (len(anLedsColorSequency), int(time.time()))) # struct.pack_into( "f"*len( anLedsColorSequency ), aBufFile, anLedsColorSequency[:] ); for peakValue in anLedsColorSequency: aBufFile += struct.pack("f", peakValue) try: file = open(szPathFilenamePeak, "wb") file.write(aBufFile) except RuntimeError, err: print("ERR: sayAndCacheAndLight( '%s' ): err: %s" % (strTextToSay, str(err)))
# if( not szFilename.isalnum() ): # the underscore is not an alphanumeric, but is valid there # debug.debug( "WRN: sayAndCache: some chars are not alphanumeric in filename '%s'" % szFilename ); szPathFilename = szPathVolatile + szFilename + ".raw" bGenerate = not filetools.isFileExists(szPathFilename) if (bGenerate): szAlternatePathFilename = pathtools.getCachePath( ) + "generatedvoices" + pathtools.getDirectorySeparator( ) + szFilename + ".raw" # look in a non volatile path if (filetools.isFileExists(szAlternatePathFilename)): debug.debug("sayAndCache: get static precomputed text for '%s'" % (strTextToSay)) filetools.copyFile(szAlternatePathFilename, szPathFilename) bGenerate = not filetools.isFileExists(szPathFilename) #update this variable # if alternate # if bGenerate if (bGenerate): # generate it! debug.debug("sayAndCache: generating '%s' to file '%s'" % (strTextToSay, szPathFilename)) sayAndCache_InformProcess() timeBegin = time.time() tts = naoqitools.myGetProxy("ALTextToSpeech") if (nUseLang != -1): # change the language to the wanted one