dangtiendatdat commited on
Commit
7a5124e
·
verified ·
1 Parent(s): 84ffeab

Update MeAI_Maincode/Detectskindisease.py

Browse files
Files changed (1) hide show
  1. MeAI_Maincode/Detectskindisease.py +39 -34
MeAI_Maincode/Detectskindisease.py CHANGED
@@ -4,7 +4,7 @@ from PIL import Image
4
  import io
5
  import numpy as np
6
  import os
7
- key = "a8108953dbmsh2baabfa3db731fdp14177bjsnfebbe465261d"
8
  trans_disease = {
9
  "acne": "mụn",
10
  "actinic_keratosis": "chứng dày sừng quang hóa",
@@ -133,63 +133,68 @@ trans_description = {
133
  def detect_skin_disease(image, key):
134
  try:
135
 
136
- img = Image.fromarray(image.astype('uint8')) # Tạo ảnh từ numpy array
137
- img_byte_array = io.BytesIO() # Tạo buffer giống tệp
138
- img.save(img_byte_array, format='PNG') # Lưu ảnh dưới dạng PNG
139
- img_byte_array.seek(0) # Quay về đầu buffer
 
140
 
141
- url = "https://detect-skin-disease1.p.rapidapi.com/skin-disease"
 
142
  files = {"image": ("image.png", img_byte_array, "image/png")}
143
  headers = {
144
  "X-RapidAPI-Key": key,
145
- "X-RapidAPI-Host": "detect-skin-disease1.p.rapidapi.com"
146
  }
147
  response = requests.post(url, files=files, headers=headers)
148
 
149
- if response.status_code != 200:
150
- return f"Lỗi API: HTTP {response.status_code} - {response.reason}"
151
- response_json = response.json()
152
 
 
 
 
153
  output = ""
154
 
155
- # Get image type
156
- image_type = response_json.get('imageType')
157
- if image_type=="normal_skin":
158
- return "Da của bạn bình thường\n"
159
- if image_type=="non_skin":
160
- return "Ảnh của bạn không phải da\n"
 
 
 
 
 
 
 
 
161
 
162
- # Get body part
163
- body_part = response_json.get('bodyPart')
164
- if body_part is not None:
165
- vnese_body = trans_body.get(body_part, body_part)
166
- output += f"Phần của cơ thể: {vnese_body}\n"
167
 
168
- # Get results and probabilities
169
- results = response_json.get('results')
170
- if results is not None:
171
- output += "Kết quả phân tích:\n"
172
 
173
- # Sort results by probability in descending order
174
- sorted_results = sorted(results.items(), key=lambda x: x[1], reverse=True)
175
 
176
- for disease, probability in sorted_results:
 
 
 
177
  probability_percent = probability * 100
178
  vnese_disease = trans_disease.get(disease, disease)
179
- description = trans_description.get(disease, "Không có mô tả")
180
-
181
  if probability_percent >= 10:
182
- output += f"- {vnese_disease} {probability_percent:.2f}%, mô tả thêm về bệnh: {description} \n"
183
 
184
- return output
 
 
185
  except Exception as e:
186
- return f"Lỗi: {str(e)}"
 
187
 
188
 
189
 
190
 
191
 
192
- def create_skin_tab(skinkey="a8108953dbmsh2baabfa3db731fdp14177bjsnfebbe465261d"):
193
  css = """
194
  .textboxskin {
195
  font-sxxxxize: 50px; !important;
 
4
  import io
5
  import numpy as np
6
  import os
7
+ key = "b71d1f41camsh51cbf0a2df5facap1b9e25jsn2ce94a61f252"
8
  trans_disease = {
9
  "acne": "mụn",
10
  "actinic_keratosis": "chứng dày sừng quang hóa",
 
133
  def detect_skin_disease(image, key):
134
  try:
135
 
136
+ # Convert NumPy array to image file-like object
137
+ img = Image.fromarray((image).astype('uint8'))
138
+ img_byte_array = io.BytesIO()
139
+ img.save(img_byte_array, format='PNG')
140
+ img_byte_array.seek(0)
141
 
142
+ url = "https://detect-skin-disease.p.rapidapi.com/facebody/analysis/detect-skin-disease"
143
+ # files = {"image": img_byte_array}
144
  files = {"image": ("image.png", img_byte_array, "image/png")}
145
  headers = {
146
  "X-RapidAPI-Key": key,
147
+ "X-RapidAPI-Host": "detect-skin-disease.p.rapidapi.com"
148
  }
149
  response = requests.post(url, files=files, headers=headers)
150
 
 
 
 
151
 
152
+
153
+ response_json = response.json()
154
+
155
  output = ""
156
 
157
+ if 'data' in response_json:
158
+ body_part = response_json['data'].get('body_part')
159
+ results = response_json['data'].get('results_english')
160
+
161
+
162
+
163
+
164
+ if body_part is not None:
165
+ vnese_body = trans_body[body_part]
166
+ output += f"Phần của cơ thể: {vnese_body} ({body_part})\n"
167
+
168
+ if results is not None:
169
+ output += " Kết quả phân tích: "
170
+
171
 
 
 
 
 
 
172
 
 
 
 
 
173
 
 
 
174
 
175
+ # Sort the results by probability percentage in descending order
176
+ sorted_results = sorted(results.items(), key=lambda x: x[1], reverse=True)
177
+
178
+ for disease, probability in sorted_results:
179
  probability_percent = probability * 100
180
  vnese_disease = trans_disease.get(disease, disease)
181
+
182
+
183
  if probability_percent >= 10:
184
+ output += f"{vnese_disease} : {probability_percent:.2f}%\n"
185
 
186
+ return output
187
+ else:
188
+ return "Không có dữ liệu phản hồi từ API."
189
  except Exception as e:
190
+ return f"Error: {str(e)}"
191
+
192
 
193
 
194
 
195
 
196
 
197
+ def create_skin_tab(skinkey="b71d1f41camsh51cbf0a2df5facap1b9e25jsn2ce94a61f252"):
198
  css = """
199
  .textboxskin {
200
  font-sxxxxize: 50px; !important;