예제 #1
0
def proc_and_copy_image (src, dest) :
  #print src
  img = cv2.imread(src)
  fore = 255 * np.ones([img.shape[0],img.shape[1]])
  skinfore = 255 * np.ones([img.shape[0],img.shape[1]])
  if args.remove_background :       
    fore = remove_background(src)
  if args.remove_skin :  
    skinfore = skin_detect(img) 
  fore = cv2.bitwise_and(fore, fore, mask = skinfore) # the foreground mask 
  b = img[:,:,0]
  g = img[:,:,1]
  r = img[:,:,2]
  img_merge = cv2.merge((b,g,r,fore))
  dot_index = dest.rfind('.')
  dest = dest[:dot_index] + '.png'
  cv2.imwrite(dest,img_merge)
예제 #2
0
def proc_and_copy_image (src, dest) :
  """Process image from src and write the result to dest."""
  if not args.remove_background and not args.remove_skin :
    shutil.copyfile(src, dest)
    return dest
  img = cv2.imread(src)
  fore = 255 * np.ones([img.shape[0],img.shape[1]])
  skinfore = 255 * np.ones([img.shape[0],img.shape[1]])
  if args.remove_background :
    fore = remove_background(src)
  if args.remove_skin :
    skinfore = skin_detect(img) 
  fore = cv2.bitwise_and(fore, fore, mask = skinfore) # the foreground mask 
  b = img[:,:,0]
  g = img[:,:,1]
  r = img[:,:,2]
  img_merge = cv2.merge((b,g,r,fore))
  dot_index = dest.rfind('.')
  dest = dest[:dot_index] + '.png'
  cv2.imwrite(dest, img_merge)
  return dest
예제 #3
0
  def detect(self, image_file_path) :
    img = cv2.imread(image_file_path)
    start_time = time.time()
    # generate the foreground
    background_removed = remove_background(image_file_path) # background removal
    skin_removed = skin_detect(img) # skin removal
    foreground = cv2.bitwise_and(background_removed, background_removed, mask = skin_removed) # the foreground mask
    skin_percentage = 1 - sum(map(sum, foreground))/1.0/(sum(map(sum, background_removed))+0.1)
    # In case of clothes in skin color.
    # print "%f skin detected." % skin_percentage
    if skin_percentage > self._max_skin_percentage:
      # print "Too much skin"
      foreground = background_removed
  
    # Find the nearest reference color for each pixel and count
    color_histogram = [0] * self._color_num
    image_foreground_pixel = 0
    for i in range(len(foreground)):
      for j in range(len(foreground[0])):
        if foreground[i][j] != 255:
          continue
        image_foreground_pixel += 1
        if self._color_table_builder.rgb_to_color[img[i][j][2], img[i][j][1], img[i][j][0]] < 0 :
          self._color_table_builder.reset_color(img[i][j][2], img[i][j][1], img[i][j][0])
  
        color_index = int(self._color_table_builder.rgb_to_color[img[i][j][2], img[i][j][1], img[i][j][0]])
        color_histogram[color_index] += 1

    # Decay colors.
    for decay_color in self._decay_color_index :
      color_histogram[decay_color] *= self._decay_percentage
  
    max_color_count = max(color_histogram)
    return (color_histogram.index(max_color_count),
        color_histogram,
        max_color_count / (float(image_foreground_pixel)+0.1),
        foreground)