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 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 entity = restTemplate.postForEntity(url, request, String.class); if(entity != null){ String jsonData = entity.getBody(); if(StringUtils.isEmpty(jsonData)){ return null; } List data = new ArrayList<>(); try { List> list = JsonUtils.jsonStringToObject(jsonData, List.class); for(Map 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 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; } }