def process(frame, x0, y0, width, height): global guessGesture, visualize, mod, gestname, path #Call CNN model loading callback print "Will load default weight file" mod = myNN.loadCNN(0) masked = skinMask(frame, x0, y0, width, height) cv2.imshow('ROI', masked)
def Main(): global guessGesture, 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 while True: ans = int(raw_input(banner)) if ans == 1: print "Will load default weight file" mod = myNN.loadCNN(0) break else: print "Your face is so ugly ,even my code does not want to see u!!" return 0 cap = cv2.VideoCapture(0) cv2.namedWindow('Original', cv2.WINDOW_NORMAL) 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) if not quietMode: cv2.imshow('Original', frame) cv2.imshow('ROI', roi) key = cv2.waitKey(10) & 0xff if key == 27: break elif key == ord('b'): binaryMode = not binaryMode if binaryMode: print "Binary Threshold filter active" elif key == ord('g'): guessGesture = not guessGesture print "Prediction Mode - {}".format(guessGesture) 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 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 load_model(): global mod, binaryMode, x0, y0, width, height print "Will load default weight file" mod = myNN.loadCNN(0)
def Main(): global guessGesture, visualize, mod, binaryMode, bkgrndSubMode, mask, takebkgrndSubMask, x0, y0, width, height, saveImg, gestname, path, flag quietMode = False font = cv2.FONT_HERSHEY_SIMPLEX size = 0.5 fx = 10 fy = 350 fh = 18 #Call CNN model loading callback mod = gCNN.loadCNN() ## 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) 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, '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 = gCNN.update(plot) cv2.imshow('Gesture Probability', plot) if gCNN.jsonarray: gesture = max(gCNN.jsonarray, key=gCNN.jsonarray.get) if gesture == 'PUNCH': flag = 1 elif gesture == 'STOP': flag = 2 elif gesture == 'NOTHING': flag = 0 else: flag = -1 pickle.dump(flag, open('gesture.p', 'wb')) ############## 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 x key to use and refresh Background SubMask filter 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)) #elif key != 255: # print key #Realse & destroy cap.release() cv2.destroyAllWindows()
time.sleep(0.01) return res, lastgesture if __name__ == "__main__": 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) print "Loading default weight file..." mod = myNN.loadCNN() while (True): ret, frame = cap.read() if ret == True: roi, gesture = skinMask(frame, x0, y0, width, height) cv2.imshow('Original', frame) cv2.imshow('ROI', roi) if gesture == -1: pic = cv2.imread("Nothing.png") cv2.imshow('gesture', pic) else: pic = cv2.imread(myNN.output[gesture] + '.png') cv2.imshow('gesture', pic)
def main(): global model, binaryMode, x0, y0, x1, y1, width, height, saveImg, gestname, path font = cv2.FONT_HERSHEY_SIMPLEX size = 0.5 fx = 10 fy = 350 fh = 18 tracker1 = InputTracker() tracker2 = InputTracker() model = myNN.loadCNN(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) plot2 = 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: timestamp = time.time() roi, guess, plot = binaryMask(frame, x0, y0, width, height, False, framecount, plot) roi2, guess2, plot2 = binaryMask(frame, x1, y1, width, height, True, framecount, plot2) cv2.putText(frame, guess, (x0, y0), font, 0.7, (0, 255, 0), 2, 1) cv2.putText(frame, guess2, (x1, y1), font, 0.7, (0, 255, 0), 2, 1) action = tracker1.update(guess, timestamp) action2 = tracker2.update(guess2, timestamp) if action != "": cv2.putText(frame, action, (x0 + 100, y0), font, 0.7, (0, 255, 0), 2, 1) if action == "DOWN": tetris.action("down") elif action == "FLIP": tetris.action("flip") elif action == "MOVE": tetris.action("right") if action2 != "": cv2.putText(frame, action2, (x1 + 100, y1), font, 0.7, (0, 255, 0), 2, 1) if action2 == "DOWN": tetris.action("down") elif action2 == "FLIP": tetris.action("flip") elif action2 == "MOVE": tetris.action("left") framecount = framecount + 1 timediff = timestamp - 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,'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) cv2.imshow('Original',frame) cv2.imshow('ROI', roi) cv2.imshow('ROI2', roi2) # cv2.imshow('Gesture Probability', plot) # cv2.imshow('Gesture Probability 2', plot2) ############## Keyboard inputs ################## key = cv2.waitKey(5) & 0xff ## Use Esc key to close the program if key == 27: break ## Use s key to start/pause/resume taking snapshots ## numOfSamples controls number of snapshots to be taken PER gesture elif key == ord('s'): 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 name: ") # Release & destroy cap.release() cv2.destroyAllWindows()
def main(): mod = myNN.loadCNN(-1) myNN.trainModel(mod)
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(): # 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()
def Main(): global flip, change_bgd, binary_mode, guess_gesture, visualize, mod, bgdSub_mode, mask, change_bgd global x0, y0, width, height, save_image, gest_name, path quite_mode = False while True: ans = int(input(banner)) if ans == 1: mod = nn.loadCNN() break else: print('BYE!') return 0 cap = cv.VideoCapture(0) cv.namedWindow('Original', cv.WINDOW_NORMAL) ret = cap.set(3, 640) ret = cap.set(4, 480) framecount = 0 fps = "" start = time.time() while True: ret, frame = cap.read() frame = cv.flip(frame, 3) frame = cv.resize(frame, (640, 480)) cv.rectangle(frame, (x0, y0), (x0+width, y0+height), (0,255,0), 1) roi = frame[y0:y0+height, x0:x0+width] if ret == True: if bgdSub_mode: roi = bgdSubMask(roi, framecount) elif binary_mode: roi = binaryMask(roi, framecount) else: roi = skinMask(roi, framecount) framecount += 1 end = time.time() if(end - start >= 1): fps = 'FPS:%s' %(framecount) start = time.time() framecount = 0 font = cv.FONT_HERSHEY_COMPLEX_SMALL size = 0.7 fx, fy, fh = 10, 350, 18 color = (0, 255, 0) cv.putText(frame, fps, (10,20), font, size, color, 2, 1) cv.putText(frame, 'Options:', (fx, fy), font, size, color, 2, 1) cv.putText(frame, 'b- Toggle Binary/SkinMask', (fx, fy+fh), font, size, color, 2, 1) cv.putText(frame, 'x- Toggle Background SubMask:', (fx, fy+2*fh), font, size, color, 2, 1) cv.putText(frame, 'g- Toggle Prediction Mode', (fx, fy+3*fh), font, size, color, 2, 1) cv.putText(frame, 'q- Toggle Quiet Mode', (fx, fy+4*fh), font, size, color, 2, 1) cv.putText(frame, 'n- To enter name of new gesture folder', (fx, fy+5*fh), font, size, color, 2, 1) cv.putText(frame, 's- To start capturing new gestures', (fx, fy+6*fh), font, size, color, 2, 1) cv.putText(frame, 'Esc - exit', (fx, fy+7*fh), font, size, color, 1, 1) if flip: roi = roi[:,-1::-1] if save_image: saveROI(roi) if not quite_mode: cv.imshow('Original', frame) cv.imshow('ROI', cv.resize(roi, (400, 400))) if guess_gesture: plot = np.zeros((512, 512, 3), np.uint8) plot = nn.update(plot) cv.imshow('Gesture Probability', plot) ##Key functions## key = cv.waitKey(5) & 0xff if key == 27: break elif key == ord('b'): binary_mode = not binary_mode bgdSub_mode = False if binary_mode: print("Binary Threshold filter active") else: print("SkinMask filter active") elif key == ord('x'): change_bgd = True bgdSub_mode = True print("BgdSubMask filter active") elif key == ord('g'): guess_gesture = not guess_gesture print("Prediction Mode - {}".format(guess_gesture)) 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'): quite_mode = not quite_mode print("Quiet Mode - {}".format(quite_mode)) elif key == ord('s'): save_image = not save_image if gest_name != '': save_image = True else: print("Enter a gesture group name first, by pressing 'n'") save_image = False elif key == ord('n'): gest_name = input("Enter the gesture folder name: ") if not os.path.isdir(os.path.join('./test' ,gest_name)): os.makedirs(os.path.join('./test' ,gest_name)) path = "./test/"+gest_name+"/" elif key == ord('f'): flip = not flip if flip: print('Flipping ON...') else: print('Flipping OFF...') cap.release() cv.destroyAllWindows()
def __init__(self): self.mod=myNN.loadCNN(0) self.skinkernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
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 mod = myNN.loadCNN() #LOAD THE MODEL 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) 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 not quietMode: cv2.imshow('Original', frame) cv2.imshow('ROI', roi) if guessGesture == True: f = open("./LivePlot/sample.txt", 'w') myNN.update(f) 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 x key to use and refresh Background SubMask filter 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)) 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 e.errno != 17: print('Some issue while creating the directory named -' + gestname) path = "./" + gestname + "/" cap.release() cv2.destroyAllWindows()