File size: 14,763 Bytes
ef18197
 
 
 
 
 
 
 
76b753e
ef18197
 
 
 
 
76b753e
ef18197
 
39557f3
ef18197
 
 
 
 
 
 
 
 
 
 
39557f3
ef18197
39557f3
 
ef18197
39557f3
 
 
ef18197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39557f3
ef18197
 
 
 
39557f3
 
 
 
ef18197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39557f3
ef18197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39557f3
ef18197
 
 
 
 
 
39557f3
ef18197
 
 
 
 
39557f3
ef18197
 
 
 
 
 
 
 
76b753e
ef18197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39557f3
ef18197
39557f3
ef18197
 
 
39557f3
ef18197
 
 
 
 
 
 
 
39557f3
ef18197
 
 
 
 
 
 
 
39557f3
ef18197
 
 
 
 
 
 
 
39557f3
ef18197
 
 
39557f3
ef18197
 
 
 
 
 
 
 
39557f3
ef18197
 
 
 
 
 
39557f3
ef18197
 
 
 
39557f3
ef18197
 
 
39557f3
ef18197
 
 
 
 
 
39557f3
ef18197
 
 
 
 
 
39557f3
ef18197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39557f3
ef18197
 
 
39557f3
ef18197
 
 
 
 
 
 
76b753e
ef18197
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import HTMLResponse, StreamingResponse
from transformers import pipeline
from PIL import Image, ImageDraw
import numpy as np
import io
import uvicorn
import base64

from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as ReportLabImage
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_CENTER
from reportlab.lib.units import inch

app = FastAPI()

# Load models
def load_models():
    return {
        "KnochenAuge": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),
        "KnochenWächter": pipeline("image-classification", model="Heem2/bone-fracture-detection-using-xray"),
        "RöntgenMeister": pipeline("image-classification",
            model="nandodeomkar/autotrain-fracture-detection-using-google-vit-base-patch-16-54382127388")
    }

models = load_models()

def translate_label(label):
    # Keep translations for internal use if needed, but for the PDF we'll use English
    translations = {
        "fracture": "Fracture",
        "no fracture": "No Fracture",
        "normal": "Normal",
        "abnormal": "Abnormal",
        "F1": "Fracture", # Assuming F1 also means fracture
        "NF": "No Fracture" # Assuming NF means no fracture
    }
    return translations.get(label.lower(), label)

