def Main(): global guessGesture, visualize, mod, binaryMode, bkgrndSubMode, mask, takebkgrndSubMask, x0, y0, width, height, saveImg, gestname, path quietMode = False font = cv2.FONT_HERSHEY_SIMPLEX size = 0.5 fx = 10 fy = 350 fh = 18 while True: ans = int(input(banner)) if ans == 1: mod = myNN.loadCNN() break elif ans == 2: mod = myNN.loadCNN(True) myNN.trainModel(mod) input("Press any key to continue") break else: print("Exiting...") return 0 cap = cv2.VideoCapture(0) cv2.namedWindow('Original', cv2.WINDOW_NORMAL) ret = cap.set(3, 640) ret = cap.set(4, 480) framecount = 0 fps = "" start = time.time() plot = np.zeros((512, 512, 3), np.uint8) while (True): ret, frame = cap.read() max_area = 0 frame = cv2.flip(frame, 3) frame = cv2.resize(frame, (640, 480)) if ret == True: if bkgrndSubMode == True: roi = bkgrndSubMask(frame, x0, y0, width, height, framecount, plot) elif binaryMode == True: roi = binaryMask(frame, x0, y0, width, height, framecount, plot) else: roi = skinMask(frame, x0, y0, width, height, framecount, plot) framecount = framecount + 1 end = time.time() timediff = (end - start) if (timediff >= 1): fps = 'FPS:%s' % (framecount) start = time.time() framecount = 0 cv2.putText(frame, fps, (10, 20), font, 0.7, (0, 255, 0), 2, 1) cv2.putText(frame, 'Options:', (fx, fy), font, 0.7, (0, 255, 0), 2, 1) cv2.putText(frame, 'b - Toggle Binary/SkinMask', (fx, fy + fh), font, size, (0, 255, 0), 1, 1) cv2.putText(frame, 'g - Toggle Prediction Mode', (fx, fy + 3 * fh), font, size, (0, 255, 0), 1, 1) cv2.putText(frame, 'ESC - Exit', (fx, fy + 7 * fh), font, size, (0, 255, 0), 1, 1) if not quietMode: cv2.imshow('Original', frame) cv2.imshow('ROI', roi) if guessGesture == True: plot = np.zeros((512, 512, 3), np.uint8) plot = myNN.update(plot) cv2.imshow('Gesture Probability', plot) key = cv2.waitKey(5) & 0xff if key == 27: break elif key == ord('b'): binaryMode = not binaryMode bkgrndSubMode = False if binaryMode: print("Binary Threshold filter active") else: print("SkinMask filter active") elif key == ord('x'): takebkgrndSubMask = True bkgrndSubMode = True print("BkgrndSubMask filter active") elif key == ord('g'): guessGesture = not guessGesture print("Prediction Mode - {}".format(guessGesture)) elif key == ord('i'): y0 = y0 - 5 elif key == ord('k'): y0 = y0 + 5 elif key == ord('j'): x0 = x0 - 5 elif key == ord('l'): x0 = x0 + 5 elif key == ord('q'): quietMode = not quietMode print("Quiet Mode - {}".format(quietMode)) elif key == ord('s'): saveImg = not saveImg if gestname != '': saveImg = True else: print("Enter a gesture group name first, by pressing 'n'") saveImg = False elif key == ord('n'): gestname = input("Enter the gesture folder name: ") try: os.makedirs(gestname) except OSError as e: # if directory already present if e.errno != 17: print('Some issue while creating the directory named -' + gestname) path = "./" + gestname + "/" cap.release() cv2.destroyAllWindows()
def Main(): global guessGesture, visualize, mod, binaryMode, bkgrndSubMode, mask, takebkgrndSubMask, x0, y0, width, height, saveImg, gestname, path quietMode = False font = cv2.FONT_HERSHEY_SIMPLEX size = 0.5 fx = 10 fy = 350 fh = 18 #Call CNN model loading callback while True: ans = int(input(banner)) if ans == 2: mod = myNN.loadCNN(-1) myNN.trainModel(mod) input("Press any key to continue") break elif ans == 1: print("Will load default weight file") mod = myNN.loadCNN(0) break elif ans == 3: if not mod: w = int(input("Which weight file to load (0 or 1)")) mod = myNN.loadCNN(w) else: print("Will load default weight file") img = int(input("Image number ")) layer = int(input("Enter which layer to visualize ")) myNN.visualizeLayers(mod, img, layer) input("Press any key to continue") continue else: print("Get out of here!!!") return 0 ## Grab camera input cap = cv2.VideoCapture(0) cv2.namedWindow('Original', cv2.WINDOW_NORMAL) # set rt size as 640x480 ret = cap.set(3, 640) ret = cap.set(4, 480) framecount = 0 fps = "" start = time.time() plot = np.zeros((512, 512, 3), np.uint8) global x while (True): ret, frame = cap.read() max_area = 0 frame = cv2.flip(frame, 3) frame = cv2.resize(frame, (640, 480)) if ret == True: if bkgrndSubMode == True: roi = bkgrndSubMask(frame, x0, y0, width, height, framecount, plot) elif binaryMode == True: roi = binaryMask(frame, x0, y0, width, height, framecount, plot) else: roi = skinMask(frame, x0, y0, width, height, framecount, plot) framecount = framecount + 1 end = time.time() timediff = (end - start) if (timediff >= 1): #timediff = end - start fps = 'FPS:%s' % (framecount) start = time.time() framecount = 0 cv2.putText(frame, fps, (10, 20), font, 0.7, (0, 255, 0), 2, 1) cv2.putText(frame, 'Options:', (fx, fy), font, 0.7, (0, 255, 0), 2, 1) cv2.putText(frame, 'b - Toggle Binary/SkinMask', (fx, fy + fh), font, size, (0, 255, 0), 1, 1) cv2.putText(frame, 'x - Toggle Background Sub Mask', (fx, fy + 2 * fh), font, size, (0, 255, 0), 1, 1) cv2.putText(frame, 'g - Toggle Prediction Mode', (fx, fy + 3 * fh), font, size, (0, 255, 0), 1, 1) cv2.putText(frame, 'q - Toggle Quiet Mode', (fx, fy + 4 * fh), font, size, (0, 255, 0), 1, 1) cv2.putText(frame, 'n - To enter name of new gesture folder', (fx, fy + 5 * fh), font, size, (0, 255, 0), 1, 1) cv2.putText(frame, 's - To start capturing new gestures for training', (fx, fy + 6 * fh), font, size, (0, 255, 0), 1, 1) cv2.putText(frame, 'ESC - Exit', (fx, fy + 7 * fh), font, size, (0, 255, 0), 1, 1) ## If enabled will stop updating the main openCV windows ## Way to reduce some processing power :) if not quietMode: cv2.imshow('Original', frame) cv2.imshow('ROI', roi) if guessGesture == True: #plot = np.zeros((512,512,3), np.uint8) plot = myNN.update(plot, x) x = (x + 1) % 2 cv2.imshow('Gesture Probability', plot) #plot = np.zeros((512,512,3), np.uint8) ############## Keyboard inputs ################## key = cv2.waitKey(5) & 0xff ## Use Esc key to close the program if key == 27: break ## Use b key to toggle between binary threshold or skinmask based filters elif key == ord('b'): binaryMode = not binaryMode bkgrndSubMode = False if binaryMode: print("Binary Threshold filter active") else: print("SkinMask filter active") ## Use g key to start gesture predictions via CNN elif key == ord('x'): takebkgrndSubMask = True bkgrndSubMode = True print("BkgrndSubMask filter active") ## Use g key to start gesture predictions via CNN elif key == ord('g'): guessGesture = not guessGesture print("Prediction Mode - {}".format(guessGesture)) ## This option is not yet complete. So disabled for now ## Use v key to visualize layers #elif key == ord('v'): # visualize = True ## Use i,j,k,l to adjust ROI window elif key == ord('i'): y0 = y0 - 5 elif key == ord('k'): y0 = y0 + 5 elif key == ord('j'): x0 = x0 - 5 elif key == ord('l'): x0 = x0 + 5 ## Quiet mode to hide gesture window elif key == ord('q'): quietMode = not quietMode print("Quiet Mode - {}".format(quietMode)) ## Use s key to start/pause/resume taking snapshots ## numOfSamples controls number of snapshots to be taken PER gesture elif key == ord('s'): saveImg = not saveImg if gestname != '': saveImg = True else: print("Enter a gesture group name first, by pressing 'n'") saveImg = False ## Use n key to enter gesture name elif key == ord('n'): gestname = input("Enter the gesture folder name: ") try: os.makedirs(gestname) except OSError as e: # if directory already present if e.errno != 17: print('Some issue while creating the directory named -' + gestname) path = "./" + gestname + "/" #elif key != 255: # print key #Realse & destroy cap.release() cv2.destroyAllWindows() cv2.waitKey(0)
def Main(): global guessGesture, visualize, mod, binaryMode, x0, y0, width, height, saveImg, gestname, path, img_flag, cap font = cv2.FONT_HERSHEY_SIMPLEX size = 0.7 fx = 10 fy = 355 fh = 18 #Call CNN model loading callback while True: # ans = int(raw_input(banner)) ans = 1 if ans == 2: mod = myNN.loadCNN(-1) myNN.trainModel(mod) raw_input("Press any key to continue") break elif ans == 1: print "Will load default weight file" mod = myNN.loadCNN(0) break elif ans == 3: if not mod: w = int(raw_input("Which weight file to load (0 or 1)")) mod = myNN.loadCNN(w) else: print "Will load default weight file" img = int(raw_input("Image number ")) layer = int(raw_input("Enter which layer to visualize ")) myNN.visualizeLayers(mod, img, layer) raw_input("Press any key to continue") continue else: print "Get out of here!!!" return 0 import os print os.getpid() ## Grab camera input cv2.namedWindow( 'Original', cv2.WND_PROP_FULLSCREEN) # Set Camera Size, cv2.WINDOW_NORMAL cv2.setWindowProperty('Original', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) import signal # MAC : SIGPROF 27 def signalCameraOnHandler(signum, frame): print signum global img_flag global cap cap = cv2.VideoCapture(0) cv2.namedWindow('Original', cv2.WINDOW_NORMAL) img_flag = "CAM" # MAC : SIGINFO 29 def signalMapHandler(signum, frame): print signum global img_flag global cap cap = cv2.VideoCapture('map.gif') cv2.namedWindow('Original', cv2.WINDOW_NORMAL) img_flag = "MAP" # MAC : SIGUSR1 30 def signalSmileFaceHandler(signum, frame): print signum global img_flag global cap cap = cv2.VideoCapture('smile_glow.gif') cv2.namedWindow('Original', cv2.WINDOW_NORMAL) img_flag = "SMILE" # MAC : SIGUSR2 31 def signalDefaultFaceHandler(signum, frame): print signum global img_flag global cap cap = cv2.VideoCapture('normal_glow.gif') cv2.namedWindow('Original', cv2.WINDOW_NORMAL) img_flag = "NORMAL" # Set Signal signal.signal(signal.SIGPROF, signalCameraOnHandler) signal.signal(signal.SIGINFO, signalMapHandler) signal.signal(signal.SIGUSR1, signalSmileFaceHandler) signal.signal(signal.SIGUSR2, signalDefaultFaceHandler) reset_flag = False thumbs_up_guess_stack = 0 smile_face_stack = 0 camera_page_stack = 0 map_page_stack = 0 while True: ret, frame = cap.read() # for end of gif if not ret: if img_flag == "NORMAL": cap = cv2.VideoCapture('normal_glow.gif') cv2.namedWindow('Original', cv2.WINDOW_NORMAL) elif img_flag == "SMILE": cap = cv2.VideoCapture('smile_glow.gif') cv2.namedWindow('Original', cv2.WINDOW_NORMAL) elif img_flag == "MAP": cap = cv2.VideoCapture('map.gif') cv2.namedWindow('Original', cv2.WINDOW_NORMAL) ret, frame = cap.read() if img_flag == "CAM": camera_page_stack += 1 frame = cv2.flip(frame, 3) guess_res = ":)" if ret: if binaryMode: # on roi, guess_res = binaryMask(frame, x0, y0, width, height) else: roi, guess_res = skinMask(frame, x0, y0, width, height) # detact thumbs-up frame if guess_res.split()[0] == "PEACE" or guess_res.split()[0] == "OK": camera_page_stack -= 1 thumbs_up_guess_stack += 1 else: thumbs_up_guess_stack = 0 # print GUESS cv2.putText(frame, str(thumbs_up_guess_stack) + " " + guess_res, (fx, fy), font, 1, (0, 0, 255), 2, 1) cv2.putText(frame, "Show Thumbs-Up on TONY's hat camera!", (fx, fy + fh), font, size, (0, 0, 255), 1, 1) elif img_flag == "SMILE": smile_face_stack += 1 cap = cv2.VideoCapture('smile_glow.gif') elif img_flag == "NORMAL": cap = cv2.VideoCapture('normal_glow.gif') elif img_flag == "MAP": map_page_stack += 1 cap = cv2.VideoCapture('map.gif') # for signal call if thumbs_up_guess_stack >= 6: # paging to smile os.system("kill -30 " + str(os.getpid())) reset_flag = True if smile_face_stack >= 30: # paging to map os.system("kill -29 " + str(os.getpid())) reset_flag = True if camera_page_stack >= 150: # paging to map os.system("kill -29 " + str(os.getpid())) reset_flag = True # now threshold == 300 if map_page_stack >= 200: # paging to camera os.system("kill -27 " + str(os.getpid())) reset_flag = True if reset_flag: thumbs_up_guess_stack = 0 smile_face_stack = 0 camera_page_stack = 0 map_page_stack = 0 reset_flag = False cv2.imshow('Original', frame) key = cv2.waitKey(10) & 0xff if key == 27: break #Realse & destroy cap.release() cv2.destroyAllWindows()
def main(): mod = myNN.loadCNN(-1) myNN.trainModel(mod)
def Main(): # pdb.set_trace() global guessGesture, visualize, mod, binaryMode, x0, y0, width, height, saveImg, gestname, path quietMode = False font = cv2.FONT_HERSHEY_SIMPLEX size = 0.5 fx = 10 fy = 355 fh = 18 #Call CNN model loading callback while True: ans = int(raw_input( banner)) if ans == 2: # 训练模型 mod = myNN.loadCNN(-1) myNN.trainModel(mod) raw_input("Press any key to continue") break elif ans == 1: # 使用已有模型 print "Will load default weight file" mod = myNN.loadCNN(0) break elif ans == 3: # 打印某张手势图片经过每层的处理 if not mod: w = int(raw_input("Which weight file to load (0 or 1)")) mod = myNN.loadCNN(w) else: print "Will load default weight file" img = int(raw_input("Image number ")) layer = int(raw_input("Enter which layer to visualize ")) myNN.visualizeLayers(mod, img, layer) raw_input("Press any key to continue") continue else: print "Get out of here!!!" return 0 ## Grab camera input cap = cv2.VideoCapture(0) cv2.namedWindow('Original', cv2.WINDOW_NORMAL) # set rt size as 640x480 ret = cap.set(3,640) ret = cap.set(4,480) while(True): ret, frame = cap.read() max_area = 0 frame = cv2.flip(frame, 3) if ret == True: # ! 其间 可以做保存图片的操作 if binaryMode == True: roi = binaryMask(frame, x0, y0, width, height) else: roi = skinMask(frame, x0, y0, width, height) cv2.putText(frame,'Options:',(fx,fy), font, 0.7,(0,255,0),2,1) cv2.putText(frame,'b - Toggle Binary/SkinMask',(fx,fy + fh), font, size,(0,255,0),1,1) cv2.putText(frame,'g - Toggle Prediction Mode',(fx,fy + 2*fh), font, size,(0,255,0),1,1) cv2.putText(frame,'q - Toggle Quiet Mode',(fx,fy + 3*fh), font, size,(0,255,0),1,1) cv2.putText(frame,'n - To enter name of new gesture folder',(fx,fy + 4*fh), font, size,(0,255,0),1,1) cv2.putText(frame,'s - To start capturing new gestures for training',(fx,fy + 5*fh), font, size,(0,255,0),1,1) cv2.putText(frame,'ESC - Exit',(fx,fy + 6*fh), font, size,(0,255,0),1,1) ## If enabled will stop updating the main openCV windows ## Way to reduce some processing power :) if not quietMode: cv2.imshow('Original',frame) # 图像窗口 cv2.imshow('ROI', roi) # 截取手势窗口 # Keyboard inputs key = cv2.waitKey(10) & 0xff ## Use Esc key to close the program if key == 27: break ## Use b key to toggle between binary threshold or skinmask based filters elif key == ord('b'): binaryMode = not binaryMode if binaryMode: print "Binary Threshold filter active" else: print "SkinMask filter active" ## Use g key to start gesture predictions via CNN elif key == ord('g'): guessGesture = not guessGesture print "Prediction Mode - {}".format(guessGesture) ## This option is not yet complete. So disabled for now ## Use v key to visualize layers # elif key == ord('v'): # visualize = True ## Use i,j,k,l to adjust ROI window elif key == ord('i'): y0 = y0 - 5 elif key == ord('k'): y0 = y0 + 5 elif key == ord('j'): x0 = x0 - 5 elif key == ord('l'): x0 = x0 + 5 ## Quiet mode to hide gesture window elif key == ord('q'): quietMode = not quietMode print "Quiet Mode - {}".format(quietMode) ## Use s key to start/pause/resume taking snapshots ## numOfSamples controls number of snapshots to be taken PER gesture elif key == ord('s'): saveImg = not saveImg if gestname != '': # 保存图片操作 binaryMask 或 skinMask 步骤操作 saveImg = True else: print "Enter a gesture group name first, by pressing 'n'" saveImg = False ## Use n key to enter gesture name elif key == ord('n'): # 获取 手势名称 建立文件夹,然后会自动保存图片 到该目录 gestname = raw_input("Enter the gesture folder name: ") try: os.makedirs(gestname) except OSError as e: # if directory already present if e.errno != 17: print 'Some issue while creating the directory named -' + gestname path = "./"+gestname+"/" #elif key != 255: # print key #Realse & destroy cap.release() cv2.destroyAllWindows()