def recognize(self, contours): 
     x, y, r, b, isCenter = imgproc.find_max_rectangle(contours)
     max_area, contours = imgproc.max_area(contours)
     max_area_n = float(max_area)/((r-x)*(b-y))
     # print 'Normalized Area: ', max_area_n 
     # print 'Actual Area: ', float(max_area)
     hull = imgproc.find_convex_hull(contours)
     mean_depth = 0
     if hull:
       cds = imgproc.find_convex_defects(contours, hull)
       if len(cds) != 0:
           mean_depth = sum([cd[3] for cd in cds])/len(cds)
     # print 'Depth: ', mean_depth      
     if self.isFist(max_area_n, mean_depth, isCenter): 
         ges = 'Fist' 
     elif self.isPalm(max_area_n, mean_depth, isCenter): 
         ges = 'Palm'
     elif self.isScissors(max_area_n, mean_depth, isCenter):
         ges = 'Scissors'    
     else: 
         ges = 'Uncertain'
     return Gesture(ges), max_area, mean_depth    
Ejemplo n.º 2
0
def mainFunction():
  im1, im2, im3 = get_input_pics_filename()
  if (im1 is not None) or (im2 is not None) or (im3 is not None): 
    initHueThreshold = 42
    initIntensityThreshold = 170
    skin_detector = skin.SkinDetector()
    skin_detector.setHueThreshold(initHueThreshold)
    skin_detector.setIntensityThreshold(initIntensityThreshold)
    print ''
    print 'Current Hue Threshold: ', initHueThreshold
    print 'Current Intensity Threshold: ', initIntensityThreshold
    print ''

    session = ImageProcessSession(skin_detector)
    ga = gesture.GestureAnalyzer()

    if im1 is not None: 
      print 'Result For 1st Image: '
      cv.Flip(im1, None, 1)
      contours1 = session.process(im1)
      img1 = cv.CreateImage((im1.width, im1.height), 8, 3)
      if contours1:
          ges1, area1, depth1 = ga.recognize(contours1) # main recognize
          print 'Area: ', float(area1) 
          print 'Depth: ', depth1  
          x1, y1, r1, b1, isCenter1 = imgproc.find_max_rectangle(contours1)
          print 'Coordinate (x1, y1, x2, y2): ', x1, y1, r1, b1
          if isCenter1 == 1:
            print 'The gesture is placed at the center!' 
          elif isCenter1 == 0:
            print 'The gesture is placed at the corner!'
          else: 
            print 'The gesture position is not recognized!'
          # cv.Rectangle(img1, (x1,y1), (r1, b1), imgproc.color.RED)
          # cv.DrawContours(img1, contours1, imgproc.color.RED, imgproc.color.GREEN, 1, thickness=3)
          print 'Detected Gesture For 1st Image is: ', ges1
          print ''

    if im2 is not None: 
      print 'Result For 2nd Image: '
      cv.Flip(im2, None, 1)
      contours2 = session.process(im2)
      img2 = cv.CreateImage((im2.width, im2.height), 8, 3)
      if contours2:
          ges2, area2, depth2 = ga.recognize(contours2) # main recognize
          print 'Area: ', float(area2) 
          print 'Depth: ', depth2
          x2, y2, r2, b2, isCenter2 = imgproc.find_max_rectangle(contours2)
          print 'Coordinate (x1, y1, x2, y2): ', x2, y2, r2, b2
          if isCenter2 == 1:
            print 'The gesture is placed at the center!' 
          elif isCenter2 == 0:
            print 'The gesture is placed at the corner!'   
          else: 
            print 'The gesture position is not recognized!'                          
          # cv.Rectangle(img2, (x2,y2), (r2, b2), imgproc.color.RED)
          # cv.DrawContours(img2, contours2, imgproc.color.RED, imgproc.color.GREEN, 1, thickness=3)
          print 'Detected Gesture For 2nd Image is: ', ges2
          print ''

    if im3 is not None: 
      print 'Result For 3rd Image: '
      cv.Flip(im3, None, 1)
      contours3 = session.process(im3)
      img3 = cv.CreateImage((im3.width, im3.height), 8, 3)
      if contours3:
          ges3, area3, depth3 = ga.recognize(contours3) # main recognize
          print 'Area: ', float(area3) 
          print 'Depth: ', depth3
          x3, y3, r3, b3, isCenter3 = imgproc.find_max_rectangle(contours3)
          print 'Coordinate (x1, y1, x2, y2): ', x3, y3, r3, b3
          if isCenter3 == 1:
            print 'The gesture is placed at the center!' 
          elif isCenter3 == 0:
            print 'The gesture is placed at the corner!'
          else: 
            print 'The gesture position is not recognized!'
          # cv.Rectangle(img3, (x3,y3), (r3, b3), imgproc.color.RED)
          # cv.DrawContours(img3, contours3, imgproc.color.RED, imgproc.color.GREEN, 1, thickness=3)
          print 'Detected Gesture For 3rd Image is: ', ges3 
          print ''
    else: 
      ges3 = None      

    ''' AUTHENTICATION PART '''  

    Password = ['Palm', 'Fist']  

    if ((str(ges1) == Password[0]) and (str(ges2) == Password[1])) or (ges3 and (str(ges2) == Password[0]) and (str(ges3) == Password[1])): 
      print 'AUTHENTICATED!'
      print ''
    else: 
      print 'WARNING: NOT AUTHENTICATED!'
      print ''
