ahmadalfian commited on
Commit
24f9cb0
1 Parent(s): 13a5c27

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ from PIL import Image, ImageDraw
4
+ from ultralytics import YOLO
5
+
6
+ # Muat model YOLO
7
+ model = YOLO('best.pt') # Pastikan path ini benar
8
+
9
+ # Fungsi untuk melakukan deteksi jerawat
10
+ def detect_acne(image):
11
+ img_np = np.array(image)
12
+
13
+ # Deteksi jerawat menggunakan YOLO
14
+ results = model.predict(source=img_np, imgsz=640)
15
+
16
+ # Gambar hasil deteksi dengan menggunakan Pillow
17
+ draw = ImageDraw.Draw(image)
18
+
19
+ for result in results:
20
+ boxes = result.boxes
21
+ if boxes is not None and len(boxes.xyxy) > 0:
22
+ for i, box in enumerate(boxes.xyxy):
23
+ x1, y1, x2, y2 = box[:4]
24
+ conf = boxes.conf[i] if boxes.conf is not None and len(boxes.conf) > i else None
25
+
26
+ # Menggambar kotak deteksi di gambar asli
27
+ draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
28
+
29
+ if conf is not None:
30
+ # Menambahkan label dengan confidence score
31
+ draw.text((x1, y1), f'{conf:.2f}', fill="red")
32
+
33
+ return image
34
+
35
+ # Judul aplikasi
36
+ st.title('Deteksi Jerawat Menggunakan YOLOv8')
37
+
38
+ # Pilihan untuk mengunggah gambar
39
+ uploaded_file = st.file_uploader("Unggah Gambar", type=["jpg", "jpeg", "png"])
40
+
41
+ # Tampilkan gambar asli terlebih dahulu
42
+ if uploaded_file is not None:
43
+ # Membaca gambar
44
+ image = Image.open(uploaded_file)
45
+
46
+ # Tampilkan gambar asli
47
+ st.image(image, caption='Gambar yang Diupload', use_column_width=True)
48
+
49
+ # Tombol untuk melakukan deteksi jerawat
50
+ if st.button('Deteksi Jerawat'):
51
+ # Deteksi jerawat dalam gambar
52
+ detected_image = detect_acne(image)
53
+
54
+ # Tampilkan gambar hasil deteksi
55
+ st.image(detected_image, caption='Hasil Deteksi Jerawat', use_column_width=True)