#!/usr/bin/python # Network Camera Interface for the Flex Stack # This application collects jpeg images from # a network camera and generates thumbnails # which are uploaded to a host computer. The # host can then select only the best images # to download the high resolution versions of. # This demo is intended to mimic low bandwidth # telepresence environments such as the Mars # Rovers. from time import time from os import system, path import sys class RmException(Exception): pass class WgetException(Exception): pass class UploadException(Exception): pass class MvException(Exception): pass class TnException(Exception): pass LS = '/bin/ls -al' RM = '/bin/rm' WGET = '/bin/wget -q' UPLOAD = 'lsz -b --zmodem ' MOVE = '/bin/mv' JPGTN = '/bin/jpgtn -p tn- -s ' nullify = ' > /dev/null 2>&1' #imName = 'image.jpg' #imUrl = 'http://10.0.0.20/jpg/' #imRepo = '/mnt/sd/images/' imName = 'fred.jpg' imUrl = 'http://10.0.0.2/' imRepo = '/tmp/' tnSize = 80 #pixels def getImage(): cmd = WGET + ' ' + imUrl + imName + nullify success = system(cmd) if success != 0: raise WgetException, 'Failed to getImage()' def saveImage(timeOfDay='bfin-'): cmd = MOVE + ' ' + imName + ' ' + imRepo + timeOfDay + '.jpg' + nullify if path.exists(imRepo): success = system(cmd) if success != 0: raise MvException, 'Failed to saveImage()' images.append(imRepo + timeOfDay + '.jpg') else: info = imRepo + ' does not exist' raise MvException, info def mkThumbnail(timeOfDay): """ makes a thumbnail of the image that is passed in. need to coorelate to the high res image that is stored on the sd card. """ cmd = JPGTN + str(tnSize) + ' ' + imRepo + timeOfDay + '.jpg' #+ nullify success = system(cmd) if success != 0: raise TnException, 'Failed to create thumbnail (is jpgtn executable?)' def rmThumbnail(imFile): cmd = RM + ' ' + imFile + nullify suc = system(cmd) if suc != 0: raise rmFailException def rmFile(imFile): """ same function but for any file. specify the path and filename """ cmd = RM + ' ' + imFile + nullify suc = system(cmd) if suc != 0: raise rmFailException def xmitImage(imFile): """ send the thumbnail/high res image to the host via ZMODEM protocol """ if path.exists(imFile): if xmodem == True: cmd = UPLOAD + imFile #+ nullify suc = system(cmd) if suc != 0: info = 'Failed to upload image ' + imFile raise UploadException, info else: diySend(imFile) else: info = imFile + ' does not exist, cancelled upload' raise UploadException, info def diySend(imFile): """ send file using custom protocol. filename.jpg:size in bytes:data """ jpg = [] #get file size in bytes cmd = LS + ' ' + imFile + ' > temp' system(cmd) fin = open('temp', 'r') line = fin.read() fin.close() filesize = line.split()[4] imFin = open(imFile, 'rb') rawImage = imFin.read() imFin.close() parts = imFile.split('/') imFileName = parts[ len(parts)-1 ] #rouge 0D0A sneak in while printing here sys.stdout.write( '$' + imFileName + ':' + filesize + ':' + rawImage ) #for byte in rawImage: # """ # if( repr(byte)[3:5] != '' && len(repr(byte)) > 3 ): # jpg.append( repr(byte)[3:5] ) # elif ( len(repr(byte)) == 3 ): # jpg.append( repr(byte) ) # """ # jpg.append( hex(ord(byte))[2:] ) #print '$' + imFile + ':' + filesize + ':' + str.join('',jpg) def sendThumbnail(): try: now = str(time()) tnFileName = 'tn-' + now +'.jpg' getImage() saveImage(now) mkThumbnail(now) xmitImage(tnFileName) rmThumbnail(tnFileName) return tnFileName except: raise def getCmd(): cmd = raw_input('cmd>') return cmd def printHelp(): print """FlexStack Remote Camera Demo HELP - Print this menu DEBUG - Toggle debugging output TN - Capture image, generate thumbnail, and transmit thumbnail via XMODEM LG - Request full resolution image where is the name of the thumbnail """ def debug(excinfo): if debugging == True: exception = str(excinfo[0]).split('.')[1] print str(exception) + ' ' + str(excinfo[1]) ####start here#### debugging = False xmodem = False cmd = '' images = [] while cmd != 'quit': cmd = getCmd() if cmd == 'TN': try: sendThumbnail() if len(images) > 5: delme = images[5:] for oldfile in delme: rmFile(oldfile) images = images[:5] print 'ACK' except: debug(sys.exc_info()) print 'NAK' elif cmd[:2] == 'LG': try: xmitImage(imRepo + cmd[5:]) print 'ACK' except: print 'NAK' elif cmd == 'HELP': printHelp() elif cmd == 'DEBUG': debugging = not debugging if(debugging): print 'Debugging is on' else: print 'Debugging is off' elif cmd == 'XMODEM': xmodem = not xmodem if(xmodem): print 'Using XMODEM for upload' else: print 'Using diy protocol filename:size:data' elif cmd == 'CLEAN': print images if len(images) > 5: delme = images[5:] for oldfile in delme: rmFile(oldfile) images = images[:5] print images