CreateObjUtil.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. package com.fdkankan.model.utils;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.util.ZipUtil;
  4. import com.fdkankan.model.constants.ConstantCmd;
  5. import com.fdkankan.model.constants.ConstantFileName;
  6. import com.fdkankan.model.constants.ConstantFilePath;
  7. import com.fdkankan.model.proto.BigSceneProto;
  8. import com.fdkankan.model.proto.Common;
  9. import com.fdkankan.model.proto.Visionmodeldata;
  10. import com.fdkankan.model.proto.format.JsonFormat;
  11. import com.google.protobuf.TextFormat;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.util.ObjectUtils;
  14. import java.io.*;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. @Slf4j
  18. public class CreateObjUtil {
  19. // private static Logger log = LoggerFactory.getLogger(CreateObjUtil.class);
  20. public void saveuploadImgs(String folderName) throws IOException, Exception
  21. {
  22. log.info("开始计算");
  23. String command = "bash /home/ubuntu/photoscan-pro/build_model.sh "+folderName;
  24. callshell(command);
  25. boolean flag = false;
  26. String sPath = ConstantFilePath.CREATE_MODEL_PATH+folderName+File.separator+"mesh"+File.separator+"mesh.obj";
  27. while(!flag)
  28. {
  29. flag = isModel(sPath,folderName);
  30. }
  31. log.info("计算完毕");
  32. }
  33. public void saveuploadImgs2(String folderName) throws IOException, Exception
  34. {
  35. log.info("开始计算");
  36. String command = "bash /home/ubuntu/photoscan-pro/build_bigscene.sh "+folderName;
  37. callshell(command);
  38. boolean flag = false;
  39. String sPath = ConstantFilePath.CREATE_BIG_SCENE_PATH+folderName+File.separator+"mesh"+File.separator+"mesh.obj";
  40. while(!flag)
  41. {
  42. flag = isModel(sPath,folderName);
  43. }
  44. log.info("计算完毕");
  45. log.info("obj和camera转换成大场景要的格式");
  46. convertobjTotxt( folderName);
  47. }
  48. //开始建模
  49. public static void unRar(String rarPath,String dataPath) throws Exception{
  50. log.info("解压rar开始");
  51. String command = "unrar e " + rarPath + " " + dataPath;
  52. callshell(command);
  53. log.info("解压rar完毕:" + command);
  54. }
  55. public static void unZip(String zipPath,String dataPath) throws Exception{
  56. log.info("解压zip开始");
  57. String command = "unzip -O GBK/GB18030CP936 " + zipPath + " -d " + dataPath;
  58. callshell(command);
  59. log.info("解压zip完毕:" + command);
  60. }
  61. //开始建模
  62. public static void build3dModel(String folderName,String isModel) throws Exception{
  63. log.info("开始建模");
  64. String command = ConstantCmd.BUILD_MODEL_COMMAND+folderName;
  65. callshell(command);
  66. log.info("计算完毕:" + command);
  67. }
  68. //开始建模
  69. public static void build3dModel2(String folderName,String isModel) throws Exception{
  70. log.info("开始建模");
  71. String command = ConstantCmd.BUILD_MODEL_COMMAND2+folderName;
  72. callshell(command);
  73. log.info("计算完毕:" + command);
  74. }
  75. //开始建模
  76. public static void build3dModelOld(String folderName,String isModel) throws Exception{
  77. log.info("开始v2建模");
  78. String command = ConstantCmd.BUILD_MODEL_OLD_COMMAND+folderName;
  79. callshell(command);
  80. log.info("计算v2完毕:" + command);
  81. }
  82. //开始建模
  83. public static void translateHoustfloorJSONFile(String filePath,String outputPath) throws Exception{
  84. log.info("开始转换houst_floor.json");
  85. String command = ConstantCmd.TRANSLATE_HOUST_FLOOR + filePath + " " + outputPath;
  86. callshell(command);
  87. log.info("转换houst_floor.json 结束");
  88. }
  89. //激光相机复制资源
  90. public static void cpfile(String filepathOld,String filepathNew) throws Exception{
  91. log.info("开始复制");
  92. String command = ConstantCmd.CP_JG_EXTRA+ " " + filepathOld + " " + filepathNew;
  93. callshell(command);
  94. log.info("复制完毕:" + command);
  95. }
  96. //激光相机复制资源laser下的全部资源
  97. public static void cplaserfile(String filepathOld,String filepathNew) throws Exception{
  98. log.info("开始复制");
  99. String command = ConstantCmd.CP_JG_ALL+ " " + filepathOld + " " + filepathNew;
  100. callshell(command);
  101. log.info("复制完毕:" + command);
  102. }
  103. //开始建模
  104. public void build3dModelSFM(String folderName,String isModel) throws Exception{
  105. log.info("开始建模");
  106. String command = ConstantCmd.BUILD_MODEL_SFM_COMMAND+folderName+" "+isModel;
  107. callshell(command);
  108. log.info("计算完毕");
  109. }
  110. //obj文件转换问txt
  111. public static void objToTxt(String folderName,String isModel) throws Exception{
  112. log.info("obj2txt开始转换");
  113. String command = ConstantCmd.OBJ_TO_TXT+folderName;
  114. callshell(command);
  115. log.info("转换完毕:" + command);
  116. }
  117. public void rebuildModelFllor(String folderName, String isModel) {
  118. try{
  119. log.info("开始建模");
  120. String command = ConstantCmd.REBUILD_MODEL_FLLOR+folderName+" "+isModel;
  121. callshell(command);
  122. log.info("计算完毕");
  123. }
  124. catch(Exception e)
  125. {
  126. e.printStackTrace();
  127. }
  128. }
  129. //切图
  130. public void cutImgs(String[] imgNames ,String folderName)
  131. {
  132. try{
  133. log.info("开始切图");
  134. for(int i=0;i<imgNames.length;++i)
  135. {
  136. String imgName=imgNames[i].replace(".jpg", "");
  137. String command = ConstantCmd.CUT_IMG_COMMAND+folderName+" "+imgName;
  138. callshell(command);
  139. }
  140. log.info("切图完毕");
  141. }
  142. catch(Exception e)
  143. {
  144. e.printStackTrace();
  145. }
  146. }
  147. //调整切图
  148. public void adjustImgs(String folderName)
  149. {
  150. try{
  151. String command = ConstantCmd.ADJUST_IMG_COMMAND + folderName;
  152. log.info("开始调整图片");
  153. callshell(command);
  154. log.info("调整图片完毕");
  155. }
  156. catch(Exception e)
  157. {
  158. e.printStackTrace();
  159. }
  160. }
  161. //obj和camera转换成大场景要的格式
  162. public void convertobjTotxt(String folderName) throws Exception
  163. {
  164. //obj
  165. String command = "/home/ubuntu/photoscan-pro/main/mesh/mesh "+folderName;
  166. callshell(command);
  167. //camera
  168. command = "/home/ubuntu/photoscan-pro/main/read_camera/read_camera "+folderName;
  169. callshell(command);
  170. String prefix = ConstantFilePath.CREATE_BIG_SCENE_PATH+folderName+File.separator+"data"+File.separator;
  171. String srcpath = prefix +"mesh.txt";
  172. String despath = prefix +"dacf7dfa24ae47fab8fcebfe4dc41ab9_50k.dam";
  173. convertTxtToDam( srcpath, despath);
  174. //dam转换成lzma
  175. command = "lzma /home/ubuntu/photo_data/bigscene/"+folderName+"/data/dacf7dfa24ae47fab8fcebfe4dc41ab9_50k.dam";
  176. callshell(command);
  177. srcpath = prefix +"vision.txt";
  178. despath = prefix +"vision.modeldata";
  179. convertTxtToVisionmodeldata( srcpath, despath);
  180. }
  181. public static void convertDamToLzma(String folderName)throws Exception
  182. {
  183. try
  184. {
  185. String command = "lzma "+ folderName+File.separator+ ConstantFileName.modelUUID+"_50k.dam";
  186. log.info("开始转换lzma");
  187. callshell(command);
  188. log.info("转换lzma完毕");
  189. }
  190. catch(Exception e)
  191. {
  192. StringWriter trace=new StringWriter();
  193. e.printStackTrace(new PrintWriter(trace));
  194. log.error(trace.toString());
  195. }
  196. }
  197. public static void convertDamToLzmaByAbsolutePath(String path)throws Exception
  198. {
  199. try
  200. {
  201. String command = "lzma "+ path;
  202. log.info("开始转换lzma");
  203. callshell(command);
  204. log.info("转换lzma完毕");
  205. }
  206. catch(Exception e)
  207. {
  208. StringWriter trace=new StringWriter();
  209. e.printStackTrace(new PrintWriter(trace));
  210. log.error(trace.toString());
  211. }
  212. }
  213. public static void convertDamToLzma2(String folderName)throws Exception
  214. {
  215. try
  216. {
  217. String command = "lzma "+ folderName+File.separator+ ConstantFileName.modelUUID+"_50k2.dam";
  218. log.info("开始转换lzma");
  219. callshell(command);
  220. log.info("转换lzma完毕");
  221. }
  222. catch(Exception e)
  223. {
  224. StringWriter trace=new StringWriter();
  225. e.printStackTrace(new PrintWriter(trace));
  226. log.error(trace.toString());
  227. }
  228. }
  229. public static void convertTxtToDam(String srcpath,String despath)throws Exception{
  230. BigSceneProto.binary_mesh.Builder builder = BigSceneProto.binary_mesh.newBuilder();
  231. InputStream inputStream = new FileInputStream(srcpath);
  232. InputStreamReader reader = new InputStreamReader(inputStream, "ASCII");
  233. TextFormat.merge(reader, builder);
  234. byte[] buf = builder.build().toByteArray();
  235. //把序列化后的数据写入本地磁盘
  236. ByteArrayInputStream stream = new ByteArrayInputStream(buf);
  237. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  238. BufferedInputStream bis = new BufferedInputStream(stream);
  239. int b = -1;
  240. while ((b = bis.read()) != -1) {
  241. bos.write(b);
  242. }
  243. bis.close();
  244. bos.close();
  245. }
  246. public static void convertVisionmodeldataToTxt(String srcpath,String despath)throws Exception
  247. {
  248. try
  249. {
  250. File file = new File(srcpath);
  251. FileInputStream fis=new FileInputStream(file);
  252. Visionmodeldata.NavigationInfo data_NavigationInfo = Visionmodeldata.NavigationInfo.parseFrom(fis);
  253. //PrintStream out = new PrintStream(despath);
  254. String jsonFormat1 = JsonFormat.printToString(data_NavigationInfo);
  255. ByteArrayInputStream stream = new ByteArrayInputStream(jsonFormat1.getBytes());
  256. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  257. BufferedInputStream bis = new BufferedInputStream(stream);
  258. int b = -1;
  259. while ((b = bis.read()) != -1) {
  260. bos.write(b);
  261. }
  262. //out.close();
  263. bis.close();
  264. bos.close();
  265. }
  266. catch(Exception e)
  267. {
  268. StringWriter trace=new StringWriter();
  269. e.printStackTrace(new PrintWriter(trace));
  270. log.error(trace.toString());
  271. }
  272. }
  273. public static void convertTxtToVisionmodeldata(String srcpath,String despath)throws Exception
  274. {
  275. try{
  276. Visionmodeldata.NavigationInfo.Builder builder = Visionmodeldata.NavigationInfo.newBuilder();
  277. String jsonFormat = readTxtFileToJson(srcpath);
  278. JsonFormat.merge(jsonFormat, builder);
  279. byte[] buf= builder.build().toByteArray();
  280. //把序列化后的数据写入本地磁盘
  281. ByteArrayInputStream stream = new ByteArrayInputStream(buf);
  282. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  283. BufferedInputStream bis = new BufferedInputStream(stream);
  284. int b = -1;
  285. while ((b = bis.read()) != -1) {
  286. bos.write(b);
  287. }
  288. bis.close();
  289. bos.close();
  290. }
  291. catch(Exception e)
  292. {
  293. StringWriter trace=new StringWriter();
  294. e.printStackTrace(new PrintWriter(trace));
  295. log.error(trace.toString());
  296. }
  297. }
  298. public static void convertVisionmodeldataToTxtCommon(String srcpath,String despath)throws Exception
  299. {
  300. try
  301. {
  302. File file = new File(srcpath);
  303. FileInputStream fis=new FileInputStream(file);
  304. Common.NavigationInfo data_NavigationInfo = Common.NavigationInfo.parseFrom(fis);
  305. //PrintStream out = new PrintStream(despath);
  306. String jsonFormat1 = JsonFormat.printToString(data_NavigationInfo);
  307. ByteArrayInputStream stream = new ByteArrayInputStream(jsonFormat1.getBytes());
  308. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  309. BufferedInputStream bis = new BufferedInputStream(stream);
  310. int b = -1;
  311. while ((b = bis.read()) != -1) {
  312. bos.write(b);
  313. }
  314. //out.close();
  315. bis.close();
  316. bos.close();
  317. }
  318. catch(Exception e)
  319. {
  320. StringWriter trace=new StringWriter();
  321. e.printStackTrace(new PrintWriter(trace));
  322. log.error(trace.toString());
  323. }
  324. }
  325. public static void convertTxtToVisionmodeldataCommon(String srcpath,String despath)throws Exception
  326. {
  327. try
  328. {
  329. Common.NavigationInfo.Builder builder = Common.NavigationInfo.newBuilder();
  330. String jsonFormat = readTxtFileToJson(srcpath);
  331. JsonFormat.merge(jsonFormat, builder);
  332. byte[] buf= builder.build().toByteArray();
  333. //把序列化后的数据写入本地磁盘
  334. ByteArrayInputStream stream = new ByteArrayInputStream(buf);
  335. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  336. BufferedInputStream bis = new BufferedInputStream(stream);
  337. int b = -1;
  338. while ((b = bis.read()) != -1) {
  339. bos.write(b);
  340. }
  341. bis.close();
  342. bos.close();
  343. }
  344. catch(Exception e)
  345. {
  346. StringWriter trace=new StringWriter();
  347. e.printStackTrace(new PrintWriter(trace));
  348. log.error(trace.toString());
  349. }
  350. }
  351. public static void callshell(String command){
  352. try {
  353. Long start = System.currentTimeMillis();
  354. Process process = Runtime.getRuntime().exec(command);
  355. StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
  356. errorGobbler.start();
  357. StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
  358. outGobbler.start();
  359. process.waitFor();
  360. log.info("脚本{}执行完毕,用时:{}ms",command,System.currentTimeMillis()-start);
  361. } catch (Exception e) {
  362. e.printStackTrace();
  363. }
  364. }
  365. public static int doWaitFor(Process process) {
  366. InputStream in = null;
  367. InputStream err = null;
  368. int exitValue = -1; // returned to caller when p is finished
  369. try {
  370. in = process.getInputStream();
  371. err = process.getErrorStream();
  372. boolean finished = false; // Set to true when p is finished
  373. while (!finished) {
  374. try {
  375. while (in.available() > 0) {
  376. // Print the output of our system call
  377. Character c = new Character((char) in.read());
  378. System.out.print(c);
  379. }
  380. while (err.available() > 0) {
  381. // Print the output of our system call
  382. Character c = new Character((char) err.read());
  383. System.out.print(c);
  384. }
  385. // Ask the process for its exitValue. If the process
  386. // is not finished, an IllegalThreadStateException
  387. // is thrown. If it is finished, we fall through and
  388. // the variable finished is set to true.
  389. exitValue = process.exitValue();
  390. finished = true;
  391. } catch (IllegalThreadStateException e) {
  392. // Process is not finished yet;
  393. // Sleep a little to save on CPU cycles
  394. Thread.currentThread().sleep(500);
  395. }
  396. }
  397. } catch (Exception e) {
  398. e.printStackTrace();
  399. } finally {
  400. try {
  401. if (in != null) {
  402. in.close();
  403. }
  404. } catch (IOException e) {
  405. e.printStackTrace();
  406. }
  407. if (err != null) {
  408. try {
  409. err.close();
  410. } catch (IOException e) {
  411. e.printStackTrace();
  412. }
  413. }
  414. }
  415. return exitValue;
  416. }
  417. private boolean isModel(String sPath,String folderName)
  418. {
  419. boolean flag = false;
  420. File file = new File(sPath);
  421. if (file.isFile() && file.exists()) {
  422. flag = true;
  423. }
  424. log.info("等待...");
  425. return flag;
  426. }
  427. public static String readTxtFileToJson(String filePath){
  428. try {
  429. String encoding="UTF-8";
  430. File file=new File(filePath);
  431. if(file.isFile() && file.exists()){
  432. InputStreamReader read = new InputStreamReader(
  433. new FileInputStream(file),encoding);
  434. BufferedReader bufferedReader = new BufferedReader(read);
  435. String lineTxt = null;
  436. String result="";
  437. while((lineTxt = bufferedReader.readLine()) != null){
  438. result+=lineTxt;
  439. //log.info(lineTxt);
  440. }
  441. read.close();
  442. return result;
  443. }else{
  444. return null;
  445. }
  446. } catch (Exception e) {
  447. e.printStackTrace();
  448. return null;
  449. }
  450. }
  451. public Map<String,String> getAllFile(String dPath,String prefix)
  452. {
  453. File dirFile = new File(dPath);
  454. if (!dirFile.isDirectory()) {
  455. }
  456. Map<String,String> map = new HashMap<String,String>();
  457. File[] files = dirFile.listFiles();
  458. for (int i = 0; i < files.length; i++) {
  459. if (files[i].isFile()) {
  460. String path = files[i].getPath();
  461. map.put(path, prefix+path.substring(path.lastIndexOf("/")+1));
  462. }
  463. }
  464. return map;
  465. }
  466. public Map<String,String> getchildFile(String dPath,String prefix,String childname)
  467. {
  468. File dirFile = new File(dPath+File.separator+childname);
  469. if (!dirFile.isDirectory()) {
  470. return null;
  471. }
  472. Map<String,String> map = new HashMap<String,String>();
  473. File[] files = dirFile.listFiles();
  474. for (int i = 0; i < files.length; i++) {
  475. if (files[i].isFile()) {
  476. String path = files[i].getPath();
  477. map.put(path, prefix+childname+path.substring(path.lastIndexOf("/")));
  478. }
  479. }
  480. return map;
  481. }
  482. //转台拼图
  483. public void buildPanoramicImgs(String folderName)throws Exception
  484. {
  485. String command = ConstantCmd.BUILD_PANORAMA + folderName;
  486. log.info("开始拼全景图");
  487. callshell(command);
  488. log.info("全景图拼接完毕");
  489. }
  490. //六目拼图、切图,计算
  491. public void buildForSix(String folderName)throws Exception
  492. {
  493. String command = ConstantCmd.BUILD_FOR_SIX + folderName;
  494. log.info("开始处理数据(六目)");
  495. callshell(command);
  496. log.info("数据处理完毕(六目)");
  497. }
  498. public static void createSoftConnection(String source, String target) {
  499. String command = "ln -s " + source + " " + target;
  500. log.info("开始创建文件夹软连接");
  501. callshell(command);
  502. log.info("数据处理完毕(六目):" + command);
  503. }
  504. //合并音频
  505. public static void mergeVideo(String oldVideo , String newVideo, String targetVideo) throws Exception{
  506. String command = ConstantCmd.MERGE_VIDEO + " " + oldVideo + " " + newVideo + " " + targetVideo + " -y";
  507. log.info("开始合并视频");
  508. callshell(command);
  509. log.info("合并视频完毕:" + command);
  510. }
  511. //生成一段静音音频
  512. public static void createMuteViode(double time , String targetVideo) throws Exception{
  513. String command = ConstantCmd.CREATE_MUTE_VIDEO + " " + time + " " + targetVideo + " -y";
  514. log.info("开始生成一段静音音频");
  515. callshell(command);
  516. log.info("生成一段静音音频完毕:" + command);
  517. }
  518. //mp4文件转换成flv文件
  519. public static void mp4ToFlv(String oldVideo, String newVideo) throws Exception{
  520. String command = ConstantCmd.MP4_TO_FLV + " " + oldVideo + " " + newVideo;
  521. log.info("mp4文件转换成flv文件");
  522. callshell(command);
  523. log.info("mp4文件转换成flv文件完毕:" + command);
  524. }
  525. //删除/mnt/data/下的数据
  526. public static void deleteFile(String filePath) throws Exception{
  527. String command = ConstantCmd.DELETE_FILE + " " + filePath;
  528. log.info("删除/mnt/data/下的数据");
  529. callshell(command);
  530. log.info("删除/mnt/data/下的数据完毕:" + command);
  531. }
  532. public static void ossUtilCp(String fileUrl , String path) throws Exception{
  533. String command = ConstantCmd.OSS_UTIL_CP + " " + fileUrl + " " + path;
  534. Long start = System.currentTimeMillis();
  535. log.info("开始oss下载文件:" + command);
  536. callshell(command);
  537. log.info("oss下载文件完成,时间为:" + (System.currentTimeMillis() - start));
  538. }
  539. public static void ossFileCp(String fileUrl , String path) throws Exception{
  540. String command = ConstantCmd.OSS_FILE_CP + " " + fileUrl + " " + path;
  541. Long start = System.currentTimeMillis();
  542. log.info("开始s3文件下载文件:" + command);
  543. callshell(command);
  544. log.info("s3文件下载文件完成,时间为:" + (System.currentTimeMillis() - start));
  545. }
  546. /**
  547. * matterpro场景获取阿里云的切图数据
  548. * @param path
  549. * @throws Exception
  550. */
  551. public static void matterproCutImg(String num , String path) throws Exception{
  552. String command = ConstantCmd.MATTERPRO_CUT_IMG + " -s " + num + " -d " + path;
  553. Long start = System.currentTimeMillis();
  554. log.info("开始matterpro获取阿里云图片方法:" + command);
  555. callshell(command);
  556. log.info("matterpro获取阿里云图片方法完成,时间为:" + (System.currentTimeMillis() - start));
  557. }
  558. }