视觉学习第三天

简介: 电子相册笔记

电子相册

目录结构

image-20200926161344287

  • common:是存放一些常量
  • config:路径配置,数据配置
  • controller:请求后台数据
  • service :图片的保存和处理
  • utils:MD5工具类
  • resources:放置了些静态文件和日志文件

前提要求

要购买人脸识别服务,不然用不了

环境

pom的依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.67</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/facebody -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>facebody</artifactId>
            <version>0.0.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.aliyun/imagerecog -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>imagerecog</artifactId>
            <version>0.0.5</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

ExpressionEnum

public enum ExpressionEnum {
    neutral ("neutral","中性"),
    happiness("happiness","高兴"),
    surprise("surprise","惊讶"),
    sadness("sadness","伤心"),
    anger("anger","生气"),
    disgust("disgust","厌恶"),
    fear("害怕","害怕");

    String name;
    String nameEn;

    ExpressionEnum(String nameEn,String name){
        this.name = name;
        this.nameEn = nameEn;
    }

    public static String getNameByNameEn(String nameEn){
        for (ExpressionEnum expressionEnum : ExpressionEnum.values()){
            if (expressionEnum.nameEn.equals(nameEn)){
                return expressionEnum.name;
            }
        }
        return "";
    }
}

ResourseService

@Service
@Data
@Slf4j
public class ResourceService {

    private String scene;

    @Value("./data")
    private String storagePath;

    @Value("./img")
    private String imagePath;

    static final String fileName = "data.json";

    private LabelModel labelModel;

    @Autowired
    private VisionService visionService;

    @PostConstruct
    public void loadMetaData(){
        log.info("load");
        try {
            FileInputStream inputStream = new FileInputStream(storagePath + fileName);
            labelModel = JSONObject.parseObject(inputStream, LabelModel.class);
        } catch (IOException e) {
            log.error(e.toString());
            labelModel = new LabelModel();
        }
    }

    @PreDestroy
    public void saveMetaData(){
        log.info("save");
        try {
            System.out.println(JSON.toJSONString(labelModel));
            FileOutputStream outputStream = new FileOutputStream(storagePath + fileName);
            outputStream.write(JSON.toJSONBytes(labelModel));
            outputStream.close();
        } catch (IOException e) {
            log.error(e.toString());
        }
    }

    public List<String> getPhotosByCateAndLabel(String cate,String label){
        return getAccessPath(labelModel.getImgByCate(cate));
    }

    public List<String> getAccessPath(List<String> imgs){
        List<String> result = new ArrayList<>();
        imgs.stream().forEach(img -> {
            result.add(String.format("/img/%s",img));
        });
        return result;
    }

    public List<String> getPhotosByCate(String cate) {
        return getAccessPath(labelModel.getImgByCate(cate));
    }

    public List<String> getAllPhotos(){
        return getAccessPath(labelModel.getAllImg());
    }

    public Object getAllCates(){
        return labelModel.cateMap;
    }


    public void saveAndRecognizeImage(String fileName, InputStream inputStream){
        log.info("saveImage");
        try {
            //识别场景
            inputStream.reset();
            inputStream.mark(0);
            List<String> scenes = visionService.recognizeScene(inputStream);
            labelModel.addImg(LabelModel.SCENE,fileName,scenes);

            inputStream.reset();
            inputStream.mark(0);
            List<String> expressions = visionService.recognizeExpression(inputStream);
            labelModel.addImg(LabelModel.EXPRESSION,fileName,expressions);

            //重复利用InputStream需要reset,mark操作
            inputStream.reset();
            inputStream.mark(0);

            //保存文件
            System.out.println(imagePath+fileName);
            FileOutputStream outputStream = new FileOutputStream(imagePath + fileName);
            IOUtils.copy(inputStream,outputStream);

            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
        }
    }



    @Data
    static class LabelModel{
        static final String SCENE = "scene";
        static final String EXPRESSION = "expression";
        static final String STYLE = "style";

        //保存不同场景标签包含的图片
        private Map<String,Set<String>> sceneMap;
        //保存不同表情包含有的图片
        private Map<String,Set<String>> expressionMap;

