Spaces:
Running
Running
alrichardbollans
commited on
Commit
·
d377551
1
Parent(s):
699de5b
Fix handling unsupported file types
Browse files
app.py
CHANGED
|
@@ -277,36 +277,46 @@ def server(input, output, session: Session):
|
|
| 277 |
im = cv2.imread(file["datapath"])
|
| 278 |
|
| 279 |
# Convert BGR to RGB for display
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
# print(f'Size of result: {sys.getsizeof(single_result)} bytes')
|
| 311 |
|
| 312 |
# Update reactive value
|
|
@@ -378,7 +388,7 @@ def server(input, output, session: Session):
|
|
| 378 |
|
| 379 |
# Create in-memory CSV file
|
| 380 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
|
| 381 |
-
print(f'result tmp csv: {tmp.name}')
|
| 382 |
df.to_csv(tmp.name, index=False)
|
| 383 |
return tmp.name
|
| 384 |
|
|
@@ -389,13 +399,16 @@ def server(input, output, session: Session):
|
|
| 389 |
tmp_img_files = []
|
| 390 |
|
| 391 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 392 |
-
print(os.listdir(os.path.dirname(temp_dir)))
|
| 393 |
for r in results:
|
| 394 |
# open your files here
|
| 395 |
named_file = os.path.join(temp_dir, r['filename'])
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp:
|
| 401 |
|
|
|
|
| 277 |
im = cv2.imread(file["datapath"])
|
| 278 |
|
| 279 |
# Convert BGR to RGB for display
|
| 280 |
+
try:
|
| 281 |
+
im_rgb = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
| 282 |
+
pil_img = Image.fromarray(im_rgb)
|
| 283 |
+
|
| 284 |
+
# Convert to base64 for HTML display
|
| 285 |
+
buffered = BytesIO()
|
| 286 |
+
pil_img.save(buffered, format="PNG")
|
| 287 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 288 |
+
|
| 289 |
+
# Run prediction with original BGR image
|
| 290 |
+
prediction = predictor(im)
|
| 291 |
+
print(f"Analyzing image {idx + 1} of {len(files)}")
|
| 292 |
+
print(f"NMS threshold: {input.nms_threshold()}")
|
| 293 |
+
print(f'Number of instances: {len(prediction["instances"])}')
|
| 294 |
+
prediction = apply_nms(prediction, mask=True, cls_agnostic_nms=input.nms_threshold())
|
| 295 |
+
print(f'Number of instances after NMS: {len(prediction["instances"])}')
|
| 296 |
+
|
| 297 |
+
classes = prediction["instances"].pred_classes.tolist()
|
| 298 |
+
|
| 299 |
+
single_result = {
|
| 300 |
+
"filename": file["name"],
|
| 301 |
+
"image_base64": img_base64,
|
| 302 |
+
"image": im,
|
| 303 |
+
**prediction,
|
| 304 |
+
"viable": classes.count(0),
|
| 305 |
+
"non-viable": classes.count(1),
|
| 306 |
+
"empty": classes.count(2),
|
| 307 |
+
"total": len(classes),
|
| 308 |
+
'NMS threshold': input.nms_threshold()
|
| 309 |
+
}
|
| 310 |
+
results.append(single_result)
|
| 311 |
+
|
| 312 |
+
except cv2.error as e:
|
| 313 |
+
print(f"Error reading image {file['name']}: {e}")
|
| 314 |
+
single_result = {
|
| 315 |
+
"filename": file["name"]
|
| 316 |
+
}
|
| 317 |
+
results.append(single_result)
|
| 318 |
+
|
| 319 |
+
|
| 320 |
# print(f'Size of result: {sys.getsizeof(single_result)} bytes')
|
| 321 |
|
| 322 |
# Update reactive value
|
|
|
|
| 388 |
|
| 389 |
# Create in-memory CSV file
|
| 390 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
|
| 391 |
+
# print(f'result tmp csv: {tmp.name}')
|
| 392 |
df.to_csv(tmp.name, index=False)
|
| 393 |
return tmp.name
|
| 394 |
|
|
|
|
| 399 |
tmp_img_files = []
|
| 400 |
|
| 401 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 402 |
+
# print(os.listdir(os.path.dirname(temp_dir)))
|
| 403 |
for r in results:
|
| 404 |
# open your files here
|
| 405 |
named_file = os.path.join(temp_dir, r['filename'])
|
| 406 |
+
try:
|
| 407 |
+
img = get_overlayed_image_from_single_result(r)
|
| 408 |
+
img.save(named_file)
|
| 409 |
+
tmp_img_files.append(named_file)
|
| 410 |
+
except KeyError as e:
|
| 411 |
+
print(f"Error reading image {r['filename']}: {e}")
|
| 412 |
|
| 413 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp:
|
| 414 |
|