qiudh commited on
Commit
9947d3e
1 Parent(s): 73207ea
Files changed (2) hide show
  1. img.py +52 -0
  2. templates/index.html +103 -0
img.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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=7860)
templates/index.html ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>AI ps</title>
6
+ <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css">
7
+ <style>
8
+ /* 自定义样式 */
9
+ body {
10
+ display: flex;
11
+ flex-direction: column;
12
+ justify-content: start;
13
+ align-items: center;
14
+ min-height: 100vh;
15
+ font-family: "Helvetica Neue", sans-serif;
16
+ }
17
+
18
+ h1 {
19
+ margin-top: 20px;
20
+ margin-bottom: 30px;
21
+ font-size: 36px;
22
+ font-weight: bold;
23
+ text-align: center;
24
+ }
25
+
26
+ form {
27
+ margin-bottom: 50px;
28
+ }
29
+
30
+ .form-input {
31
+ display: block;
32
+ margin: 0 auto;
33
+ margin-bottom: 30px;
34
+ }
35
+
36
+ input[type="file"] {
37
+ display: none;
38
+ }
39
+
40
+ .custom-file-upload {
41
+ display: inline-block;
42
+ padding: 10px 20px;
43
+ cursor: pointer;
44
+ background-color: #007bff;
45
+ color: #fff;
46
+ border-radius: 5px;
47
+ font-size: 18px;
48
+ font-weight: bold;
49
+ }
50
+
51
+ .form-img {
52
+ display: flex;
53
+ justify-content: center;
54
+ margin-top: 50px;
55
+ margin-bottom: 50px;
56
+ }
57
+
58
+ .form-img img {
59
+ max-width: 45%;
60
+ margin-right: 10%;
61
+ box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
62
+ }
63
+
64
+ .d-flex {
65
+ display: flex;
66
+ justify-content: center;
67
+ }
68
+
69
+ </style>
70
+ </head>
71
+ <body>
72
+ <div class="container">
73
+ <h1>ai抠图</h1>
74
+ {% if error %}
75
+ <div class="alert alert-danger" role="alert">{{ error }}</div>
76
+ {% endif %}
77
+ <form enctype="multipart/form-data" method="post">
78
+ <div class="d-flex">
79
+ <div class="form-group" style="margin-right: 50px;">
80
+ <label for="file_input" class="custom-file-upload">上传图片</label>
81
+ <input type="file" id="file_input" name="file_input" onchange="document.getElementById('file_name').innerHTML=this.value.split('\\').pop();">
82
+ <span id="file_name"></span>
83
+ </div>
84
+ <div class="ml-3">
85
+ <input name="image" type="submit" class="btn btn-primary btn-lg" value="人像抠图">
86
+ </div>
87
+ <div class="ml-3">
88
+ <input name="universal" type="submit" class="btn btn-primary btn-lg" value="通用抠图">
89
+ </div>
90
+ </div>
91
+ </form>
92
+ {% if image_path %}
93
+ <hr>
94
+ <div class="form-img">
95
+ <img src="{{ image_path }}" alt="上传的图片">
96
+ {% if result_path %}
97
+ <img src="{{ result_path }}" alt="人像抠图">
98
+ {% endif %}
99
+ </div>
100
+ {% endif %}
101
+ </div>
102
+ </body>
103
+ </html>