        //保存不同风格包含的图片
        private Map<String,Set<String>> styleMap;
        private Map<String,Set<String>> cateMap;

        //图片包含的标签
        //key:图片地址
        //value:Set中value值为:{[style| expression|style]}_{label},例如:style_怀旧
        private Map<String,Set<String>> imgLables;

        public LabelModel(){
            this.imgLables = new HashMap<>();
            this.sceneMap = new HashMap<>();
            this.expressionMap = new HashMap<>();
            this.styleMap = new HashMap<>();
            this.cateMap = new HashMap<>();
        }

        //获取所有的图片
        public List<String> getAllImg(){
            List<String> result = new ArrayList<>();
            imgLables.forEach((k,v)->{
                result.add(k);
            });

            return result.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        }

        //获取图片的种类
        public List<String> getImgByCate(String cate){
            Map<String,Set<String>> data = new HashMap<>();
            switch (cate){
                case SCENE:{
                    data = sceneMap;
                    break;
                }
                case EXPRESSION:{
                    data = expressionMap;
                    break;
                }
                case STYLE:{
                    data = styleMap;
                    break;
                }
            }

            Set<String> result = new HashSet<>();
            data.forEach((k,d)->{
                result.addAll(d);
            });

            return result.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        }


        public List<String> getImgByCateAndLabel(String cate,String label){
            System.out.println(cate);
            System.out.println(label);

            String key = String.format("%s_%s",cate,label);

            Set<String> result = new HashSet<>();
            switch (cate){
                case SCENE:{
                    result = sceneMap.get(key);
                    break;
                }
                case EXPRESSION:{
                    result = expressionMap.get(key);
                    break;
                }
                case STYLE:{
                    result = styleMap.get(key);
                    break;
                }
            }
            return result.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        }

        void addImg(String cate,String img,List<String> labels){
            for (String label : labels) {
                String item = String.format("%s_%s", cate, label);
                Set<String> imgSet = imgLables.getOrDefault(img, new HashSet<>());
                imgSet.add(item);
                imgLables.put(img, imgSet);
                switch (cate) {
                    case SCENE: {
                        Set<String> sceneSet = sceneMap.getOrDefault(item, new HashSet<>());
                        sceneSet.add(img);
                        sceneMap.put(item, sceneSet);
                        Set<String> cateSet = cateMap.getOrDefault(cate, new HashSet<>());
                        cateSet.add(label);
                        cateMap.put(cate, cateSet);
                        break;
                    }
                    case EXPRESSION: {
                        Set<String> expressionSet = expressionMap.getOrDefault(item, new HashSet<>());
                        expressionSet.add(img);
                        expressionMap.put(item, expressionSet);
                        Set<String> cateSet = cateMap.getOrDefault(cate, new HashSet<>());
                        cateSet.add(label);
                        cateMap.put(cate, cateSet);
                        break;
                    }
                    case STYLE: {
                        Set<String> styleSet = styleMap.getOrDefault(item, new HashSet<>());
                        styleSet.add(img);
                        styleMap.put(item, styleSet);
                        Set<String> cateSet = cateMap.getOrDefault(cate, new HashSet<>());
                        cateSet.add(label);
                        cateMap.put(cate, cateSet);
                        break;
                    }
                }
            }
        }


        void removeImg(String img) {
            Set<String> labels = imgLables.remove(img);
            labels.stream().forEach(label -> {
                String[] segs = label.split("_");
                String cate = segs[0];
                String labelKey = segs[1];
                switch (cate) {
                    case SCENE: {
                        sceneMap.get(labelKey).remove(img);
                        break;
                    }
                    case EXPRESSION: {
                        expressionMap.get(labelKey).remove(img);
                        break;
                    }
                    case STYLE: {
                        styleMap.get(labelKey).remove(img);
                        break;
                    }
                }
            });
        }
    }
}

VisionService

@Service
@Slf4j
public class VisionService {

    @Value("${aliyun.accessKeyId}")
    private String accessKey;

    @Value("${aliyun.accessKeySecret}")
    private String accessSecret;

