Faethon88 commited on
Commit
e9274bc
·
verified ·
1 Parent(s): bb387d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -2
app.py CHANGED
@@ -6,6 +6,8 @@ from collections import Counter
6
  from PIL import Image
7
  from huggingface_hub import hf_hub_download
8
  import os
 
 
9
 
10
  # -------------------------------
11
  # Load YOLO model safely with HF token
@@ -75,9 +77,28 @@ def detect_ships(image: Image.Image, confidence: float):
75
  return None, f"❌ Detection failed: {e}"
76
 
77
  # -------------------------------
78
- # Gradio API function
79
  # -------------------------------
80
  def predict(image, confidence):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  return detect_ships(image, confidence)
82
 
83
  # -------------------------------
@@ -103,4 +124,4 @@ if __name__ == "__main__":
103
  server_port=7860,
104
  show_error=True, # Show detailed errors in browser
105
  debug=True # Print detailed logs to console
106
- )
 
6
  from PIL import Image
7
  from huggingface_hub import hf_hub_download
8
  import os
9
+ import base64
10
+ from io import BytesIO
11
 
12
  # -------------------------------
13
  # Load YOLO model safely with HF token
 
77
  return None, f"❌ Detection failed: {e}"
78
 
79
  # -------------------------------
80
+ # Gradio API function with wrapper for remote dict input
81
  # -------------------------------
82
  def predict(image, confidence):
83
+ print("DEBUG: predict called")
84
+ print("DEBUG: raw image type:", type(image))
85
+ print("DEBUG: confidence type/value:", type(confidence), confidence)
86
+
87
+ # Handle dict input from remote client (Flask sends {"name":..., "data": data_uri})
88
+ if isinstance(image, dict):
89
+ data = image.get("data") or image.get("image") or ""
90
+ if data and isinstance(data, str) and data.startswith("data:image"):
91
+ try:
92
+ header, b64 = data.split(",", 1)
93
+ image = Image.open(BytesIO(base64.b64decode(b64))).convert("RGB")
94
+ print("DEBUG: decoded image from data URI to PIL.Image")
95
+ except Exception as e:
96
+ print("ERROR decoding data URI:", e)
97
+ raise
98
+
99
+ if image is None:
100
+ raise ValueError("Received empty image")
101
+
102
  return detect_ships(image, confidence)
103
 
104
  # -------------------------------
 
124
  server_port=7860,
125
  show_error=True, # Show detailed errors in browser
126
  debug=True # Print detailed logs to console
127
+ )