api.py 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(file_path,need_cutout_images)
  26. # print("need_cutout_images",need_cutout_images)
  27. # if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  28. # raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
  29. return success(need_cutout_images)
  30. @app.post("/api/segment_images", description="执行抠图操作")
  31. async def segmentImages(params: SegmentImages):
  32. token = params.token
  33. if token is None:
  34. raise UnicornException("token不能为空")
  35. image_type = params.image_type
  36. segment_type = params.segment_type
  37. output_type = params.output_type
  38. need_cutout_images = params.need_cutout_images
  39. result = None
  40. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  41. raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
  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. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  68. raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
  69. if len([x for x in need_cutout_images if x["need_cutout"]]) > 30:
  70. raise UnicornException("最多允许上传30张图片")
  71. modelFormClazz = DealModelForm(token=params.token,params=params)
  72. modelFormClazz.need_cutout_images = need_cutout_images
  73. result ,save_root_path,generate_ids=modelFormClazz.startDispose()
  74. return success({"result": result, "save_root_path": save_root_path,"generate_ids":generate_ids})
  75. @app.post("/api/search_bacth_progress", description="人台抠图")
  76. def search_bacth_progress(params:SearchProgress):
  77. modelFormClazz = DealModelForm(token=params.token,params=params)
  78. is_finished,successCount,failCount,root_path = modelFormClazz.search_progress()
  79. return success({"is_finished": is_finished,"success_count": successCount,"fail_count": failCount,"save_root_path":root_path})
  80. @app.post("/api/request_hlm", description="请求查询可用余额")
  81. async def requestHlm(params: RequestHlm):
  82. token = params.token
  83. if token is None:
  84. raise UnicornException("token不能为空")
  85. onlineData = GetOnlineData(token=token)
  86. result = onlineData.get_cutout_image_times()
  87. if result == False:
  88. raise UnicornException("请求失败")
  89. return success({"result": result})