    static String faceBodyEndpoint = "facebody";
    static String imageRecogEndpoint = "imagerecog";
    static String objectDetEndpoint = "objectdet";


    private Client getFaceBodyClient(String endpoint) throws Exception {
        Config config = new Config();
        config.accessKeyId = accessKey;
        config.accessKeySecret = accessSecret;
        config.type = "access_key";
        config.regionId = "cn-shanghai";
        config.endpointType = "internal";
        config.endpoint = String.format("%s.%s",endpoint,"cn-shanghai.aliyuncs.com");
        config.protocol = "http";
        return new Client(config);
    }

    private com.aliyun.imagerecog.Client getImageRecognClient(String endpoint) throws Exception {
        com.aliyun.imagerecog.models.Config config = new com.aliyun.imagerecog.models.Config();
        config.accessKeyId = accessKey;
        config.accessKeySecret = accessSecret;
        config.type = "access_key";
        config.regionId = "cn-shanghai";
        config.endpointType = "internal";
        config.endpoint = String.format("%s.%s",endpoint,"cn-shanghai.aliyuncs.com");
        config.protocol = "http";
        return new com.aliyun.imagerecog.Client(config);
    }

    public static InputStream getImageStream(String url) throws Exception{
        URLConnection conn = new URL(url).openConnection();
        conn.setConnectTimeout(10 * 1000);
        conn.setReadTimeout(10 * 1000);
        return conn.getInputStream();
    }

    public List<String> recognizeScene(InputStream inputStream) throws Exception {
        RecognizeSceneAdvanceRequest request = new RecognizeSceneAdvanceRequest();
        request.imageURLObject = inputStream;

        List<String> labels = new ArrayList<>();
        try {
            com.aliyun.imagerecog.Client client = getImageRecognClient(imageRecogEndpoint);
            RecognizeSceneResponse resp = client.recognizeSceneAdvance(request, new RuntimeObject());
            for (RecognizeSceneResponse.RecognizeSceneResponseDataTags tag: resp.data.tags) {
                labels.add(tag.value);
            }
        } catch (ClientException e) {
            log.error("ErrCode:{}, ErrMsg:{}, RequestId:{}", e.getErrCode(), e.getErrMsg(), e.getRequestId());
        }
        return labels;
    }

    public List<String> recognizeExpression(InputStream inputStream) throws Exception {
        RecognizeExpressionAdvanceRequest request = new RecognizeExpressionAdvanceRequest();
        request.imageURLObject = inputStream;

        List<String> labels = new ArrayList<>();
        try {
            Client client = getFaceBodyClient(faceBodyEndpoint);
            RecognizeExpressionResponse resp = client.recognizeExpressionAdvance(request, new RuntimeObject());
            for (RecognizeExpressionResponse.RecognizeExpressionResponseDataElements element : resp.data.elements) {
                labels.add(ExpressionEnum.getNameByNameEn(element.expression));
            }
        } catch (ClientException e) {
            log.error("ErrCode:{}, ErrMsg:{}, RequestId:{}", e.getErrCode(), e.getErrMsg(), e.getRequestId());
        }
        return labels;
    }


}

Controller

@RestController
@RequestMapping("/album/v1")
@Slf4j
public class AlbumController {

    @Autowired
    private VisionService visionService;

    @Autowired
    private ResourceService resourceService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Object getList() throws Exception {
        return resourceService.getAllPhotos();
    }


    @RequestMapping(value = "/allCates", method = RequestMethod.GET)
    public Object getCates() throws Exception {
        return resourceService.getAllCates();
    }

    @RequestMapping(value = "/getPhotosByCateAndLabel", method = RequestMethod.GET)
    public Object getPhotosByCateAndLabel(@RequestParam(name="cate") String cate, @RequestParam(name="tag") String tag) throws Exception {
        return resourceService.getPhotosByCateAndLabel(cate, tag);
    }

