api.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from models import *
  2. import datetime
  3. from services.SegmentService import SegmentService
  4. from services.deal_cutout import DealCutout,DealCloths
  5. @app.get("/")
  6. async def index():
  7. """入口文件"""
  8. return {
  9. "message": "请求成功",
  10. "data": f"Hello, World!",
  11. }
  12. @app.post("/api/check_select_images", description="检查目录或文件")
  13. async def checkSelect(params: CheckSelectImages):
  14. service = SegmentService()
  15. file_path = params.path
  16. path_type = params.path_type
  17. image_list = params.image_list
  18. if path_type == 0:
  19. need_cutout_images = service.initImages(image_list=image_list)
  20. else:
  21. need_cutout_images = service.check_need_cutout_images(file_path)
  22. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  23. raise UnicornException("您所选文件夹下没有jpg图片,或对应图片已扣图")
  24. return success(need_cutout_images)
  25. @app.post("/api/segment_images", description="检查目录或文件")
  26. async def segmentImages(params: SegmentImages):
  27. image_type = params.image_type
  28. segment_type = params.segment_type
  29. output_type = params.output_type
  30. need_cutout_images = params.need_cutout_images
  31. result = None
  32. if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
  33. raise UnicornException("您所选文件夹下没有jpg图片,或对应图片已扣图")
  34. if image_type == 1 and segment_type == 1:
  35. raise UnicornException("服装暂不支持精细化抠图")
  36. elif image_type == 1 and segment_type == 0:
  37. deal_cutout_cloth = DealCloths()
  38. # 服装抠图
  39. # need_cutout_images = service.check_need_cutout_images(file_path)
  40. deal_cutout_cloth.need_cutout_images = need_cutout_images
  41. deal_cutout_cloth.output_type = output_type
  42. result = deal_cutout_cloth.startDispose()
  43. elif image_type == 0 and segment_type == 1:
  44. deal_cutout_mode = DealCutout()
  45. # 通用-精细化抠图
  46. deal_cutout_mode.need_cutout_images = need_cutout_images
  47. deal_cutout_mode.output_type = output_type
  48. result = deal_cutout_mode.startDispose()
  49. elif image_type == 0 and segment_type == 0:
  50. # 通用-普通抠图
  51. deal_cutout_mode = DealCutout()
  52. deal_cutout_mode.need_cutout_images = need_cutout_images
  53. deal_cutout_mode.output_type = output_type
  54. result = deal_cutout_mode.normalMode()
  55. return success({"result": result})