api.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if path_type == 0:
  20. need_cutout_images = service.initImages(image_list=image_list)
  21. else:
  22. need_cutout_images = service.check_need_cutout_images(file_path)
  23. # print("need_cutout_images",need_cutout_images)
  24. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  25. raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
  26. service.need_cutout_images = []
  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. try:
  39. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  40. raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
  41. except Exception as e:
  42. raise UnicornException(repr(e))
  43. if image_type == 1 and segment_type == 1:
  44. raise UnicornException("服装暂不支持精细化抠图")
  45. elif image_type == 1 and segment_type == 0:
  46. deal_cutout_cloth = DealCloths(token)
  47. # 服装抠图
  48. # need_cutout_images = service.check_need_cutout_images(file_path)
  49. deal_cutout_cloth.need_cutout_images = need_cutout_images
  50. deal_cutout_cloth.output_type = output_type
  51. result, save_root_path = deal_cutout_cloth.startDispose()
  52. elif image_type == 0 and segment_type == 1:
  53. deal_cutout_mode = DealCutout(token)
  54. # 通用-精细化抠图
  55. deal_cutout_mode.need_cutout_images = need_cutout_images
  56. deal_cutout_mode.output_type = output_type
  57. result, save_root_path = deal_cutout_mode.startDispose()
  58. elif image_type == 0 and segment_type == 0:
  59. # 通用-普通抠图
  60. deal_cutout_mode = DealCutout(token)
  61. deal_cutout_mode.need_cutout_images = need_cutout_images
  62. deal_cutout_mode.output_type = output_type
  63. result, save_root_path = deal_cutout_mode.normalMode()
  64. return success({"result": result, "save_root_path": save_root_path})
  65. @app.post("/api/model_form_segment", description="人台抠图")
  66. def model_form_segment(params:ModelFormModel):
  67. need_cutout_images = params.need_cutout_images
  68. try:
  69. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  70. raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
  71. except Exception as e:
  72. raise UnicornException(repr(e))
  73. try:
  74. if len([x for x in need_cutout_images if x["need_cutout"]]) > 30:
  75. raise UnicornException("最多允许上传30张图片")
  76. except Exception as e:
  77. raise UnicornException(repr(e))
  78. modelFormClazz = DealModelForm(token=params.token,params=params)
  79. modelFormClazz.need_cutout_images = need_cutout_images
  80. result ,save_root_path,generate_ids=modelFormClazz.startDispose()
  81. return success({"result": result, "save_root_path": save_root_path,"generate_ids":generate_ids})
  82. @app.post("/api/search_bacth_progress", description="人台抠图")
  83. def search_bacth_progress(params:SearchProgress):
  84. modelFormClazz = DealModelForm(token=params.token,params=params)
  85. is_finished,successCount,failCount,root_path = modelFormClazz.search_progress()
  86. return success({"is_finished": is_finished,"success_count": successCount,"fail_count": failCount,"save_root_path":root_path})
  87. @app.post("/api/request_hlm", description="请求查询可用余额")
  88. async def requestHlm(params: RequestHlm):
  89. token = params.token
  90. if token is None:
  91. raise UnicornException("token不能为空")
  92. onlineData = GetOnlineData(token=token)
  93. result = onlineData.get_cutout_image_times()
  94. if result == False:
  95. raise UnicornException("请求失败")
  96. return success({"result": result})