午夜91福利视频,午夜成人在线观看,午夜在线视频免费观看,午夜福利短视频,精品午夜成人免费视频APP

小程序模板網

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

發布時(shi)間(jian):2018-06-19 15:28 所屬欄目:小程序開發教程

效(xiao)果圖創(chuang)建(jian)一(yi)個AI應用(yong)//ai.qq.com/cgi-bin/console_overview登(deng)錄騰訊AI,創(chuang)建(jian)并勾選相(xiang)關接口(kou)即可。記得復制APPID APPKEY使用(yong)Java接入該功能(neng)創(chuang)建(jian)一(yi)個SpringMVC工程,包含上(shang)傳(chuan)相(xiang)關jar。或者SpringBoot工程也(ye)行。 ...

 
 
 

 

效果圖

創建一個AI應用

//ai.qq.com/cgi-bin/console_overview 登錄騰(teng)訊AI,創建并勾選相(xiang)關接口即可。

記得(de)復制APPID APPKEY

使用Java接入該功能

創建一個SpringMVC工(gong)(gong)程(cheng),包含上傳(chuan)相關(guan)jar。或者(zhe)SpringBoot工(gong)(gong)程(cheng)也行。鄙人后端還在完善(shan)并沒有完全開源。項目

Java調用(yong)騰訊AI接口。小帥丶已經封裝成SDK。

如果使用maven搭(da)建(jian)。直接(jie)pom引入即可哦


<!-- //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://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();
		}
	}
}

微信小程序代碼實現

小帥一(yi)點資訊小程序代碼是開源的(de)哦(e)

學習并使用(yong)了微信小程序scroll-view組件

 

微信小程序上傳限(xian)制(zhi)為2048kb。相(xiang)關圖片過多超(chao)了限(xian)制(zhi)。因此需要用到(dao)云(yun)(yun)存(cun)儲。使(shi)用阿里(li)云(yun)(yun) 騰訊云(yun)(yun)都可(ke)以(yi)(yi)哦。在親測的情況(kuang)下發現(xian)。即使(shi)云(yun)(yun)儲存(cun)域(yu)名(ming)不在小程序里(li)面添加也(ye)是(shi)可(ke)以(yi)(yi)正常訪問哦

截圖

 


易優小程序(企(qi)業版(ban))+靈活api+前后代碼開源 碼云倉庫:
本文地址://www.jinyoudianli.com/wxmini/doc/course/24560.html 復制鏈接 如需定制請聯(lian)系易優(you)客服咨詢:

工作日 8:30-12:00 14:30-18:00
周(zhou)六(liu)及(ji)部分節假日(ri)提供值班服務(wu)

易小優(you)
轉(zhuan)人(ren)工 ×