    @RequestMapping(value = "/getPhotosByCate", method = RequestMethod.GET)
    public Object getPhotosByCate(@RequestParam(name="cate") String cate) throws Exception {
        return resourceService.getPhotosByCate(cate);
    }

    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) throws Exception {
        //计算上传文件的md5值,作为文件名
        byte[] bytes = file.getBytes();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        String md5Str = Md5CaculateUtil.getMD5(inputStream);
        inputStream.reset();
        inputStream.mark(0);

        String fileName = file.getOriginalFilename();
        String fType = fileName.substring(fileName.lastIndexOf("."));
        fileName = String.format("%s%s", md5Str, fType);
        resourceService.saveAndRecognizeImage(fileName, inputStream);
        return fileName;
    }


}

前端是用vue来实现的界面

image-20200926162039892

相关文章
|
2月前
|
人工智能 自然语言处理
Sora如何用?小白教程,一文带你看清
仅仅凭借文字就可以生成一段视频!近日,OpenAI的一款新产品又火出圈了。当地时间周四(2月15日),OpenAI发布了首个文生视频模型Sora。 2024年2月16日,OpenAI在官网上正式宣布推出**文本生成视频**的[大模型 Sora](https://openai.com/sora),该工具可以通过使用文本迅速制作出一段长达60秒的视频,视频中可以呈现多个角色、特定动作、以及复杂场景。OpenAI的官网上现已更新了48个视频案例,可以说是效果逼真,能展现艳丽的色彩,呈现准确的细节,连人物角色都是表情丰富。
|
1月前
|
人工智能 机器人 API
ChatGPT4.0直接出图功能上线!神奇一键画,新手小白也能绘画(chatgpt4.0直接出圖)
ChatGPT4.0直接出图功能上线!神奇一键画,新手小白也能绘画(chatgpt4.0直接出圖)
88 2
|
11月前
|
人工智能 并行计算 网络安全
只需三分钟,让大家都能体验到AI写文章的乐趣!
只需三分钟,让大家都能体验到AI写文章的乐趣!
167 1
|
11月前
|
机器学习/深度学习 人工智能 算法
【周末闲谈】AI作图,你真的了解它吗?
【周末闲谈】AI作图,你真的了解它吗?
104 0
|
11月前
|
开发者
ChatGPT能够教你学习游戏制作吗?
嗨!大家好,我是小蚂蚁。 下面的这张是我的一位学员在学员群里发的同 ChatGPT 对话的截图。 可以看到,ChatGPT 知道微信小游戏制作工具,知道它可以通过拖拽积木块儿的方式编写小游戏代码,甚至知道它的一些基础操作,以及它的优点和弊端。 如果单单只是这些,还不足以让人惊讶,因为毕竟这些信息也都是可以通过搜索查到的。但是当看到下面的具体实现问题的回答时,我还是被惊讶到了。
108 0
|
11月前
|
人工智能 搜索推荐 区块链
ChatGPT还在2G冲浪?新模型「youChat」:我已能够解说2022世界杯(1)
ChatGPT还在2G冲浪?新模型「youChat」:我已能够解说2022世界杯
|
11月前
|
人工智能 搜索推荐 iOS开发
ChatGPT还在2G冲浪?新模型「youChat」:我已能够解说2022世界杯(2)
ChatGPT还在2G冲浪?新模型「youChat」:我已能够解说2022世界杯
ChatGPT还在2G冲浪?新模型「youChat」:我已能够解说2022世界杯(2)
|
11月前
|
人工智能 JSON 前端开发
大火AutoGPT星标超PyTorch,网友:看清它的局限性
大火AutoGPT星标超PyTorch,网友:看清它的局限性
|
11月前
|
机器学习/深度学习 Web App开发 自然语言处理
强化学习大牛Sergey Levine新作:三个大模型教会机器人认路
强化学习大牛Sergey Levine新作:三个大模型教会机器人认路
246 0
|
机器学习/深度学习 C++
百度飞桨世界冠军带你从零实践强化学习第四天(三岁白话时间)
这里是三岁,这里吧第四的素材和资料整理了一下,大家康康,有什么不足的欢迎提出,批评指正!!!
94 0
百度飞桨世界冠军带你从零实践强化学习第四天(三岁白话时间)