def detect_image(model, file): image = common.imread(file) objs = detect(model, image) for obj in objs: common.drawbbox(image, obj) common.imwrite("detect_result/" + common.file_name_no_suffix(file) + ".draw.jpg", image)
def detect_image_list(image_list: typing.List, output_dir, align_face: bool = False): if output_dir != "" and not os.path.exists(output_dir): raise ValueError(f"output folder ({output_dir}) is not existed") ret = {} for image_file_name in image_list: print(f"detect for {image_file_name}") output_file = "" if output_dir != "": base_name = common.file_name_no_suffix(image_file_name) output_file = f"{output_dir}/{base_name}.draw.jpg" face_info = do_detect_image(image_file_name, output_file, align_face) ret[image_file_name] = face_info return ret
def _do_align_face(raw_image: np.array, face_infos: List[common.BBox], output_filename: str): for inx, face_info in enumerate(face_infos): if not face_info: continue info = face_info.json landmark = info["landmark"] bbox = info["bbox"] aligned_face_image = align_face(raw_image, landmark, bbox) if output_filename == "": out_face_file = f"/tmp/aligned_face_{inx}.jpg" else: no_suffix_name = common.file_name_no_suffix(output_filename) base_dir = os.path.dirname(output_filename) out_face_file = f"{base_dir}/{no_suffix_name}_aligned_face_{inx}.jpg" cv2.imwrite(out_face_file, aligned_face_image) with open(out_face_file, "rb") as image_file: encoded_image = base64.b64encode(image_file.read()) face_encode_str = encoded_image.decode('utf-8') face_info.aligned_face = face_encode_str return face_infos
mean = [0.408, 0.447, 0.47] std = [0.289, 0.274, 0.278] files, anns = zip( *common.load_webface("webface/val/label.txt", "webface/WIDER_val/images")) # forward and summary prefix = "webface/WIDER_val/images/" all_result_dict = {} total_file = len(files) for i in range(total_file): # preper key and file_name file = files[i] key = file[len(prefix):file.rfind("/")] file_name = common.file_name_no_suffix(file) # load image and forward image = common.imread(file) objs = eval_tool.detect_image(model, image, mean, std, 0.01) # summary to all_result_dict image_pred = [] for obj in objs: image_pred.append(obj.xywh + [obj.score]) # build all_result_dict if key not in all_result_dict: all_result_dict[key] = {} all_result_dict[key][file_name] = np.array(image_pred)