小程序中如何接入人脸融合功能

小程序人脸融合功能是一个什么样的样式效果,小程序接入人脸融合功能需要如何开发,下面为大家解答。

效果图

小程序中如何接入人脸融合功能

创建一个AI应用

https://ai.qq.com/cgi-bin/console_overview 登录腾讯AI,创建并勾选相关接口即可。

记得复制APPID APPKEY

使用Java接入该功能

创建一个SpringMVC工程,包含上传相关jar。或者SpringBoot工程也行。鄙人后端还在完善并没有完全开源。具体可以参考https://gitee.com/xshuai/xai 项目

Java调用腾讯AI接口。小帅丶已经封装成SDK。也是开源的 https://gitee.com/xshuai/taip 

如果使用maven搭建。直接pom引入即可哦

<!-- https://mvnrepository.com/artifact/cn.xsshome/taip -->
<dependency>
    <groupId>cn.xsshome</groupId>
    <artifactId>taip</artifactId>
    <version>4.2.1</version>
</dependency>
  • FaceMergeController(后端处理代码)
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import cn.xsshome.taip.ptu.TAipPtu;
/**
* 人脸融合接口
* url:http://www.xxx.com/facemerge/uploadFM
*/
@Controller
@RequestMapping(value="/facemerge")
@Scope("prototype")
public class FaceMergeController extends BaseController{
	private static final Logger logger = Logger.getLogger(FaceMergeController.class);
	/**
	 * 人脸融合
	 * @throws Exception
	 */
	@RequestMapping(value="/uploadFM",method=RequestMethod.POST)
	public void UploadBDANIMAL()throws Exception{
		TAipPtu aipPtu = new TAipPtu("APPID", "APPKEY");
		String model = request.getParameter("model");
		logger.info("model的值是===="+model);
		String model = request.getParameter("model");
		logger.info("model的值是===="+model);
		    String result = "";
		    MultipartHttpServletRequest mpRequest = (MultipartHttpServletRequest)this.request;
		    Iterator iter = mpRequest.getFileNames();
		    MultipartFile file = null;
		    while (iter.hasNext()) {
		      file = mpRequest.getFile((String)iter.next());
		      if ((file != null) && (file.getSize() != 0L)){
		        byte[] image = file.getBytes();
		        String apiPtuResult = aipPtu.faceMerge(image,Integer.parseInt(model));
		        PrintUtil.printJson(this.response, apiPtuResult);
		      } else {
		        logger.error("请检查上传文件是否正确");
		        result = "{\"result\", \"FAIL\",\"msg\":\"服务器开小差了\"}";
		        PrintUtil.printJson(this.response, result);
		      }
		    }
		}
}
  • BaseController(基类)
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ModelAttribute;
/**
 * 基类Controller 
 * 一些参数
 * Title: BaseController	
 * @author 小帅丶
 * @version 1.0
 */
public class BaseController {
	public Map session;
	public String openId;
	public String errMsg;
	public String jsonParam;
	public String callback;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	/**
	 * 每次请求都会带上
	 * @param jsonParam
	 * @param callback
	 * @param openId
	 */
	@ModelAttribute
	public void setReqAndRes(Map session, String openId, String errMsg,
			String jsonParam, String callback, HttpServletRequest request,
			HttpServletResponse response) {
		this.session = session;
		this.openId = openId;
		this.errMsg = errMsg;
		this.jsonParam = jsonParam;
		this.callback = callback;
		this.request = request;
		this.response = response;
	}
	public Map getSession() {
		return session;
	}
	public void setSession(Map session) {
		this.session = session;
	}
	public String getOpenId() {
		return openId;
	}
	public void setOpenId(String openId) {
		this.openId = openId;
	}
	public String getErrMsg() {
		return errMsg;
	}
	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}
	public String getJsonParam() {
		return jsonParam;
	}
	public void setJsonParam(String jsonParam) {
		this.jsonParam = jsonParam;
	}
	public String getCallback() {
		return callback;
	}
	public void setCallback(String callback) {
		this.callback = callback;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	public String getRealPath(String path) {
		return request.getSession().getServletContext().getRealPath(path);

	}
}
  • PrintUtil(响应类)
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
/**
 * 输出结果
 * @author 小帅丶
 *
 */
public class PrintUtil {
	public static void printXml(HttpServletResponse response,String result){
		try {
			response.setContentType("text/xml; charset=UTF-8");  
			PrintWriter sos = response.getWriter();
			sos.write(result);
			sos.flush();
			sos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 采用json 或 jsonp
	 * @param callback
	 * @param response
	 * @param result
	 */
	public static void printJson(String callback,HttpServletResponse response,String result){
		boolean jsonP = false;
		if (callback != null) {
		    jsonP = true;
		    response.setContentType("text/javascript;charset=utf-8");
		} else {
		    response.setContentType("application/x-json;charset=utf-8");
		}
		
			try {
				Writer out = response.getWriter();
				if (jsonP) {
				    out.write(callback + "(");
				}
				out.write(result.toString());
				if (jsonP) {
				    out.write(");");
				}
				out.flush();
				out.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
	}
	
	public static void printJson(HttpServletResponse response,String result){
		try {
			response.setCharacterEncoding("UTF-8");  
		    response.setContentType("application/json; charset=utf-8");  
			PrintWriter sos = response.getWriter();
			sos.write(result);
			sos.flush();
			sos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 


  微信小程序上传限制为2048kb。相关图片过多超了限制。因此需要用到云存储。使用阿里云 腾讯云都可以哦。在亲测的情况下发现。即使云储存域名不在小程序里面添加也是可以正常访问哦


【本站声明】
  1、本站文章中所选用的图片及文字来源于网络以及用户投稿,由于未联系到知识产权人或未发现有关知识产权的登记,如有知识产权人并不愿意我们使用,如果有侵权请立即联系。
  2、本网站不对文章中所涉及的内容真实性、准确性、可靠性负责,仅系客观性描述,如您需要了解该类商品/服务详细的资讯,请您直接与该类商品/服务的提供者联系。


KESION 科汛软件

KESION 科汛软件是国内领先的在线教育软件及私域社交电商软件服务提供商,长期专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。
公司核心产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化独立部署品牌网校和在线教育咨询等。

KESION 不断通过技术创新,提供产品和服务,助力企业向数字化转型,通过科技驱动商业革新,让商业变得更智慧!



▼点击进入科汛官网了解更多



热门标签
微信小程序 SaaS
上/下篇
换一换相关推荐
精选内容
热点精选