File size: 13,091 Bytes
075fc85
1822728
 
 
 
075fc85
 
3e25509
1822728
075fc85
3e25509
075fc85
 
 
 
 
 
 
 
 
1822728
 
 
 
075fc85
1822728
 
 
075fc85
1822728
075fc85
1822728
 
 
 
075fc85
 
 
 
1822728
 
075fc85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1822728
075fc85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1822728
075fc85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1822728
075fc85
 
 
1822728
075fc85
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from flask import Flask, request, jsonify, send_file, render_template_string
import os
import cv2
from rembg import new_session, remove
from rembg.sessions import sessions_class
import base64
import uuid
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

# セッションの初期化
for session in sessions_class:
    session.download_models()

def process_image(file_path, mask, model, x, y):
    im = cv2.imread(file_path, cv2.IMREAD_COLOR)
    input_path = f"temp_input_{uuid.uuid4().hex}.png"
    output_path = f"temp_output_{uuid.uuid4().hex}.png"
    cv2.imwrite(input_path, im)

    with open(input_path, 'rb') as i:
        with open(output_path, 'wb') as o:
            input_data = i.read()
            session = new_session(model)

            output = remove(
                input_data,
                session=session,
                **{"sam_prompt": [{"type": "point", "data": [x, y], "label": 1}]},
                only_mask=(mask == "Mask only")
            )
            o.write(output)

    # 一時ファイルを削除
    if os.path.exists(input_path):
        os.remove(input_path)
    
    return output_path

@app.route('/api/process', methods=['POST'])
def api_process():
    if 'file' not in request.files:
        return jsonify({'error': 'No file uploaded'}), 400
    
    file = request.files['file']
    mask = request.form.get('mask', 'Default')
    model = request.form.get('model', 'isnet-general-use')
    x = request.form.get('x', None)
    y = request.form.get('y', None)
    
    try:
        x = float(x) if x is not None else None
        y = float(y) if y is not None else None
    except (TypeError, ValueError):
        x = None
        y = None
    
    # 一時ファイルに保存
    temp_input = f"temp_{uuid.uuid4().hex}.png"
    file.save(temp_input)
    
    try:
        output_path = process_image(temp_input, mask, model, x, y)
        return send_file(output_path, mimetype='image/png')
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        # 一時ファイルを削除
        if os.path.exists(temp_input):
            os.remove(temp_input)

HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
    <title>RemBG API</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        .row {
            display: flex;
            gap: 20px;
        }
        .column {
            flex: 1;
        }
        img {
            max-width: 100%;
            height: auto;
            border: 1px solid #ddd;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        select, input, button {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            padding: 10px;
        }
        button:hover {
            background-color: #45a049;
        }
        #fetch-code {
            width: 100%;
            height: 150px;
            font-family: monospace;
            padding: 10px;
            box-sizing: border-box;
            background-color: #f5f5f5;
            border: 1px solid #ddd;
        }
        .coords-input {
            display: none;
        }
    </style>
