Upload 3 files
Browse files- app.py +85 -0
- best.pt +3 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from ultralytics import YOLO
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
+
import io
|
5 |
+
|
6 |
+
# Tải mô hình YOLOv8
|
7 |
+
@st.cache_resource
|
8 |
+
def load_model():
|
9 |
+
return YOLO("best.pt") # Đảm bảo bạn đã có file best.pt trong thư mục làm việc
|
10 |
+
|
11 |
+
model = load_model()
|
12 |
+
|
13 |
+
# Tiêu đề của ứng dụng
|
14 |
+
st.title("Ứng Dụng Phát Hiện Sâu Răng với YOLOv8")
|
15 |
+
|
16 |
+
# Mô tả ngắn
|
17 |
+
st.write("""
|
18 |
+
Tải lên hình ảnh hoặc chụp hình bằng camera để hệ thống tự động phát hiện sâu răng và hiển thị các bounding boxes.
|
19 |
+
""")
|
20 |
+
|
21 |
+
# Tùy chọn tải lên hoặc chụp hình
|
22 |
+
option = st.radio("Chọn phương thức nhập hình ảnh:", ("Tải lên từ máy tính", "Chụp hình bằng camera"))
|
23 |
+
|
24 |
+
uploaded_file = None
|
25 |
+
|
26 |
+
if option == "Tải lên từ máy tính":
|
27 |
+
uploaded_file = st.file_uploader("Chọn hình ảnh", type=["jpg", "jpeg", "png"])
|
28 |
+
elif option == "Chụp hình bằng camera":
|
29 |
+
camera_file = st.camera_input("Chụp hình bằng camera")
|
30 |
+
if camera_file:
|
31 |
+
uploaded_file = camera_file
|
32 |
+
|
33 |
+
if uploaded_file is not None:
|
34 |
+
# Hiển thị hình ảnh gốc
|
35 |
+
image = Image.open(uploaded_file).convert("RGB")
|
36 |
+
st.image(image, caption='Hình ảnh đã tải lên', use_container_width=True)
|
37 |
+
|
38 |
+
# Phát hiện đối tượng
|
39 |
+
with st.spinner("Đang phát hiện sâu răng..."):
|
40 |
+
results = model.predict(image)
|
41 |
+
|
42 |
+
# Lấy kết quả đầu tiên
|
43 |
+
result = results[0]
|
44 |
+
|
45 |
+
# Tạo đối tượng vẽ trên hình
|
46 |
+
draw = ImageDraw.Draw(image)
|
47 |
+
font = ImageFont.load_default()
|
48 |
+
|
49 |
+
# Danh sách bounding boxes
|
50 |
+
boxes = []
|
51 |
+
|
52 |
+
for box in result.boxes:
|
53 |
+
x1, y1, x2, y2 = [int(coord) for coord in box.xyxy[0]]
|
54 |
+
class_id = int(box.cls[0].item())
|
55 |
+
confidence = round(box.conf[0].item(), 2)
|
56 |
+
|
57 |
+
# Nếu chỉ phát hiện lớp 0 (sâu răng)
|
58 |
+
if class_id != 0:
|
59 |
+
continue
|
60 |
+
|
61 |
+
label = result.names[class_id]
|
62 |
+
boxes.append([x1, y1, x2, y2, label, confidence])
|
63 |
+
|
64 |
+
# Vẽ bounding box
|
65 |
+
draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
|
66 |
+
|
67 |
+
# Vẽ nhãn và độ tin cậy
|
68 |
+
text = f"{label} {confidence}"
|
69 |
+
text_bbox = draw.textbbox((0, 0), text, font=font)
|
70 |
+
text_size = (text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1])
|
71 |
+
draw.rectangle([x1, y1 - text_size[1], x1 + text_size[0], y1], fill="red")
|
72 |
+
draw.text((x1, y1 - text_size[1]), text, fill="white", font=font)
|
73 |
+
|
74 |
+
# Hiển thị hình ảnh với bounding boxes
|
75 |
+
st.image(image, caption='Hình ảnh sau khi phát hiện', use_container_width=True)
|
76 |
+
|
77 |
+
# Hiển thị bảng kết quả
|
78 |
+
if boxes:
|
79 |
+
st.subheader("Kết Quả Phát Hiện")
|
80 |
+
# Tạo DataFrame để hiển thị bảng đẹp hơn
|
81 |
+
import pandas as pd
|
82 |
+
df = pd.DataFrame(boxes, columns=["X1", "Y1", "X2", "Y2", "Loại Đối Tượng", "Độ Tin Cậy"])
|
83 |
+
st.table(df)
|
84 |
+
else:
|
85 |
+
st.write("Không phát hiện sâu răng nào trong hình ảnh này.")
|
best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4a8e6a17d9cf8b79e6fa541bd696b8d9b6c44efa1a1ce1284c044ffa2de5cedc
|
3 |
+
size 52033601
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
ultralytics
|
2 |
+
flask
|
3 |
+
waitress
|
4 |
+
pillow
|
5 |
+
streamlit
|
6 |
+
opencv-python-headless
|