Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
|
6 |
+
|
7 |
+
model = load_model('/content/drive/MyDrive/AI SDG 3/klasifikasi_jerawat_model_klmpk2.keras')
|
8 |
+
|
9 |
+
acne_labels = {
|
10 |
+
0: 'papules',
|
11 |
+
1: 'nodules',
|
12 |
+
2: 'pustules',
|
13 |
+
3: 'comedones'
|
14 |
+
}
|
15 |
+
|
16 |
+
def detect_acne(image, threshold=0.5):
|
17 |
+
image_resized = cv2.resize(image, (224, 224))
|
18 |
+
input_data = preprocess_input(np.expand_dims(image_resized, axis=0))
|
19 |
+
|
20 |
+
predictions = model.predict(input_data)
|
21 |
+
|
22 |
+
detections = []
|
23 |
+
for i, prediction in enumerate(predictions[0]):
|
24 |
+
if prediction > threshold:
|
25 |
+
detections.append({
|
26 |
+
'class': acne_labels[i],
|
27 |
+
'confidence': float(prediction)
|
28 |
+
})
|
29 |
+
|
30 |
+
annotated_image = image.copy()
|
31 |
+
if detections:
|
32 |
+
for detection in detections:
|
33 |
+
height, width, _ = image.shape
|
34 |
+
xmin, ymin = int(width * 0.25), int(height * 0.25)
|
35 |
+
xmax, ymax = int(width * 0.75), int(height * 0.75)
|
36 |
+
|
37 |
+
color = (203, 0, 203) # Purple-red
|
38 |
+
cv2.rectangle(annotated_image, (xmin, ymin), (xmax, ymax), color, 2)
|
39 |
+
|
40 |
+
return annotated_image, f"Detected acne: {detections}"
|
41 |
+
else:
|
42 |
+
return annotated_image, "No acne detected. Congrats!"
|
43 |
+
|
44 |
+
interface = gr.Interface(
|
45 |
+
fn=detect_acne,
|
46 |
+
inputs=gr.Image(type="numpy", label="Upload an image"),
|
47 |
+
outputs=[
|
48 |
+
gr.Image(type="numpy", label="Annotated Image"),
|
49 |
+
gr.Textbox(label="Detection Result")
|
50 |
+
]
|
51 |
+
)
|
52 |
+
|
53 |
+
interface.launch()
|