api.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from models import *
  2. import datetime
  3. from services.SegmentService import SegmentService
  4. from services.deal_cutout import DealCutout, DealCloths,DealModelForm
  5. from services.other.module_online_data import GetOnlineData
  6. @app.get("/")
  7. async def index():
  8. """入口文件"""
  9. return {
  10. "message": "请求成功",
  11. "data": f"Hello, World!",
  12. }
  13. @app.post("/api/check_select_images", description="检查目录或文件,生成可执行的文件结构")
  14. async def checkSelect(params: CheckSelectImages):
  15. service = SegmentService()
  16. file_path = params.path
  17. path_type = params.path_type
  18. image_list = params.image_list
  19. need_cutout_images = []
  20. if path_type == 0:
  21. need_cutout_images = service.initImages(image_list=image_list)
  22. else:
  23. need_cutout_images = service.check_need_cutout_images(file_path)
  24. # print("need_cutout_images",need_cutout_images)
  25. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  26. raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
  27. return success(need_cutout_images)
  28. @app.post("/api/segment_images", description="执行抠图操作")
  29. async def segmentImages(params: SegmentImages):
  30. token = params.token
  31. if token is None:
  32. raise UnicornException("token不能为空")
  33. image_type = params.image_type
  34. segment_type = params.segment_type
  35. output_type = params.output_type
  36. need_cutout_images = params.need_cutout_images
  37. result = None
  38. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  39. raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
  40. if image_type == 1 and segment_type == 1:
  41. raise UnicornException("服装暂不支持精细化抠图")
  42. elif image_type == 1 and segment_type == 0:
  43. deal_cutout_cloth = DealCloths(token)
  44. # 服装抠图
  45. # need_cutout_images = service.check_need_cutout_images(file_path)
  46. deal_cutout_cloth.need_cutout_images = need_cutout_images
  47. deal_cutout_cloth.output_type = output_type
  48. result, save_root_path = deal_cutout_cloth.startDispose()
  49. elif image_type == 0 and segment_type == 1:
  50. deal_cutout_mode = DealCutout(token)
  51. # 通用-精细化抠图
  52. deal_cutout_mode.need_cutout_images = need_cutout_images
  53. deal_cutout_mode.output_type = output_type
  54. result, save_root_path = deal_cutout_mode.startDispose()
  55. elif image_type == 0 and segment_type == 0:
  56. # 通用-普通抠图
  57. deal_cutout_mode = DealCutout(token)
  58. deal_cutout_mode.need_cutout_images = need_cutout_images
  59. deal_cutout_mode.output_type = output_type
  60. result, save_root_path = deal_cutout_mode.normalMode()
  61. return success({"result": result, "save_root_path": save_root_path})
  62. @app.post("/api/model_form_segment", description="人台抠图")
  63. def model_form_segment(params:ModelFormModel):
  64. need_cutout_images = params.need_cutout_images
  65. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  66. raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
  67. if len([x for x in need_cutout_images if x["need_cutout"]]) > 30:
  68. raise UnicornException("最多允许上传30张图片")
  69. modelFormClazz = DealModelForm(token=params.token,params=params)
  70. modelFormClazz.need_cutout_images = need_cutout_images
  71. result ,save_root_path,generate_ids=modelFormClazz.startDispose()
  72. return success({"result": result, "save_root_path": save_root_path,"generate_ids":generate_ids})
  73. @app.post("/api/search_bacth_progress", description="人台抠图")
  74. def search_bacth_progress(params:SearchProgress):
  75. modelFormClazz = DealModelForm(token=params.token,params=params)
  76. is_finished,successCount,failCount,root_path = modelFormClazz.search_progress()
  77. return success({"is_finished": is_finished,"success_count": successCount,"fail_count": failCount,"save_root_path":root_path})
  78. @app.post("/api/request_hlm", description="请求查询可用余额")
  79. async def requestHlm(params: RequestHlm):
  80. token = params.token
  81. if token is None:
  82. raise UnicornException("token不能为空")
  83. onlineData = GetOnlineData(token=token)
  84. result = onlineData.get_cutout_image_times()
  85. if result == False:
  86. raise UnicornException("请求失败")
  87. return success({"result": result})