def detect_face(record_id, filepath): """ 调用face++ API检测人脸并写入数据库 :param record_id: (int) 数据库记录 :param file_path: (string) 图像路径 :return: (int) 是否检测到人脸(默认选第一个) """ files = {'image_file': (os.path.basename(filepath), open(filepath, 'rb'), mimetypes.guess_type(filepath)[0]),} response = requests.post(url, files=files) info = response.json() FemaleFace.update(record_id, info=json.dumps(info)) faces = info.get('faces', []) print faces if not faces: return 0 landmark = faces[0].get('landmark',[]) print landmark if not landmark: return 0 FemaleFace.update(record_id, landmark=json.dumps(landmark)) return 1
def detect_face(record_id, file_path): """ 调用face++ API检测人脸并写入数据库 :param record_id: (int) 数据库记录 :param file_path: (string) 图像路径 :return: (int) 是否检测到人脸(默认选第一个) """ upload_url = '{}/detect?api_key={}&api_secret={}&return_landmark=1'.format( BASE_URL, API_KEY, API_SECRET) files = { 'image_file': (os.path.basename(file_path), open(file_path, 'rb'), mimetypes.guess_type(file_path)[0]), } response = requests.post(upload_url, files=files) info = response.json() FemaleFace.update(record_id, info=json.dumps(info)) faces = info.get('faces', []) if not faces: return 0 #print faces #face_id = faces[0].get('face_id', '') #landmark_url = '{}/landmark?api_key={}&api_secret={}&face_id={}&type=83p'.format( # BASE_URL, API_KEY, API_SECRET, face_id #) #response = requests.get(landmark_url) #landmarks = response.json().get('result', []) landmark = faces[0].get('landmark', []) if not landmark: return 0 FemaleFace.update(record_id, landmark=json.dumps(landmark)) return 1
def get_face_record(offset=0): """读取头像信息""" global face_record filename = get_filename(offset) face_record = FemaleFace.get(filename) if not face_record: face_record = FemaleFace.add(filename, label=-1)
def detect_face(record_id, file_path): """ 调用face++ API检测人脸并写入数据库 :param record_id: (int) 数据库记录 :param file_path: (string) 图像路径 :return: (int) 是否检测到人脸(默认选第一个) """ upload_url = '{}/detection/detect?api_key={}&api_secret={}&attribute=pose'.format( BASE_URL, API_KEY, API_SECRET ) files = {'img': (os.path.basename(file_path), open(file_path, 'rb'), mimetypes.guess_type(file_path)[0]), } response = requests.post(upload_url, files=files) info = response.json() FemaleFace.update(record_id, info=json.dumps(info)) faces = info.get('face', []) if not faces: return 0 face_id = faces[0].get('face_id', '') landmark_url = '{}/detection/landmark?api_key={}&api_secret={}&face_id={}&type=83p'.format( BASE_URL, API_KEY, API_SECRET, face_id ) response = requests.get(landmark_url) landmarks = response.json().get('result', []) if not landmarks: return 0 landmark = landmarks[0].get('landmark') if not landmark: return 0 FemaleFace.update(record_id, landmark=json.dumps(landmark)) return 1
def readImages(fileList) : imagesArray, pointsArray = [], [] #List all files in the directory and read points from text files one by one for filePath in fileList: if filePath.endswith(".jpg"): # Read image found. img = cv2.imread(filePath) # Convert to floating point img = np.float32(img)/255.0 # Add to array of images imagesArray.append(img) filename = os.path.basename(filePath) landmark = json.loads(FemaleFace.get(filename).landmark) pointsArray.append(readPoints(landmark)) return imagesArray, pointsArray
# Copy triangular region of the rectangular patch to the output image img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] = img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] * ( (1.0, 1.0, 1.0) - mask ) img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] = img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] + img2Rect if __name__ == '__main__' : # Dimensions of output image w = 600 h = 600 # 0丑 1一般 2漂亮 fileList = FemaleFace.get_file_by_label(2) fileList = [u'../data/{}'.format(x) for x in fileList] # Read all images images, allPoints = readImages(fileList) # Eye corners eyecornerDst = [ (np.int(0.35 * w ), np.int(h / 2)), (np.int(0.65 * w ), np.int(h / 2)) ] imagesNorm = [] pointsNorm = [] # Add boundary points for delaunay triangulation boundaryPts = np.array([(0,0), (w/2,0), (w-1,0), (w-1,h/2), ( w-1, h-1 ), ( w/2, h-1 ), (0, h-1), (0,h/2) ]) # Initialize location of average points to 0s pointsAvg = np.array([(0,0)]* ( len(allPoints[0]) + len(boundaryPts) ), np.float32())
def set_face_label(): """设置头像标签""" print face_record.id,face_label.get() FemaleFace.update(face_record.id, label=face_label.get()) handle_next()
# Copy triangular region of the rectangular patch to the output image img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] = img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] * ( (1.0, 1.0, 1.0) - mask ) img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] = img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] + img2Rect if __name__ == '__main__' : # Dimensions of output image w = 500 h = 500 # 0丑 1一般 2漂亮 fileList = FemaleFace.get_file_by_label(2) fileList = [u'/Users/ruoyuliu/Downloads/aaa/{}'.format(x) for x in fileList] # Read all images images, allPoints = readImages(fileList) # Eye corners eyecornerDst = [ (np.int(0.3 * w ), np.int(h / 3)), (np.int(0.7 * w ), np.int(h / 3)) ] imagesNorm = [] pointsNorm = [] # Add boundary points for delaunay triangulation boundaryPts = np.array([(0,0), (w/2,0), (w-1,0), (w-1,h/2), ( w-1, h-1 ), ( w/2, h-1 ), (0, h-1), (0,h/2) ]) # Initialize location of average points to 0s pointsAvg = np.array([(0,0)]* ( len(allPoints[0]) + len(boundaryPts) ), np.float32())