import os
import subprocess
import sys
# import tensorflow as tf
import numpy as np
from PIL import Image
import cv2
import gradio as gr
# from numpy import asarray
from transformers import pipeline
from tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, BatchNormalization, Dropout,AveragePooling2D
import tensorflow as tf
from tensorflow.keras.applications import DenseNet201
from keras.models import Model
from keras.models import Sequential
from keras.regularizers import *
from tensorflow import keras
from tensorflow.keras import layers
import tensorflow as tf
import matplotlib.pyplot as plt
from PIL import Image
import cv2
from transformers import pipeline
from weasyprint import HTML
from pathlib import Path
# تابع پیشبینی
def predict_demo(image, model_name):
if model_name == "how dense is":
image = np.asarray(image)
# مدل اول
def load_model():
model = tf.keras.models.load_model("model.h5", compile=False)
model.compile(optimizer=tf.keras.optimizers.legacy.Adam(learning_rate=0.00001, decay=0.0001),
metrics=["accuracy"], loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1))
model.load_weights("modeldense1.h5")
return model
model = load_model()
def preprocess(image):
image = cv2.resize(image, (224, 224))
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
im = cv2.filter2D(image, -1, kernel)
if im.ndim == 3:
# اضافه کردن بعد جدید برای ورودی مدل
im = np.expand_dims(im, axis=0)
elif im.ndim == 2:
# اگر تصویر سیاه و سفید باشد
im = np.expand_dims(im, axis=-1)
im = np.repeat(im, 3, axis=-1)
im = np.expand_dims(im, axis=0)
return im
class_name = ['Benign with Density=1', 'Malignant with Density=1', 'Benign with Density=2',
'Malignant with Density=2', 'Benign with Density=3', 'Malignant with Density=3',
'Benign with Density=4', 'Malignant with Density=4']
def predict_img(img):
img = preprocess(img)
img = img / 255.0
pred = model.predict(img)[0]
return {class_name[i]: float(pred[i]) for i in range(8)}
predict_mamo= predict_img(image)
return predict_mamo
elif model_name == "what kind is":
image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(image)
pipe = pipeline("image-classification", model="DHEIVER/finetuned-BreastCancer-Classification")
def predict(image):
result = pipe(image)
return {result[i]['label']: float(result[i]['score']) for i in range(2)}
return predict(im_pil)
def generate_fixed_size_chart(predictions, image_file, chart_width=6, chart_height=5):
# بارگذاری تصویر ماموگرافی
mammo_image = plt.imread(image_file)
# تعداد مدلها
num_models = len(predictions)
# ایجاد figure با تنظیم عرض و ارتفاع هر زیرنمودار
fig, axes = plt.subplots(1, num_models + 1, figsize=(chart_width * (num_models + 1), chart_height), constrained_layout=True)
# fig.subplots_adjust(wspace=0.7) # فاصله ثابت بین نمودارها
# نمایش تصویر ماموگرافی در subplot اول
axes[0].imshow(mammo_image, cmap='gray')
axes[0].axis('off')
axes[0].set_title("Mammogram")
# ایجاد نمودارهای پیشبینی برای هر مدل در subplots بعدی
for i, (model_name, prediction) in enumerate(predictions.items(), start=1):
labels, values = zip(*prediction.items())
axes[i].barh(labels, values, color='skyblue')
axes[i].set_xlabel('Probability (%)')
axes[i].set_title(f'{model_name}')
# ذخیرهی نمودار در فایل
chart_path = f"{os.getcwd()}/{os.path.basename(image_file)}_combined_chart.png"
plt.savefig(chart_path, bbox_inches='tight')
plt.close(fig)
return chart_path
# استفاده از pdfkit برای تولید PDF
def generate_pdf(patient_info, predictions):
all_charts = []
for image_file, prediction in predictions:
chart = generate_fixed_size_chart(prediction, image_file)
all_charts.append(chart)
# تبدیل مسیر نسبی به مسیر مطلق
image_path = str(Path(patient_info.get('ImagePath', '')).resolve())
# تولید محتوای HTML برای PDF
html_content = f"""
Patient Report
Patient Image:
Patient Information:
{"".join(f"
{key}: {value if value else '-'}
" for key, value in patient_info.items() if key != "ImagePath")}
Predictions:
{"".join(f"" for chart in all_charts)}
"""
# # تنظیمات PDF
pdf_path = "patient_report.pdf"
html = HTML(string= html_content, base_url="/app")
html.write_pdf(pdf_path)
return pdf_path
# تابع نمایش گزارش و تولید PDF
def display_report(patient_info, predictions):
pdf_path = generate_pdf(patient_info, predictions)
report_content = f"Patient Report
{patient_info}
Predictions
{predictions}"
return report_content, pdf_path
# پردازش تصاویر ماموگرافی با مدلهای انتخابی
def process_images(patient_info, selected_models, images):
all_predictions = []
total_images = len(images)
# حلقه برای پردازش تصاویر
for idx, image_file in enumerate(images):
# بهروزرسانی وضعیت پردازش
status = f"Processing Image {idx + 1} of {total_images}..."
yield all_predictions, status # خروجی موقت
image = Image.open(image_file)
image_predictions = {model: predict_demo(image, model) for model in selected_models}
all_predictions.append((image_file, image_predictions))
# پس از اتمام پردازش
final_status = "Processing Complete!"
yield all_predictions, final_status # خروجی نهایی
# رابط Gradio
with gr.Blocks() as demo:
gr.Markdown("## Breast Cancer Detection - Multi-Model Interface")
# صفحه اول - اطلاعات بیمار
with gr.Tab("Patient Info"):
patient_image = gr.Image(label="Upload Patient Profile Image", type="pil")
name = gr.Textbox(label="Name")
height = gr.Number(label="Height (cm)")
weight = gr.Number(label="Weight (kg)")
age = gr.Number(label="Age")
gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
residence = gr.Textbox(label="Residence")
birth_place = gr.Textbox(label="Birth Place")
occupation = gr.Textbox(label="Occupation")
medical_history = gr.Textbox(label="Medical History")
patient_info = gr.State()
snackbar = gr.HTML(" Patient information was saved
", visible=False) # پیام snackbar
patient_info_submit = gr.Button("Save")
# صفحه دوم - انتخاب مدلها و آپلود تصاویر ماموگرافی
with gr.Tab("Model & Image Selection"):
model_choice = gr.CheckboxGroup(["how dense is", "what kind is"], label="Select Model(s)", interactive=True)
mammography_images = gr.File(label="Upload Mammography Image(s)", file_count="multiple", type="filepath")
predictions = gr.State()
process_button = gr.Button("Process Images")
# المان برای نمایش وضعیت پردازش
status_box = gr.Textbox(label="Processing Status", interactive=False)
# صفحه سوم - نمایش اطلاعات و پیشبینی
with gr.Tab("Results"):
report_display = gr.HTML(label="Patient Report")
download_button = gr.Button("Download Report")
# جمعآوری اطلاعات بیمار و انتقال به مرحله بعدی
def collect_patient_info(image, name, height, weight, age, gender, residence, birth_place, occupation, medical_history):
# ذخیره تصویر بیمار و اضافه کردن مسیر به اطلاعات بیمار
image_path = "patient_image.jpg"
image.save(image_path)
return {
"Name": name,
"Gender": gender,
"Height": height,
"Weight": weight,
"Age": age,
"Residence": residence,
"Birth Place": birth_place,
"Occupation": occupation,
"Medical History": medical_history,
"ImagePath": image_path # اضافه کردن مسیر تصویر
}, gr.update(visible=True) # نمایش snackbar و مخفی شدن آن پس از 3 ثانیه
patient_info_submit.click(
collect_patient_info,
inputs=[patient_image, name, height, weight, age, gender, residence, birth_place, occupation, medical_history],
outputs=[patient_info, snackbar] # نمایش اطلاعات و snackbar
)
process_button.click(
process_images,
inputs=[patient_info, model_choice, mammography_images],
outputs=[predictions, status_box] # دو خروجی تنظیم شده است
)
# نمایش گزارش بیمار و پیشبینیها در صفحه سوم
download_button.click(
display_report,
inputs=[patient_info, predictions],
outputs=[report_display, gr.File(label="Download PDF Report")] # اصلاح خروجی برای Gradio
)
demo.launch(debug=True)