def test_shape_list_images(): """ Test to check the dimensions when using a list of images instead of a single image. This is used with analyzing batches of images with similar dimensions """ image_rgb = convert_color_channels(TEST_IMAGE) # Creating a list with 3 repeated images images = [image_rgb, image_rgb, image_rgb] boxes, probs, faces = face_detection_mtcnn.detect_face(images) boxes_shape = np.array(boxes).shape probs_shape = np.array(probs).shape faces_shape = np.array(faces).shape # There should be 3 images with 18 faces with 4 points marking the # bounding box for the face assert boxes_shape == (3, 18, 4) # There should be 3 images with 18 probs to indicate the probability # of each bounding box to belong to a face assert probs_shape == (3, 18) # There should be 3 images with 18 faces with 5 points. Each point should # have to values that represent the location of the 5 landmarks # in the face assert faces_shape == (3, 18, 5, 2)
def test_shape_one_image(): """ Test to check if dimensions when using one image are correct """ image_rgb = convert_color_channels(TEST_IMAGE) boxes, probs, faces = face_detection_mtcnn.detect_face(image_rgb) boxes_shape = np.array(boxes).shape probs_shape = np.array(probs).shape faces_shape = np.array(faces).shape # There should be 18 faces with 4 points marking the # bounding box for the face assert boxes_shape == (1, 18, 4) # There should be 18 probs to indicate the probability # of each bounding box to belong to a face assert probs_shape == (1, 18) # There should be 18 faces with 5 points. Each point should # have to values that represent the location of the 5 landmarks # in the face assert faces_shape == (1, 18, 5, 2)
def get_require_direction(indir, outdir): if not os.path.exists(outdir): os.makedirs(outdir) MIN_FACE_PER_DIRECTION = 10 four_drawed_region_counter = [0] * 5 four_drawed_region_files = dict() for i in range(5): four_drawed_region_files[i] = [] list_images_file = glob.glob(os.path.join(indir, '*/*.jpg')) + glob.glob( os.path.join(indir, '*.jpg')) for image_file in list_images_file: img = cv2.imread(image_file) img = cv2.flip(img, 1) location, points = detect_face(img, MIN_SIZE, MAX_SIZE) if len(location) > 0: max_size = -1 best_index = -1 for i in range(len(location)): (l, t, r, b) = location[i] size = (r - l) * (b - t) if size > max_size: max_size = size best_index = i face_location = location[best_index] face = img[t:b, l:r] if face.shape[0] > 0 and face.shape[1] > 0: is_good = util.is_good(face, points[:, best_index]) # if is_good: (x1, y1), (x2, y2), (x3, y3), (x4, y4), (x_nose, y_nose) = util.get_point_coords( points[:, best_index]) new_region = evaluate_region_direction(face_location, (x1, y1), (x2, y2), (x3, y3), (x4, y4), (x_nose, y_nose), img) four_drawed_region_counter[new_region] += 1 if four_drawed_region_counter[ new_region] <= MIN_FACE_PER_DIRECTION: four_drawed_region_files[new_region].append(image_file) basename = os.path.basename(image_file) basename = basename[:basename.rfind('.')] write_file = os.path.join( outdir, basename + '_' + str(new_region) + '.jpg') cv2.imwrite(write_file, img) require_direction = [] for i in range(5): if len(four_drawed_region_files[i]) < MIN_FACE_PER_DIRECTION: require_direction.append(i) return require_direction
def test_number_detections(): """ Test to check if the correct number of detections was done using the detect_face function """ image_rgb = convert_color_channels(TEST_IMAGE) boxes, probs, faces = face_detection_mtcnn.detect_face(image_rgb) assert len(boxes[0]) == FACES_IN_IMAGE assert len(probs[0]) == FACES_IN_IMAGE assert len(faces[0]) == FACES_IN_IMAGE
def get_require_direction(indir): base_folder = indir[:indir.rfind('/')] indir_name = indir[:indir.rfind('/')] outdir_name = 'tmp_out' outdir = os.path.join(base_folder, outdir_name) # os.system("rm -rf " + outdir) if not os.path.exists(outdir): os.makedirs(outdir) if DEBUG: test_outdir = '/home/cuong/VNG/temp/face_system/data/test_data/test_result' tmp_test_outdir = '/home/cuong/VNG/temp/face_system/data/test_data/tmp_test_outdir' os.system("rm -rf " + test_outdir) os.system("mkdir " + test_outdir) os.system("rm -rf " + tmp_test_outdir) os.system("mkdir " + tmp_test_outdir) four_drawed_region_counter = [0] * 5 four_drawed_region_files = dict() for i in range(5): four_drawed_region_files[i] = [] vecto_length_each_region = dict() for i in range(5): vecto_length_each_region[i] = [] list_images_file = glob.glob(os.path.join(indir, '*/*.jpg')) + glob.glob( os.path.join(indir, '*.jpg')) ignore_img_count = 0 rotation_angle = None new_file_count = 0 for image_file in list_images_file: filename = os.path.split(image_file)[-1] filename = filename[:filename.rfind('.')] postfix = filename.split('_') if len(postfix) >= 3 and postfix[-1] == 'processed': direction = int(postfix[-3]) vector_length = float(postfix[-2]) four_drawed_region_counter[direction] += 1 four_drawed_region_files[direction].append(image_file) vecto_length_each_region[direction].append(vector_length) continue else: new_file_count += 1 img = cv2.imread(image_file) padding_img = add_padding_to_img(img, 50) if rotation_angle is None: location, points, angle = detect_face_all_directions( padding_img, MIN_SIZE, MAX_SIZE) rotation_angle = angle if rotation_angle is None: continue img = preprocess_image(img, rotation_angle=rotation_angle) else: img = preprocess_image(img, rotation_angle=rotation_angle) location, points = detect_face(padding_img, MIN_SIZE, MAX_SIZE) cv2.imwrite(image_file, img) if len(location) > 0: max_size = -1 best_index = -1 for i in range(len(location)): (l, t, r, b) = location[i] size = (r - l) * (b - t) if size > max_size: max_size = size best_index = i face_location = location[best_index] face = img[t:b, l:r] if face.shape[0] > 0 and face.shape[1] > 0: # (x1,y1), (x2,y2), (x3,y3), (x4, y4), (x_nose, y_nose) = util.get_point_coords(points[:, best_index]) (x1, y1), (x2, y2), (x3, y3), (x4, y4), (x_nose, y_nose) = get_point_coords( points[:, best_index]) new_region, vector_length = evaluate_region_direction( face_location, (x1, y1), (x2, y2), (x3, y3), (x4, y4), (x_nose, y_nose), img) four_drawed_region_counter[new_region] += 1 four_drawed_region_files[new_region].append(image_file) vecto_length_each_region[new_region].append(vector_length) if DEBUG: basename = os.path.basename(image_file) basename = basename[:basename.rfind('.')] test_write_file = os.path.join( test_outdir, basename + '_' + str(new_region) + '.jpg') cv2.imwrite(test_write_file, img) tmp_test_write_file = os.path.join( tmp_test_outdir, basename + '_' + str(new_region) + '.jpg') cv2.imwrite(tmp_test_write_file, img) else: print(image_file) ignore_img_count += 1 else: print(image_file) ignore_img_count += 1 print 'file ignore = ', ignore_img_count print 'new file =', new_file_count print 'file consider = ', len(list_images_file) - ignore_img_count final_four_drawed_region_files = dict() for i in range(5): final_four_drawed_region_files[i] = [] final_vecto_length_each_region = dict() for i in range(5): final_vecto_length_each_region[i] = [] require_direction = [] for i in [0, 1, 2, 3, 4]: if check_false_condition(i, four_drawed_region_counter): require_direction.append(i) end_index = four_drawed_region_counter[i] else: end_index = min(MIN_FACE_DIRECTION_DICT[i] + 4, four_drawed_region_counter[i]) if i == 0: reverse = False else: reverse = True file_and_vector_length = [ [f, vector_length] for f, vector_length in zip( four_drawed_region_files[i], vecto_length_each_region[i]) ] file_and_vector_length = sorted(file_and_vector_length, key=lambda element: element[1], reverse=reverse) if len(file_and_vector_length) > 0: for image_file_and_vector_length in file_and_vector_length[: end_index]: image_file, vector_length = image_file_and_vector_length final_four_drawed_region_files[i].append(image_file) final_vecto_length_each_region[i].append(vector_length) image_number = 0 for i in range(5): print 'Huong ' + str(i) + ': ', four_drawed_region_counter[i], len( final_four_drawed_region_files[i]) image_number += len(final_four_drawed_region_files[i]) print 'image_number = ', image_number for i in range(5): for image_file, vector_length in zip( final_four_drawed_region_files[i], final_vecto_length_each_region[i]): basename = os.path.basename(image_file) basename = basename[:basename.rfind('.')] postfix = basename.split('_') if len(postfix) >= 3 and postfix[-1] == 'processed': write_file = os.path.join(outdir, basename + '.jpg') else: write_file = os.path.join( outdir, basename + '_' + str(i) + '_' + str(vector_length)[:min(5, len(str(vector_length)))] + '_processed' + '.jpg') img = cv2.imread(image_file) cv2.imwrite(write_file, img) if DEBUG: for i in range(5): for test_image_file in set(four_drawed_region_files[i]) - set( final_four_drawed_region_files[i]): basename = os.path.basename(test_image_file) basename = basename[:basename.rfind('.')] os.rename( os.path.join(test_outdir, basename + '_' + str(i) + '.jpg'), os.path.join(test_outdir, basename + '_' + str(i) + 'x.jpg')) ''' remain_region_files = [] remain_vector_length = [] for i in range(5): for f, vector_length in zip(four_drawed_region_files[i], vecto_length_each_region[i]): if f not in final_four_drawed_region_files[i]: remain_region_files.append(f) remain_vector_length.append(f) add_final_four_drawed_region_files = [] file_and_vector_length = [[f, vector_length] for f, vector_length in zip(remain_region_files, remain_vector_length)] file_and_vector_length = sorted(file_and_vector_length, key=lambda element:element[1], reverse=True) if len(file_and_vector_length) > 0: for image_file_and_vector_length in file_and_vector_length[:min(80 - image_number, len(file_and_vector_length))]: image_file = image_file_and_vector_length[0] add_final_four_drawed_region_files.append(image_file) for image_file in add_final_four_drawed_region_files: basename = os.path.basename(image_file) basename = basename[:basename.rfind('.')] write_file = os.path.join(outdir, basename + '_' +str(i) +'.jpg') img = cv2.imread(image_file) cv2.imwrite(write_file ,img) # new_write_file = glob.glob(os.path.join(test_outdir, basename) + '*')[0] # not_extend_write_file = new_write_file[:new_write_file.rfind('.')] # os.rename(new_write_file, not_extend_write_file +'o.jpg') for i in range(5): not_satify_set = set(four_drawed_region_files[i]) - set(final_four_drawed_region_files[i]) - set(add_final_four_drawed_region_files) for test_image_file in not_satify_set: basename = os.path.basename(test_image_file) basename = basename[:basename.rfind('.')] # new_write_file = glob.glob(os.path.join(test_outdir, basename) + '*')[0] # writefile_basename = os.path.basename(new_write_file) # writefile_basename = writefile_basename[:writefile_basename.rfind('x.')] # os.remove(os.path.join(tmp_test_outdir, writefile_basename) +'.jpg') print 'added image number = ', len(add_final_four_drawed_region_files) print 'final image_number = ', image_number + len(add_final_four_drawed_region_files) ''' os.system("rm -rf " + indir) os.rename(outdir, indir) return require_direction
def get_require_direction(indir): base_folder = indir[:indir.rfind('/')] user_id = indir.split('/')[-1] outdir_name = 'tmp_out_' + user_id outdir = os.path.join(base_folder, outdir_name) raw_upload = os.path.join(config.face_upload_raw, user_id) if not os.path.exists(raw_upload): os.makedirs(raw_upload) # os.system("rm -rf " + outdir) if not os.path.exists(outdir): os.makedirs(outdir) if DEBUG: test_outdir = '/home/cuong/VNG/temp/face_system/data/test_data/test_result' tmp_test_outdir = '/home/cuong/VNG/temp/face_system/data/test_data/tmp_test_outdir' os.system("rm -rf " + test_outdir) os.system("mkdir " + test_outdir) os.system("rm -rf " + tmp_test_outdir) os.system("mkdir " + tmp_test_outdir) four_drawed_region_counter = [0] * 5 four_drawed_region_files = dict() for i in range(5): four_drawed_region_files[i] = [] vecto_length_each_region = dict() for i in range(5): vecto_length_each_region[i] = [] list_images_file = glob.glob(os.path.join(indir, '*/*.jpg')) + glob.glob( os.path.join(indir, '*.jpg')) ignore_img_count = 0 rotation_angle = None new_file_count = 0 for image_file in list_images_file: filename = os.path.split(image_file)[-1] filename = filename[:filename.rfind('.')] postfix = filename.split('_') if len(postfix) >= 3 and postfix[-1] == 'processed': direction = int(postfix[-3]) vector_length = float(postfix[-2]) four_drawed_region_counter[direction] += 1 four_drawed_region_files[direction].append(image_file) vecto_length_each_region[direction].append(vector_length) continue else: new_file_count += 1 img = cv2.imread(image_file) img = cv2.resize(img, (960, 540)) try: shutil.move(image_file, raw_upload) except: try: random_name = np.random.randint(0, 10000) cv2.imwrite( os.path.join(raw_upload, str(random_name) + '.jpg'), img) os.remove(image_file) except: pass # padding_img = add_padding_to_img(img, 50) img = preprocess_image(img, image_size=config.MAX_IMAGE_SIZE) if rotation_angle is None: location, points, angle = detect_face_all_directions( img, MIN_SIZE, MAX_SIZE) rotation_angle = angle if rotation_angle is None: continue img = preprocess_image(img, rotation_angle=rotation_angle) else: img = preprocess_image(img, rotation_angle=rotation_angle) location, points = detect_face(img, MIN_SIZE, MAX_SIZE) if len(location) > 0: max_size = -1 best_index = -1 for i in range(len(location)): (l, t, r, b) = location[i] size = (r - l) * (b - t) if size > max_size: max_size = size best_index = i face_location = location[best_index] face = img[t:b, l:r] cv2.imwrite(image_file, face) if face.shape[0] > 0 and face.shape[1] > 0: is_low_quality = util.low_quality(face) if not is_low_quality: (x1, y1), (x2, y2), (x3, y3), (x4, y4), (x_nose, y_nose) = get_point_coords( points[:, best_index]) new_region, vector_length = evaluate_region_direction( face_location, (x1, y1), (x2, y2), (x3, y3), (x4, y4), (x_nose, y_nose), img) four_drawed_region_counter[new_region] += 1 four_drawed_region_files[new_region].append(image_file) vecto_length_each_region[new_region].append(vector_length) if DEBUG: basename = os.path.basename(image_file) basename = basename[:basename.rfind('.')] test_write_file = os.path.join( test_outdir, basename + '_' + str(new_region) + '.jpg') cv2.imwrite(test_write_file, img) tmp_test_write_file = os.path.join( tmp_test_outdir, basename + '_' + str(new_region) + '.jpg') cv2.imwrite(tmp_test_write_file, img) else: # print(image_file) ignore_img_count += 1 else: # print(image_file) ignore_img_count += 1 # print 'file ignore = ', ignore_img_count # print 'new file =', new_file_count # print 'file consider = ', len(list_images_file) - ignore_img_count final_four_drawed_region_files = dict() for i in range(5): final_four_drawed_region_files[i] = [] final_vecto_length_each_region = dict() for i in range(5): final_vecto_length_each_region[i] = [] require_direction = [] for i in [0, 1, 2, 3, 4]: if check_false_condition(i, four_drawed_region_counter): require_direction.append(i) end_index = four_drawed_region_counter[i] else: end_index = min(MIN_FACE_DIRECTION_DICT[i] + 4, four_drawed_region_counter[i]) if i == 0: reverse = False else: reverse = True file_and_vector_length = [ [f, vector_length] for f, vector_length in zip( four_drawed_region_files[i], vecto_length_each_region[i]) ] file_and_vector_length = sorted(file_and_vector_length, key=lambda element: element[1], reverse=reverse) if len(file_and_vector_length) > 0: for image_file_and_vector_length in file_and_vector_length[: end_index]: image_file, vector_length = image_file_and_vector_length final_four_drawed_region_files[i].append(image_file) final_vecto_length_each_region[i].append(vector_length) image_number = 0 for i in range(5): # print 'Huong ' + str(i) + ': ', four_drawed_region_counter[i], len(final_four_drawed_region_files[i]) image_number += len(final_four_drawed_region_files[i]) # print 'image_number = ', image_number for i in range(5): for image_file, vector_length in zip( final_four_drawed_region_files[i], final_vecto_length_each_region[i]): basename = os.path.basename(image_file) basename = basename[:basename.rfind('.')] postfix = basename.split('_') if len(postfix) >= 3 and postfix[-1] == 'processed': write_file = os.path.join(outdir, basename + '.jpg') else: basename = basename + str(np.random.randint(0, 1000)) write_file = os.path.join( outdir, basename + '_' + str(i) + '_' + str(vector_length)[:min(5, len(str(vector_length)))] + '_processed' + '.jpg') img = cv2.imread(image_file) cv2.imwrite(write_file, img) if DEBUG: for i in range(5): for test_image_file in set(four_drawed_region_files[i]) - set( final_four_drawed_region_files[i]): basename = os.path.basename(test_image_file) basename = basename[:basename.rfind('.')] os.rename( os.path.join(test_outdir, basename + '_' + str(i) + '.jpg'), os.path.join(test_outdir, basename + '_' + str(i) + 'x.jpg')) os.system("rm -rf " + indir) os.rename(outdir, indir) return require_direction