Delete utils.py
Browse files
utils.py
DELETED
|
@@ -1,62 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
from transformers import AutoModelForImageClassification, AutoProcessor
|
| 3 |
-
import torch
|
| 4 |
-
from PIL import Image
|
| 5 |
-
|
| 6 |
-
# Load models once
|
| 7 |
-
bline_model = AutoModelForImageClassification.from_pretrained("RTLucassen/B-line_detection")
|
| 8 |
-
bline_processor = AutoProcessor.from_pretrained("RTLucassen/B-line_detection")
|
| 9 |
-
|
| 10 |
-
hamdan_model = AutoModelForImageClassification.from_pretrained("hamdan07/UltraSound-Lung")
|
| 11 |
-
hamdan_processor = AutoProcessor.from_pretrained("hamdan07/UltraSound-Lung")
|
| 12 |
-
|
| 13 |
-
def analyze_image(image):
|
| 14 |
-
print("🔍 Running B-line detection...")
|
| 15 |
-
inputs_bline = bline_processor(images=image, return_tensors="pt")
|
| 16 |
-
outputs_bline = bline_model(**inputs_bline)
|
| 17 |
-
logits_bline = outputs_bline.logits
|
| 18 |
-
print("B-line logits:", logits_bline.tolist())
|
| 19 |
-
pred_bline = torch.argmax(logits_bline, dim=1).item()
|
| 20 |
-
print("B-line prediction:", pred_bline)
|
| 21 |
-
|
| 22 |
-
bline_labels = ["No B-lines", "Few", "Multiple", "Coalescent"]
|
| 23 |
-
bline_label = bline_labels[pred_bline]
|
| 24 |
-
bline_count = [0, 2, 5, 10][pred_bline]
|
| 25 |
-
|
| 26 |
-
print(f"→ B-line label: {bline_label} | Estimated count: {bline_count}")
|
| 27 |
-
|
| 28 |
-
print("🔍 Running Consolidation detection...")
|
| 29 |
-
inputs_hamdan = hamdan_processor(images=image, return_tensors="pt")
|
| 30 |
-
outputs_hamdan = hamdan_model(**inputs_hamdan)
|
| 31 |
-
logits_hamdan = outputs_hamdan.logits
|
| 32 |
-
print("Consolidation logits:", logits_hamdan.tolist())
|
| 33 |
-
pred_hamdan = torch.argmax(logits_hamdan, dim=1).item()
|
| 34 |
-
print("Consolidation prediction:", pred_hamdan)
|
| 35 |
-
|
| 36 |
-
consolidation_label = "No consolidation ✅" if pred_hamdan == 0 else "Possible consolidation ⚠️"
|
| 37 |
-
|
| 38 |
-
if pred_hamdan != 0:
|
| 39 |
-
score = 3
|
| 40 |
-
interpretation = "Consolidation detected — severe abnormality"
|
| 41 |
-
elif bline_count == 0:
|
| 42 |
-
score = 0
|
| 43 |
-
interpretation = "Normal image with A-lines"
|
| 44 |
-
elif bline_count <= 2:
|
| 45 |
-
score = 1
|
| 46 |
-
interpretation = "Mild abnormality with few B-lines"
|
| 47 |
-
elif bline_count <= 6:
|
| 48 |
-
score = 2
|
| 49 |
-
interpretation = "Moderate abnormality with multiple B-lines"
|
| 50 |
-
else:
|
| 51 |
-
score = 3
|
| 52 |
-
interpretation = "Severe abnormality — B-lines + possible consolidation"
|
| 53 |
-
|
| 54 |
-
print(f"🧮 Final LUS Score: {score} → {interpretation}")
|
| 55 |
-
|
| 56 |
-
return {
|
| 57 |
-
"bline_count": bline_count,
|
| 58 |
-
"bline_label": bline_label,
|
| 59 |
-
"consolidation_label": consolidation_label,
|
| 60 |
-
"lus_score": score,
|
| 61 |
-
"lus_interpretation": interpretation
|
| 62 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|