ComputerUtil.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package com.fdkankan.model.utils;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.fdkankan.common.constant.ErrorCode;
  8. import com.fdkankan.common.exception.BusinessException;
  9. import com.fdkankan.common.util.FileUtils;
  10. import com.fdkankan.model.constants.ConstantFileName;
  11. import com.fdkankan.model.constants.ConstantFilePath;
  12. import com.fdkankan.model.constants.UploadFilePath;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import java.io.File;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.UUID;
  19. /**
  20. * 生成场景和计算场景
  21. * Created by Hb_zzZ on 2019/5/8.
  22. */
  23. public class ComputerUtil {
  24. private static Logger log = LoggerFactory.getLogger(ComputerUtil.class.getName());
  25. public static void computer(String projectNum, String path, String buildType) throws Exception{
  26. Map<String,String> map = new HashMap<String,String>();
  27. path = path.replace("//", "/");
  28. log.info("开始建模:"+projectNum);
  29. //构建算法isModel去掉,因此改成空字符串
  30. if("V2".equals(buildType)){
  31. CreateObjUtil.build3dModelOld(path, "");
  32. }
  33. if("V3".equals(buildType)){
  34. CreateObjUtil.build3dModel(path, "");
  35. }
  36. log.info("建模完成转换数据:"+projectNum);
  37. boolean vision2 = false;
  38. //读取upload文件,检验需要上传的文件是否存在
  39. String uploadJsonPath = path + File.separator + "results" +File.separator+"upload.json";
  40. String uploadData = FileUtils.readFile(uploadJsonPath);
  41. log.info("upload.json 文件路径:{}, 内容:{}" , uploadJsonPath, uploadData);
  42. JSONObject uploadJson = null;
  43. JSONArray array = null;
  44. if(uploadData!=null) {
  45. uploadJson = JSONObject.parseObject(uploadData);
  46. array = uploadJson.getJSONArray("upload");
  47. }
  48. if(array == null){
  49. String instanceId = FileUtils.readFile("/opt/hosts/hosts.txt");
  50. FileUtils.writeFile(path + File.separator + "javaErrorNow.log", instanceId + ":计算错误!");
  51. Thread.sleep(10000L);
  52. FileUtils.writeFile(path + File.separator + "javaError.log", instanceId + ":计算错误!");
  53. throw new Exception("upload.json数据出错");
  54. }
  55. }
  56. public static Map<String,String> computerRebuildVideo(String projectNum, String path) throws Exception{
  57. String videosPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, projectNum); //新版本文件路径
  58. Map<String,String> map = new HashMap<String,String>();
  59. path = path.replace("//", "/");
  60. log.info("开始建模:"+projectNum);
  61. //构建算法isModel去掉,因此改成空字符串
  62. CreateObjUtil.build3dModel(path, "");
  63. log.info("建模完成转换数据:"+projectNum);
  64. boolean vision2 = false;
  65. //读取upload文件,检验需要上传的文件是否存在
  66. String uploadData = FileUtils.readFile(path + File.separator + "results" +File.separator+"upload.json");
  67. JSONObject uploadJson = null;
  68. JSONArray array = null;
  69. if(uploadData!=null) {
  70. uploadJson = JSONObject.parseObject(uploadData);
  71. array = uploadJson.getJSONArray("upload");
  72. }
  73. if(array == null){
  74. String instanceId = FileUtils.readFile("/opt/hosts/hosts.txt");
  75. FileUtils.writeFile(path + File.separator + "javaErrorNow.log", instanceId + ":计算错误!");
  76. Thread.sleep(10000L);
  77. FileUtils.writeFile(path + File.separator + "javaError.log", instanceId + ":计算错误!");
  78. throw new Exception("upload.json数据出错");
  79. }
  80. JSONObject fileJson = null;
  81. String fileName = "";
  82. for(int i = 0, len = array.size(); i < len; i++) {
  83. fileJson = array.getJSONObject(i);
  84. fileName = fileJson.getString("file");
  85. //文件不存在抛出异常
  86. if (!new File(path + File.separator + "results" + File.separator + fileName).exists()) {
  87. throw new Exception(path + File.separator + "results" + File.separator + fileName + "文件不存在");
  88. }
  89. //video视频文件或封面图
  90. if (fileJson.getIntValue("clazz") == 20) {
  91. if (fileName.contains(".flv")) {
  92. map.put(path + File.separator + "results" + File.separator + fileName,
  93. "video/video" + projectNum + File.separator + fileName.replace("videos/", ""));
  94. //todo 待删除oldPath
  95. map.put(path + File.separator + "results" + File.separator + fileName,
  96. videosPath + fileName.replace("videos/", ""));
  97. }
  98. if (fileName.contains(".mp4")) {
  99. map.put(path + File.separator + "results" + File.separator + fileName,
  100. "video/video" + projectNum + File.separator + fileName.replace("videos/", ""));
  101. map.put(path + File.separator + "results" + File.separator + fileName,
  102. videosPath+ fileName.replace("videos/", ""));
  103. }
  104. }
  105. }
  106. log.info("准备上传文件到oss:"+projectNum);
  107. return map;
  108. }
  109. /**
  110. * 标定算法
  111. * @param path
  112. * @throws Exception
  113. */
  114. public static Map<String,String> computerCalibration(String path) throws Exception{
  115. Map<String,String> map = new HashMap<String,String>();
  116. log.info("开始标定:" );
  117. //构建算法isModel去掉,因此改成空字符串
  118. CreateObjUtil.build3dModel(path, "");
  119. // CreateObjUtil.build3dModel(unicode, "");
  120. log.info("标定完成转换数据:" );
  121. boolean vision2 = false;
  122. //读取upload文件,检验需要上传的文件是否存在
  123. String uploadData = FileUtils.readFile(path + File.separator + "results" +File.separator+"upload.json");
  124. JSONObject uploadJson = null;
  125. JSONArray array = null;
  126. if(uploadData!=null) {
  127. uploadJson = JSONObject.parseObject(uploadData);
  128. array = uploadJson.getJSONArray("upload");
  129. }
  130. if(array == null){
  131. throw new Exception("upload.json数据出错");
  132. }
  133. JSONObject fileJson = null;
  134. String fileName = "";
  135. for(int i = 0, len = array.size(); i < len; i++) {
  136. fileJson = array.getJSONObject(i);
  137. fileName = fileJson.getString("file");
  138. //文件不存在抛出异常
  139. if (!new File(path + File.separator + "results" + File.separator + fileName).exists()) {
  140. throw new Exception(path + File.separator + "results" + File.separator + fileName + "文件不存在");
  141. }
  142. if(fileJson.getIntValue("clazz") == 13 || fileJson.getIntValue("clazz") == 14){
  143. map.put(path + File.separator + "results" +File.separator+ fileName,
  144. ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "") +
  145. File.separator + fileName);
  146. }
  147. }
  148. return map;
  149. }
  150. //
  151. public static void createJson(String path, String splitType, String skyboxType, String dataDescribe,
  152. String sceneNum, String dataSource) throws Exception{
  153. JSONObject projectJson = new JSONObject();
  154. projectJson.put("version", "201909231830");
  155. projectJson.put("protocol", "file api 1.4");
  156. projectJson.put("uuid", UUID.randomUUID().toString());
  157. projectJson.put("description", "");
  158. projectJson.put("time", System.currentTimeMillis());
  159. projectJson.put("category", "default");
  160. projectJson.put("tag", null);
  161. projectJson.put("status", null);
  162. projectJson.put("sceneNum", sceneNum);
  163. projectJson.put("dataSource", dataSource);
  164. FileUtils.writeFile(path + File.separator + "project.json", projectJson.toString());
  165. JSONObject dataJson = new JSONObject();
  166. dataJson.put("split_type", splitType);
  167. dataJson.put("skybox_type", skyboxType);
  168. dataJson.put("extras", null);
  169. FileUtils.writeFile(path + File.separator + "data.json", dataJson.toString());
  170. }
  171. public static void createExtras(String rebuildParam, String hdrParam, String path) throws Exception {
  172. FileUtils.writeFile( path + File.separator + "extras" + File.separator + "videos_hdr_param.json", hdrParam);
  173. FileUtils.writeFile( path + File.separator + "extras" + File.separator + "required_videos.json", rebuildParam);
  174. }
  175. public static void createExtras(String rebuildParam,String path) throws Exception {
  176. FileUtils.writeFile( path + File.separator + "extras" + File.separator + "image-ROI.json", rebuildParam);
  177. }
  178. /**
  179. * 生成标定数据
  180. * @param calPath
  181. * @param prefix
  182. * @throws Exception
  183. */
  184. public static void createCalibrationData(String calPath, String prefix) throws Exception{
  185. File calFile = new File(calPath);
  186. if(calFile.exists()){
  187. calFile.mkdirs();
  188. }
  189. //删除results和capture文件夹
  190. FileUtils.deleteDirectory(calPath + "/capture");
  191. FileUtils.deleteDirectory(calPath + "/results");
  192. // CreateObjUtil.ossUtilCp(ConstantFilePath.OSS_PREFIX + prefix, calPath + "/capture");
  193. for(File oldFile : new File(prefix).listFiles()){
  194. FileUtils.copyFile(oldFile.getAbsolutePath(), calPath + "/capture/" + oldFile.getName(), true);
  195. }
  196. JSONObject dataJson = new JSONObject();
  197. dataJson.put("split_type", "SPLIT_V7");
  198. dataJson.put("skybox_type", "SKYBOX_V5");
  199. FileUtils.writeFile(calPath + "/data.json", dataJson.toString());
  200. }
  201. public static Map<String, String> getTypeString(String cameraType, String algorithm, String resolution){
  202. Map<String, String> map = new HashMap<String, String>();
  203. String splitType = "";
  204. String skyboxType = "";
  205. String dataDescribe = "";
  206. if(Integer.parseInt(cameraType) >= 4){
  207. if("0".equals(resolution)){
  208. // skyboxType = "SKYBOX_V4"; //tiles
  209. // skyboxType = "SKYBOX_V6"; //high,low,4k
  210. skyboxType = "SKYBOX_V7"; //high,low,2k
  211. }else {
  212. skyboxType = "SKYBOX_V1";
  213. }
  214. splitType = "SPLIT_V1";
  215. // skyboxType = "SKYBOX_V4"; //tiles
  216. dataDescribe = "double spherical";
  217. if(Integer.parseInt(cameraType) == 5 ){
  218. //新双目相机
  219. // skyboxType = "SKYBOX_V9";
  220. splitType = "SPLIT_V9";
  221. skyboxType = "SKYBOX_V1";
  222. }
  223. if(Integer.parseInt(cameraType) == 6){
  224. //小红屋新双目相机
  225. // skyboxType = "SKYBOX_V9";
  226. splitType = "SPLIT_V3";
  227. skyboxType = "SKYBOX_V7";
  228. }
  229. if(Integer.parseInt(cameraType) == 13){
  230. //转台相机
  231. skyboxType = "SKYBOX_V6";
  232. splitType = "SPLIT_V12";
  233. }
  234. if(Integer.parseInt(cameraType) == 14){
  235. //转台相机
  236. log.info("激光转台相机调用算法");
  237. skyboxType = "SKYBOX_V11";
  238. splitType = "SPLIT_V14";
  239. }
  240. }else {
  241. if("sfm".equals(algorithm)){
  242. splitType = "SPLIT_V2";
  243. skyboxType = "SKYBOX_V1";
  244. dataDescribe = "old sfm";
  245. }else {
  246. splitType = "SPLIT_V3";
  247. skyboxType = "SKYBOX_V1";
  248. dataDescribe = "old slam";
  249. }
  250. }
  251. map.put("splitType", splitType);
  252. map.put("skyboxType", skyboxType);
  253. map.put("dataDescribe", dataDescribe);
  254. return map;
  255. }
  256. /**
  257. * 循环检测算法是否计算成功
  258. * @param uploadJsonPath uploadjson路径
  259. * @param maxCheckTimes 循环次数
  260. * @param waitTime 每次检测间隔时间,毫秒
  261. * @return boolean
  262. * @throws Exception
  263. */
  264. public static boolean checkComputeCompleted(String uploadJsonPath, int maxCheckTimes, long waitTime) throws Exception{
  265. int checkTimes = 1;
  266. boolean exist = false;
  267. do {
  268. if(new File(uploadJsonPath).exists()){
  269. exist = true;
  270. break;
  271. }
  272. Thread.sleep(waitTime);
  273. ++checkTimes;
  274. }while (checkTimes <= maxCheckTimes);
  275. return exist;
  276. }
  277. /**
  278. * 获取计算完需要上传的文件数组
  279. * @param uploadJsonPath uploadjson路径
  280. * @param maxCheckTimes 校验upload.json文件是否存在循环次数
  281. * @param waitTime 每次检测间隔时间,毫秒
  282. * @return boolean
  283. * @throws Exception
  284. */
  285. public static JSONArray getUploadArray(String uploadJsonPath, int maxCheckTimes, long waitTime) throws Exception{
  286. boolean buildCompeleted = ComputerUtil.checkComputeCompleted(uploadJsonPath, maxCheckTimes, waitTime);
  287. if(!buildCompeleted){
  288. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  289. }
  290. String uploadData = FileUtil.readUtf8String(uploadJsonPath);
  291. JSONObject uploadJson;
  292. JSONArray array = null;
  293. if(StrUtil.isNotEmpty(uploadData)) {
  294. uploadJson = JSONObject.parseObject(uploadData);
  295. array = uploadJson.getJSONArray("upload");
  296. }
  297. if(CollUtil.isEmpty(array)){
  298. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  299. }
  300. return array;
  301. }
  302. }