SalmanAboAraj commited on
Commit
b508a71
·
verified ·
1 Parent(s): 49dbf35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -54
app.py CHANGED
@@ -1,54 +1,29 @@
1
- import platform
2
- import pathlib
3
-
4
- # معالجة مشكلة المسارات على الأنظمة المختلفة
5
- plt = platform.system()
6
- pathlib.WindowsPath = pathlib.PosixPath
7
-
8
- import gradio as gr
9
- from tensorflow.keras.models import load_model
10
- import numpy as np
11
- import cv2
12
- from huggingface_hub import hf_hub_download
13
- import base64
14
- from io import BytesIO
15
- from PIL import Image
16
-
17
- model_path = hf_hub_download(repo_id="SalmanAboAraj/Tooth1", filename="unet_model.h5")
18
- model = load_model(model_path)
19
-
20
- def decode_base64(image_str):
21
- image_data = base64.b64decode(image_str)
22
- image = Image.open(BytesIO(image_data))
23
- image = np.array(image)
24
- return image
25
-
26
- def encode_base64(image):
27
- pil_image = Image.fromarray(image)
28
- buffered = BytesIO()
29
- pil_image.save(buffered, format="PNG")
30
- return base64.b64encode(buffered.getvalue()).decode("utf-8")
31
-
32
- def predict(image_str):
33
- image = decode_base64(image_str)
34
- original_height, original_width, _ = image.shape
35
- image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
36
- image = cv2.resize(image, (128, 128))
37
- image = np.expand_dims(image, axis=0)
38
- image = np.expand_dims(image, axis=-1)
39
- image = image / 255.0
40
- mask = model.predict(image)
41
- mask = (mask[0] > 0.5).astype(np.uint8) * 255
42
- mask = cv2.resize(mask, (original_width, original_height))
43
- return encode_base64(mask)
44
-
45
- iface = gr.Interface(
46
- fn=predict,
47
- inputs=gr.Textbox(label="Input X-ray Image (base64)"),
48
- outputs=gr.Textbox(label="Annotation Mask (base64)"),
49
- title="Tooth Segmentation Model",
50
- description="Upload a dental X-ray image in base64 format to generate the annotation mask in base64 format.",
51
- )
52
-
53
- if __name__ == "__main__":
54
- interface.launch()
 
1
+ import gradio as gr
2
+ from tensorflow.keras.models
3
+ import load_model
4
+ import numpy as np
5
+ import cv2 from huggingface_hub
6
+ import hf_hub_download
7
+
8
+ model_path = hf_hub_download(repo_id="SalmanAboAraj/Tooth1", filename="unet_model.h5")
9
+ model = load_model(model_path)
10
+ def predict(image):
11
+ original_height, original_width, _ = image.shape image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
12
+ image = cv2.resize(image, (128, 128))
13
+ image = np.expand_dims(image, axis=0)
14
+ image = np.expand_dims(image, axis=-1)
15
+ image = image / 255.0
16
+ mask = model.predict(image)
17
+ mask = (mask[0] > 0.5).astype(np.uint8) * 255
18
+ mask = cv2.resize(mask, (original_width, original_height))
19
+ return mask
20
+
21
+ iface = gr.Interface(
22
+ fn=predict,
23
+ inputs=gr.Image(type="numpy", label="Input X-ray Image"),
24
+ outputs=gr.Image(type="numpy", label="Annotation Mask"),
25
+ title="Tooth Segmentation Model",
26
+ description="Upload a dental X-ray image to generate the annotation mask.", )
27
+
28
+ if __name__ == "__main__":
29
+ iface.launch()