imageMatting.js 3.3 KB

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