def get_face_alignment(image_bytes): im = transform_image(image_bytes=image_bytes) # Detect Landmark points = fbc.getLandmarks(faceDetector, landmarkDetector, im) points = np.array(points) print('Landmarks detected') # Convert image to floating point in the range 0 to 1 im = np.float32(im) / 255.0 # Specify the size of aligned face image. Compute the normalized image by using the similarity transform # Dimension of output Image h = im.shape[0] # 600 w = im.shape[1] # 600 # Normalize the image to output coordinates imNorm, points = fbc.normalizeImagesAndLandmarks((h, w), im, points) imNorm = np.uint8(imNorm * 255) print("Aligned image is made.....") # This is aligned image return imNorm
def perform_face_alignment(image_bytes): h=w=600 points = fbc.getLandmarks(faceDetector, landmarkDetector, image_bytes) points = np.array(points) image_bytes = np.float32(image_bytes)/255.0 imNorm, points = fbc.normalizeImagesAndLandmarks((h,w), image_bytes, points) imNorm = np.uint8(imNorm * 255) return imNorm#[:,:,::-1]
def align_face(bytes_im): im = transform_image(image_bytes=bytes_im) points = fbc.getLandmarks(faceDetector, landmarkDetector5, im) points = np.array(points) im = np.float32(im) / 255.0 # Dimensions of the output image h = im.shape[0] w = im.shape[1] imNorm, points = fbc.normalizeImagesAndLandmarks((h, w), im, points) imNorm = np.uint8(imNorm * 255) return imNorm
def get_aligned_image(im): try: face_detector = dlib.get_frontal_face_detector() points = fbc.getLandmarks(face_detector, landmark_detector, im) points = np.array(points) im = np.float32(im) / 255.0 print("Inside the get_aligned_face function.") h = 600 w = 600 imNorm, points = fbc.normalizeImagesAndLandmarks((h, w), im, points) imNorm = np.uint8(imNorm * 255) return imNorm return except Exception as e: print(repr(e)) raise (e)
def classify_image (event, context): try: content_type_header = event['headers']['content-type'] body = base64.b64decode(event["body"]) picture = decoder.MultipartDecoder(body, content_type_header).parts[0] im_arr = np.frombuffer(picture.content, dtype=np.uint8) im = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR) points = fbc.getLandmarks(faceDetector, landmarkDetector, im) points = np.array(points) im = np.float32(im)/255.0 h = 600 w = 600 imNorm, points = fbc.normalizeImagesAndLandmarks((h, w), im, points) imNorm = np.uint8(imNorm*255) filename = picture.headers[b'Content-Disposition'].decode().split(';')[1].split('=')[1] if len(filename) < 4: filename = picture.headers[b'Content-Disposition'].decode().split(';')[2].split('=')[1] print ('all done') return { "statusCode": 200, "headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', "Access-Control-Allow-Credentials": True }, "body": json.dumps({'file': filename.replace('"', ''), 'aligned_image': str (base64.b64encode (cv2.imencode ('.jpg', imNorm)[1])) }) } except Exception as e: print(repr(e)) return { "statusCode": 500, "headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', "Access-Control-Allow-Credentials": True }, "body": json.dumps({"error": repr(e)}) }
def get_aligned_image(image_bytes): print('Alignment process started...') (faceDetector, landmarkDetector) = face_landmark_detector() im = transform_image(image_bytes) # Detect landmarks points = fbc.getLandmarks(faceDetector, landmarkDetector, im) points = np.array(points) landmarksDetected = len(points) print(f'Landmarks detected: {landmarksDetected}') if landmarksDetected == 0: return None im = np.float32(im)/255.0 h = im.shape[0] # 600 w = im.shape[1] # 600 imNorm, points = fbc.normalizeImagesAndLandmarks((h, w), im, points) imNorm = np.uint8(imNorm * 255) return imNorm # aligned image
# 归一化图片和特征点 imagesNorm = [] pointsNorm = [] # 平均值 pointsAvg = np.zeros((numLandmarks,2), dtype=np.float32) # 将图片规整到输出坐标系中并求解平均值 for i, img in enumerate(images): points = allPoints[i] points = np.array(points) # print(points) img, points = fbc.normalizeImagesAndLandmarks((h,w),img,points) cv2.imshow("{}".format(i), img) pointsAvg = pointsAvg + (points/(1.0*numImages)) # 添加边界点 points = np.concatenate((points, boundaryPts), axis=0) pointsNorm.append(points) imagesNorm.append(img) pointsAvg = np.concatenate((pointsAvg, boundaryPts), axis=0) # 计算delaunay三角形 rect = (0, 0, w, h)
def align_face(event,context): try: print(event) print(context) content_type_header = event['headers']['content-type'] print(event['body']) body = base64.b64decode(event["body"]) print(body) print("BODY LOADED...") content_type = event.get('headers', {"content-type" : ''}).get('content-type') # body = bytes(body,'utf-8') multipart_data = decoder.MultipartDecoder(body, content_type).parts[0] img_io = io.BytesIO(multipart_data.content) img = Image.open(img_io) img = cv2.cvtColor(np.array(img),cv2.COLOR_BGR2RGB) PREDICTOR_PATH = "shape_predictor_5_face_landmarks.dat" faceDetector = dlib.get_frontal_face_detector() landmarkDetector = dlib.shape_predictor(PREDICTOR_PATH) points = fbc.getLandmarks(faceDetector, landmarkDetector, img) if len(points) == 0: return { "statusCode": 202, "headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', "Access-Control-Allow-Credentials": True }, "body": json.dumps({'error': 'No Face detected in the image'}) } points = np.array(points) img = np.float32(img)/255.0 h = 600 w = 600 #Normalize image to output co-ord imNorm, points = fbc.normalizeImagesAndLandmarks((h,w), img, points) imNorm = np.uint8(imNorm*255) #encoded_img = base64.b64encode(imNorm).decode("utf-8") serialized_img = base64.b64encode(cv2.imencode('.jpg', imNorm)[1]).decode() return { "statusCode": 200, "headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', "Access-Control-Allow-Credentials": True }, "body": json.dumps({'aligned': serialized_img}) } except Exception as e: print(repr(e)) return { "statuscode" : 500, "headers" : { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Credentials' : True }, "body": json.dumps({"error": repr(e)}) }
img2 = cv2.imread("../data/images/girls/hexiaoping.jpg") # 获取关键特征点 points1 = fbc.getLandmarks(faceDetector, landmarkDetector, img1) points2 = fbc.getLandmarks(faceDetector, landmarkDetector, img2) points1 = np.array(points1) points2 = np.array(points2) img1 = np.float32(img1) / 255.0 img2 = np.float32(img2) / 255.0 h = 480 w = 480 imgNorm1, points1 = fbc.normalizeImagesAndLandmarks((h, w), img1, points1) imgNorm2, points2 = fbc.normalizeImagesAndLandmarks((h, w), img2, points2) pointsAvg = (points1 + points2) / 2.0 # 边界点 boundaryPoints = fbc.getEightBoundaryPoints(h, w) points1 = np.concatenate((points1, boundaryPoints), axis=0) points2 = np.concatenate((points2, boundaryPoints), axis=0) pointsAvg = np.concatenate((pointsAvg, boundaryPoints), axis=0) # 就算细分三角形 rect = (0, 0, w, h) dt = fbc.calculateDelaunayTriangles(rect, pointsAvg) winName = "Face Morphing"