File size: 2,690 Bytes
afc5fe7
 
1f93d26
 
 
 
 
 
 
afc5fe7
 
b508a71
1f93d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cedee1f
1f93d26
cedee1f
 
 
1f93d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f63cd7c
1f93d26
a47c5f2
 
 
 
 
 
 
 
 
 
f63cd7c
1f93d26
 
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
import platform
import pathlib
import gradio as gr
import numpy as np
import cv2
from tensorflow.keras.models import load_model
from huggingface_hub import hf_hub_download

# إصلاح مشكلة المسارات في Windows
plt = platform.system()
pathlib.WindowsPath = pathlib.PosixPath

# تحميل النموذج من Hugging Face
model_path = hf_hub_download(repo_id="SalmanAboAraj/Tooth1", filename="unet_model_256.h5")
model = load_model(model_path)

# تعريف ألوان الفئات (من بياناتك)
COLOR_MAP = {
    0: (0, 0, 0),        # Background
    1: (215, 179, 255),  # Bone
    2: (246, 51, 81),    # Cavity
    3: (58, 132, 255),   # Crown
    4: (134, 202, 218),  # Dental Implant
    5: (221, 195, 130),  # Dental Implant Crown
    6: (255, 255, 127),  # Dentin
    7: (255, 255, 255),  # Enamel
    8: (1, 13, 27),      # Filling Metal
    9: (0, 133, 255),    # Filling Non-Metal
    10: (24, 250, 143),  # Periapical Radiolucence
    11: (255, 105, 248), # Pulp
    12: (17, 253, 231),  # Root Canal
    13: (255, 146, 119), # Sinus
    14: (131, 224, 112)  # Missing
}

# وظيفة التنبؤ
def predict(image):
    original_height, original_width, _ = image.shape

    # تحويل الصورة إلى حجم يناسب النموذج
    image_resized = cv2.resize(image, (256, 256))
    image_resized = image_resized / 255.0
    image_resized = np.expand_dims(image_resized, axis=0)

    # التنبؤ بالقناع
    mask_pred = model.predict(image_resized)[0]  # الإخراج يكون (256, 256, 15)

    # تحويل القناع إلى صورة بألوان الفئات
    mask_class = np.argmax(mask_pred, axis=-1)  # إخراج التصنيفات (256, 256)
    mask_colored = np.zeros((256, 256, 3), dtype=np.uint8)

    for class_idx, color in COLOR_MAP.items():
        mask_colored[mask_class == class_idx] = color

    # إرجاع القناع بنفس أبعاد الصورة الأصلية
    mask_final = cv2.resize(mask_colored, (original_width, original_height), interpolation=cv2.INTER_NEAREST)
    return mask_final

# إنشاء واجهة Gradio
with gr.Blocks() as iface: 
    gr.Markdown("# Tooth Segmentation Model") 
    gr.Markdown("Upload a dental X-ray image to generate the annotation mask.") 
    with gr.Row(): 
        with gr.Column(): 
            input_image = gr.Image(label="Input X-ray Image", type="numpy") 
            submit_button = gr.Button("Predict") 
        with gr.Column(): 
            output_image = gr.Image(label="Annotation Mask", type="numpy") 
            
    submit_button.click(fn=predict, inputs=input_image, outputs=output_image)
if __name__ == "__main__":
    iface.launch()