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 getCroppedEyeRegion(targetImage): landmarks = fbc.getLandmarks(detector, predictor, cv2.cvtColor(targetImage, cv2.COLOR_BGR2RGB)) #Get points from landmarks detector x1 = landmarks[0][0] x2 = landmarks[16][0] y1 = min(landmarks[24][1], landmarks[19][1]) y2 = landmarks[29][1] cropped = targetImage[y1:y2, x1:x2, :] cropped = cv2.resize(cropped, (96, 32), interpolation=cv2.INTER_CUBIC) return cropped
def lip_makeup(im, color): # Read image #im = cv2.imread(im_path) imDlib = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) #img1Warped = np.copy(imDlib) landmarks = fbc.getLandmarks(faceDetector, landmarkDetector, imDlib) #selectedIndex_upper_lip = [ 48, 49, 50, 51, 52, 53, 54, 61, 62, 63] #selectedIndex_botto_lip = [ 55, 56, 57, 58, 59, 60, 64, 65, 66, 67] selectedIndex_upper_lip = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59] selectedIndex_botto_lip = [60, 61, 62, 63, 64, 65, 66, 67] hull1, hull2 = [], [] for i in selectedIndex_upper_lip: hull1.append(landmarks[i]) for j in selectedIndex_botto_lip: hull2.append(landmarks[j]) mask1 = np.zeros(imDlib.shape, dtype=imDlib.dtype) cv2.fillConvexPoly(mask1, np.array([hull1], dtype=np.int32), color) mask2 = np.zeros(imDlib.shape, dtype=imDlib.dtype) cv2.fillConvexPoly(mask2, np.array([hull2], dtype=np.int32), color) mask = cv2.addWeighted(mask1, 1, mask2, 1, 0.0) # Blurring face mask to alpha blend to hide seams #kernel = np.ones((10,10),np.uint8) maskHeight, maskWidth = mask.shape[0:2] maskSmall = cv2.resize(mask, (256, int(maskHeight * 256.0 / maskWidth))) maskSmall = cv2.erode(maskSmall, (-1, -1), 5) #maskSmall = cv2.erode(maskSmall, kernel, 1) maskSmall = cv2.GaussianBlur(maskSmall, (11, 11), 0, 0) mask = cv2.resize(maskSmall, (maskWidth, maskHeight)) feature_image = cv2.addWeighted(mask, 0.1, imDlib, 1, 0.0) #displayImage = np.hstack((imDlib, feature_image)) feature_image = cv2.cvtColor(feature_image, cv2.COLOR_RGB2BGR) cv2.imwrite(OUTPUT_PATH, feature_image) # end time #cc = time.time() - start return OUTPUT_PATH
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 blush_makeup(im, color): #im = cv2.imread(im_path) imDlib = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) #img1Warped = np.copy(imDlib) landmarks = fbc.getLandmarks(faceDetector, landmarkDetector, imDlib) #selectedIndex_upper_lip = [ 1 , 2 , 3 , 4 , 31, 40, 41] #selectedIndex_botto_lip = [ 13, 14, 15, 16, 35, 46, 47] selectedIndex_upper_lip = [1, 2, 3, 4, 31, 40, 41] selectedIndex_botto_lip = [13, 14, 15, 16, 35, 46, 47] hull1, hull2 = [], [] for i in selectedIndex_upper_lip: hull1.append(landmarks[i]) for i in selectedIndex_botto_lip: hull2.append(landmarks[i]) mask1 = np.zeros(imDlib.shape, dtype=imDlib.dtype) cv2.fillConvexPoly(mask1, np.array([hull1], dtype=np.int32), color) mask2 = np.zeros(imDlib.shape, dtype=imDlib.dtype) cv2.fillConvexPoly(mask2, np.array([hull2], dtype=np.int32), color) mask = cv2.addWeighted(mask1, 1, mask2, 1, 0.0) # Blurring face mask to alpha blend to hide seams maskHeight, maskWidth = mask.shape[0:2] maskSmall = cv2.resize(mask, (256, int(maskHeight * 256.0 / maskWidth))) maskSmall = cv2.erode(maskSmall, (-1, -1), 20) maskSmall = cv2.GaussianBlur(maskSmall, (51, 51), 0, 0) mask = cv2.resize(maskSmall, (maskWidth, maskHeight)) feature_image = cv2.addWeighted(mask, 0.1, imDlib, 0.98, 0.0) #displayImage = np.hstack((imDlib, feature_image)) feature_image = cv2.cvtColor(feature_image, cv2.COLOR_RGB2BGR) cv2.imwrite(OUTPUT_PATH, feature_image) return OUTPUT_PATH
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
labelsFaceTrain = [] # Load face detector faceDetector = dlib.get_frontal_face_detector() # Load landmark detector. landmarkDetector = dlib.shape_predictor( "../../common/shape_predictor_68_face_landmarks.dat") for j, imagePath in enumerate(imagePaths): im = cv2.imread(imagePath, 0) imHeight, imWidth = im.shape[:2] # Detect faces in the image # Find landmarks. landmarks = fbc.getLandmarks(faceDetector, landmarkDetector, im) landmarks = np.array(landmarks) if len(landmarks) < 68: print("{}, Only {} Landmarks located".format( imagePath, len(landmarks))) continue else: print("Processing : {}".format(imagePath)) x1Limit = landmarks[0][0] - (landmarks[36][0] - landmarks[0][0]) x2Limit = landmarks[16][0] + (landmarks[16][0] - landmarks[45][0]) y1Limit = landmarks[27][1] - 3 * (landmarks[30][1] - landmarks[27][1]) y2Limit = landmarks[8][1] + (landmarks[30][1] - landmarks[29][1]) x1 = max(x1Limit, 0) x2 = min(x2Limit, imWidth)
# Load landmark detector. landmarkDetector = dlib.shape_predictor( "../../common/shape_predictor_68_face_landmarks.dat") ap = argparse.ArgumentParser() ap.add_argument("-f", "--filename", help="filename") args = vars(ap.parse_args()) filename = "../data/images/hillary_clinton.jpg" if args["filename"]: filename = args["filename"] img = cv2.imread(filename) # Find landmarks. landmarks = fbc.getLandmarks(faceDetector, landmarkDetector, cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) landmarks = np.array(landmarks) # Calculate face mask by finding the convex hull and filling it GC_FGD faceMask = np.zeros(img.shape[:2], np.uint8) hull = cv2.convexHull(landmarks, False, True) # Convert to array for fillConvexPoly hullInt = [] for i in range(0, len(hull)): hullInt.append((hull[i][0][0], hull[i][0][1])) # Fill face region with foreground indicator GC_FGD cv2.fillConvexPoly(faceMask, np.int32(hullInt), cv2.GC_FGD) # cv2.imshow("facemask", faceMask*60)
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) im_arr1 = np.frombuffer(picture.parts[0].content, dtype=np.uint8) img1 = cv2.imdecode(im_arr1, flags=cv2.IMREAD_COLOR) im_arr2 = np.frombuffer(picture.parts[1].content, dtype=np.uint8) img2 = cv2.imdecode(im_arr2, flags=cv2.IMREAD_COLOR) im1Display = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB) im2Display = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB) img1Warped = np.copy(img2) print('step1') points1 = fbc.getLandmarks(detector, predictor, img1) points2 = fbc.getLandmarks(detector, predictor, img2) print('step2') hullIndex = cv2.convexHull(np.array(points2), returnPoints=False) # Create convex hull lists hull1 = [] hull2 = [] for i in range(0, len(hullIndex)): hull1.append(points1[hullIndex[i][0]]) hull2.append(points2[hullIndex[i][0]]) print('step3') # Calculate Mask for Seamless cloning hull8U = [] for i in range(0, len(hull2)): hull8U.append((hull2[i][0], hull2[i][1])) mask = np.zeros(img2.shape, dtype=img2.dtype) cv2.fillConvexPoly(mask, np.int32(hull8U), (255, 255, 255)) print('step4') # Find Centroid m = cv2.moments(mask[:, :, 1]) center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00'])) print('step5') # Find Delaunay traingulation for convex hull points sizeImg2 = img2.shape rect = (0, 0, sizeImg2[1], sizeImg2[0]) dt = fbc.calculateDelaunayTriangles(rect, hull2) imTemp1 = im1Display.copy() imTemp2 = im2Display.copy() tris1 = [] tris2 = [] for i in range(0, len(dt)): tri1 = [] tri2 = [] for j in range(0, 3): tri1.append(hull1[dt[i][j]]) tri2.append(hull2[dt[i][j]]) tris1.append(tri1) tris2.append(tri2) cv2.polylines(imTemp1, np.array(tris1), True, (0, 0, 255), 2) cv2.polylines(imTemp2, np.array(tris2), True, (0, 0, 255), 2) print('step6') for i in range(0, len(tris1)): fbc.warpTriangle(img1, img1Warped, tris1[i], tris2[i]) print('step7') output = cv2.seamlessClone(np.uint8(img1Warped), img2, mask, center, cv2.NORMAL_CLONE) print('all done') return { "statusCode": 200, "headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', "Access-Control-Allow-Credentials": True }, "body": json.dumps({ 'swaped_image': str(base64.b64encode(cv2.imencode('.jpg', output)[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)}) }
dt = fbc.calculateDelaunayTriangles(rect, featurePoints1) if len(dt) == 0: quit() targetImage = cv2.imread(imageFile) height, width = targetImage.shape[:2] IMAGE_RESIZE = np.float32(height) / RESIZE_HEIGHT targetImage = cv2.resize(targetImage, None, fx=1.0 / IMAGE_RESIZE, fy=1.0 / IMAGE_RESIZE, interpolation=cv2.INTER_LINEAR) points2 = fbc.getLandmarks(detector, predictor, cv2.cvtColor(targetImage, cv2.COLOR_BGR2RGB), FACE_DOWNSAMPLE_RATIO) featurePoints2 = [] for p in selectedIndex: pt = points2[p] pt = fbc.constrainPoint(pt, width, height) featurePoints2.append(pt) targetImage = np.float32(targetImage) / 255 beardWarped = np.zeros(targetImage.shape) beardAlphaWarped = np.zeros(targetImage.shape) # Apply affine transformation to Delaunay triangles for i in range(0, len(dt)): t1 = []
def face_swap(img1, img2,predictor68): # Read images # img2 = cv2.imread('assets/amit.jpg') # img1 = cv2.imread('assets/arvind.jpg') im1Display = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB) im2Display = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB) img1Warped = np.copy(img2) # # Display Images # plt.figure(figsize = (20,10)) # plt.subplot(121); plt.imshow(im1Display); plt.axis('off'); # plt.subplot(122); plt.imshow(im2Display); plt.axis('off'); # !wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 # !bzip2 -dk shape_predictor_68_face_landmarks.dat.bz2 # Initialize the dlib facial landmakr detector detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(predictor68) # Read array of corresponding points points1 = fbc.getLandmarks(detector, predictor, img1) points2 = fbc.getLandmarks(detector, predictor, img2) # # Display Landmarks # imTemp = im2Display.copy() # for p in points2: # cv2.circle(imTemp, p, 5, (255,0,0), -1) # plt.figure(figsize = (20,10)); plt.imshow(imTemp); plt.axis('off'); # Find convex hull hullIndex = cv2.convexHull(np.array(points2), returnPoints=False) # Create convex hull lists hull1 = [] hull2 = [] for i in range(0, len(hullIndex)): hull1.append(points1[hullIndex[i][0]]) hull2.append(points2[hullIndex[i][0]]) # # Display Convex Hull # imTemp = im2Display.copy() # numPoints = len(hull2) # for i in range(0, numPoints): # cv2.line(imTemp, hull2[i], hull2[(i+1)%numPoints], (255,0,0), 3) # cv2.circle(imTemp, hull2[i], 5, (0,0,255), -1) # plt.figure(figsize = (20,10)); plt.imshow(imTemp); plt.axis('off'); # Calculate Mask for Seamless cloning hull8U = [] for i in range(0, len(hull2)): hull8U.append((hull2[i][0], hull2[i][1])) mask = np.zeros(img2.shape, dtype=img2.dtype) cv2.fillConvexPoly(mask, np.int32(hull8U), (255, 255, 255)) # Find Centroid m = cv2.moments(mask[:,:,1]) center = (int(m['m10']/m['m00']), int(m['m01']/m['m00'])) # # Display Mask # plt.figure(figsize = (20,10)); plt.imshow(mask); plt.axis('off'); # Find Delaunay traingulation for convex hull points sizeImg2 = img2.shape rect = (0, 0, sizeImg2[1], sizeImg2[0]) dt = fbc.calculateDelaunayTriangles(rect, hull2) # If no Delaunay Triangles were found, quit if len(dt) == 0: quit() imTemp1 = im1Display.copy() imTemp2 = im2Display.copy() tris1 = [] tris2 = [] for i in range(0, len(dt)): tri1 = [] tri2 = [] for j in range(0, 3): tri1.append(hull1[dt[i][j]]) tri2.append(hull2[dt[i][j]]) tris1.append(tri1) tris2.append(tri2) cv2.polylines(imTemp1,np.array(tris1),True,(0,0,255),2) cv2.polylines(imTemp2,np.array(tris2),True,(0,0,255),2) # # Display Triangulation # plt.figure(figsize = (20,10)); # plt.subplot(121); plt.imshow(imTemp1); plt.axis('off'); # plt.subplot(122); plt.imshow(imTemp2); plt.axis('off'); # Simple Alpha Blending # Apply affine transformation to Delaunay triangles for i in range(0, len(tris1)): fbc.warpTriangle(img1, img1Warped, tris1[i], tris2[i]) # plt.figure(figsize=(20,10)); # plt.imshow(np.uint8(img1Warped)[:,:,::-1]); plt.axis('off'); # Clone seamlessly. output = cv2.seamlessClone(np.uint8(img1Warped), img2, mask, center, cv2.NORMAL_CLONE) # plt.figure(figsize=(20,10)) # plt.subplot((121)); plt.imshow(np.uint8(img1Warped)[:,:,::-1]); plt.axis('off'); # plt.subplot((122)); plt.imshow(output[:,:,::-1]); plt.axis('off'); # plt.figure(figsize=(20,10)) # plt.subplot((121)); plt.imshow(np.uint8(img1Warped)[:,:,::-1]); plt.axis('off'); # plt.subplot((122)); plt.imshow(output[:,:,::-1]); plt.axis('off'); return output
def get_swapped_image(img1, img2): try: im1Display = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB) im2Display = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB) img1Warped = np.copy(img2) detector = dlib.get_frontal_face_detector() # Read array of corresponding points points1 = fbc.getLandmarks(detector, predictor, img1) points2 = fbc.getLandmarks(detector, predictor, img2) # Find convex hull hullIndex = cv2.convexHull(np.array(points2), returnPoints=False) # Create convex hull lists hull1 = [] hull2 = [] for i in range(0, len(hullIndex)): hull1.append(points1[hullIndex[i][0]]) hull2.append(points2[hullIndex[i][0]]) # Calculate Mask for Seamless cloning hull8U = [] for i in range(0, len(hull2)): hull8U.append((hull2[i][0], hull2[i][1])) mask = np.zeros(img2.shape, dtype=img2.dtype) cv2.fillConvexPoly(mask, np.int32(hull8U), (255, 255, 255)) # Find Centroid m = cv2.moments(mask[:, :, 1]) center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00'])) # Find Delaunay traingulation for convex hull points sizeImg2 = img2.shape rect = (0, 0, sizeImg2[1], sizeImg2[0]) dt = fbc.calculateDelaunayTriangles(rect, hull2) # If no Delaunay Triangles were found, quit if len(dt) == 0: quit() imTemp1 = im1Display.copy() imTemp2 = im2Display.copy() tris1 = [] tris2 = [] for i in range(0, len(dt)): tri1 = [] tri2 = [] for j in range(0, 3): tri1.append(hull1[dt[i][j]]) tri2.append(hull2[dt[i][j]]) tris1.append(tri1) tris2.append(tri2) cv2.polylines(imTemp1, np.array(tris1), True, (0, 0, 255), 2) cv2.polylines(imTemp2, np.array(tris2), True, (0, 0, 255), 2) # Simple Alpha Blending # Apply affine transformation to Delaunay triangles for i in range(0, len(tris1)): fbc.warpTriangle(img1, img1Warped, tris1[i], tris2[i]) # Clone seamlessly. output = cv2.seamlessClone(np.uint8(img1Warped), img2, mask, center, cv2.NORMAL_CLONE) return output return except Exception as e: print(repr(e)) raise (e)
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)}) }
# 初始化dlib检测器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(model_path) # 计算用时 t = time.time() # 要处理的图片 filename1 = "../data/images/ted_cruz.jpg" filename2 = "../data/images/donald_trump.jpg" img1 = cv2.imread(filename1) img2 = cv2.imread(filename2) img1Warped = np.copy(img2) # 关键点检测 points1 = fbc.getLandmarks(detector, predictor, img1) points2 = fbc.getLandmarks(detector, predictor, img2) # 检测外框 hull1 = [] hull2 = [] hullIndex = cv2.convexHull(np.array(points2), returnPoints=False) for i in range(0, len(hullIndex)): hull1.append(points1[hullIndex[i][0]]) hull2.append(points2[hullIndex[i][0]]) sizeImg2 = img2.shape rect = (0, 0, sizeImg2[1], sizeImg2[0]) dt = fbc.calculateDelaunayTriangles(rect, hull2)
predictor = dlib.shape_predictor(modelPath) # Processing input file filename1 = '../data/images/obama.jpg' # Read the image and resize it img1 = cv2.imread(filename1) height, width = img1.shape[:2] IMAGE_RESIZE = np.float32(height)/RESIZE_HEIGHT img1 = cv2.resize(img1,None, fx=1.0/IMAGE_RESIZE, fy=1.0/IMAGE_RESIZE, interpolation = cv2.INTER_LINEAR) # Find landmark points points1 = fbc.getLandmarks(detector, predictor, cv2.cvtColor(img1, cv2.COLOR_BGR2RGB), FACE_DOWNSAMPLE_RATIO) img1 = np.float32(img1) # Find convex hull for delaunay triangulation using the landmark points hull1 = [] hullIndex = cv2.convexHull(np.array(points1),clockwise=False, returnPoints = False) addPoints = [[48],[49],[50],[51],[52],[53],[54],[55],[56],[57],[58]] hullIndex = np.concatenate((hullIndex,addPoints)) for i in range(0, len(hullIndex)): hull1.append(points1[hullIndex[i][0]]) # Find delanauy traingulation for convex hull points sizeImg1 = img1.shape rect = (0, 0, sizeImg1[1], sizeImg1[0]) dt = fbc.calculateDelaunayTriangles(rect, hull1)
# 合成图片 filename1 = '../data/images/obama.jpg' # Read the image and resize it img1 = cv2.imread(filename1) height, width = img1.shape[:2] IMAGE_RESIZE = np.float32(height) / RESIZE_HEIGHT img1 = cv2.resize(img1, None, fx=1.0 / IMAGE_RESIZE, fy=1.0 / IMAGE_RESIZE, interpolation=cv2.INTER_LINEAR) # Find landmark points points1 = fbc.getLandmarks(detector, predictor, img1, FACE_DOWNSAMPLE_RATIO) img1 = np.float32(img1) # Find convex hull for delaunay triangulation using the landmark points hull1 = [] hullIndex = cv2.convexHull(np.array(points1), clockwise=False, returnPoints=False) addPoints = [[48], [49], [50], [51], [52], [53], [54], [55], [56], [57], [58]] hullIndex = np.concatenate((hullIndex, addPoints)) for i in range(0, len(hullIndex)): hull1.append(points1[hullIndex[i][0]]) # Find delanauy traingulation for convex hull points
import dlib import cv2 import faceBlendCommon as fbc # Load face detection and pose estimation models. modelPath = "../models/shape_predictor_68_face_landmarks.dat" detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(modelPath) error_files = [] files = glob.glob("../data/images/glassesDataset/OD_openEyes/*.jpg") files.sort() for i, fi in enumerate(files): try: targetImage = cv2.imread(fi) landmarks = fbc.getLandmarks( detector, predictor, cv2.cvtColor(targetImage, cv2.COLOR_BGR2RGB)) # Get points from landmarks detector x1 = landmarks[0][0] x2 = landmarks[16][0] y1 = min(landmarks[24][1], landmarks[19][1]) y2 = landmarks[29][1] # Crop the eye area cropped = targetImage[y1:y2, x1:x2, :] cropped = cv2.resize(cropped, (96, 32), interpolation=cv2.INTER_CUBIC) cv2.imwrite( "../data/images/glassesDataset/cropped_withoutGlasses2/without_glasses_{:04d}.jpg" .format(i), cropped)
landmarkDetector = dlib.shape_predictor(PREDICTOR_PATH) # face1 is the image of the girl w/o makeup face1 = cv2.imread("girl-no-makeup.jpg") # convert to RGB and preserve a copy for later use face1 = cv2.cvtColor(face1, cv2.COLOR_BGR2RGB) face1_cp = np.copy(face1) # face2 is the image of the girl w/ makeup face2 = cv2.imread("girl-blush.jpg") # Resize face2 to the shape of face1 and convert to RGB face2 = cv2.resize(face2, (face1.shape[1], face1.shape[0])) face2 = cv2.cvtColor(face2, cv2.COLOR_BGR2RGB) # Get all 68 facepoints for both the images face1_points = fbc.getLandmarks(faceDetector, landmarkDetector, face1) face2_points = fbc.getLandmarks(faceDetector, landmarkDetector, face2) # Based on the Dlib face points template, identify 4 points around the right and left cheeks. # The area inside these 4 points is where the blush will be applied. face1_Rcheek = (face1_points[35], face1_points[42], face1_points[15], face1_points[12]) face1_Lcheek = (face1_points[31], face1_points[39], face1_points[1], face1_points[4]) # Right cheek area is divided into 2 triangles i.e upper right and lower right # Warp upper right triangle from face2 to face1 face1_tU_R = (face1_points[35], face1_points[42], face1_points[15]) face2_tU_R = (face2_points[35], face2_points[42], face2_points[15]) fbc.warpTriangle(face2, face1, face2_tU_R, face1_tU_R) # Warp lower right triangle from face2 to face1
t = time.time() # Read an image and get the landmark points ret, src = cap.read() height, width = src.shape[:2] IMAGE_RESIZE = np.float32(height) / RESIZE_HEIGHT src = cv2.resize(src, None, fx=1.0 / IMAGE_RESIZE, fy=1.0 / IMAGE_RESIZE, interpolation=cv2.INTER_LINEAR) # find landmarks after skipping SKIP_FRAMES number of frames if (count % SKIP_FRAMES == 0): landmarks = fbc.getLandmarks(faceDetector, landmarkDetector, cv2.cvtColor(src, cv2.COLOR_BGR2RGB), FACE_DOWNSAMPLE_RATIO) if len(landmarks) != 68: print("points no detected") continue ################ Optical Flow and Stabilization Code ##################### srcGray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) if (isFirstFrame == False): isFirstFrame = True landmarksPrev = np.array(landmarks, np.float32) srcGrayPrev = np.copy(srcGray) lk_params = dict(winSize=(101, 101),
filename2 = "../data/images/ted_cruz.jpg" if len(sys.argv) == 2: filename1 = sys.argv[1] elif len(sys.argv) == 3: filename1 = sys.argv[1] filename2 = sys.argv[2] t = time.time() # Read images img1 = cv2.imread(filename1) img2 = cv2.imread(filename2) # Find landmarks points1 = fbc.getLandmarks(faceDetector, landmarkDetector, cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)) points2 = fbc.getLandmarks(faceDetector, landmarkDetector, cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)) # Find forehead points appendForeheadPoints(points1) appendForeheadPoints(points2) # Find Delaunay Triangulation sizeImg1 = img1.shape rect = (0, 0, sizeImg1[1], sizeImg1[0]) dt = fbc.calculateDelaunayTriangles(rect, points1) # Convert image for warping img1 = np.float32(img1)/255.0 img2 = np.float32(img2)/255.0
def run_face_swap(from_image, to_image, output_filename): """Switch faces between two input images using dlib and OpenCV.""" # Credits to https://github.com/spmallick/ try: img1 = read_url_or_local_image(from_image, im_format='cv2') img2 = read_url_or_local_image(to_image, im_format='cv2') img1Warped = np.copy(img2) # Initialize the dlib facial landmark detector detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor( "shape_predictor_68_face_landmarks.dat") # Read array of corresponding points points1 = fbc.getLandmarks(detector, predictor, img1) points2 = fbc.getLandmarks(detector, predictor, img2) # Find convex hull hullIndex = cv2.convexHull( np.array(points2).astype(np.int32), returnPoints=False ) # add .astype(np.int32) to fix TypeError: data type = 9 not supported # Create convex hull lists hull1 = [] hull2 = [] for i in range(0, len(hullIndex)): hull1.append(points1[hullIndex[i][0]]) hull2.append(points2[hullIndex[i][0]]) # Calculate Mask for Seamless cloning hull8U = [] for i in range(0, len(hull2)): hull8U.append((hull2[i][0], hull2[i][1])) mask = np.zeros(img2.shape, dtype=img2.dtype) cv2.fillConvexPoly(mask, np.int32(hull8U), (255, 255, 255)) # Find Centroid m = cv2.moments(mask[:, :, 1]) center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00'])) # Find Delaunay traingulation for convex hull points sizeImg2 = img2.shape rect = (0, 0, sizeImg2[1], sizeImg2[0]) dt = fbc.calculateDelaunayTriangles(rect, hull2) # If no Delaunay Triangles were found, quit if len(dt) == 0: quit() # Continue triangulation tris1 = [] tris2 = [] for i in range(0, len(dt)): tri1 = [] tri2 = [] for j in range(0, 3): tri1.append(hull1[dt[i][j]]) tri2.append(hull2[dt[i][j]]) tris1.append(tri1) tris2.append(tri2) # Apply affine transformation to Delaunay triangles for i in range(0, len(tris1)): fbc.warpTriangle(img1, img1Warped, tris1[i], tris2[i]) # Seamless Cloning using OpenCV output = cv2.seamlessClone(np.uint8(img1Warped), img2, mask, center, cv2.NORMAL_CLONE) # Write output image cv2.imwrite(output_filename, output) except Exception as e: print(e.message, e.args)
deformedPoints = [5, 6, 8, 10, 11] t = time.time() # Read an image and get the landmark points filename = '../data/images/hillary_clinton.jpg' src = cv2.imread(filename) height, width = src.shape[:2] IMAGE_RESIZE = np.float32(height) / RESIZE_HEIGHT src = cv2.resize(src, None, fx=1.0 / IMAGE_RESIZE, fy=1.0 / IMAGE_RESIZE, interpolation=cv2.INTER_LINEAR) landmarks = fbc.getLandmarks(detector, predictor, cv2.cvtColor(src, cv2.COLOR_BGR2RGB), FACE_DOWNSAMPLE_RATIO) print("Landmarks calculated in {}".format(time.time() - t)) # Set the center of face to be the nose tip centerx, centery = landmarks[30][0], landmarks[30][1] # Variables for storing the original and deformed points srcPoints = [] dstPoints = [] # Adding the original and deformed points using the landmark points for idx in anchorPoints: srcPoints.append([landmarks[idx][0], landmarks[idx][1]]) dstPoints.append([landmarks[idx][0], landmarks[idx][1]])
def get_face_swap(image_bytes1, image_bytes2): img1 = transform_image(image_bytes=image_bytes1) img2 = transform_image(image_bytes=image_bytes2) img1Warped = np.copy(img2) # Detect Landmark points1 = fbc.getLandmarks(detector, predictor, img1) points2 = fbc.getLandmarks(detector, predictor, img2) print('Landmarks detected') # Find convex hull hullIndex = cv2.convexHull(np.array(points2), returnPoints=False) # Create convex hull lists hull1 = [] hull2 = [] for i in range(0, len(hullIndex)): hull1.append(points1[hullIndex[i][0]]) hull2.append(points2[hullIndex[i][0]]) # Calculate Mask for Seamless cloning hull8U = [] for i in range(0, len(hull2)): hull8U.append((hull2[i][0], hull2[i][1])) mask = np.zeros(img2.shape, dtype=img2.dtype) cv2.fillConvexPoly(mask, np.int32(hull8U), (255, 255, 255)) print(f'Calculated Mask for Seamless cloning') # Find Centroid m = cv2.moments(mask[:, :, 1]) center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00'])) # Find Delaunay traingulation for convex hull points sizeImg2 = img2.shape rect = (0, 0, sizeImg2[1], sizeImg2[0]) dt = fbc.calculateDelaunayTriangles(rect, hull2) # If no Delaunay Triangles were found, quit if len(dt) == 0: print("ERROR: No Delaunay Triangles were found") #quit() print(f'Found Delaunay Triangles') tris1 = [] tris2 = [] for i in range(0, len(dt)): tri1 = [] tri2 = [] for j in range(0, 3): tri1.append(hull1[dt[i][j]]) tri2.append(hull2[dt[i][j]]) tris1.append(tri1) tris2.append(tri2) # Simple Alpha Blending # Apply affine transformation to Delaunay triangles for i in range(0, len(tris1)): fbc.warpTriangle(img1, img1Warped, tris1[i], tris2[i]) # Clone seamlessly. output = cv2.seamlessClone(np.uint8(img1Warped), img2, mask, center, cv2.NORMAL_CLONE) print(f'Cloned seamlessly') # This is swapped image return output
cv2.imshow(winName, imgShow) if __name__ == "__main__": # 加载识别器 PREDICTOR_DIR = "../../common/resources/zxm_shape_predictor_70_face_landmarks.dat" faceDetector = dlib.get_frontal_face_detector() landmarkDetector = dlib.shape_predictor(PREDICTOR_DIR) # 加载基本图片 img1 = cv2.imread("../data/images/girls/xiaohuizi.jpg") 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
# initialize the dlib facial landmakr detector detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(modelPath) t = time.time() # Read images filename1 = '../data/images/ted_cruz.jpg' filename2 = '../data/images/donald_trump.jpg' img1 = cv2.imread(filename1) img2 = cv2.imread(filename2) img1Warped = np.copy(img2) # Read array of corresponding points points1 = fbc.getLandmarks(detector, predictor, cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)) points2 = fbc.getLandmarks(detector, predictor, cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)) # Find convex hull hull1 = [] hull2 = [] hullIndex = cv2.convexHull(np.array(points2), returnPoints=False) for i in range(0, len(hullIndex)): hull1.append(points1[hullIndex[i][0]]) hull2.append(points2[hullIndex[i][0]]) # Find delanauy traingulation for convex hull points sizeImg2 = img2.shape
imagePaths = readImagesPath(dirName) if len(imagePaths) == 0: print("没有读取到对应的图片") sys.exit(0) images = [] allPoints = [] for imagePath in imagePaths: im = cv2.imread(imagePath) if im is None: print("image:{} 读取失败".format(imagePath)) else: print("image:{} 读取成功".format(imagePath)) points=fbc.getLandmarks(faceDetector, landmarkDetector, im, 2) if len(points) > 0: allPoints.append(points) im = np.float32(im)/255.0 images.append(im) else: print("未检测到landmarks") if len(images) == 0: print("没有检测到合适的图像") sys.exit(0) # 定义输出图像的尺寸 w = 300 h = 300
def execute_face_swap(img1, img2): im1Display = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB) im2Display = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB) img1Warped = np.copy(img2) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # Read array of corresponding points points1 = fbc.getLandmarks(detector, predictor, img1) points2 = fbc.getLandmarks(detector, predictor, img2) hullIndex = cv2.convexHull(np.array(points2), returnPoints=False) if(len(points1) == 0 or len(points2) == 0): print("Landmark detection failed for selected Images Source:{} Dest:{}".format(len(points1), len(points))) # Create convex hull lists hull1 = [] hull2 = [] for i in range(0, len(hullIndex)): hull1.append(points1[hullIndex[i][0]]) hull2.append(points2[hullIndex[i][0]]) hull8U = [] for i in range(0, len(hull2)): hull8U.append((hull2[i][0], hull2[i][1])) mask = np.zeros(img2.shape, dtype=img2.dtype) cv2.fillConvexPoly(mask, np.int32(hull8U), (255, 255, 255)) # Find Centroid m = cv2.moments(mask[:,:,1]) center = (int(m['m10']/m['m00']), int(m['m01']/m['m00'])) sizeImg2 = img2.shape rect = (0, 0, sizeImg2[1], sizeImg2[0]) dt = fbc.calculateDelaunayTriangles(rect, hull2) # If no Delaunay Triangles were found, quit if len(dt) == 0: #quit() print("No Delaunay Triangles were found!") return None imTemp1 = im1Display.copy() imTemp2 = im2Display.copy() tris1 = [] tris2 = [] for i in range(0, len(dt)): tri1 = [] tri2 = [] for j in range(0, 3): tri1.append(hull1[dt[i][j]]) tri2.append(hull2[dt[i][j]]) tris1.append(tri1) tris2.append(tri2) cv2.polylines(imTemp1,np.array(tris1),True,(0,0,255),2); cv2.polylines(imTemp2,np.array(tris2),True,(0,0,255),2); for i in range(0, len(tris1)): fbc.warpTriangle(img1, img1Warped, tris1[i], tris2[i]) output = cv2.seamlessClone(np.uint8(img1Warped[:,:,::-1]), img2, mask, center,cv2.NORMAL_CLONE) ### Default scaling to 25 percent scale_percent = 25 width = int(output.shape[1] * scale_percent / 100) height = int(output.shape[0] * scale_percent / 100) # dsize dsize = (width, height) # resize image output = cv2.resize(output, dsize) return output