shikeying
2022-09-24 7b3b249a7f2320f97e21e94e26a65f4b4ead0b6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.iplatform.recvideo.util;
 
import com.iplatform.model.po.Rc_video_t1;
import com.iplatform.recvideo.Constants;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.NumberGenerator;
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
public class PythonInvokeUtils {
 
    private static final transient Logger logger = LoggerFactory.getLogger(PythonInvokeUtils.class);
 
    public static List<Rc_video_t1> acquireImageSearchResult(String videoId, String imgPath
            , String topN, String url, RestTemplate restTemplate) throws Exception{
        SearchRequest request = new SearchRequest();
        request.setImg_path(imgPath);
        request.setTop_n(topN);
 
        ResponseEntity<String> entity = restTemplate.postForEntity(url, request, String.class);
        if(entity != null){
            String jsonData = entity.getBody();
            if(StringUtils.isEmpty(jsonData)){
                return null;
            }
 
            List<Rc_video_t1> data = new ArrayList<>();
 
            try {
                List<Map<String, Object>> list = JsonUtils.jsonStringToObject(jsonData, List.class);
                for(Map<String, Object> obj : list){
                    data.add(transferToVideoT1(obj, videoId, imgPath));
                }
                logger.debug("++++++ 请求返回 Rc_video_t1: " + data.size());
                return data;
 
            } catch (Exception e) {
                logger.error("解析json结果错误:" + jsonData, e);
                throw e;
            }
        }
        return null;
    }
 
    /**
     * 从文件路径中,截取文件ID,文件用id命名。
     * @param videoPath
     * @return
     */
    public static final String getFileNameWithoutSuffix(String videoPath, String suffix){
        String[] array = videoPath.split("/");
        if(array == null || array.length == 0){
            logger.error("视频名称截取id错误:" + videoPath);
            return null;
        }
        String fileName = array[array.length-1];
//        String[] idValue = fileName.split(".");
        return fileName.replaceAll(suffix, StringUtils.EMPTY_STRING);
    }
 
    private static Rc_video_t1 transferToVideoT1(Map<String, Object> map, String videoId, String imgPath){
        String videoPath = map.get("video").toString();
        String distance = map.get("dis").toString();
        String srcImageId = getFileNameWithoutSuffix(imgPath, Constants.IMAGE_SUFFIX);
 
        Rc_video_t1 textBlock = new Rc_video_t1();
        textBlock.setSim_video_id(getFileNameWithoutSuffix(videoPath, Constants.VIDEO_SUFFIX));
        textBlock.setDistance(Double.parseDouble(distance));
        textBlock.setSrc_video_id(videoId);
        textBlock.setSrc_img(srcImageId);
        textBlock.setId(NumberGenerator.getLongSequenceId());
        return textBlock;
    }
}