imageMatting.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict';
  2. const { Controller } = require('ee-core');
  3. const Log = require('ee-core/log');
  4. const { dialog } = require('electron');
  5. const { checkSelectImages,segmentImages,modelFormSegment } = require('../api/imageMatting')
  6. const errData = {
  7. msg :'请求失败,请联系管理员',
  8. code:999
  9. }
  10. /**
  11. * example
  12. * @class
  13. */
  14. class ImageMattingController extends Controller {
  15. constructor(ctx) {
  16. super(ctx);
  17. }
  18. /**
  19. * 所有方法接收两个参数
  20. * @param args 前端传的参数
  21. * @param event - ipc通信时才有值。详情见:控制器文档
  22. */
  23. /**
  24. * upload
  25. */
  26. async upload (params) {
  27. // const result = await Services.get('example').test('electron');
  28. let dialogParams = {}
  29. if(params.path_type === 0){
  30. dialogParams = {
  31. title:"选择图片",
  32. properties: ['openFile'], filters: [
  33. { name: '支持JPG', extensions: ['jpg','jpeg'] },
  34. ]
  35. }
  36. }else{
  37. dialogParams = {
  38. title:"选择文件夹",
  39. properties: ['openDirectory']
  40. }
  41. }
  42. const filePaths = dialog.showOpenDialogSync(dialogParams)
  43. Log.info('filePaths:::::::::'+filePaths);
  44. if(!filePaths || !filePaths[0]){
  45. return false;
  46. }
  47. Log.info(filePaths);
  48. Log.info({
  49. path_type: params.path_type, // path_type 地址类型;0图像;1目录
  50. path: filePaths[0]
  51. }
  52. );
  53. let selectParams = {
  54. path_type:params.path_type , // path_type 地址类型;0图像;1目录
  55. path:filePaths[0]
  56. }
  57. if(params.path_type === 0){
  58. selectParams = {
  59. path_type:params.path_type , // path_type 地址类型;0图像;1目录
  60. image_list:filePaths
  61. }
  62. }
  63. Log.info('selectPara===================ms');
  64. Log.info(selectParams);
  65. let res = null;
  66. let error = false
  67. res = await checkSelectImages(selectParams).catch(e=>{
  68. Log.info(e);
  69. error = errData
  70. return error;
  71. })
  72. if( res.data){
  73. if( res.data.code === 0 && res.data.data){
  74. console.log("params.image_type");
  75. console.log(params.image_type);
  76. Log.info(res.data.data.length);
  77. Log.info(res);
  78. if(params.image_type == 2){
  79. let modelParams = {
  80. "token": params.token,
  81. "image_type": 2, //图像类型;0非服装;1服装 2人台
  82. "output_mode": params.output_type, //出图模式 ;0透明;1白底
  83. "size_mode": params.size, // 0=>指定大小;1=>最小边框;2原图尺寸
  84. "out_width": params.customWidth, // 宽度
  85. "out_height": params.customHeight, // 高度
  86. "need_cutout_images": res.data.data
  87. }
  88. if(modelParams.size_mode !== 0){
  89. delete modelParams.out_width
  90. delete modelParams.out_height
  91. }
  92. let error = false
  93. const img = await modelFormSegment(modelParams).catch(e=>{
  94. Log.info(e);
  95. error = errData
  96. return error;
  97. })
  98. if(img.data){
  99. return img.data
  100. }
  101. return error;
  102. }
  103. let error = false
  104. const img = await segmentImages({
  105. "token": params.token,
  106. "image_type": params.image_type, //图像类型;0非服装;1服装 2人台
  107. "segment_type": params.segment_type, //抠图精细度;0普通;1精细
  108. "output_type": params.output_type, //出图模式 ;0透明;1白底
  109. "need_cutout_images": res.data.data
  110. }).catch(e=>{
  111. Log.info(e);
  112. error = errData
  113. return error;
  114. })
  115. if(img.data){
  116. return img.data
  117. }
  118. return error;
  119. }
  120. return res.data
  121. }
  122. return error;
  123. }
  124. }
  125. ImageMattingController.toString = () => '[class ExampleController]';
  126. module.exports = ImageMattingController;