Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -102,31 +102,54 @@ def estimate_quantity(pred):
|
|
| 102 |
# =====================
|
| 103 |
# ROBOFLOW (HTTP)
|
| 104 |
# =====================
|
|
|
|
|
|
|
| 105 |
def detect(image_path):
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
try:
|
|
|
|
| 109 |
with open(image_path, "rb") as f:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
if response.status_code != 200:
|
| 118 |
return f"❌ Roboflow Error: {response.text}"
|
| 119 |
|
| 120 |
data = response.json()
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
|
|
|
| 124 |
|
| 125 |
return []
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
return f"❌ Request Failed: {str(e)}"
|
| 129 |
-
|
| 130 |
# =====================
|
| 131 |
# MAIN FUNCTION
|
| 132 |
# =====================
|
|
|
|
| 102 |
# =====================
|
| 103 |
# ROBOFLOW (HTTP)
|
| 104 |
# =====================
|
| 105 |
+
import base64
|
| 106 |
+
|
| 107 |
def detect(image_path):
|
| 108 |
+
api_key = os.getenv("ROBOFLOW_API_KEY")
|
| 109 |
+
|
| 110 |
+
if not api_key:
|
| 111 |
+
return "❌ API KEY NOT FOUND"
|
| 112 |
+
|
| 113 |
+
url = "https://serverless.roboflow.com/rishab-5ghrt/detect-count-and-visualize-4"
|
| 114 |
|
| 115 |
try:
|
| 116 |
+
# convert image → base64
|
| 117 |
with open(image_path, "rb") as f:
|
| 118 |
+
image_bytes = f.read()
|
| 119 |
+
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
|
| 120 |
+
|
| 121 |
+
payload = {
|
| 122 |
+
"inputs": {
|
| 123 |
+
"image": {
|
| 124 |
+
"type": "base64",
|
| 125 |
+
"value": image_base64
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
response = requests.post(
|
| 131 |
+
url,
|
| 132 |
+
json=payload,
|
| 133 |
+
headers={
|
| 134 |
+
"Authorization": f"Bearer {api_key}",
|
| 135 |
+
"Content-Type": "application/json"
|
| 136 |
+
},
|
| 137 |
+
timeout=15
|
| 138 |
+
)
|
| 139 |
|
| 140 |
if response.status_code != 200:
|
| 141 |
return f"❌ Roboflow Error: {response.text}"
|
| 142 |
|
| 143 |
data = response.json()
|
| 144 |
|
| 145 |
+
# extract predictions
|
| 146 |
+
if "outputs" in data:
|
| 147 |
+
return data["outputs"][0].get("predictions", [])
|
| 148 |
|
| 149 |
return []
|
| 150 |
|
| 151 |
except Exception as e:
|
| 152 |
return f"❌ Request Failed: {str(e)}"
|
|
|
|
| 153 |
# =====================
|
| 154 |
# MAIN FUNCTION
|
| 155 |
# =====================
|