Spaces:
Sleeping
Sleeping
Samanta Das
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw
|
3 |
+
import sys
|
4 |
+
import os
|
5 |
+
import time
|
6 |
+
import threading
|
7 |
+
import logging
|
8 |
+
|
9 |
+
# Set up logging configuration
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
|
12 |
+
# Add the backend app to the system path for imports
|
13 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
|
14 |
+
|
15 |
+
# Import models
|
16 |
+
from yolo import FractureDetector
|
17 |
+
from llama import generate_response_based_on_yolo
|
18 |
+
|
19 |
+
# Define model path and output folder relative to the app's directory
|
20 |
+
MODEL_PATH = 'yolov8n_custom.pkl' # Update this to the relative path of your model on Hugging Face Spaces
|
21 |
+
OUTPUT_FOLDER = '/app/output_images' # Default output folder in Hugging Face Spaces
|
22 |
+
|
23 |
+
# Ensure the output folder exists
|
24 |
+
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
25 |
+
|
26 |
+
# Initialize the fracture detector
|
27 |
+
detector = FractureDetector(MODEL_PATH, OUTPUT_FOLDER)
|
28 |
+
|
29 |
+
def delete_file_after_delay(file_path, delay):
|
30 |
+
"""Delete the specified file after a given delay."""
|
31 |
+
def delete_file():
|
32 |
+
time.sleep(delay)
|
33 |
+
try:
|
34 |
+
os.remove(file_path)
|
35 |
+
logging.info(f"Temporary file {file_path} has been deleted.")
|
36 |
+
except Exception as e:
|
37 |
+
logging.error(f"Error deleting temporary file: {e}")
|
38 |
+
|
39 |
+
thread = threading.Thread(target=delete_file)
|
40 |
+
thread.start()
|
41 |
+
|
42 |
+
def mark_fracture_area(image, detections):
|
43 |
+
"""Draw bounding boxes on the image based on detected fractures."""
|
44 |
+
draw = ImageDraw.Draw(image)
|
45 |
+
for detection in detections:
|
46 |
+
x1, y1, x2, y2 = map(int, detection['coordinates'])
|
47 |
+
draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
|
48 |
+
return image
|
49 |
+
|
50 |
+
def analyze_image(input_image):
|
51 |
+
"""Analyze the uploaded image for fractures and generate a report."""
|
52 |
+
# Save the uploaded image to a temporary location
|
53 |
+
temp_image_path = os.path.join(OUTPUT_FOLDER, 'temp_uploaded_image.jpg')
|
54 |
+
input_image.save(temp_image_path)
|
55 |
+
|
56 |
+
try:
|
57 |
+
# Perform fracture detection
|
58 |
+
detections = detector.detect_fractures(temp_image_path, conf_threshold=0.25)
|
59 |
+
|
60 |
+
# Mark the fracture areas on the image
|
61 |
+
marked_image = mark_fracture_area(input_image.copy(), detections)
|
62 |
+
|
63 |
+
# Generate analysis report
|
64 |
+
analysis_report = generate_response_based_on_yolo(detections)
|
65 |
+
|
66 |
+
# Schedule deletion of the temporary file after 2 minutes
|
67 |
+
delete_file_after_delay(temp_image_path, 120)
|
68 |
+
|
69 |
+
return marked_image, analysis_report
|
70 |
+
|
71 |
+
except Exception as e:
|
72 |
+
logging.error(f"An error occurred during analysis: {str(e)}")
|
73 |
+
return input_image, f"An error occurred during analysis: {str(e)}"
|
74 |
+
|
75 |
+
# Define the Gradio interface
|
76 |
+
iface = gr.Interface(
|
77 |
+
fn=analyze_image,
|
78 |
+
inputs=gr.Image(type="pil", label="Upload X-ray Image"),
|
79 |
+
outputs=[
|
80 |
+
gr.Image(type="pil", label="Output Image with Marked Fractures"),
|
81 |
+
gr.Textbox(label="Fracture Analysis Report")
|
82 |
+
],
|
83 |
+
title="Fracture Detection and Analysis System",
|
84 |
+
description="Upload an X-ray image to detect fractures, view marked areas, and receive a detailed analysis report."
|
85 |
+
)
|
86 |
+
|
87 |
+
# Launch the Gradio interface
|
88 |
+
if __name__ == "__main__":
|
89 |
+
iface.launch()
|