api.py 4.3 KB

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