Spaces:
Configuration error
Configuration error
Upload 7 files
Browse files- app.py +44 -0
- classify.py +24 -0
- compare.py +22 -0
- detect.py +28 -0
- model/cls_best.pt +3 -0
- model/obd_best.pt +3 -0
- requirements.txt +77 -0
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import matplotlib.patches as patches
|
5 |
+
from detect import Detection
|
6 |
+
from classify import Classification
|
7 |
+
from compare import Compare
|
8 |
+
|
9 |
+
# Streamlit app
|
10 |
+
def main():
|
11 |
+
st.title("Metal Defect Detection and Classification App")
|
12 |
+
det_res = Detection()
|
13 |
+
cls_res = Classification()
|
14 |
+
comp = Compare()
|
15 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
16 |
+
|
17 |
+
if uploaded_file is not None:
|
18 |
+
# Display uploaded image
|
19 |
+
image = Image.open(uploaded_file)
|
20 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
21 |
+
|
22 |
+
df = det_res.detect_defect(image)
|
23 |
+
df1 = cls_res.classify_defect(image)
|
24 |
+
|
25 |
+
# Perform comparison between scores of detection and classification
|
26 |
+
detection_results = comp.comparison(df, df1)
|
27 |
+
|
28 |
+
# Display results
|
29 |
+
fig, ax = plt.subplots(1)
|
30 |
+
ax.imshow(image)
|
31 |
+
|
32 |
+
for index, row in detection_results.iterrows():
|
33 |
+
x1, y1, x2, y2 = row['x1'], row['y1'], row['x2'], row['y2']
|
34 |
+
width, height = x2 - x1, y2 - y1
|
35 |
+
rect = patches.Rectangle((x1, y1), width, height, linewidth=1, edgecolor='r', facecolor='none')
|
36 |
+
ax.add_patch(rect)
|
37 |
+
ax.text(x1, y1 - 5, f"{row['fnl_cls']}: {row['fnl_pred']:.2f}", color='r')
|
38 |
+
|
39 |
+
ax.axis('off')
|
40 |
+
st.pyplot(fig)
|
41 |
+
|
42 |
+
# Run the app
|
43 |
+
if __name__ == "__main__":
|
44 |
+
main()
|
classify.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from ultralytics import YOLO
|
3 |
+
|
4 |
+
class Classification:
|
5 |
+
def __init__(self) -> None:
|
6 |
+
self.__cls_model = YOLO('/home/pratham/MDD/bin/model/cls_best.pt')
|
7 |
+
|
8 |
+
def classify_defect(self, image_path) -> pd.DataFrame:
|
9 |
+
result_cls = self.__cls_model.predict(image_path, stream=False)
|
10 |
+
# Prepare data for CSV
|
11 |
+
data1 = []
|
12 |
+
for result in result_cls:
|
13 |
+
cnt1 = 0
|
14 |
+
for i in result_cls[0].probs.top5:
|
15 |
+
data1.append({
|
16 |
+
"Image/File Name": result_cls[0].path,
|
17 |
+
"Detected class by cls": self.__cls_model.names[i],
|
18 |
+
"Conf score": result_cls[0].probs.top5conf.tolist()[cnt1]
|
19 |
+
})
|
20 |
+
cnt1 = cnt1 + 1
|
21 |
+
|
22 |
+
# Convert to DataFrame and save as CSV
|
23 |
+
return pd.DataFrame(data1)
|
24 |
+
# df1.to_csv('classification_results.csv', index=False)
|
compare.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
|
3 |
+
class Compare:
|
4 |
+
def comparison(self, df, df1):
|
5 |
+
final = df.merge(df1, on='Image/File Name')
|
6 |
+
final.drop_duplicates(subset=['Confidence Score'], keep='first', inplace=True)
|
7 |
+
final = final.reset_index().drop(columns=['index'])
|
8 |
+
|
9 |
+
fnl_cls = []
|
10 |
+
fnl_cnf = []
|
11 |
+
for i in range(0, len(final['Confidence Score'])):
|
12 |
+
if final['Confidence Score'][i] > final['Conf score'][i]:
|
13 |
+
fnl_cls.append(final['Detected Class'][i])
|
14 |
+
fnl_cnf.append(final['Confidence Score'][i])
|
15 |
+
else:
|
16 |
+
fnl_cls.append(final['Detected class by cls'][i])
|
17 |
+
fnl_cnf.append(final['Conf score'][i])
|
18 |
+
|
19 |
+
final['fnl_cls'] = fnl_cls
|
20 |
+
final['fnl_pred'] = fnl_cnf
|
21 |
+
final = final.drop(columns=['Detected Class', 'Confidence Score', 'Detected class by cls', 'Conf score'])
|
22 |
+
return final
|
detect.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from ultralytics import YOLO
|
3 |
+
|
4 |
+
class Detection:
|
5 |
+
def __init__(self) -> None:
|
6 |
+
self.__obd_model = YOLO('/home/pratham/MDD/bin/model/obd_best.pt')
|
7 |
+
|
8 |
+
def detect_defect(self, image_path) -> pd.DataFrame:
|
9 |
+
result_obd = self.__obd_model.predict(image_path, stream=False) # Adjust paths as needed
|
10 |
+
|
11 |
+
# Prepare data for CSV
|
12 |
+
data = []
|
13 |
+
for result in result_obd:
|
14 |
+
cnt = 0
|
15 |
+
for i in result_obd[0].boxes.cls.tolist():
|
16 |
+
data.append({
|
17 |
+
"Image/File Name": result.path,
|
18 |
+
"Detected Class": self.__obd_model.names[int(i)],
|
19 |
+
"Confidence Score": result.boxes.conf.tolist()[cnt],
|
20 |
+
"x1": result.boxes.xyxy.tolist()[cnt][0],
|
21 |
+
"y1": result.boxes.xyxy.tolist()[cnt][1],
|
22 |
+
"x2": result.boxes.xyxy.tolist()[cnt][2],
|
23 |
+
"y2": result.boxes.xyxy.tolist()[cnt][3]
|
24 |
+
})
|
25 |
+
cnt = cnt + 1
|
26 |
+
|
27 |
+
# Convert to DataFrame and save as CSV
|
28 |
+
return pd.DataFrame(data)
|
model/cls_best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:aeae51ca684bae2c7bc8ca51a9b0646b72467f8354c526a0c7584f7d3e7b3be3
|
3 |
+
size 2973377
|
model/obd_best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3f423bbe8c4876ff23e4585399acce014b51cf3db3a094f494514a881bed8a99
|
3 |
+
size 6256729
|
requirements.txt
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
altair==5.3.0
|
2 |
+
attrs==23.2.0
|
3 |
+
blinker==1.8.2
|
4 |
+
cachetools==5.3.3
|
5 |
+
certifi==2024.2.2
|
6 |
+
charset-normalizer==3.3.2
|
7 |
+
click==8.1.7
|
8 |
+
contourpy==1.2.1
|
9 |
+
cycler==0.12.1
|
10 |
+
dill==0.3.8
|
11 |
+
filelock==3.14.0
|
12 |
+
fonttools==4.51.0
|
13 |
+
fsspec==2024.5.0
|
14 |
+
gitdb==4.0.11
|
15 |
+
GitPython==3.1.43
|
16 |
+
idna==3.7
|
17 |
+
Jinja2==3.1.4
|
18 |
+
jsonschema==4.22.0
|
19 |
+
jsonschema-specifications==2023.12.1
|
20 |
+
kiwisolver==1.4.5
|
21 |
+
markdown-it-py==3.0.0
|
22 |
+
MarkupSafe==2.1.5
|
23 |
+
matplotlib==3.9.0
|
24 |
+
mdurl==0.1.2
|
25 |
+
mpmath==1.3.0
|
26 |
+
networkx==3.3
|
27 |
+
numpy==1.26.4
|
28 |
+
nvidia-cublas-cu12==12.1.3.1
|
29 |
+
nvidia-cuda-cupti-cu12==12.1.105
|
30 |
+
nvidia-cuda-nvrtc-cu12==12.1.105
|
31 |
+
nvidia-cuda-runtime-cu12==12.1.105
|
32 |
+
nvidia-cudnn-cu12==8.9.2.26
|
33 |
+
nvidia-cufft-cu12==11.0.2.54
|
34 |
+
nvidia-curand-cu12==10.3.2.106
|
35 |
+
nvidia-cusolver-cu12==11.4.5.107
|
36 |
+
nvidia-cusparse-cu12==12.1.0.106
|
37 |
+
nvidia-nccl-cu12==2.20.5
|
38 |
+
nvidia-nvjitlink-cu12==12.4.127
|
39 |
+
nvidia-nvtx-cu12==12.1.105
|
40 |
+
opencv-python==4.9.0.80
|
41 |
+
packaging==24.0
|
42 |
+
pandas==2.2.2
|
43 |
+
pillow==10.3.0
|
44 |
+
protobuf==4.25.3
|
45 |
+
psutil==5.9.8
|
46 |
+
py-cpuinfo==9.0.0
|
47 |
+
pyarrow==16.1.0
|
48 |
+
pydeck==0.9.1
|
49 |
+
Pygments==2.18.0
|
50 |
+
pyparsing==3.1.2
|
51 |
+
python-dateutil==2.9.0.post0
|
52 |
+
pytz==2024.1
|
53 |
+
PyYAML==6.0.1
|
54 |
+
referencing==0.35.1
|
55 |
+
requests==2.31.0
|
56 |
+
rich==13.7.1
|
57 |
+
rpds-py==0.18.1
|
58 |
+
scipy==1.13.0
|
59 |
+
seaborn==0.13.2
|
60 |
+
six==1.16.0
|
61 |
+
smmap==5.0.1
|
62 |
+
streamlit==1.34.0
|
63 |
+
sympy==1.12
|
64 |
+
tenacity==8.3.0
|
65 |
+
thop==0.1.1.post2209072238
|
66 |
+
toml==0.10.2
|
67 |
+
toolz==0.12.1
|
68 |
+
torch==2.3.0
|
69 |
+
torchvision==0.18.0
|
70 |
+
tornado==6.4
|
71 |
+
tqdm==4.66.4
|
72 |
+
triton==2.3.0
|
73 |
+
typing_extensions==4.11.0
|
74 |
+
tzdata==2024.1
|
75 |
+
ultralytics==8.2.16
|
76 |
+
urllib3==2.2.1
|
77 |
+
watchdog==4.0.0
|