File size: 3,193 Bytes
e1dbe51
 
bc62e6b
67ea11c
e1dbe51
 
 
 
 
 
 
 
 
 
 
 
a07e44f
67ea11c
e1dbe51
 
 
 
67ea11c
 
e1dbe51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67ea11c
 
 
e1dbe51
 
 
 
67ea11c
 
e1dbe51
67ea11c
 
e1dbe51
67ea11c
 
e1dbe51
67ea11c
 
e1dbe51
67ea11c
 
e1dbe51
67ea11c
e1dbe51
 
67ea11c
e1dbe51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from PIL import Image, ImageDraw
import sys
import os
import time
import threading
import logging

# Set up logging configuration
logging.basicConfig(level=logging.INFO)

# Import models
from yolo import FractureDetector
from llama import generate_response_based_on_yolo

# Define model path and output folder relative to the app's directory
MODEL_PATH = 'yolov8n_custom_exported.pt'  # Update this to the relative path of your model on Hugging Face Spaces
OUTPUT_FOLDER = 'output_images'  # Use a relative path to avoid permission issues

# Ensure the output folder exists
os.makedirs(OUTPUT_FOLDER, exist_ok=True)

# Initialize the fracture detector with both parameters
detector = FractureDetector(MODEL_PATH, OUTPUT_FOLDER)

def delete_file_after_delay(file_path, delay):
    """Delete the specified file after a given delay."""
    def delete_file():
        time.sleep(delay)
        try:
            os.remove(file_path)
            logging.info(f"Temporary file {file_path} has been deleted.")
        except Exception as e:
            logging.error(f"Error deleting temporary file: {e}")

    thread = threading.Thread(target=delete_file)
    thread.start()

def mark_fracture_area(image, detections):
    """Draw bounding boxes on the image based on detected fractures."""
    draw = ImageDraw.Draw(image)
    for detection in detections:
        x1, y1, x2, y2 = map(int, detection['coordinates'])
        draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
    return image

def analyze_image(input_image):
    """Analyze the uploaded image for fractures and generate a report."""
    logging.info("Starting analysis on uploaded image.")

    # Save the uploaded image to a temporary location
    temp_image_path = os.path.join(OUTPUT_FOLDER, 'temp_uploaded_image.jpg')
    input_image.save(temp_image_path)

    try:
        logging.info("Performing fracture detection.")
        # Perform fracture detection
        detections = detector.detect_fractures(temp_image_path, conf_threshold=0.25)

        # Mark the fracture areas on the image
        marked_image = mark_fracture_area(input_image.copy(), detections)

        # Generate analysis report
        analysis_report = generate_response_based_on_yolo(detections)

        # Schedule deletion of the temporary file after 2 minutes
        delete_file_after_delay(temp_image_path, 120)

        logging.info("Analysis completed successfully.")
        return marked_image, analysis_report

    except Exception as e:
        logging.error(f"An error occurred during analysis: {str(e)}")
        return input_image, f"An error occurred during analysis: {str(e)}"

# Define the Gradio interface
iface = gr.Interface(
    fn=analyze_image,
    inputs=gr.Image(type="pil", label="Upload X-ray Image"),
    outputs=[
        gr.Image(type="pil", label="Output Image with Marked Fractures"),
        gr.Textbox(label="Fracture Analysis Report")
    ],
    title="Fracture Detection and Analysis System",
    description="Upload an X-ray image to detect fractures, view marked areas, and receive a detailed analysis report."
)

# Launch the Gradio interface
if __name__ == "__main__":
    iface.launch()