Spaces:
No application file
No application file
File size: 6,794 Bytes
c433bd4 |
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 |
import os
import cv2
import numpy as np
import streamlit as st
from google.generativeai import configure, GenerativeModel
import base64
from datetime import datetime
# Page configuration
st.set_page_config(
page_title="Advanced Dental X-Ray Analysis System",
page_icon="🦷",
layout="wide"
)
# Custom CSS for better appearance
st.markdown("""
<style>
.main {
padding: 2rem;
}
.stButton>button {
width: 100%;
background-color: #0083B8;
color: white;
}
.disclaimer {
background-color: #FFF3CD;
padding: 1rem;
border-radius: 5px;
margin: 1rem 0;
}
</style>
""", unsafe_allow_html=True)
# Main title with professional styling
st.title("🦷 Advanced Dental X-Ray Analysis System")
st.markdown("---")
# Initialize session state
if 'patient_history' not in st.session_state:
st.session_state.patient_history = {}
# Sidebar for API configuration
with st.sidebar:
st.header("Configuration")
api_key = st.text_input("Your API Key", type="password")
if not api_key:
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
st.error("⚠️ Please provide an API key.")
st.stop()
st.markdown("---")
st.header("Patient Information")
# Patient history form
with st.form("patient_history"):
st.session_state.patient_history.update({
'name': st.text_input("Patient Name"),
'age': st.number_input("Age", 1, 120),
'gender': st.selectbox("Gender", ["Male", "Female", "Other"]),
'medical_history': st.multiselect("Medical History",
["Diabetes", "Hypertension", "Heart Disease", "None"]),
'dental_complaints': st.text_area("Current Dental Complaints"),
'previous_treatments': st.text_area("Previous Dental Treatments"),
'smoking': st.selectbox("Smoking Status", ["Non-smoker", "Former smoker", "Current smoker"]),
'last_dental_visit': st.date_input("Last Dental Visit")
})
submit_button = st.form_submit_button("Save Patient Information")
# Configure Gemini Pro
configure(api_key=api_key)
model = GenerativeModel("gemini-pro")
# Main content area
col1, col2 = st.columns([1, 1])
with col1:
st.header("X-Ray Upload & Processing")
uploaded_file = st.file_uploader("Upload a dental X-ray image", type=["jpg", "jpeg", "png"])
if uploaded_file:
# Display original image
st.image(uploaded_file, caption="Original X-ray", use_column_width=True)
# Image processing options
st.subheader("Image Enhancement Options")
denoise_strength = st.slider("Denoising Strength", 1, 20, 10)
contrast_limit = st.slider("Contrast Enhancement", 1.0, 5.0, 2.0)
# Process image
img = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), cv2.IMREAD_GRAYSCALE)
# Enhanced preprocessing
img = cv2.fastNlMeansDenoising(img, None, denoise_strength, 7, 21)
clahe = cv2.createCLAHE(clipLimit=contrast_limit, tileGridSize=(8, 8))
img = clahe.apply(img)
# Display enhanced image
st.image(img, caption="Enhanced X-ray", use_column_width=True)
# Encode image
_, img_encoded = cv2.imencode('.png', img)
base64_image = base64.b64encode(img_encoded).decode('utf-8')
with col2:
if uploaded_file and 'name' in st.session_state.patient_history and st.session_state.patient_history['name']:
st.header("Analysis & Results")
# Analysis options
analysis_type = st.multiselect("Select Analysis Focus Areas",
["Cavity Detection", "Bone Density", "Root Canal Assessment",
"Periodontal Status", "Wisdom Teeth", "Overall Assessment"])
if st.button("Generate Analysis"):
with st.spinner("Analyzing X-ray..."):
# Construct detailed prompt
prompt = f"""
Please analyze this dental X-ray image with the following context:
Patient Information:
- Name: {st.session_state.patient_history['name']}
- Age: {st.session_state.patient_history['age']}
- Gender: {st.session_state.patient_history['gender']}
- Medical History: {', '.join(st.session_state.patient_history['medical_history'])}
- Current Complaints: {st.session_state.patient_history['dental_complaints']}
- Previous Treatments: {st.session_state.patient_history['previous_treatments']}
- Smoking Status: {st.session_state.patient_history['smoking']}
Focus Areas: {', '.join(analysis_type)}
Please provide a detailed analysis including:
1. Identified abnormalities or concerns
2. Potential diagnosis considerations
3. Recommended additional examinations if needed
4. Treatment suggestions
5. Risk factors based on patient history
Format the response in a clear, structured manner.
"""
response = model.generate_content(prompt)
# Display results
st.markdown("### Analysis Results")
st.write(response.text)
# Generate report
st.markdown("### Report Generation")
if st.button("Generate PDF Report"):
st.info("Report generation functionality can be implemented here")
# Display disclaimer
st.markdown("""
<div class="disclaimer">
<h4>⚠️ Medical Disclaimer</h4>
<p>This analysis is generated by AI and is for informational purposes only.
It should not be considered as a definitive diagnosis. Please consult with a
qualified dental professional for accurate diagnosis and treatment planning.</p>
</div>
""", unsafe_allow_html=True)
else:
st.info("Please upload an X-ray image and complete patient information to proceed with analysis.")
# Footer
st.markdown("---")
st.markdown(
"""
<div style='text-align: center'>
<p>Advanced Dental X-Ray Analysis System v2.0</p>
<p>Developed by:</p>
<p>Supervised and reviewed by:</p>
<p>Participants:</p>
<p>UI Designer:</p>
</div>
""",
unsafe_allow_html=True
)
|