├── static └── test.jpg ├── requirements.txt ├── README.md ├── img.py └── templates └── index.html /static/test.jpg: -------------------------------------------------------------------------------- 1 | aaa 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | datasets==2.* 2 | fastapi==0.74.* 3 | requests==2.27.* 4 | sentencepiece==0.1.* 5 | torch==1.11.* 6 | transformers==4.* 7 | uvicorn[standard]==0.17.* 8 | modelscope 9 | tensorflow 10 | flask 11 | opencv-python 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CutImage 2 | 基于ai深度训练的全能抠图工具 3 | 4 | 在 Hugging Face 上体验(最方便,但是速度比较慢,可以点logs看进度) 5 | 6 | [](https://huggingface.co/spaces/chow-q/cut-image) 7 | 8 | 9 | 10 | docker部署: 11 | 12 | docker pull showtim2007/modelscope:1.4.3 13 | 14 | 基于达摩院BSHM模型的深度训练的全能抠图工具。 15 | 16 | 运行: docker run --name BSHM -p 8000:5000 -d showtim2007/modelscope:1.4.3 bash -c "cd /opt &&python img.py" 17 | 18 | 体验地址: 19 | 20 | http://ai-img.v-chatgpt.com:8800/ 21 | 22 | 账号:ai-img 密码:ai-img123321 23 | 24 | 示例1: 25 | 26 |  27 | 28 | 示例2: 29 | 30 |  31 | 32 | 示例3: 33 | 34 |  35 | 36 | 示例4: 37 | 38 |  39 | -------------------------------------------------------------------------------- /img.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request 2 | import os 3 | import uuid 4 | import cv2 5 | from modelscope.pipelines import pipeline 6 | from modelscope.utils.constant import Tasks 7 | from modelscope.outputs import OutputKeys 8 | app = Flask(__name__) 9 | 10 | @app.route("/", methods=["GET", "POST"]) 11 | def index(): 12 | if request.method == "POST": 13 | # 判断是否有文件上传 14 | if "file_input" not in request.files: 15 | return render_template("index.html", error="请选择一个图片上传!") 16 | 17 | file = request.files["file_input"] 18 | print("##################",request.form) 19 | 20 | # 判断上传的文件类型是否合法 21 | allowed_extensions = {"jpg", "jpeg", "png", "gif"} 22 | _, file_extension = os.path.splitext(file.filename) 23 | if not file_extension[1:] in allowed_extensions: 24 | return render_template("index.html", error="只允许上传 .jpg、.jpeg、.png、.gif 格式的图片!") 25 | 26 | # 生成一个唯一文件名,避免重复 27 | image_filename = str(uuid.uuid4()) + file_extension 28 | 29 | # 保存上传的图片 30 | file.save(os.path.join("static", image_filename)) 31 | 32 | if 'image' in request.form: 33 | #人物抠图 34 | portrait_matting = pipeline(Tasks.portrait_matting,model='damo/cv_unet_image-matting') 35 | result = portrait_matting(f"static/{image_filename}") 36 | result_filename = str(uuid.uuid4()) + ".png" 37 | cv2.imwrite(f"static/{result_filename}", result[OutputKeys.OUTPUT_IMG]) 38 | else: 39 | #通用抠图 40 | universal_matting = pipeline(Tasks.universal_matting,model='damo/cv_unet_universal-matting') 41 | result = universal_matting(f"static/{image_filename}") 42 | result_filename = str(uuid.uuid4()) + ".png" 43 | cv2.imwrite(f"static/{result_filename}", result[OutputKeys.OUTPUT_IMG]) 44 | 45 | # 显示图片 46 | image_path = f"static/{image_filename}" 47 | result_path = f"static/{result_filename}" 48 | return render_template("index.html", image_path=image_path, result_path=result_path) 49 | 50 | return render_template("index.html") 51 | if __name__ == '__main__': 52 | app.run(host="0.0.0.0",port=5000) 53 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |