File size: 5,843 Bytes
8df85b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
976b5ca
8df85b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Highly Accurate Dichotomous Image Segmentation</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        .container {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        .upload-section {
            border: 2px dashed #ccc;
            padding: 20px;
            text-align: center;
            border-radius: 5px;
        }
        .results {
            display: flex;
            gap: 20px;
            flex-wrap: wrap;
        }
        .result-box {
            flex: 1;
            min-width: 300px;
        }
        img {
            max-width: 100%;
            height: auto;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        .code-block {
            background-color: #f5f5f5;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Highly Accurate Dichotomous Image Segmentation</h1>
        <p>This is a demo for DIS, a model that can remove the background from a given image.</p>
        
        <div class="upload-section">
            <h2>Upload Image</h2>
            <input type="file" id="imageInput" accept="image/*">
            <button onclick="processImage()">Remove Background</button>
        </div>
        
        <div class="results">
            <div class="result-box">
                <h3>Original Image</h3>
                <img id="originalImage" src="" alt="Original image will appear here" style="display: none;">
            </div>
            <div class="result-box">
                <h3>Result (RGBA)</h3>
                <img id="resultImage" src="" alt="Result will appear here" style="display: none;">
            </div>
            <div class="result-box">
                <h3>Mask</h3>
                <img id="maskImage" src="" alt="Mask will appear here" style="display: none;">
            </div>
        </div>
        
        <div>
            <h2>API Usage Example</h2>
            <p>You can also use the API directly with this JavaScript code:</p>
            <div class="code-block">
                <pre><code>
async function removeBackground(imageFile) {
    const formData = new FormData();
    formData.append('image', imageFile);
    
    try {
        const response = await fetch('/api/remove_bg', {
            method: 'POST',
            body: formData
        });
        
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        
        const data = await response.json();
        console.log('Result:', data);
        return data;
    } catch (error) {
        console.error('Error:', error);
        throw error;
    }
}

// Usage example:
// const fileInput = document.querySelector('input[type="file"]');
// removeBackground(fileInput.files[0])
//     .then(data => {
//         // Handle response data
//         document.getElementById('resultImage').src = data.rgba_url;
//         document.getElementById('maskImage').src = data.mask_url;
//     });
                </code></pre>
            </div>
        </div>
    </div>

    <script>
        function processImage() {
            const fileInput = document.getElementById('imageInput');
            if (!fileInput.files || fileInput.files.length === 0) {
                alert('Please select an image first');
                return;
            }
            
            const file = fileInput.files[0];
            const reader = new FileReader();
            
            reader.onload = function(e) {
                document.getElementById('originalImage').src = e.target.result;
                document.getElementById('originalImage').style.display = 'block';
            };
            reader.readAsDataURL(file);
            
            removeBackground(file)
                .then(data => {
                    document.getElementById('resultImage').src = data.rgba_url;
                    document.getElementById('resultImage').style.display = 'block';
                    document.getElementById('maskImage').src = data.mask_url;
                    document.getElementById('maskImage').style.display = 'block';
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('An error occurred while processing the image');
                });
        }
        
        async function removeBackground(imageFile) {
            const formData = new FormData();
            formData.append('image', imageFile);
            
            try {
                const response = await fetch('/api/remove_bg', {
                    method: 'POST',
                    body: formData
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const data = await response.json();
                console.log('Result:', data);
                return data;
            } catch (error) {
                console.error('Error:', error);
                throw error;
            }
        }
    </script>
</body>
</html>