我的明星脸-人脸识别DEMO

产品介绍

大家平常经常会听到这样的话语,“某某某,你跟周杰伦长得很像哦”,本产品设计的初衷就是:根据用户的人脸照片,借助百度云的人脸识别接口,从已有的明星库中查找与该用户长得最相像的男女明星

DEMO

请使用微信APP关注公共号 颜值打分器,通过一级菜单 其他 进入二级菜单 百度云大赛,即可以根据网页内帮助体验测试

总体架构

  1. 百度中搜索 明星 关键字,出现下图结果 Star Baidu
  2. 使用chrome浏览器开发者工具,分析出获取明星(如下图)接口 Star interface
  3. 将所有明星的姓名、图片url以及性别等信息爬取到本地文件 Star file
  4. 使用百度云BOS接口,将所有的明星图片按照性别分类上传至BOS服务器
  5. 准备调用人脸识别接口的初始运行环境:遍历BOS系统中存储的所有明星对象,使用百度云BFR接口,按照性别分类创建组和成员
  6. 依据性别遍历所有分组(由于每个分组最多包含100个用户,故分组数量达到45组),使用百度云BFR接口,计算出给定图片在当前分组中的最相像的成员,对所有分组进行汇总并按置信度排序得出与给定图片最相像的男女明星

主界面展示

Main UI

调用百度云的功能介绍

public IdentifyImageResponse identifyImageInGroup(IdentifyImageRequest request, String appId, String groupName, String imageInfo, boolean isBosPath) {
    // json请求参数
    StringWriter writer = new StringWriter();
    try {
        JsonGenerator jsonGenerator = JsonUtils.jsonGeneratorOf(writer);
        jsonGenerator.writeStartObject();
        if(isBosPath) {
            jsonGenerator.writeStringField("bosPath", imageInfo);
        }
        else {
            jsonGenerator.writeStringField("base64", imageInfo);
        }
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
    } catch (IOException e) {
        throw new BceClientException("Fail to generate json", e);
    }

    byte[] json = null;
    try {
        json = writer.toString().getBytes(DEFAULT_ENCODING);
    } catch (UnsupportedEncodingException e) {
        throw new BceClientException("Fail to get UTF-8 bytes", e);
    }

    InternalRequest internalRequest =
            this.createRequest(request, HttpMethodName.POST, APPSTR, appId, GROUPSTR, groupName);
    internalRequest.addParameter(IDENTIFYSTR, null);
    internalRequest.addHeader(Headers.CONTENT_LENGTH, String.valueOf(json.length));
    internalRequest.setContent(RestartableInputStream.wrap(json));

    IdentifyImageResponse response = this.invokeHttpClient(internalRequest, IdentifyImageResponse.class);
    return response;
}
public static Map<String, String> detectStarFace(BfrClient bfrClient, String appId, String imgInfo, boolean isBosPath) {
    Map<String, String> result = new HashMap<String, String>();
    result.clear();

    Map<String, Float> manMap = new ConcurrentHashMap<String, Float>();
    manMap.clear();
    Map<String, Float> womanMap = new ConcurrentHashMap<String, Float>();
    womanMap.clear();


    ListAllGroupsResponse listAllGroupsResponse = bfrClient.listAllGroups(BfrFaceUtil.APP_ID);
    List<GroupModel> groups = listAllGroupsResponse.getGroups();
    int consumeTime = 0;
    int manGroupNum = 0;
    int womanGroupNum = 0;
    for(int i = 0; i < groups.size(); i++) {
        String groupName = groups.get(i).getGroupName();
        if(groupName.contains(BfrFaceUtil.BFR_WOMANSTAR_GROUP_PREFIX)) {
            // 女组
            FixedThreadPool.executor.execute(new BfrFaceIdentifyThread(womanMap, bfrClient, appId, groupName, imgInfo, isBosPath));
            womanGroupNum++;
        }
        else {
            // 男组
            FixedThreadPool.executor.execute(new BfrFaceIdentifyThread(manMap, bfrClient, appId, groupName, imgInfo, isBosPath));
            manGroupNum++;
        }
    }
    while(manMap.size() < manGroupNum && womanMap.size() < womanGroupNum && consumeTime < 5000) {
        try {
            Thread.sleep(500);
            consumeTime += 500;
        }
        catch (Exception e) {
            logger.error("Detect star face Thread.sleep() occurs erorr: " + e);
        }
    }
    logger.info("Do detect star face consume time: " + consumeTime + "ms");

    if(manMap.isEmpty() || womanMap.isEmpty()) {
        result.put("msg", "FAILED");
    }
    else {
        result.put("msg", "OK");
        
        Map<String, Float> sortedManMap = MapUtil.sortByValue(manMap, false);
        Person manStar = FaceUtil.allStarMap.get(FaceUtil.person_id_prefix + sortedManMap.keySet().toArray()[0].toString());
        result.put("manName", manStar.getName());
        result.put("manImageUrl", manStar.getPicUrl());
        result.put("manConf", String.valueOf(sortedManMap.get(sortedManMap.keySet().toArray()[0].toString())));

        Map<String, Float> sortedWomanMap = MapUtil.sortByValue(womanMap, false);
        Person womanStar = FaceUtil.allStarMap.get(FaceUtil.person_id_prefix + sortedWomanMap.keySet().toArray()[0].toString());
        result.put("womanName", womanStar.getName());
        result.put("womanImageUrl", womanStar.getPicUrl());
        result.put("womanConf", String.valueOf(sortedWomanMap.get(sortedWomanMap.keySet().toArray()[0].toString())));
    }

    return result;
}

产品优势和市场前景

有部分趣味性和社交传播性

关于我

姓名:朱建
QQ:546629477
邮箱:zhujian.whu.cs@gmail.com
手机:15827483297