api.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from models import *
  2. import datetime
  3. from services.SegmentService import SegmentService
  4. from services.deal_cutout import DealCutout, DealCloths
  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. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  24. raise UnicornException("您所选文件夹下没有jpg图片,或对应图片已扣图")
  25. return success(need_cutout_images)
  26. @app.post("/api/segment_images", description="执行抠图操作")
  27. async def segmentImages(params: SegmentImages):
  28. token = params.token
  29. if token is None:
  30. raise UnicornException("token不能为空")
  31. image_type = params.image_type
  32. segment_type = params.segment_type
  33. output_type = params.output_type
  34. need_cutout_images = params.need_cutout_images
  35. result = None
  36. try:
  37. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  38. raise UnicornException("您所选文件夹下没有jpg图片,或对应图片已扣图")
  39. except Exception as e:
  40. raise UnicornException(repr(e))
  41. if image_type == 1 and segment_type == 1:
  42. raise UnicornException("服装暂不支持精细化抠图")
  43. elif image_type == 1 and segment_type == 0:
  44. deal_cutout_cloth = DealCloths()
  45. deal_cutout_cloth.token = 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 = deal_cutout_cloth.startDispose()
  51. elif image_type == 0 and segment_type == 1:
  52. deal_cutout_mode = DealCutout()
  53. deal_cutout_mode.token = token
  54. # 通用-精细化抠图
  55. deal_cutout_mode.need_cutout_images = need_cutout_images
  56. deal_cutout_mode.output_type = output_type
  57. result = deal_cutout_mode.startDispose()
  58. elif image_type == 0 and segment_type == 0:
  59. # 通用-普通抠图
  60. deal_cutout_mode = DealCutout()
  61. deal_cutout_mode.token = token
  62. deal_cutout_mode.need_cutout_images = need_cutout_images
  63. deal_cutout_mode.output_type = output_type
  64. result = deal_cutout_mode.normalMode()
  65. return success({"result": result})
  66. @app.post("/api/request_hlm", description="请求查询可用余额")
  67. async def requestHlm(params: RequestHlm):
  68. token = params.token
  69. if token is None:
  70. raise UnicornException("token不能为空")
  71. onlineData = GetOnlineData(token=token)
  72. result = onlineData.get_cutout_image_times()
  73. if result == False:
  74. raise UnicornException("请求失败")
  75. return success({"result": result})