• 人工智能几行代码实现换脸


    本篇介绍的换脸方法需要借助Face++,关于Face++的API,大家可自行查看说明文档,都比较简单。

    文档地址:https://console.faceplusplus.com.cn/documents/20813963

     PYTHON:

    import requests
    import simplejson
    import json
    import base64
     
    #Face++网址:[url]https://console.faceplusplus.com.cn/dashboard[/url]
    #第一步,获取人脸关键点
    def find_face(imgpath):
        print("正在查找……")
        http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
        data = {"api_key": '自己申请',
                "api_secret": '自己申请TLJC',
                "image_url": imgpath, "return_landmark": 1}
        files = {"image_file": open(imgpath, "rb")}
        response = requests.post(http_url, data=data, files=files)
        req_con = response.content.decode('utf-8')
        req_dict = json.JSONDecoder().decode(req_con)
        this_json = simplejson.dumps(req_dict)
     
        this_json2 = simplejson.loads(this_json)
        print(this_json2)
        faces = this_json2['faces']
        list0 = faces[0]
        rectangle = list0['face_rectangle']
        #print(rectangle)
        return rectangle
     
    #第二步,换脸,其中图片的大小应不超过2M
    # number表示换脸的相似度
    def merge_face(image_url1, image_url2, image_url, number):
     
        ff1 = find_face(image_url1)
        ff2 = find_face(image_url2)
     
        rectangle1 = str(str(ff1['top']) + "," + str(ff1['left']) + "," + str(ff1['width']) + "," + str(ff1['height']))
        rectangle2 = str(ff2['top']) + "," + str(ff2['left']) + "," + str(ff2['width']) + "," + str(ff2['height'])
        print(rectangle2)
        url_add = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"
        f1 = open(image_url1, 'rb')
        f1_64 = base64.b64encode(f1.read())
        f1.close()
        f2 = open(image_url2, 'rb')
        f2_64 = base64.b64encode(f2.read())
        f2.close()
     
        data = {"api_key": '自己申请',
                "api_secret": '自己申请TLJC',
                "template_base64": f1_64, "template_rectangle": rectangle1,
                "merge_base64": f2_64, "merge_rectangle": rectangle2, "merge_rate": number}
     
        response = requests.post(url_add, data=data)
        req_con1 = response.content.decode('utf-8')
        #print(req_con1)
        req_dict = json.JSONDecoder().decode(req_con1)
        result = req_dict['result']
        imgdata = base64.b64decode(result)
        file = open(image_url, 'wb')
        file.write(imgdata)
        file.close()
     
    image1 = r"E:	estimg01.jpg"
    image2 = r"E:	estimg02.jpg"
    image = r"E:	estimgh.jpg"
     
    merge_face(image2, image1, image, 90)

    C#:

    /*POST请求类*/
     
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    using System.Threading;
     
    namespace MgLiveCOM_Eyes
    {
        public static class HttpHelper4MultipartForm
        {
            public class FileParameter
            {
                public byte[] File
                {
                    get;
                    set;
                }
     
                public string FileName
                {
                    get;
                    set;
                }
     
                public string ContentType
                {
                    get;
                    set;
                }
     
                public FileParameter(byte[] file) : this(file, null)
                {
                }
     
                public FileParameter(byte[] file, string filename) : this(file, filename, null)
                {
                }
     
                public FileParameter(byte[] file, string filename, string contenttype)
                {
                    this.File = file;
                    this.FileName = filename;
                    this.ContentType = contenttype;
                }
            }
            private static readonly Encoding encoding = Encoding.UTF8;
            /// <summary>
            /// MultipartForm请求
            /// </summary>
            /// <param name="postUrl">服务地址</param>
            /// <param name="userAgent"></param>
            /// <param name="postParameters">参数</param>
            /// <returns></returns>
            public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, object> postParameters)
            {
                string text = string.Format("----------{0:N}", Guid.NewGuid());
                string contentType = "multipart/form-data; boundary=" + text;//multipart类型
                byte[] multipartFormData = HttpHelper4MultipartForm.GetMultipartFormData(postParameters, text);
                return HttpHelper4MultipartForm.PostForm(postUrl, userAgent, contentType, multipartFormData);
            }
     
            private static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData)
            {
                HttpWebRequest httpWebRequest = WebRequest.Create(postUrl) as HttpWebRequest;
                if (httpWebRequest == null)
                {
                    throw new NullReferenceException("request is not a http request");
                }
              
                httpWebRequest.Method = "POST";//post方式
                httpWebRequest.SendChunked = false;
                httpWebRequest.KeepAlive = true;
                httpWebRequest.Proxy = null;
                httpWebRequest.Timeout = Timeout.Infinite;
                httpWebRequest.ReadWriteTimeout = Timeout.Infinite;
                httpWebRequest.AllowWriteStreamBuffering = false;
                httpWebRequest.ProtocolVersion = HttpVersion.Version11;
                httpWebRequest.ContentType = contentType;
                httpWebRequest.CookieContainer = new CookieContainer();
                httpWebRequest.ContentLength = (long)formData.Length;
     
                try
                {
                    using (Stream requestStream = httpWebRequest.GetRequestStream())
                    {
                        int bufferSize = 4096;
                        int position = 0;
                        while (position < formData.Length)
                        {
                            bufferSize = Math.Min(bufferSize, formData.Length - position);
                            byte[] data = new byte[bufferSize];
                            Array.Copy(formData, position, data, 0, bufferSize);
                            requestStream.Write(data, 0, data.Length);
                            position += data.Length;
                        }
                        requestStream.Close();
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
     
                HttpWebResponse result;
                try
                {
                    result = (httpWebRequest.GetResponse() as HttpWebResponse);
                }
                catch (WebException arg_9C_0)
                {
                    result = (arg_9C_0.Response as HttpWebResponse);
                }
                return result;
            }
     
            public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            }
            /// <summary>
            /// 从表单中获取数据
            /// </summary>
            /// <param name="postParameters"></param>
            /// <param name="boundary"></param>
            /// <returns></returns>
            private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
            {
                Stream stream = new MemoryStream();
                bool flag = false;
                foreach (KeyValuePair<string, object> current in postParameters)
                {
                    if (flag)
                    {
                        stream.Write(HttpHelper4MultipartForm.encoding.GetBytes("
    "), 0, HttpHelper4MultipartForm.encoding.GetByteCount("
    "));
                    }
                    flag = true;
                    if (current.Value is HttpHelper4MultipartForm.FileParameter)
                    {
                        HttpHelper4MultipartForm.FileParameter fileParameter = (HttpHelper4MultipartForm.FileParameter)current.Value;
                        string s = string.Format("--{0}
    Content-Disposition: form-data; name="{1}"; filename="{2}"
    Content-Type: {3}
    
    ", new object[]
                        {
                            boundary,
                            current.Key,
                            fileParameter.FileName ?? current.Key,
                            fileParameter.ContentType ?? "application/octet-stream"
                        });
                        stream.Write(HttpHelper4MultipartForm.encoding.GetBytes(s), 0, HttpHelper4MultipartForm.encoding.GetByteCount(s));
                        stream.Write(fileParameter.File, 0, fileParameter.File.Length);
                    }
                    else
                    {
                        string s2 = string.Format("--{0}
    Content-Disposition: form-data; name="{1}"
    
    {2}", boundary, current.Key, current.Value);
                        stream.Write(HttpHelper4MultipartForm.encoding.GetBytes(s2), 0, HttpHelper4MultipartForm.encoding.GetByteCount(s2));
                    }
                }
                string s3 = "
    --" + boundary + "--
    ";
                stream.Write(HttpHelper4MultipartForm.encoding.GetBytes(s3), 0, HttpHelper4MultipartForm.encoding.GetByteCount(s3));
                stream.Position = 0L;
                byte[] array = new byte[stream.Length];
                stream.Read(array, 0, array.Length);
                stream.Close();
                return array;
            }
        }
    }
     
     
     
    调用示例
    //参数字典
    Dictionary<string, object> verifyPostParameters = new Dictionary<string, object>();
    verifyPostParameters.Add("api_key", "KEY");
    verifyPostParameters.Add("api_secret", "SECRET");
    verifyPostParameters.Add("return_landmark", "1");
    verifyPostParameters.Add("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
    Bitmap bmp = new Bitmap("/tmp/1.jpg"); // 图片地址
    byte[] fileImage;
    using (Stream stream1 = new MemoryStream())
    {
        bmp.Save(stream1, ImageFormat.Jpeg);
        byte[] arr = new byte[stream1.Length];
        stream1.Position = 0;
        stream1.Read(arr, 0, (int)stream1.Length);
        stream1.Close();
        fileImage= arr;
    }
    //添加图片参数
    verifyPostParameters.Add("image_file", new HttpHelper4MultipartForm.FileParameter(fileImage, "1.jpg", "application/octet-stream"));
    HttpWebResponse verifyResponse = HttpHelper4MultipartForm.MultipartFormDataPost("https://api-cn.faceplusplus.com/facepp/v3/detect", "", verifyPostParameters);
  • 相关阅读:
    C语言基础:C语言结构体(2)
    C语言基础:C语言结构体(1)
    C语言基础:C语言变量类型
    开源魔兽服务端代码托管地址大全
    新浪微博_第三期整理
    UITextField特性整理
    Xcode快捷键整理
    sleep和wait区别
    IOS7新特性-AVSpeechSynthesisVoice
    【OBJC类扩展之MD5加密】NSString+MD5
  • 原文地址:https://www.cnblogs.com/xxxyz/p/11504645.html
Copyright © 2020-2023  润新知