imageMatting.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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'] },
  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. const res = await checkSelectImages(selectParams).catch(e=>{
  64. Log.info(e);
  65. return errData
  66. })
  67. Log.info(res.data);
  68. if( res.data){
  69. if( res.data.code === 0 && res.data.data){
  70. console.log("params.image_type");
  71. console.log(params.image_type);
  72. if(params.image_type == 2){
  73. let modelParams = {
  74. "token": params.token,
  75. "image_type": 2, //图像类型;0非服装;1服装 2人台
  76. "output_mode": params.output_type, //出图模式 ;0透明;1白底
  77. "size_mode": params.size, // 0=>指定大小;1=>最小边框;2原图尺寸
  78. "out_width": params.customWidth, // 宽度
  79. "out_height": params.customHeight, // 高度
  80. "need_cutout_images": res.data.data
  81. }
  82. if(modelParams.size_mode !== 0){
  83. delete modelParams.out_width
  84. delete modelParams.out_height
  85. }
  86. const img = await modelFormSegment(modelParams).catch(e=>{
  87. Log.info(e);
  88. return errData
  89. })
  90. if(img.data){
  91. return img.data
  92. }
  93. return false;
  94. }
  95. const img = await segmentImages({
  96. "token": params.token,
  97. "image_type": params.image_type, //图像类型;0非服装;1服装 2人台
  98. "segment_type": params.segment_type, //抠图精细度;0普通;1精细
  99. "output_type": params.output_type, //出图模式 ;0透明;1白底
  100. "need_cutout_images": res.data.data
  101. }).catch(e=>{
  102. Log.info(e);
  103. return errData
  104. })
  105. if(img.data){
  106. return img.data
  107. }
  108. return false;
  109. }
  110. return res.data
  111. }
  112. return false;
  113. }
  114. }
  115. ImageMattingController.toString = () => '[class ExampleController]';
  116. module.exports = ImageMattingController;