Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,31 +25,33 @@ def base64_to_image(data_url):
|
|
| 25 |
@app.route("/remove_background", methods=["POST"])
|
| 26 |
def remove_background():
|
| 27 |
data = request.json
|
| 28 |
-
|
| 29 |
|
| 30 |
-
if not
|
| 31 |
return jsonify({"error": "image_url is required"}), 400
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
input_image = Image.open(io.BytesIO(response.content)).convert("RGB")
|
| 36 |
|
| 37 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
result = client.predict(
|
| 39 |
-
image=handle_file(
|
| 40 |
api_name="/predict"
|
| 41 |
)
|
| 42 |
|
| 43 |
-
depth_image_path = result[0]
|
| 44 |
-
depth_image = Image.open(depth_image_path).convert("L")
|
| 45 |
|
| 46 |
-
# 入力画像をRGBAに変換
|
| 47 |
rgba_image = input_image.convert("RGBA")
|
| 48 |
rgba_array = np.array(rgba_image)
|
| 49 |
-
alpha_array = np.array(depth_image)
|
|
|
|
| 50 |
|
| 51 |
-
# アルファチャンネルとして適用
|
| 52 |
-
rgba_array[:, :, 3] = alpha_array # Aチャンネルに深度画像を使用
|
| 53 |
transparent_image = Image.fromarray(rgba_array, mode="RGBA")
|
| 54 |
|
| 55 |
return jsonify({
|
|
|
|
| 25 |
@app.route("/remove_background", methods=["POST"])
|
| 26 |
def remove_background():
|
| 27 |
data = request.json
|
| 28 |
+
data_url = data.get("image_url")
|
| 29 |
|
| 30 |
+
if not data_url:
|
| 31 |
return jsonify({"error": "image_url is required"}), 400
|
| 32 |
|
| 33 |
+
# base64からPIL画像へ変換
|
| 34 |
+
input_image = base64_to_image(data_url).convert("RGB")
|
|
|
|
| 35 |
|
| 36 |
+
# 入力画像を一時ファイルに保存
|
| 37 |
+
temp_file = BytesIO()
|
| 38 |
+
input_image.save(temp_file, format="PNG")
|
| 39 |
+
temp_file.seek(0)
|
| 40 |
+
|
| 41 |
+
# Gradio モデルで背景除去
|
| 42 |
result = client.predict(
|
| 43 |
+
image=handle_file(temp_file),
|
| 44 |
api_name="/predict"
|
| 45 |
)
|
| 46 |
|
| 47 |
+
depth_image_path = result[0]
|
| 48 |
+
depth_image = Image.open(depth_image_path).convert("L")
|
| 49 |
|
|
|
|
| 50 |
rgba_image = input_image.convert("RGBA")
|
| 51 |
rgba_array = np.array(rgba_image)
|
| 52 |
+
alpha_array = np.array(depth_image)
|
| 53 |
+
rgba_array[:, :, 3] = alpha_array
|
| 54 |
|
|
|
|
|
|
|
| 55 |
transparent_image = Image.fromarray(rgba_array, mode="RGBA")
|
| 56 |
|
| 57 |
return jsonify({
|