api.py 4.4 KB

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