from fastapi import FastAPI, File, UploadFile, Form from fastapi.responses import HTMLResponse, Response 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, Image as ReportLabImage, Paragraph, Spacer from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.colors import red, blue, black from reportlab.lib.units import inch app = FastAPI() # Chargement des modèles 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): translations = { "fracture": "Knochenbruch", "no fracture": "Kein Knochenbruch", "normal": "Normal", "abnormal": "Auffällig", "F1": "Knochenbruch", "NF": "Kein Knochenbruch" } 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 = f"{translate_label(pred['label'])} ({score:.1%} • {temp:.1f}°C)" text_bbox = draw.textbbox((box['xmin'], box['ymin']-20), label) 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}" def generate_report(patient_name, analyzed_image_bytes, prediction, confidence): buffer = io.BytesIO() doc = SimpleDocTemplate(buffer, pagesize=letter) styles = getSampleStyleSheet() title_style = ParagraphStyle( name='TitleStyle', parent=styles['Normal'], fontSize=16, textColor=blue, alignment=1 # Center alignment ) heading_style = ParagraphStyle( name='HeadingStyle', parent=styles['Normal'], fontSize=12, textColor=red ) prediction_style = ParagraphStyle( name='PredictionStyle', parent=styles['Normal'], fontSize=14, alignment=1 ) story = [] # Hospital Name hospital_name = Paragraph("youesh hospital , mumbai ( west )", title_style) story.append(hospital_name) story.append(Spacer(1, 0.2*inch)) # Patient Greeting greeting = Paragraph(f"hello , {patient_name} thank you for using our services this is your radiology report", heading_style) story.append(greeting) story.append(Spacer(1, 0.2*inch)) # Horizontal Line story.append(Paragraph("
{str(e)}