</head>
<body>
    <h1>RemBG API</h1>
    <p>Upload an image to process with RemBG. Select options and click "Process Image".</p>
    
    <div class="container">
        <div class="row">
            <div class="column">
                <div class="form-group">
                    <label for="file">Input Image:</label>
                    <input type="file" id="file" accept="image/*">
                </div>
                <img id="input-image" src="" alt="Input image will appear here">
            </div>
            <div class="column">
                <div class="form-group">
                    <label>Output Image:</label>
                    <img id="output-image" src="" alt="Output image will appear here">
                </div>
            </div>
        </div>
        
        <div class="row">
            <div class="column">
                <div class="form-group">
                    <label for="mask">Output Type:</label>
                    <select id="mask">
                        <option value="Default">Default</option>
                        <option value="Mask only">Mask only</option>
                    </select>
                </div>
            </div>
            <div class="column">
                <div class="form-group">
                    <label for="model">Model Selection:</label>
                    <select id="model">
                        <option value="u2net">u2net</option>
                        <option value="u2netp">u2netp</option>
                        <option value="u2net_human_seg">u2net_human_seg</option>
                        <option value="u2net_cloth_seg">u2net_cloth_seg</option>
                        <option value="silueta">silueta</option>
                        <option value="isnet-general-use" selected>isnet-general-use</option>
                        <option value="isnet-anime">isnet-anime</option>
                        <option value="sam">sam</option>
                        <option value="birefnet-general">birefnet-general</option>
                        <option value="birefnet-general-lite">birefnet-general-lite</option>
                        <option value="birefnet-portrait">birefnet-portrait</option>
                        <option value="birefnet-dis">birefnet-dis</option>
                        <option value="birefnet-hrsod">birefnet-hrsod</option>
                        <option value="birefnet-cod">birefnet-cod</option>
                        <option value="birefnet-massive">birefnet-massive</option>
                    </select>
                </div>
            </div>
        </div>
        
        <div id="coords-section" style="display: none;">
            <h3>SAM Model Coordinates</h3>
            <p>Click on the image to set coordinates (for SAM model only)</p>
            <div class="row">
                <div class="column">
                    <div class="form-group">
                        <label for="x">X Coordinate:</label>
                        <input type="number" id="x" class="coords-input">
                    </div>
                </div>
                <div class="column">
                    <div class="form-group">
                        <label for="y">Y Coordinate:</label>
                        <input type="number" id="y" class="coords-input">
                    </div>
                </div>
            </div>
        </div>
        
        <button id="process-btn">Process Image</button>
        
        <div class="form-group">
            <label for="fetch-code">Fetch Code:</label>
            <textarea id="fetch-code" readonly></textarea>
        </div>
    </div>
    
    <script>
        const fileInput = document.getElementById('file');
        const inputImage = document.getElementById('input-image');
        const outputImage = document.getElementById('output-image');
        const maskSelect = document.getElementById('mask');
        const modelSelect = document.getElementById('model');
        const xInput = document.getElementById('x');
        const yInput = document.getElementById('y');
        const coordsSection = document.getElementById('coords-section');
        const processBtn = document.getElementById('process-btn');
        const fetchCodeTextarea = document.getElementById('fetch-code');
        
        // 画像プレビュー
        fileInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(event) {
                    inputImage.src = event.target.result;
                    updateFetchCode();
                };
                reader.readAsDataURL(file);
            }
        });
        
        // モデル選択でSAMの場合は座標入力表示
        modelSelect.addEventListener('change', function() {
            const isSam = modelSelect.value === 'sam';
            coordsSection.style.display = isSam ? 'block' : 'none';
            document.querySelectorAll('.coords-input').forEach(el => {
                el.style.display = isSam ? 'block' : 'none';
            });
            updateFetchCode();
        });
        
        // 画像クリックで座標取得 (SAMモデルのみ)
        inputImage.addEventListener('click', function(e) {
            if (modelSelect.value === 'sam') {
                const rect = e.target.getBoundingClientRect();
                const x = e.clientX - rect.left;
                const y = e.clientY - rect.top;
                
                xInput.value = Math.round(x);
                yInput.value = Math.round(y);
                updateFetchCode();
            }
        });
        
        // その他の入力変更時
        [maskSelect, xInput, yInput].forEach(el => {
            el.addEventListener('change', updateFetchCode);
        });
        
        // 画像処理
        processBtn.addEventListener('click', async function() {
            if (!fileInput.files || fileInput.files.length === 0) {
                alert('Please select an image file');
                return;
            }
            
            const formData = new FormData();
            formData.append('file', fileInput.files[0]);
            formData.append('mask', maskSelect.value);
            formData.append('model', modelSelect.value);
            
            if (modelSelect.value === 'sam' && xInput.value && yInput.value) {
                formData.append('x', xInput.value);
                formData.append('y', yInput.value);
            }
            
            try {
                const response = await fetch('/api/process', {
                    method: 'POST',
                    body: formData
                });
                
                if (!response.ok) {
                    const error = await response.json();
                    throw new Error(error.error || 'Failed to process image');
                }
                
                const blob = await response.blob();
                outputImage.src = URL.createObjectURL(blob);
            } catch (error) {
                alert('Error: ' + error.message);
                console.error(error);
            }
        });
        
        // Fetchコード生成
        function updateFetchCode() {
            const file = fileInput.files && fileInput.files[0];
            if (!file) {
                fetchCodeTextarea.value = '// Select an image first';
                return;
            }
            
            const mask = maskSelect.value;
            const model = modelSelect.value;
            const x = xInput.value;
            const y = yInput.value;
            
            let code = `const formData = new FormData();\n`;
            code += `formData.append('file', fileInput.files[0]);\n`;
            code += `formData.append('mask', '${mask}');\n`;
            code += `formData.append('model', '${model}');\n`;
            
            if (model === 'sam' && x && y) {
                code += `formData.append('x', '${x}');\n`;
                code += `formData.append('y', '${y}');\n`;
            }
            
            code += `\n`;
            code += `fetch('http://${window.location.host}/api/process', {\n`;
            code += `  method: 'POST',\n`;
            code += `  body: formData\n`;
            code += `})\n`;
            code += `.then(response => {\n`;
            code += `  if (!response.ok) {\n`;
            code += `    return response.json().then(err => { throw new Error(err.error); });\n`;
            code += `  }\n`;
            code += `  return response.blob();\n`;
            code += `})\n`;
            code += `.then(blob => {\n`;
            code += `  // Handle the processed image blob\n`;
            code += `  const imgUrl = URL.createObjectURL(blob);\n`;
            code += `  document.getElementById('output-image').src = imgUrl;\n`;
            code += `})\n`;
            code += `.catch(error => {\n`;
            code += `  console.error('Error:', error);\n`;
            code += `  alert('Error: ' + error.message);\n`;
            code += `});`;
            
            fetchCodeTextarea.value = code;
        }
        
        // 初期化
        updateFetchCode();
    </script>
</body>
</html>
"""

@app.route('/')
def index():
    return render_template_string(HTML_TEMPLATE)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)