soiz1 commited on
Commit
edb58d6
·
verified ·
1 Parent(s): fe67549

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
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
- image_url = base64_to_image(data.get("image_url"))
29
 
30
- if not image_url:
31
  return jsonify({"error": "image_url is required"}), 400
32
 
33
- # 入力画像を取得
34
- response = requests.get(image_url)
35
- input_image = Image.open(io.BytesIO(response.content)).convert("RGB")
36
 
37
- # Gradioモデルで背景除去と深度画像生成
 
 
 
 
 
38
  result = client.predict(
39
- image=handle_file(image_url),
40
  api_name="/predict"
41
  )
42
 
43
- depth_image_path = result[0] # 深度画像(白=近い)
44
- depth_image = Image.open(depth_image_path).convert("L") # Grayscale
45
 
46
- # 入力画像をRGBAに変換
47
  rgba_image = input_image.convert("RGBA")
48
  rgba_array = np.array(rgba_image)
49
- alpha_array = np.array(depth_image) # 0〜255の輝度がそのままアルファ値になる
 
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({