gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #得到灰度图像 faces = face_cascade.detectMultiScale(gray) #使用opencv自带库寻找照片中是否有人脸 if (len(faces) > 0): #返回值大于0 存在人脸 face_re = True else: face_re = False time.sleep(2) print face_re if (face_re): #如何人脸存在 res = api.detect(api_key=API_KEY, api_secret=API_SECRET, image_file=File(img_re)) print_result(printFuctionTitle("人脸检测"), res) # 人脸比对:https://console.faceplusplus.com.cn/documents/4887586 compare_res = api.compare(api_key=API_KEY, api_secret=API_SECRET, image_file1=File(img2_re), image_file2=File(img_re)) #compare_res = api.compare(image_file1=File(face_search_img), image_file2=File(face_search_img)) print_result(printFuctionTitle("compare"), compare_res) print compare_res.confidence #打印俩张照片的置信度 越高则越相似 # 人脸搜索:https://console.faceplusplus.com.cn/documents/4888381 # 人脸搜索步骤 # 1,创建faceSet:用于存储人脸信息(face_token) # 2,向faceSet中添加人脸信息(face_token) # 3,开始搜索 # 删除无用的人脸库,这里删除了,如果在项目中请注意是否要删除 # api.faceset.delete(outer_id='faceplusplus', check_empty=0) # # 1.创建一个faceSet # ret = api.faceset.create(outer_id='faceplusplus') #
class Analyze(object): def __init__(self, path_to_analyze_config, output_dir_name="analyzed_result", wait_time=None): self.path_to_analyze_config = path_to_analyze_config with open(self.path_to_analyze_config, "r") as f: self.config = yaml.load(f, Loader=yaml.SafeLoader) self.output_dir_name = output_dir_name self.wait_time = wait_time #自然数で指定する必要がある. if self.config["output_json_name"] is None: self.output_json_name = "output" else: self.output_json_name = self.config["output_json_name"] self.api = API(API_KEY=self.config["API_KEY"], API_SECRET=self.config["API_SECRET"]) del self.config["API_KEY"], self.config["API_SECRET"] def get_dataset(self, serch_config_name="config_camera.yaml", path_to_image=None): if path_to_image is None: path_to_image = self.config["target_dir_name"] if serch_config_name in os.listdir(path_to_image): self.abs_path = os.path.abspath(path_to_image) path_to_camera_config = os.path.join( *[self.abs_path, serch_config_name]) with open(path_to_camera_config, "r") as f: self.config_camera = yaml.load(f, Loader=yaml.SafeLoader) self.path_to_image = os.path.join( *[self.abs_path, self.config_camera["image_dir_name"]]) else: print("'{}' do not have Image dirctory, ex) {}".format( path_to_image, os.path.join(*[path_to_image, "Images/"]))) exit() path_list = glob.glob( os.path.join(*[ self.path_to_image, "*{}".format( self.config_camera['image_style']) ])) path_list = sorted(path_list, key=lambda x: int(x.split("/")[-1].split(".")[0])) output_dir_path = os.path.join(*[self.abs_path, self.output_dir_name]) os.makedirs(output_dir_path, exist_ok=True) with open( os.path.join(*[ output_dir_path, self.path_to_analyze_config.split("/")[-1] ]), "w") as f: yaml.dump(self.config, f) return path_list def choise_function(self, encode_image, api_kind): if api_kind == "Detect": return self.api.detect(image_base64=encode_image, return_landmark=1, return_attributes=",".join( self.config["Attribute_Detect"])) elif api_kind == "Dence": return self.api.face.thousandlandmark(image_base64=encode_image, return_landmark="all") def detect(self, path_list, api_kind): print("-" * 10 + api_kind + "-" * 10) output_list = [] p_bar = ProgressBar(maxval=len(path_list)) for i, each_file in enumerate(path_list): p_bar.update(i) encode_img = self.img_to_bese64(each_file) output = self.choise_function(encode_image=encode_img, api_kind=api_kind) output["image_path"] = each_file output_list.append(output) if (api_kind == "Dence") and (self.wait_time is not None): import time time.sleep(int(self.wait_time)) #実行制限の関係から,ここの値を調整する必要がある. json_name, file_name = self.get_json_name(api_kind) self.save_output(json_name, file_name, output_list) def compere(self, path_list, target_file=None): if target_file is None: print("target_file is None, pleasse give any path to image file") exit() print("-" * 10 + "Get Compere" + "-" * 10) p_bar = ProgressBar(maxval=len(path_list)) output_list = [] if target_file is None: target_file = self.config["target_file"] target_img = self.img_to_bese64(target_file) for i, each_file in enumerate(path_list): p_bar.update(i) if each_file != target_file: each_img = self.img_to_bese64(each_file) output = self.api.compare(image_base64_1=target_img, image_base64_2=each_img) output["image_path"] = each_img output_list.append(output) json_name, file_name = self.get_json_name( "Compere", name=target_file.split("/")[-1].split(".")[0]) self.save_output(json_name, file_name, output_list) def get_json_name(self, api_kind, name=None, name_concat=False): if name is None: if not name_concat: return os.path.join( *[self.abs_path, self.output_dir_name, api_kind ]), self.output_json_name + ".json" else: return os.path.join(*[ self.abs_path, self.output_dir_name, api_kind, self.output_json_name + ".json" ]) else: if not name_concat: return os.path.join( *[self.abs_path, self.output_dir_name, api_kind ]), name + ".json" else: return os.path.join(*[ self.abs_path, self.output_dir_name, api_kind, name + ".json" ]) def save_output(self, path, file_name, output_list): os.makedirs(path, exist_ok=True) with open(os.path.join(*[path, file_name]), "w") as f: json.dump(output_list, f) def img_to_bese64(self, target_file): with open(target_file, 'rb') as f: data = f.read() return base64.b64encode(data) def __call__(self, target_file=None, path_to_image=None): path_list = self.get_dataset(path_to_image=path_to_image) if self.config["USE_API_Detect"]: self.detect(path_list, api_kind="Detect") Detect_api_json_to_csv( path_to_json=self.get_json_name("Detect", name_concat=True))() if self.config["USE_API_Compare"]: self.compere(path_list, target_file) if self.config["USE_API_Dense_Facial_Landmarks"]: self.detect(path_list, "Dence") Dence_api_json_to_csv( path_to_json=self.get_json_name("Dence", name_concat=True))() sys.exit()