Spaces:
Build error
Build error
Upload 5 files
Browse files- app.py +49 -0
- best.pt +3 -0
- model.py +34 -0
- readme.MD +54 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image as PILImage
|
5 |
+
from ultralytics import YOLO
|
6 |
+
from model import DetectNet
|
7 |
+
|
8 |
+
st.set_page_config(page_title="Crack Detection", page_icon=":mag:", layout="centered")
|
9 |
+
|
10 |
+
st.title("Crack Detection using YOLO V8")
|
11 |
+
|
12 |
+
# Load YOLO model
|
13 |
+
yolo = YOLO("best.pt")
|
14 |
+
save_dir = "detect.png"
|
15 |
+
model = DetectNet(yolo, save_name=save_dir)
|
16 |
+
|
17 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
18 |
+
|
19 |
+
if uploaded_file is not None:
|
20 |
+
# Convert the uploaded file to an OpenCV image
|
21 |
+
file_bytes = np.frombuffer(uploaded_file.read(), np.uint8)
|
22 |
+
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
23 |
+
|
24 |
+
# Run detection
|
25 |
+
result = model(img)
|
26 |
+
|
27 |
+
# Hiển thị ảnh sau khi detect
|
28 |
+
detected_img = cv2.imread(save_dir)
|
29 |
+
# Chuyển BGR sang RGB để hiển thị trên Streamlit
|
30 |
+
detected_img_rgb = cv2.cvtColor(detected_img, cv2.COLOR_BGR2RGB)
|
31 |
+
|
32 |
+
st.image(detected_img_rgb, caption="Detected Image")
|
33 |
+
|
34 |
+
# Xuất kết quả
|
35 |
+
if len(result) == 0:
|
36 |
+
st.write("No crack found")
|
37 |
+
elif len(result) == 1:
|
38 |
+
area, score = result[0]
|
39 |
+
st.write(
|
40 |
+
f"Crack predicted accuracy: {score:.2f}%\n"
|
41 |
+
f"The area of crack is: {area:.2f} cm²"
|
42 |
+
)
|
43 |
+
else:
|
44 |
+
for i, out in enumerate(result):
|
45 |
+
area, score = out
|
46 |
+
st.write(
|
47 |
+
f"Crack {i+1} predicted accuracy: {score:.2f}%\n"
|
48 |
+
f"The area of crack {i+1} is: {area:.2f} cm²\n"
|
49 |
+
)
|
best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b1427ce06ce51373f25677d1ec00aeb01f4b0cecfdd9e9f1720f803b048c59a3
|
3 |
+
size 94850350
|
model.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import torch.nn as nn
|
5 |
+
from ultralytics import YOLO
|
6 |
+
|
7 |
+
class DetectNet(nn.Module):
|
8 |
+
def __init__(self, model, save_name):
|
9 |
+
super().__init__()
|
10 |
+
self.model = model
|
11 |
+
self.save_name = save_name
|
12 |
+
|
13 |
+
def forward(self, img):
|
14 |
+
# Prediction
|
15 |
+
prediction = self.model(img)
|
16 |
+
detect_img = prediction[0].plot()
|
17 |
+
|
18 |
+
# Save detected image
|
19 |
+
cv2.imwrite(self.save_name, detect_img)
|
20 |
+
|
21 |
+
# Take out area and prediction score
|
22 |
+
boxes = 0
|
23 |
+
for predict in prediction:
|
24 |
+
boxes = predict.boxes.numpy()
|
25 |
+
|
26 |
+
return self.result(boxes)
|
27 |
+
|
28 |
+
def result(self, boxes):
|
29 |
+
out = []
|
30 |
+
for box in boxes:
|
31 |
+
x_min, y_min, x_max, y_max, score, _ = [item for i in box.data for item in i]
|
32 |
+
area = (x_max - x_min) * (y_max - y_min)
|
33 |
+
out.append([np.round(area, 2), np.round(score * 100, 2)])
|
34 |
+
return out
|
readme.MD
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Crack Detection
|
2 |
+
|
3 |
+
Ứng dụng phát hiện vết nứt trong hình ảnh sử dụng YOLO V8 và Streamlit.
|
4 |
+
|
5 |
+
## Giới thiệu
|
6 |
+
|
7 |
+
Crack Detection là một ứng dụng web dựa trên Streamlit sử dụng mô hình YOLO để phát hiện và phân tích các vết nứt trong hình ảnh. Ứng dụng cung cấp giao diện người dùng thân thiện, cho phép tải lên hình ảnh và nhận kết quả phát hiện chi tiết, bao gồm diện tích vết nứt và độ chính xác dự đoán.
|
8 |
+
|
9 |
+
## Tính năng
|
10 |
+
|
11 |
+
- Tải lên hình ảnh để phát hiện vết nứt.
|
12 |
+
- Phát hiện thời gian thực sử dụng mô hình YOLO.
|
13 |
+
- Hiển thị hình ảnh đã phát hiện với các bounding box.
|
14 |
+
- Hiển thị diện tích vết nứt và độ chính xác dự đoán.
|
15 |
+
- Giao diện web dễ sử dụng.
|
16 |
+
|
17 |
+
## Demo
|
18 |
+
|
19 |
+

|
20 |
+
|
21 |
+
## Cài đặt
|
22 |
+
|
23 |
+
Thực hiện các bước sau để cài đặt và chạy dự án trên máy tính của bạn.
|
24 |
+
|
25 |
+
### Yêu cầu hệ thống
|
26 |
+
|
27 |
+
- Python 3.7 hoặc cao hơn
|
28 |
+
- pip (trình quản lý gói của Python)
|
29 |
+
|
30 |
+
### Bước 1: Sao chép repository
|
31 |
+
|
32 |
+
Mở terminal hoặc command prompt và chạy lệnh sau để sao chép repository về máy:
|
33 |
+
|
34 |
+
```bash
|
35 |
+
git clone https://github.com/ducdatit2002/crack-detection.git
|
36 |
+
cd crack-detection
|
37 |
+
```
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
### Bước 2: Cài đặt các phụ thuộc
|
42 |
+
|
43 |
+
```bash
|
44 |
+
pip install -r requirements.txt
|
45 |
+
```
|
46 |
+
Hoặc
|
47 |
+
```bash
|
48 |
+
pip install streamlit ultralytics opencv-python Pillow torch torchvision
|
49 |
+
```
|
50 |
+
Chạy ứng dụng Streamlit bằng lệnh sau:
|
51 |
+
|
52 |
+
```bash
|
53 |
+
streamlit run app.py
|
54 |
+
```
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
ultralytics
|
3 |
+
opencv-python
|
4 |
+
Pillow
|
5 |
+
torch
|
6 |
+
torchvision
|