def mainFunction():
  interface_trigger = False
  # Set up the window objects and environment.
  proc_win_name = "Processing window"
  proc_win = cv.NamedWindow(proc_win_name, 1)
  # Initialize the real time camera.
  cam = cv.CaptureFromCAM(0)
  # Initialize the skin detector. the skin detector is defined in skin.py
  skin_detector = skin.SkinDetector()
  # Initialize delay, hue threshold and intensity threshold.
  msdelay = 3
  initHueThreshold = 42
  initIntensityThreshold = 191
  skin_detector.setHueThreshold(initHueThreshold)
  skin_detector.setIntensityThreshold(initIntensityThreshold)
  # Initialize the image processor.
  session = ImageProcessSession(skin_detector)
  ga = gesture.GestureAnalyzer()

  while True:
    k = cv.WaitKey(msdelay)
    k = chr(k) if k > 0 else 0
    # Get the current frame from the video camera.
    bgrimg = cv.QueryFrame(cam)
    # Flip the image.
    cv.Flip(bgrimg, None, 1)
    # Computing the skin and contour for the frame image.
    contours = session.process(bgrimg)
    # Create an image for displaying on the screen.
    img = cv.CreateImage((bgrimg.width, bgrimg.height), 8, 3)

    if interface_trigger:
      # If the contours is not null, the system will launch the gesture analysis
      if contours is not None:
          ges, area, depth = ga.recognize(contours)
          x, y, r, b = imgproc.find_max_rectangle(contours)
          # Drawing a green rectange around the contours on the image.
          cv.Rectangle(img, (x,y), (r, b), imgproc.color.GREEN, thickness=2)
          # Drawing contour points on the image.
          cv.DrawContours(img, contours, imgproc.color.RED, imgproc.color.GREEN, 1, thickness=3)

          #Read national news if palm is detected.
          if str(ges) == 'Palm':
            if len(process_list) == 0:
              # If no current running sub process, the program will create a new sub process to filter and read national news. The process ID will be stored in the process list.
              system("say Palm detected. national news selected")
              process_list.append(subprocess.Popen("say " + news.readNationalNews(), stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid))
          
          #Read international news if scissors is detected.
          elif str(ges) == 'Scissors':
            if len(process_list) == 0:
              # If no current running sub process, the program will create a new sub process to filter and read international news. The process ID will be stored in the process list.
              system("say Scissor detected. international news selected")
              process_list.append(subprocess.Popen("say " + news.readInternationalNews(), stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid))

          # Exit interface if fist is selected. All processes in the process list will be terminated.
          elif str(ges) == 'Fist':
            if len(process_list) != 0:
              # If there exist current running sub process, the program will send a signal to terminate all running processes.
              for item in process_list:
                os.killpg(item.pid, signal.SIGTERM)
                process_list.remove(item)
            system("say Fist detected. Exit Visual News Interface")
            sys.exit()
    
    # Exit interface by pressing EXIT button.
    if cv.WaitKey(1) == 27:
      if len(process_list) != 0:
        # If there exist current running sub process, the program will send a signal to terminate all running processes.
        for item in process_list:
          os.killpg(item.pid, signal.SIGTERM)
          process_list.remove(item)
      system("say Exit Visual News Interface")
      sys.exit()
    
    # Terminate all reading processes in the process list by pressing SPACE.
    if cv.WaitKey(1) == 32:
      if len(process_list) != 0:
        for item in process_list:
          # If there exist current running sub process, the program will send a signal to terminate all running processes.
          os.killpg(item.pid, signal.SIGTERM)
          process_list.remove(item)
        system("say Exit Reading")
      else:
        system("say no current reading news")

	# Turn on and off the hand gesture detector by pressing ENTER.
    if cv.WaitKey(1) == 13:
      if interface_trigger == False:
        system("say Hand gesture detector started.")
        interface_trigger = True
      else:
        system("say Hand gesture detector closed")
        interface_trigger = False

	# Reading national news by pressing the 'n' button.
    if cv.WaitKey(1) == 110:
      if len(process_list) == 0:
        # If no current running sub process, the program will create a new sub process to filter and read national news. The process ID will be stored in the process list.
        system("say National news selected")
        process_list.append(subprocess.Popen("say " + news.readNationalNews(), stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid))

	# Reading international news by pressing the 'i' button.
    if cv.WaitKey(1) == 105:
      if len(process_list) == 0:
        # If no current running sub process, the program will create a new sub process to filter and read international news. The process ID will be stored in the process list.
        system("say International news selected")
        process_list.append(subprocess.Popen("say " + news.readInternationalNews(), stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid))
    cv.ShowImage(proc_win_name, img)