溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

小程序中怎么接入人臉融合功能

發(fā)布時間:2022-03-15 10:55:27 來源:億速云 閱讀:199 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下小程序中怎么接入人臉融合功能 的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

創(chuàng)建一個AI應(yīng)用

https://ai.qq.com/cgi-bin/console_overview 登錄騰訊AI,創(chuàng)建并勾選相關(guān)接口即可。

記得復(fù)制APPID APPKEY

使用Java接入該功能

創(chuàng)建一個SpringMVC工程,包含上傳相關(guān)jar。或者SpringBoot工程也行。鄙人后端還在完善并沒有完全開源。具體可以參考https://gitee.com/xshuai/xai 項(xiàng)目

Java調(diào)用騰訊AI接口。小帥丶已經(jīng)封裝成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\":\"服務(wù)器開小差了\"}";
		        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 
 * 一些參數(shù)
 * 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(響應(yīng)類)

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
/**
 * 輸出結(jié)果
 * @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();
		}
	}
}

以上就是“小程序中怎么接入人臉融合功能 ”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI