Spaces:
Sleeping
Sleeping
Navy
commited on
Commit
·
1108adc
1
Parent(s):
b0f7e66
rafactor code for images
Browse files
core/defect_detection_image.py
CHANGED
|
@@ -156,7 +156,7 @@ async def run_detection_group(
|
|
| 156 |
return_exceptions=True
|
| 157 |
)
|
| 158 |
|
| 159 |
-
# Bersihkan
|
| 160 |
clean_results = []
|
| 161 |
for r in results:
|
| 162 |
if isinstance(r, Exception):
|
|
@@ -167,15 +167,19 @@ async def run_detection_group(
|
|
| 167 |
else:
|
| 168 |
clean_results.append(r)
|
| 169 |
|
|
|
|
| 170 |
has_error = any(r.get("status") == "error" for r in clean_results)
|
| 171 |
all_error = all(r.get("status") == "error" for r in clean_results)
|
| 172 |
|
| 173 |
-
status = "success"
|
| 174 |
-
message = "Success detecting defects"
|
| 175 |
if all_error:
|
| 176 |
-
status
|
|
|
|
| 177 |
elif has_error:
|
| 178 |
-
status
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
payload = {
|
| 181 |
"status": status,
|
|
@@ -186,10 +190,12 @@ async def run_detection_group(
|
|
| 186 |
"data": make_serializable(clean_results),
|
| 187 |
}
|
| 188 |
|
|
|
|
| 189 |
try:
|
| 190 |
async with httpx.AsyncClient(timeout=WEBHOOK_TIMEOUT) as client:
|
| 191 |
-
await client.post(webhook_url, json=payload)
|
| 192 |
-
|
|
|
|
| 193 |
except Exception as e:
|
| 194 |
logger.exception(f"[ERROR] Webhook failed for Station {station_id}: {e}")
|
| 195 |
|
|
|
|
| 156 |
return_exceptions=True
|
| 157 |
)
|
| 158 |
|
| 159 |
+
# Bersihkan hasil dengan aman
|
| 160 |
clean_results = []
|
| 161 |
for r in results:
|
| 162 |
if isinstance(r, Exception):
|
|
|
|
| 167 |
else:
|
| 168 |
clean_results.append(r)
|
| 169 |
|
| 170 |
+
# Tentukan status keseluruhan
|
| 171 |
has_error = any(r.get("status") == "error" for r in clean_results)
|
| 172 |
all_error = all(r.get("status") == "error" for r in clean_results)
|
| 173 |
|
|
|
|
|
|
|
| 174 |
if all_error:
|
| 175 |
+
status = "error"
|
| 176 |
+
message = "All cameras failed during detection"
|
| 177 |
elif has_error:
|
| 178 |
+
status = "partial_error"
|
| 179 |
+
message = "Some cameras failed during detection"
|
| 180 |
+
else:
|
| 181 |
+
status = "success"
|
| 182 |
+
message = "Success detecting defects"
|
| 183 |
|
| 184 |
payload = {
|
| 185 |
"status": status,
|
|
|
|
| 190 |
"data": make_serializable(clean_results),
|
| 191 |
}
|
| 192 |
|
| 193 |
+
# Kirim webhook
|
| 194 |
try:
|
| 195 |
async with httpx.AsyncClient(timeout=WEBHOOK_TIMEOUT) as client:
|
| 196 |
+
response = await client.post(webhook_url, json=payload)
|
| 197 |
+
response.raise_for_status()
|
| 198 |
+
logger.info(f"[DONE] Station {station_id} → webhook sent ({response.status_code})")
|
| 199 |
except Exception as e:
|
| 200 |
logger.exception(f"[ERROR] Webhook failed for Station {station_id}: {e}")
|
| 201 |
|