[1] OpenCV Libraries. Retrieved from
http://opencv.org/
[2] Pygame Libraries. Retrieved from
http://pygame.org/hifi.html
[3] FFmpeg Libraries. Retrieved from
https://www.ffmpeg.org/
[4] NumPy Libraries. Retrieved from
http://www.numpy.org/
[5] 2014, July 7. Face Recognition With Python, in Under 25 Lines of Code. Retrieved from
https://realpython.com/blog/python/face-recognition-with-python/
[6] Adrian Rosebrock (2015, February 23). Install OpenCV and Python on your Raspberry Pi 2 and B+. Retrieved from
http://www.pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/
[7] Zach Smith (2007, August 21). How do I... Convert images to grayscale and sepia tone using C#. Retrieved from
http://www.techrepublic.com/blog/how-do-i/how-do-i-convert-images-to-grayscale-and-sepia-tone-using-c/
####################################################### #Team Member:Yu-Chieh Fu(yf277), Feng-Ming Chang(fc296) #Embedded OS Final Project : Smart Camera System #Date: 11/19/2015 ####################################################### #####################Library Import##################### import cv2 import sys sys.path.append('/usr/lib/python2.7/dist-packages') sys.path.append('/usr/local/lib/python2.7/dist-packages') #import picamera from time import sleep import pygame from pygame.locals import * import os import numpy as np #####################System Argument##################### #sys.argv[1] #sys.argv[2] #####################Parameter Definition##################### #piTFT screen size param_ScreenWidth = 320 param_ScreenHeight = 240 #piCamera capture size param_ImageWidth = 640 param_ImageHeight = 480 #usual Colours define BLACK = (0, 0, 0) WHITE = (255, 255, 255) BLUE = (0, 0, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) #histogram blobk size block_size = 5 #####################Initial Setup##################### os.putenv('SDL_FBDEV', '/dev/fb1') #use piTFT as display #os.system('sudo modprobe bcm2835-v4l2') os.putenv('SDL_MOUSEDRV', 'TSLIB') os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen') #####################Class Definition##################### class SmartCamera: def __init__(self): #Screen Size self.ScreenWidth = param_ScreenWidth self.ScreenHeight = param_ScreenHeight self.screenimg = (self.ScreenWidth, self.ScreenHeight) #Camera Captured Image Size self.ImageWidth = param_ImageWidth self.ImageHeight = param_ImageHeight self.frame = (self.ImageWidth, self.ImageHeight) self.processframe = (self.ImageWidth, self.ImageHeight) self.frame_downsample = (self.ScreenWidth, self.ScreenHeight) #cascade classifier self.cascPath = "./haarcascade_frontalface_default.xml" self.faceCascade = cv2.CascadeClassifier(self.cascPath) #Camera Setting self.camera = cv2.VideoCapture(0) self.camera.set(3, self.ImageWidth) self.camera.set(4, self.ImageHeight) #Screen Setting self.screen = pygame.display.set_mode((self.ScreenWidth,self.ScreenHeight)) #Button Setting self.iconDictionary = {'camera':'src/camera.jpg','album':'src/album.jpg','video':'src/video.jpg', 'garbage':'src/garbage.jpg', 'snsd':'src/snsd.jpg','save':'src/save.jpg','videoplay':'src/videoplay.jpg','play':'src/play.png', 'quit':'src/quit.png', 'stop':'src/stop.png', 'left':'src/left.png', 'right':'src/right.png', 'surveilliance':'src/surveilliance.jpg', 'pencil':'src/pencil.jpg', 'vintage':'src/vintage.jpg'} #photo system self._mainState = 0 # 0:free viewing 1:take photo 2:preview self._photoPtr = 1 # record the index of saved image self.photoPath = './photo/' self.photoFormat = '.jpg' self._photo_list = [] self._albumPageIndex = 0 # also for video album self._selectedPhoto = None self._isPhotoAlbum = True # False for VideoAlbum #video system self._videoPtr = 1 self.videoPath = './video/' self.videoFormat = '.avi' self._video_list = [] self._isRecording = False self._filteringOn = False self.fourcc = cv2.cv.CV_FOURCC(*'XVID') self.videoOut = None self.videoFPS = 20.0 self._selectedVideo = None self.videoPlayer = None #for video filtering self.BgMBSize = 16 self.BgArray = np.zeros((int(self.ScreenHeight/self.BgMBSize), int(self.ScreenWidth/self.BgMBSize)), np.float32) self.BgRefreshFlag = True self.BgUpdateRatio = 0.2 self.BgColorDiffTh = 35 self.BgDiffCountTh = 0.125*(self.ScreenHeight/self.BgMBSize)*(self.ScreenWidth/self.BgMBSize) self.BgRecordFrameIdx = 60 self.BgRecordCycle = 60 def face_detection(self, image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = self.faceCascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags = cv2.cv.CV_HAAR_SCALE_IMAGE ) for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) return image def cvimage_to_pygame(self, image): #Convert cvimage into a pygame image image2 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) return pygame.image.frombuffer(image2.tostring(), image2.shape[1::-1], "RGB") def silentRemove(self, filename): try: os.remove(filename) except OSError: pass def DrawButton(self, name, x, y): tmp = pygame.image.load(self.iconDictionary[name]).convert() tmp.set_alpha(75) self.screen.blit(tmp, (x, y)) def DrawNewFrame(self): retval, self.frame = self.camera.read() if retval == False: print 'No video frame captured!' else: self.frame_downsample = cv2.resize(self.frame, (0,0), fx=0.5, fy=0.5 , interpolation = cv2.INTER_CUBIC) #self.processframe = self.face_detection(self.frame_downsample) self.screenimg = self.cvimage_to_pygame(self.frame_downsample) self.screen.blit(self.screenimg, (0, 0)) def TakePhoto(self): retval, self.frame = self.camera.read() if retval == False: print 'No video frame captured!' else: self.frame_downsample = cv2.resize(self.frame, (0,0), fx=0.5, fy=0.5 , interpolation = cv2.INTER_CUBIC) self.screenimg = self.cvimage_to_pygame(self.frame_downsample) filename = self.photoPath + str(self._photoPtr) + self.photoFormat cv2.imwrite(filename, self.frame) self._photoPtr = self._photoPtr + 1 self._photo_list.append(filename) def DrawShotFrame(self): self.screen.blit(self.screenimg, (0, 0)) def RemoveShotFrame(self): filename = self.photoPath + str(self._photoPtr-1) + self.photoFormat self.silentRemove(filename) self._photo_list.remove(filename) self.EnterPhotoAlbumMode() def VintageProcessing(self): inputBlue,inputGreen,inputRed = cv2.split(self.frame) outputRed = (inputRed * .393) + (inputGreen *.769) + (inputBlue * .189) outputGreen = (inputRed * .349) + (inputGreen *.686) + (inputBlue * .168) outputBlue = (inputRed * .272) + (inputGreen *.534) + (inputBlue * .131) temp = cv2.merge([outputBlue, outputGreen, outputRed]) filename = self.photoPath + str(self._photoPtr) + self.photoFormat cv2.imwrite(filename, temp) self.processframe = cv2.imread(filename) self.frame_downsample = cv2.resize(self.processframe, (0,0), fx=0.5, fy=0.5 , interpolation = cv2.INTER_CUBIC) self.screenimg = self.cvimage_to_pygame(self.frame_downsample) self._photoPtr = self._photoPtr + 1 self._photo_list.append(filename) def HistogramProcessing(self): Y = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY) DividY = cv2.cv.CreateMat(self.ImageHeight, self.ImageWidth, cv2.cv.CV_8U) New = cv2.cv.CreateMat(self.ImageHeight, self.ImageWidth, cv2.cv.CV_32F) DividY = np.uint8(Y*0.125) for i in range(0, self.ImageHeight/block_size): for j in range(0, self.ImageWidth/block_size): Histogram = [0] * 256 for m in range(0, block_size): for n in range(0, block_size): a = DividY[(i*block_size+m), (j*block_size+n)] Histogram[a] = Histogram[a] + 1 Accum = [0] * 256 temp = 0 for p in range(1, 256): temp = temp + Histogram[p] Accum[p] = float(temp)/(block_size*block_size) for m in range(0, block_size): for n in range(0, block_size): New[(i*block_size+m), (j*block_size+n)] = Accum[DividY[(i*block_size+m), (j*block_size+n)]]*255.0 New_U = np.uint8(New) filename = self.photoPath + str(self._photoPtr) + self.photoFormat cv2.imwrite(filename, New_U) self.processframe = cv2.imread(filename) self.frame_downsample = cv2.resize(self.processframe, (0,0), fx=0.5, fy=0.5 , interpolation = cv2.INTER_CUBIC) self.screenimg = self.cvimage_to_pygame(self.frame_downsample) self._photoPtr = self._photoPtr + 1 self._photo_list.append(filename) def DrawPhotoAlbumSingleFrame(self): self.screen.blit(self.screenimg, (0, 0)) def RemovePhotoAlbumSingleFrame(self): self.silentRemove(self._selectedPhoto) self._photo_list.remove(self._selectedPhoto) self._selectedPhoto = None self.EnterPhotoAlbumMode() def DrawPhotoPreview(self): self.screen.fill(BLACK) photoNumber = 1 # 1:UpLeft, 2:UpRight, 3:BottomLeft, 4:BottomRight while (self._albumPageIndex*4 + photoNumber) <= len(self._photo_list): filename = self._photo_list[(self._albumPageIndex*4 + photoNumber - 1)] self.frame = cv2.imread(filename) quarterFrame = cv2.resize(self.frame, (0,0), fx=0.25, fy=0.25 , interpolation = cv2.INTER_CUBIC) quarterScreenImg = self.cvimage_to_pygame(quarterFrame) if photoNumber == 1:#UpLeft self.screen.blit(quarterScreenImg, (0, 0)) elif photoNumber == 2:#UpRight self.screen.blit(quarterScreenImg, (160, 0)) elif photoNumber == 3:#BottomLeft self.screen.blit(quarterScreenImg, (0, 120)) else:#BottomRight self.screen.blit(quarterScreenImg, (160, 120)) if photoNumber == 4: break else: photoNumber = photoNumber + 1 def AddAlbumPageIdex(self): # photo & video album share this function if self._isPhotoAlbum == True: if len(self._photo_list) > ((self._albumPageIndex + 1) * 4): self._albumPageIndex = self._albumPageIndex + 1 else: if len(self._video_list) > ((self._albumPageIndex + 1) * 4): self._albumPageIndex = self._albumPageIndex + 1 def SubAlbumPageIdex(self): # photo & video album share this function if self._albumPageIndex >= 1: self._albumPageIndex = self._albumPageIndex - 1 def CreateVideo(self): if self._isRecording == True: print 'Video is recording now! Cannot start new record!' else: self._isRecording = True videoFileName = self.videoPath + str(self._videoPtr) + self.videoFormat self.videoOut = cv2.VideoWriter(videoFileName, self.fourcc, self.videoFPS, (self.ScreenWidth,self.ScreenHeight)) self._video_list.append(videoFileName) self._videoPtr = self._videoPtr + 1 def ReleaseVideo(self): if self._isRecording == False: print 'No Recording Video can be generated!' else: self.videoOut.release() self._isRecording = False def ProcessVideo(self): #downsample first self.frame_downsample = cv2.resize(self.frame, (0,0), fx=0.5, fy=0.5 , interpolation = cv2.INTER_CUBIC) if(self._filteringOn == False): self.videoOut.write(self.frame_downsample) else: #filtering mode Y = cv2.cvtColor(self.frame_downsample, cv2.COLOR_BGR2GRAY) BgDiffCount = 0 if(self.BgRefreshFlag == True):#background refresh for i in range(0, self.ScreenHeight/self.BgMBSize): for j in range(0, self.ScreenWidth/self.BgMBSize): CurrMBAvg = cv2.mean(Y[(self.BgMBSize*i):(self.BgMBSize*i+self.BgMBSize-1), (self.BgMBSize*j):(self.BgMBSize*j+self.BgMBSize-1) ]) self.BgArray[i, j] = CurrMBAvg[0] self.BgRefreshFlag = False self.BgRecordFrameIdx = 0 else: for i in range(0, self.ScreenHeight/self.BgMBSize): for j in range(0, self.ScreenWidth/self.BgMBSize): #compute current MB average CurrMBAvg = cv2.mean(Y[(self.BgMBSize*i):(self.BgMBSize*i+self.BgMBSize-1), (self.BgMBSize*j):(self.BgMBSize*j+self.BgMBSize-1) ]) #compute difference between background ColorDiff = abs(self.BgArray[i, j] - CurrMBAvg[0]) #accumulate color difference if(ColorDiff >= self.BgColorDiffTh): BgDiffCount = BgDiffCount + 1; #backgroung update self.BgArray[i, j] = (1 - self.BgUpdateRatio)*self.BgArray[i, j] + self.BgUpdateRatio*CurrMBAvg[0] if(BgDiffCount >= self.BgDiffCountTh): self.BgRefreshFlag = True self.BgRecordFrameIdx = 0 if(self.BgRecordFrameIdx <= self.BgRecordCycle): self.videoOut.write(self.frame_downsample) self.BgRecordFrameIdx = self.BgRecordFrameIdx + 1 def VideoFilteringOnOff(self): if self._isRecording == True: print 'Cannot change mode during recording' else: self._filteringOn = not self._filteringOn self.BgRefreshFlag = True def DrawRedCircle(self): pygame.draw.circle(self.screen,(255,0,0),(20, 20), 5) def DrawBlueCircle(self): pygame.draw.circle(self.screen,(0,0,255),(300, 20), 3) def DrawAlbumMenu(self): self.screen.fill(WHITE) sc.DrawButton('camera', 40, 100) sc.DrawButton('video', 200, 100) def DrawVideoPreview(self): self.screen.fill(WHITE) videoNumber = 1 # 1:UpLeft, 2:UpRight, 3:BottomLeft, 4:BottomRight while (self._albumPageIndex*4 + videoNumber) <= len(self._video_list): filename = self._video_list[(self._albumPageIndex*4 + videoNumber - 1)] self.videoPlayer = cv2.VideoCapture(filename) retval, self.frame = self.videoPlayer.read() if retval == False: print 'Cannot open video file' else: self.videoPlayer.release() quarterFrame = cv2.resize(self.frame, (0,0), fx=0.5, fy=0.5 , interpolation = cv2.INTER_CUBIC) quarterScreenImg = self.cvimage_to_pygame(quarterFrame) if videoNumber == 1:#UpLeft self.screen.blit(quarterScreenImg, (0, 0)) self.DrawButton('play', 65, 35) elif videoNumber == 2:#UpRight self.screen.blit(quarterScreenImg, (160, 0)) self.DrawButton('play', 225, 35) elif videoNumber == 3:#BottomLeft self.screen.blit(quarterScreenImg, (0, 120)) self.DrawButton('play', 65, 165) else:#BottomRight self.screen.blit(quarterScreenImg, (160, 120)) self.DrawButton('play', 225, 165) if videoNumber == 4: break else: videoNumber = videoNumber + 1 def DrawVideoAlbumSingleFrame(self): self.screen.blit(self.screenimg, (0, 0)) self.DrawButton('play', 145, 85) def RemoveVideoAlbumSingleFrame(self): self.silentRemove(self._selectedVideo) self._video_list.remove(self._selectedVideo) self._selectedVideo = None self.EnterVideoAlbumMode() def PlayVideoAlbumSingle(self): clock = pygame.time.Clock() playing = True self.videoPlayer = cv2.VideoCapture(self._selectedVideo) while playing: for event in pygame.event.get(): if(event.type is MOUSEBUTTONUP): playing = False break retval, self.frame_downsample = self.videoPlayer.read() if(retval == False): playing = False break self.screenimg = self.cvimage_to_pygame(self.frame_downsample) self.screen.blit(self.screenimg,(0,0)) pygame.display.update() pygame.time.wait(80) def EnterFreeViewMode(self): self._mainState = 0 def EnterPhotoAlbumMode(self): self._mainState = 2 self._albumPageIndex = 0 self._isPhotoAlbum = True def EnterShotPhotoMode(self): self._mainState = 1 def EnterPhotoAlbumSingleMode(self, Index): if len(self._photo_list) >= (self._albumPageIndex*4 + Index): self._mainState = 3 self._selectedPhoto = self._photo_list[self._albumPageIndex*4 + Index - 1] self.frame = cv2.imread(self._selectedPhoto) self.frame_downsample = cv2.resize(self.frame, (0,0), fx=0.5, fy=0.5 , interpolation = cv2.INTER_CUBIC) self.screenimg = self.cvimage_to_pygame(self.frame_downsample) def EnterVideoMode(self): self._mainState = 4 self._filteringOn = False def EnterSelectAlubumMode(self): self._mainState = 5 self._albumPageIndex = 0 def EnterVideoAlbumMode(self): self._mainState = 6 self._albumPageIndex = 0 self._isPhotoAlbum = False def EnterVideoAlbumSingleMode(self, Index): if len(self._video_list) >= (self._albumPageIndex*4 + Index): self._mainState = 7 self._selectedVideo = self._video_list[self._albumPageIndex*4 + Index - 1] self.videoPlayer = cv2.VideoCapture(self._selectedVideo) retval, self.frame = self.videoPlayer.read() if retval == False: print 'Cannot open video file' else: self.videoPlayer.release() #self.frame_downsample = cv2.resize(self.frame, (0,0), fx=0.5, fy=0.5 , interpolation = cv2.INTER_CUBIC) self.screenimg = self.cvimage_to_pygame(self.frame) def EnterProcessImageMode(self): self._mainState = 8 def CurrMode(self): return self._mainState def isRecording(self): return self._isRecording def isFiltering(self): return self._filteringOn if __name__ == '__main__': pygame.init() pygame.mouse.set_visible(False) sc = SmartCamera() while True: try: if sc.CurrMode() == 0: #free viewing mode sc.DrawNewFrame() #sc.DrawButton(sc.btns_freeview) sc.DrawButton('camera', 0, 200) sc.DrawButton('album', 80, 200) sc.DrawButton('video', 160, 200) sc.DrawButton('quit', 240, 200) pygame.display.update() for event in pygame.event.get(): if(event.type is MOUSEBUTTONUP): pos = pygame.mouse.get_pos() #Find the place of screen we're in x,y = pos if y > 200 and x < 80: #take a photo sc.TakePhoto() sc.EnterShotPhotoMode() elif y > 200 and x >= 80 and x < 160: # enter album sc.EnterSelectAlubumMode() elif y > 200 and x >= 160 and x < 240: sc.EnterVideoMode() elif y > 200 and x >= 240: pygame.quit() sys.exit() elif sc.CurrMode() == 4: # video mode sc.DrawNewFrame() #sc.DrawButton(sc.btns_videoview) sc.DrawButton('camera', 0, 200) sc.DrawButton('video', 80, 200) sc.DrawButton('stop', 160, 200) sc.DrawButton('surveilliance', 240, 200) if sc.isRecording() == True: sc.DrawRedCircle() sc.ProcessVideo() if sc.isFiltering() == True: sc.DrawBlueCircle() pygame.display.update() for event in pygame.event.get(): if(event.type is MOUSEBUTTONUP): pos = pygame.mouse.get_pos() #Find the place of screen we're in x,y = pos if y > 200 and x < 80: #back to image free viewing mode if sc.isRecording() == True: print 'Video is recording! Press Stop first!' else: sc.EnterFreeViewMode() elif y > 200 and x >= 80 and x < 160: # start recording sc.CreateVideo() elif y > 200 and x >= 160 and x < 240: # stop recording sc.ReleaseVideo() elif y > 200 and x >= 240: #filtering mode on/off button sc.VideoFilteringOnOff() elif sc.CurrMode() == 5: # select which album sc.DrawAlbumMenu() pygame.display.update() for event in pygame.event.get(): if(event.type is MOUSEBUTTONUP): pos = pygame.mouse.get_pos() #Find the place of screen we're in x,y = pos if y >= 100 and y < 140 and x >= 40 and x < 120: #take a photo sc.EnterPhotoAlbumMode() elif y >= 100 and y < 140 and x >= 200 and x < 280: # enter album sc.EnterVideoAlbumMode() else: sc.EnterFreeViewMode() elif sc.CurrMode() == 1: # shot photo mode sc.DrawShotFrame() #sc.DrawButton(sc.btns_shotview) sc.DrawButton('camera', 0, 200) sc.DrawButton('garbage', 80, 200) pygame.display.update() for event in pygame.event.get(): if(event.type is MOUSEBUTTONUP): pos = pygame.mouse.get_pos() #Find the place of screen we're in x,y = pos if y > 200 and x < 80: #back to free viewing sc.EnterFreeViewMode() elif y > 200 and x >= 80 and x < 160: # remove shot frame sc.RemoveShotFrame() elif sc.CurrMode() == 2: # preview, photo album mode sc.DrawPhotoPreview() #sc.DrawButton(sc.btns_albumview) sc.DrawButton('left', 0, 200) sc.DrawButton('camera', 80, 200) sc.DrawButton('right', 240, 200) pygame.display.update() for event in pygame.event.get(): if(event.type is MOUSEBUTTONUP): pos = pygame.mouse.get_pos() #Find the place of screen we're in x,y = pos if x < 80 and y > 200: #previous page sc.SubAlbumPageIdex() elif x >= 80 and x < 160 and y > 200: # back to free viewing sc.EnterFreeViewMode() elif x >= 160 and x < 240 and y > 200: print 'no function hit' elif x >= 240 and y > 200: #next page sc.AddAlbumPageIdex() elif x < 160 and y < 120: sc.EnterPhotoAlbumSingleMode(1) elif x >= 160 and y < 120: sc.EnterPhotoAlbumSingleMode(2) elif x < 160 and y >= 120: sc.EnterPhotoAlbumSingleMode(3) else: sc.EnterPhotoAlbumSingleMode(4) elif sc.CurrMode() == 6: # preview, video album mode sc.DrawVideoPreview() #sc.DrawButton(sc.btns_albumview) sc.DrawButton('left', 0, 200) sc.DrawButton('camera', 80, 200) sc.DrawButton('right', 240, 200) pygame.display.update() for event in pygame.event.get(): if(event.type is MOUSEBUTTONUP): pos = pygame.mouse.get_pos() #Find the place of screen we're in x,y = pos if x < 80 and y > 200: #previous page sc.SubAlbumPageIdex() elif x >= 80 and x < 160 and y > 200: # back to free viewing sc.EnterFreeViewMode() elif x >= 160 and x < 240 and y > 200: print 'no function hit' elif x >= 240 and y > 200: #next page sc.AddAlbumPageIdex() elif x < 160 and y < 120: sc.EnterVideoAlbumSingleMode(1) elif x >= 160 and y < 120: sc.EnterVideoAlbumSingleMode(2) elif x < 160 and y >= 120: sc.EnterVideoAlbumSingleMode(3) else: sc.EnterVideoAlbumSingleMode(4) elif sc.CurrMode() == 3: # photo album single mode sc.DrawPhotoAlbumSingleFrame() #sc.DrawButton(sc.btns_albumsingleview) sc.DrawButton('album', 0, 200) sc.DrawButton('garbage', 80, 200) sc.DrawButton('vintage', 160, 200) sc.DrawButton('pencil', 240, 200) pygame.display.update() for event in pygame.event.get(): if(event.type is MOUSEBUTTONUP): pos = pygame.mouse.get_pos() #Find the place of screen we're in x,y = pos if y > 200 and x < 80: #back to album mode sc.EnterPhotoAlbumMode() elif y > 200 and x >= 80 and x < 160: # remove photo sc.RemovePhotoAlbumSingleFrame() elif y > 200 and x >= 160 and x < 240: sc.VintageProcessing() sc.EnterProcessImageMode() elif y > 200 and x >= 240: sc.HistogramProcessing() sc.EnterProcessImageMode() elif sc.CurrMode() == 7: # video album single mode sc.DrawVideoAlbumSingleFrame() #sc.DrawButton(sc.btns_albumsingleview) sc.DrawButton('album', 0, 200) sc.DrawButton('garbage', 80, 200) sc.DrawButton('videoplay', 160, 200) pygame.display.update() for event in pygame.event.get(): if(event.type is MOUSEBUTTONUP): pos = pygame.mouse.get_pos() #Find the place of screen we're in x,y = pos if y > 200 and x < 80: #back to album mode sc.EnterVideoAlbumMode() elif y > 200 and x >= 80 and x < 160: # remove photo sc.RemoveVideoAlbumSingleFrame() elif y > 200 and x >= 160 and x < 240: sc.PlayVideoAlbumSingle() elif sc.CurrMode() == 8: # processed image mode sc.DrawShotFrame() #sc.DrawButton(sc.btns_albumsingleview) sc.DrawButton('album', 0, 200) sc.DrawButton('garbage', 80, 200) pygame.display.update() for event in pygame.event.get(): if(event.type is MOUSEBUTTONUP): pos = pygame.mouse.get_pos() #Find the place of screen we're in x,y = pos if y > 200 and x < 80: #back to album mode sc.EnterPhotoAlbumMode() elif y > 200 and x >= 80 and x < 160: # remove photo sc.RemoveShotFrame() except KeyboardInterrupt: pygame.quit() sys.exit()