CreateObjUtil.java 19 KB

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