def create_heatmap_overlay(image, box, score):
    overlay = Image.new('RGBA', image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(overlay)

    x1, y1 = box['xmin'], box['ymin']
    x2, y2 = box['xmax'], box['ymax']

    if score > 0.8:
        fill_color = (255, 0, 0, 100)
        border_color = (255, 0, 0, 255)
    elif score > 0.6:
        fill_color = (255, 165, 0, 100)
        border_color = (255, 165, 0, 255)
    else:
        fill_color = (255, 255, 0, 100)
        border_color = (255, 255, 0, 255)

    draw.rectangle([x1, y1, x2, y2], fill=fill_color)
    draw.rectangle([x1, y1, x2, y2], outline=border_color, width=2)

    return overlay

def draw_boxes(image, predictions):
    result_image = image.copy().convert('RGBA')

    for pred in predictions:
        box = pred['box']
        score = pred['score']

        overlay = create_heatmap_overlay(image, box, score)
        result_image = Image.alpha_composite(result_image, overlay)

        draw = ImageDraw.Draw(result_image)
        temp = 36.5 + (score * 2.5)
        # Label in English
        label = f"{translate_label(pred['label'])} ({score:.1%}{temp:.1f}°C)"

        try:
            text_bbox = draw.textbbox((box['xmin'], box['ymin'] - 20), label)
        except AttributeError:
            font_size = 10
            text_width = len(label) * font_size * 0.6
            text_height = font_size * 1.2
            text_bbox = (box['xmin'], box['ymin'] - text_height, box['xmin'] + text_width, box['ymin'])

        draw.rectangle(text_bbox, fill=(0, 0, 0, 180))

        draw.text(
            (box['xmin'], box['ymin']-20),
            label,
            fill=(255, 255, 255, 255)
        )

    return result_image

def image_to_base64(image):
    buffered = io.BytesIO()
    image.save(buffered, format="PNG")
    img_str = base64.b64encode(buffered.getvalue()).decode()
    return f"data:image/png;base64,{img_str}"

COMMON_STYLES = """
    body {
        font-family: system-ui, -apple-system, sans-serif;
        background: #f0f2f5;
        margin: 0;
        padding: 20px;
        color: #1a1a1a;
    }
    ::-webkit-scrollbar {
        width: 8px;
        height: 8px;
    }

    ::-webkit-scrollbar-track {
        background: transparent;
    }

    ::-webkit-scrollbar-thumb {
        background-color: rgba(156, 163, 175, 0.5);
        border-radius: 4px;
    }

    .container {
        max-width: 1200px;
        margin: 0 auto;
        background: white;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .button {
        background: #2d2d2d;
        color: white;
        border: none;
        padding: 12px 30px;
        border-radius: 8px;
        cursor: pointer;
        font-size: 1.1em;
        transition: all 0.3s ease;
        position: relative;
    }
    .button:hover {
        background: #404040;
    }
    @keyframes progress {
        0% { width: 0; }
        100% { width: 100%; }
    }
    .button-progress {
        position: absolute;
        bottom: 0;
        left: 0;
        height: 4px;
        background: rgba(255, 255, 255, 0.5);
        width: 0;
    }
    .button:active .button-progress {
        animation: progress 2s linear forwards;
    }
    img {
        max-width: 100%;
        height: auto;
        border-radius: 8px;
    }
    @keyframes blink {
        0% { opacity: 1; }
        50% { opacity: 0; }
        100% { opacity: 1; }
    }
    #loading {
        display: none;
        color: white;
        margin-top: 10px;
        animation: blink 1s infinite;
        text-align: center;
    }
"""

@app.get("/", response_class=HTMLResponse)
async def main():
    content = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>Fracture Detection</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            {COMMON_STYLES}

            .upload-section {{
                background: #2d2d2d;
                padding: 40px;
                border-radius: 12px;
                margin: 20px 0;
                text-align: center;
                border: 2px dashed #404040;
                transition: all 0.3s ease;
                color: white;
            }}
            .upload-section:hover {{
                border-color: #555;
            }}
            input[type="file"] {{
                font-size: 1.1em;
                margin: 20px 0;
                color: white;
            }}
            input[type="file"]::file-selector-button {{
                font-size: 1em;
                padding: 10px 20px;
                border-radius: 8px;
                border: 1px solid #404040;
                background: #2d2d2d;
                color: white;
                transition: all 0.3s ease;
                cursor: pointer;
            }}
            input[type="file"]::file-selector-button:hover {{
                background: #404040;
            }}
            .confidence-slider {{
                width: 100%;
                max-width: 300px;
                margin: 20px auto;
            }}
            input[type="range"] {{
                width: 100%;
                height: 8px;
                border-radius: 4px;
                background: #404040;
                outline: none;
                transition: all 0.3s ease;
                -webkit-appearance: none;
            }}
            input[type="range"]::-webkit-slider-thumb {{
                -webkit-appearance: none;
                width: 20px;
                height: 20px;
                border-radius: 50%;
                background: white;
                cursor: pointer;
                border: none;
            }}
            .input-field {{
                margin-bottom: 20px;
            }}
            .input-field label {{
                display: block;
                margin-bottom: 5px;
                font-size: 1.1em;
            }}
            .input-field input[type="text"] {{
                width: calc(100% - 20px);
                padding: 10px;
                border-radius: 5px;
                border: 1px solid #ccc;
                background: #fff;
                color: #1a1a1a;
                font-size: 1em;
            }}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="upload-section">
                <form action="/analyze" method="post" enctype="multipart/form-data" onsubmit="document.getElementById('loading').style.display = 'block';">
                    <div class="input-field">
                        <label for="patient_name">Patient Name:</label>
                        <input type="text" id="patient_name" name="patient_name" required>
                    </div>
                    <div>
                        <input type="file" name="file" accept="image/*" required>
                    </div>
                    <div class="confidence-slider">
                        <label for="threshold">Confidence Threshold: <span id="thresholdValue">0.60</span></label>
                        <input type="range" id="threshold" name="threshold"
                               min="0" max="1" step="0.05" value="0.60"
                               oninput="document.getElementById('thresholdValue').textContent = parseFloat(this.value).toFixed(2)">
                    </div>
                    <button type="submit" class="button">
                        Analyze & Generate PDF
                        <div class="button-progress"></div>
                    </button>
                    <div id="loading">Loading...</div>
                </form>
            </div>
        </div>
    </body>
    </html>
    """
    return content

@app.post("/analyze", response_class=StreamingResponse)
async def analyze_file(patient_name: str = Form(...), file: UploadFile = File(...), threshold: float = Form(0.6)):
    try:
        contents = await file.read()
        image = Image.open(io.BytesIO(contents)).convert("RGB") # Ensure RGB for PDF

        predictions_watcher = models["KnochenWächter"](image)
        predictions_master = models["RöntgenMeister"](image)
        predictions_locator = models["KnochenAuge"](image)

        filtered_preds = [p for p in predictions_locator if p['score'] >= threshold]
        if filtered_preds:
            result_image = draw_boxes(image, filtered_preds)
        else:
            result_image = image

        # Generate PDF
        buffer = io.BytesIO()
        doc = SimpleDocTemplate(buffer, pagesize=letter)
        styles = getSampleStyleSheet()
        centered_style = ParagraphStyle(
            name='Centered',
            parent=styles['Normal'],
            alignment=TA_CENTER,
            fontSize=12,
            leading=14
        )
        heading_style = ParagraphStyle(
            name='Heading',
            parent=styles['h1'],
            alignment=TA_CENTER,
            fontSize=24,
            spaceAfter=20
        )
        subheading_style = ParagraphStyle(
            name='SubHeading',
            parent=styles['h2'],
            alignment=TA_CENTER,
            fontSize=16,
            spaceAfter=10
        )
        report_text_style = ParagraphStyle(
            name='ReportText',
            parent=styles['Normal'],
            alignment=TA_CENTER,
            fontSize=12,
            spaceAfter=5
        )

        story = []

        story.append(Paragraph("<b>Fracture Detection Report</b>", heading_style))
        story.append(Spacer(1, 0.2 * inch))
        story.append(Paragraph(f"<b>Patient Name:</b> {patient_name}", subheading_style))
        story.append(Spacer(1, 0.4 * inch))

        # KnochenWächter results
        story.append(Paragraph("<b>KnochenWächter Results:</b>", subheading_style))
        for pred in predictions_watcher:
            story.append(Paragraph(
                f"{translate_label(pred['label'])}: {pred['score']:.1%}",
                report_text_style
            ))
        story.append(Spacer(1, 0.2 * inch))

        # RöntgenMeister results
        story.append(Paragraph("<b>RöntgenMeister Results:</b>", subheading_style))
        for pred in predictions_master:
            story.append(Paragraph(
                f"{translate_label(pred['label'])}: {pred['score']:.1%}",
                report_text_style
            ))
        story.append(Spacer(1, 0.4 * inch))

        # Analyzed Image
        story.append(Paragraph("<b>X-ray Image Analysis:</b>", subheading_style))

        img_buffer = io.BytesIO()
        result_image.save(img_buffer, format="PNG")
        img_buffer.seek(0)
        img_rl = ReportLabImage(img_buffer)

        img_width, img_height = img_rl.drawWidth, img_rl.drawHeight
        aspect_ratio = img_height / img_width
        max_width = 5 * inch
        if img_width > max_width:
            img_rl.drawWidth = max_width
            img_rl.drawHeight = max_width * aspect_ratio

        img_rl.hAlign = 'CENTER'

        story.append(img_rl)
        story.append(Spacer(1, 0.4 * inch))

        # Final report text based on object detection
        if filtered_preds:
            story.append(Paragraph(
                "<b>The X-ray image analysis shows potential fracture localization.</b>",
                report_text_style
            ))
            for pred in filtered_preds:
                score = pred['score']
                temp = 36.5 + (score * 2.5)
                story.append(Paragraph(
                    f"Detection: {translate_label(pred['label'])} with {score:.1%} confidence ({temp:.1f}°C)",
                    report_text_style
                ))
        else:
            story.append(Paragraph(
                "<b>Based on object localization analysis, no fracture was detected with sufficient confidence.</b>",
                report_text_style
            ))
        story.append(Spacer(1, 0.2 * inch))
        story.append(Paragraph("This is an automatically generated report and should be reviewed by a medical professional.", centered_style))


        doc.build(story)
        buffer.seek(0)

        return StreamingResponse(buffer, media_type="application/pdf",
                                 headers={"Content-Disposition": f"attachment; filename=Fracture_Report_{patient_name.replace(' ', '_')}.pdf"})

    except Exception as e:
        return HTMLResponse(f"""
        <!DOCTYPE html>
        <html>
        <head>
            <title>Error</title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style>
                {COMMON_STYLES}
                .error-box {{
                    background: #fee2e2;
                    border: 1px solid #ef4444;
                    padding: 20px;
                    border-radius: 8px;
                    margin: 20px 0;
                }}
            </style>
        </head>
        <body>
            <div class="container">
                <div class="error-box">
                    <h3>Error</h3>
                    <p>{str(e)}</p>
                </div>
                <a href="/" class="button back-button">
                    ← Back
                    <div class="button-progress"></div>
                </a>
            </div>
        </body>
        </html>
        """)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)