Spaces:
Sleeping
Sleeping
dangtiendatdat
commited on
Update MeAI_Maincode/Detectskindisease.py
Browse files
MeAI_Maincode/Detectskindisease.py
CHANGED
@@ -79,52 +79,117 @@ trans_body = {
|
|
79 |
"chin": "cằm",
|
80 |
"unknown" : "bộ phận chưa rõ"
|
81 |
}
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
try:
|
84 |
-
|
85 |
# Convert NumPy array to image file-like object
|
86 |
img = Image.fromarray((image).astype('uint8'))
|
87 |
img_byte_array = io.BytesIO()
|
88 |
img.save(img_byte_array, format='PNG')
|
89 |
img_byte_array.seek(0)
|
90 |
|
91 |
-
|
92 |
-
|
93 |
files = {"image": ("image.png", img_byte_array, "image/png")}
|
94 |
headers = {
|
95 |
"X-RapidAPI-Key": key,
|
96 |
-
"X-RapidAPI-Host": "detect-skin-
|
97 |
}
|
98 |
response = requests.post(url, files=files, headers=headers)
|
|
|
|
|
|
|
99 |
response_json = response.json()
|
100 |
-
|
101 |
output = ""
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
113 |
|
114 |
-
|
115 |
-
|
116 |
|
117 |
-
|
118 |
probability_percent = probability * 100
|
119 |
vnese_disease = trans_disease.get(disease, disease)
|
|
|
|
|
120 |
if probability_percent >= 10:
|
121 |
-
|
122 |
|
123 |
-
|
124 |
-
else:
|
125 |
-
return "Không có dữ liệu phản hồi từ API."
|
126 |
except Exception as e:
|
127 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
def create_skin_tab(skinkey="b71d1f41camsh51cbf0a2df5facap1b9e25jsn2ce94a61f252"):
|
130 |
css = """
|
|
|
79 |
"chin": "cằm",
|
80 |
"unknown" : "bộ phận chưa rõ"
|
81 |
}
|
82 |
+
trans_description = {
|
83 |
+
"acne": "Tình trạng da phổ biến với mụn trứng cá",
|
84 |
+
"pityriasis_rosea": "Phát ban thường bắt đầu bằng một đốm lớn",
|
85 |
+
"actinic_keratosis": "Các mảng da thô ráp, có vảy do tổn thương từ ánh nắng",
|
86 |
+
"prurigo_nodularis": "Các cục cứng và ngứa trên da",
|
87 |
+
"alopecia_androgenetica": "Rụng tóc theo kiểu mẫu",
|
88 |
+
"alopecia_areata": "Rụng tóc từng mảng",
|
89 |
+
"bullous_dermatosis": "Tình trạng da bị phồng rộp",
|
90 |
+
"chloasma": "Các mảng nâu trên da",
|
91 |
+
"corn": "Vùng da bị dày lên",
|
92 |
+
"dermatofibroma": "Sự phát triển da lành tính phổ biến",
|
93 |
+
"eczema_dermatitis": "Da bị viêm và ngứa",
|
94 |
+
"erysipelas": "Nhiễm trùng da do vi khuẩn",
|
95 |
+
"erythema_multiforme": "Phản ứng trên da",
|
96 |
+
"folliculitis": "Viêm nang lông",
|
97 |
+
"furuncle": "Nhiễm trùng sâu trong nang lông",
|
98 |
+
"haemangioma": "Sự phát triển lành tính của mạch máu",
|
99 |
+
"herpes": "Bệnh zona thần kinh",
|
100 |
+
"herpes_simplex": "Mụn rộp môi",
|
101 |
+
"iga_vasculitis": "Viêm mạch máu",
|
102 |
+
"keloid": "Mô sẹo phát triển quá mức",
|
103 |
+
"keratosis_follicularism": "Các mảng thô ráp và mụn nhỏ",
|
104 |
+
"lichen_planus": "Tình trạng viêm",
|
105 |
+
"lupus_erythematosus": "Bệnh tự miễn",
|
106 |
+
"molluscum_contagiosum": "Nhiễm trùng da do virus",
|
107 |
+
"nevus": "Nốt ruồi",
|
108 |
+
"paronychia": "Nhiễm trùng móng tay/móng chân",
|
109 |
+
"pityriasis_alba": "Vùng da sáng màu",
|
110 |
+
"psoriasis": "Tình trạng da mãn tính",
|
111 |
+
"rosacea": "Mặt đỏ và có mụn nhỏ",
|
112 |
+
"sebaceous_cyst": "U nang không phải ung thư",
|
113 |
+
"sebaceousnevus": "Dấu vết bẩm sinh",
|
114 |
+
"seborrheic_dermatitis": "Da đỏ và có vảy",
|
115 |
+
"seborrheic_keratosis": "Sự phát triển màu nâu và có vảy",
|
116 |
+
"skin_tag": "Sự phát triển nhỏ, mềm trên da",
|
117 |
+
"stasis_dermatitis": "Viêm da",
|
118 |
+
"syringoma": "Các nốt nhỏ trên da",
|
119 |
+
"tinea_capitis": "Nấm da đầu",
|
120 |
+
"tinea_corporis": "Nấm trên cơ thể",
|
121 |
+
"tinea_cruris": "Nấm bẹn",
|
122 |
+
"tinea_manuum": "Nấm tay",
|
123 |
+
"tinea_pedis": "Nấm chân",
|
124 |
+
"tinea_unguium": "Nấm móng tay/móng chân",
|
125 |
+
"tinea_versicolor": "Nhiễm trùng do nấm",
|
126 |
+
"urticaria": "Phát ban mề đay",
|
127 |
+
"urticaria_papular": "Nốt ngứa",
|
128 |
+
"varicella": "Bệnh thủy đậu",
|
129 |
+
"verruca_plana": "Mụn cóc phẳng",
|
130 |
+
"verruca_vulgaris": "Mụn cóc thông thường",
|
131 |
+
"vitiligo": "Mất màu da"
|
132 |
+
}
|
133 |
+
def detect_skin_disease(image, key):
|
134 |
try:
|
|
|
135 |
# Convert NumPy array to image file-like object
|
136 |
img = Image.fromarray((image).astype('uint8'))
|
137 |
img_byte_array = io.BytesIO()
|
138 |
img.save(img_byte_array, format='PNG')
|
139 |
img_byte_array.seek(0)
|
140 |
|
141 |
+
|
142 |
+
url = "https://detect-skin-disease1.p.rapidapi.com/skin-disease"
|
143 |
files = {"image": ("image.png", img_byte_array, "image/png")}
|
144 |
headers = {
|
145 |
"X-RapidAPI-Key": key,
|
146 |
+
"X-RapidAPI-Host": "detect-skin-disease1.p.rapidapi.com"
|
147 |
}
|
148 |
response = requests.post(url, files=files, headers=headers)
|
149 |
+
|
150 |
+
if response.status_code != 200:
|
151 |
+
return f"Lỗi API: HTTP {response.status_code} - {response.reason}"
|
152 |
response_json = response.json()
|
153 |
+
|
154 |
output = ""
|
155 |
|
156 |
+
# Get image type
|
157 |
+
image_type = response_json.get('imageType')
|
158 |
+
if image_type=="normal_skin":
|
159 |
+
return "Da của bạn bình thường\n"
|
160 |
+
if image_type=="non_skin":
|
161 |
+
return "Ảnh của bạn không phải da\n"
|
162 |
|
163 |
+
# Get body part
|
164 |
+
body_part = response_json.get('bodyPart')
|
165 |
+
if body_part is not None:
|
166 |
+
vnese_body = trans_body.get(body_part, body_part)
|
167 |
+
output += f"Phần của cơ thể: {vnese_body}\n"
|
168 |
+
|
169 |
+
# Get results and probabilities
|
170 |
+
results = response_json.get('results')
|
171 |
+
if results is not None:
|
172 |
+
output += "Kết quả phân tích:\n"
|
173 |
|
174 |
+
# Sort results by probability in descending order
|
175 |
+
sorted_results = sorted(results.items(), key=lambda x: x[1], reverse=True)
|
176 |
|
177 |
+
for disease, probability in sorted_results:
|
178 |
probability_percent = probability * 100
|
179 |
vnese_disease = trans_disease.get(disease, disease)
|
180 |
+
description = trans_description.get(disease, "Không có mô tả")
|
181 |
+
|
182 |
if probability_percent >= 10:
|
183 |
+
output += f"- {vnese_disease} {probability_percent:.2f}%, mô tả thêm về bệnh: {description} \n"
|
184 |
|
185 |
+
return output
|
|
|
|
|
186 |
except Exception as e:
|
187 |
+
return f"Lỗi: {str(e)}"
|
188 |
+
|
189 |
+
|
190 |
+
|
191 |
+
|
192 |
+
|
193 |
|
194 |
def create_skin_tab(skinkey="b71d1f41camsh51cbf0a2df5facap1b9e25jsn2ce94a61f252"):
|
195 |
css = """
|