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 boolean postLoadVideoGather(String batchId, String url, RestTemplate restTemplate) throws Exception{
|
LoadRequest request = new LoadRequest();
|
request.setDate(batchId);
|
request.setFrame_rate("30");
|
|
ResponseEntity<String> entity = restTemplate.postForEntity(url, request, String.class);
|
if(entity == null){
|
return false;
|
}
|
String jsonData = entity.getBody();
|
try {
|
Map<String, Object> map = JsonUtils.jsonStringToObject(jsonData, Map.class);
|
if(map == null || !map.containsKey("code")){
|
logger.error("python返回结果为空,或者没有code标志,无法判断调用加载视频成功!");
|
return false;
|
}
|
String code = map.get("code").toString();
|
if(code.equals("0")){
|
logger.info("python notify_gather_once() 加载视频处理成功! batchId = " + batchId);
|
return true;
|
}
|
logger.warn("python notify_gather_once() 加载视频处理失败, batchId = " + batchId);
|
return false;
|
|
} catch (Exception e) {
|
logger.error("解析json结果错误:" + jsonData, e);
|
throw e;
|
}
|
}
|
|
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){
|
// 如果存在windows反斜杠,先转换成正斜杠。2022-10-11
|
videoPath = videoPath.replaceAll("\\\\", StringUtils.FOLDER_SEPARATOR);
|
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;
|
}
|
}
|