Spaces:
Runtime error
Runtime error
mingyang91
commited on
Update UI
Browse files- .idea/workspace.xml +43 -24
- Dockerfile +3 -1
- demo.py +55 -34
- evaluator.py +20 -7
- yolo_dataset.py +32 -1
- yolo_fire.py → yolo_model.py +71 -15
.idea/workspace.xml
CHANGED
@@ -4,7 +4,15 @@
|
|
4 |
<option name="autoReloadType" value="SELECTIVE" />
|
5 |
</component>
|
6 |
<component name="ChangeListManager">
|
7 |
-
<list default="true" id="d7806539-b6d6-42e7-bb45-1565f5d54891" name="Changes" comment="IoU evaluator"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
<option name="SHOW_DIALOG" value="false" />
|
9 |
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
10 |
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
@@ -37,25 +45,25 @@
|
|
37 |
<option name="hideEmptyMiddlePackages" value="true" />
|
38 |
<option name="showLibraryContents" value="true" />
|
39 |
</component>
|
40 |
-
<component name="PropertiesComponent"
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
}
|
58 |
-
}
|
59 |
<component name="RecentsManager">
|
60 |
<key name="CopyFile.RECENT_KEYS">
|
61 |
<recent name="$PROJECT_DIR$/tests" />
|
@@ -66,7 +74,7 @@
|
|
66 |
<recent name="$PROJECT_DIR$" />
|
67 |
</key>
|
68 |
</component>
|
69 |
-
<component name="RunManager" selected="Python.
|
70 |
<configuration name="demo" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
|
71 |
<module name="detector" />
|
72 |
<option name="ENV_FILES" value="" />
|
@@ -153,8 +161,8 @@
|
|
153 |
</configuration>
|
154 |
<recent_temporary>
|
155 |
<list>
|
156 |
-
<item itemvalue="Python.evaluator" />
|
157 |
<item itemvalue="Python.yolo_dataset" />
|
|
|
158 |
<item itemvalue="Python.demo" />
|
159 |
</list>
|
160 |
</recent_temporary>
|
@@ -177,7 +185,9 @@
|
|
177 |
<workItem from="1702301554378" duration="4025000" />
|
178 |
<workItem from="1702450051021" duration="10065000" />
|
179 |
<workItem from="1702476484538" duration="9613000" />
|
180 |
-
<workItem from="1705757850315" duration="
|
|
|
|
|
181 |
</task>
|
182 |
<task id="LOCAL-00001" summary="init commit">
|
183 |
<option name="closed" value="true" />
|
@@ -347,7 +357,15 @@
|
|
347 |
<option name="project" value="LOCAL" />
|
348 |
<updated>1705770964768</updated>
|
349 |
</task>
|
350 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
351 |
<servers />
|
352 |
</component>
|
353 |
<component name="TypeScriptGeneratedFilesManager">
|
@@ -393,8 +411,9 @@
|
|
393 |
<option name="LAST_COMMIT_MESSAGE" value="IoU evaluator" />
|
394 |
</component>
|
395 |
<component name="com.intellij.coverage.CoverageDataManagerImpl">
|
|
|
396 |
<SUITE FILE_PATH="coverage/detector$demo.coverage" NAME="demo Coverage Results" MODIFIED="1702302380580" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
|
397 |
-
<SUITE FILE_PATH="coverage/detector$evaluator.coverage" NAME="evaluator Coverage Results" MODIFIED="
|
398 |
<SUITE FILE_PATH="coverage/detector$extract.coverage" NAME="yolo_dataset Coverage Results" MODIFIED="1705764465837" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
|
399 |
</component>
|
400 |
</project>
|
|
|
4 |
<option name="autoReloadType" value="SELECTIVE" />
|
5 |
</component>
|
6 |
<component name="ChangeListManager">
|
7 |
+
<list default="true" id="d7806539-b6d6-42e7-bb45-1565f5d54891" name="Changes" comment="IoU evaluator">
|
8 |
+
<change afterPath="$PROJECT_DIR$/yolo_model.py" afterDir="false" />
|
9 |
+
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
10 |
+
<change beforePath="$PROJECT_DIR$/Dockerfile" beforeDir="false" afterPath="$PROJECT_DIR$/Dockerfile" afterDir="false" />
|
11 |
+
<change beforePath="$PROJECT_DIR$/demo.py" beforeDir="false" afterPath="$PROJECT_DIR$/demo.py" afterDir="false" />
|
12 |
+
<change beforePath="$PROJECT_DIR$/evaluator.py" beforeDir="false" afterPath="$PROJECT_DIR$/evaluator.py" afterDir="false" />
|
13 |
+
<change beforePath="$PROJECT_DIR$/yolo_dataset.py" beforeDir="false" afterPath="$PROJECT_DIR$/yolo_dataset.py" afterDir="false" />
|
14 |
+
<change beforePath="$PROJECT_DIR$/yolo_fire.py" beforeDir="false" />
|
15 |
+
</list>
|
16 |
<option name="SHOW_DIALOG" value="false" />
|
17 |
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
18 |
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
|
|
45 |
<option name="hideEmptyMiddlePackages" value="true" />
|
46 |
<option name="showLibraryContents" value="true" />
|
47 |
</component>
|
48 |
+
<component name="PropertiesComponent"><![CDATA[{
|
49 |
+
"keyToString": {
|
50 |
+
"Python.demo.executor": "Run",
|
51 |
+
"Python.evaluator.executor": "Run",
|
52 |
+
"Python.extract.executor": "Run",
|
53 |
+
"Python.yolo_dataset.executor": "Run",
|
54 |
+
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
55 |
+
"RunOnceActivity.ShowReadmeOnStart": "true",
|
56 |
+
"git-widget-placeholder": "main",
|
57 |
+
"last_opened_file_path": "/Users/famer.me/PycharmProjects/detector/tests",
|
58 |
+
"node.js.detected.package.eslint": "true",
|
59 |
+
"node.js.detected.package.tslint": "true",
|
60 |
+
"node.js.selected.package.eslint": "(autodetect)",
|
61 |
+
"node.js.selected.package.tslint": "(autodetect)",
|
62 |
+
"nodejs_package_manager_path": "npm",
|
63 |
+
"settings.editor.selected.configurable": "settings.qodana",
|
64 |
+
"vue.rearranger.settings.migration": "true"
|
65 |
}
|
66 |
+
}]]></component>
|
67 |
<component name="RecentsManager">
|
68 |
<key name="CopyFile.RECENT_KEYS">
|
69 |
<recent name="$PROJECT_DIR$/tests" />
|
|
|
74 |
<recent name="$PROJECT_DIR$" />
|
75 |
</key>
|
76 |
</component>
|
77 |
+
<component name="RunManager" selected="Python.yolo_dataset">
|
78 |
<configuration name="demo" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
|
79 |
<module name="detector" />
|
80 |
<option name="ENV_FILES" value="" />
|
|
|
161 |
</configuration>
|
162 |
<recent_temporary>
|
163 |
<list>
|
|
|
164 |
<item itemvalue="Python.yolo_dataset" />
|
165 |
+
<item itemvalue="Python.evaluator" />
|
166 |
<item itemvalue="Python.demo" />
|
167 |
</list>
|
168 |
</recent_temporary>
|
|
|
185 |
<workItem from="1702301554378" duration="4025000" />
|
186 |
<workItem from="1702450051021" duration="10065000" />
|
187 |
<workItem from="1702476484538" duration="9613000" />
|
188 |
+
<workItem from="1705757850315" duration="14186000" />
|
189 |
+
<workItem from="1705812482464" duration="711000" />
|
190 |
+
<workItem from="1705845330999" duration="5355000" />
|
191 |
</task>
|
192 |
<task id="LOCAL-00001" summary="init commit">
|
193 |
<option name="closed" value="true" />
|
|
|
357 |
<option name="project" value="LOCAL" />
|
358 |
<updated>1705770964768</updated>
|
359 |
</task>
|
360 |
+
<task id="LOCAL-00022" summary="IoU evaluator">
|
361 |
+
<option name="closed" value="true" />
|
362 |
+
<created>1705771207294</created>
|
363 |
+
<option name="number" value="00022" />
|
364 |
+
<option name="presentableId" value="LOCAL-00022" />
|
365 |
+
<option name="project" value="LOCAL" />
|
366 |
+
<updated>1705771207294</updated>
|
367 |
+
</task>
|
368 |
+
<option name="localTasksCounter" value="23" />
|
369 |
<servers />
|
370 |
</component>
|
371 |
<component name="TypeScriptGeneratedFilesManager">
|
|
|
411 |
<option name="LAST_COMMIT_MESSAGE" value="IoU evaluator" />
|
412 |
</component>
|
413 |
<component name="com.intellij.coverage.CoverageDataManagerImpl">
|
414 |
+
<SUITE FILE_PATH="coverage/detector$yolo_dataset.coverage" NAME="yolo_dataset Coverage Results" MODIFIED="1705850662461" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
|
415 |
<SUITE FILE_PATH="coverage/detector$demo.coverage" NAME="demo Coverage Results" MODIFIED="1702302380580" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
|
416 |
+
<SUITE FILE_PATH="coverage/detector$evaluator.coverage" NAME="evaluator Coverage Results" MODIFIED="1705847678258" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
|
417 |
<SUITE FILE_PATH="coverage/detector$extract.coverage" NAME="yolo_dataset Coverage Results" MODIFIED="1705764465837" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
|
418 |
</component>
|
419 |
</project>
|
Dockerfile
CHANGED
@@ -12,5 +12,7 @@ RUN chown -R user $HOME/.cache
|
|
12 |
|
13 |
RUN pip install -r requirements.txt
|
14 |
RUN --mount=type=secret,id=HF_TOKEN,mode=0444,required=true \
|
15 |
-
HF_TOKEN=$(cat /run/secrets/HF_TOKEN) python
|
|
|
|
|
16 |
ENTRYPOINT ["streamlit", "run", "demo.py"]
|
|
|
12 |
|
13 |
RUN pip install -r requirements.txt
|
14 |
RUN --mount=type=secret,id=HF_TOKEN,mode=0444,required=true \
|
15 |
+
HF_TOKEN=$(cat /run/secrets/HF_TOKEN) python yolo_model.py \
|
16 |
+
--weight_files=SHOU-ISD/fire-and-smoke:yolov8n.pt \
|
17 |
+
--weight_files=SHOU-ISD/yolo-cracks:best.pt
|
18 |
ENTRYPOINT ["streamlit", "run", "demo.py"]
|
demo.py
CHANGED
@@ -9,52 +9,73 @@ from PIL import Image
|
|
9 |
|
10 |
import evaluator
|
11 |
from yolo_dataset import YoloDataset
|
12 |
-
from
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
def main():
|
16 |
# Header & Page Config.
|
17 |
st.set_page_config(
|
18 |
-
page_title="
|
19 |
layout="centered")
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
detect_tab, evaluate_tab = st.tabs(["Detect", "Evaluate"])
|
23 |
|
24 |
with evaluate_tab:
|
25 |
-
|
|
|
|
|
26 |
|
27 |
-
if buffer:
|
28 |
-
with st.spinner('Wait for it...'):
|
29 |
-
# Slider for changing confidence
|
30 |
-
confidence = st.slider('Confidence Threshold', 0, 100, 30)
|
31 |
-
yolo_dataset = YoloDataset.from_zip_file(ZipFile(buffer))
|
32 |
-
capture_output(evaluator.evaluate)(coco_gt=yolo_dataset.to_coco(), loader=yolo_dataset.load_image,
|
33 |
-
confidence_threshold=confidence / 100.0)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
#
|
56 |
-
st.
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
|
60 |
def capture_output(func):
|
|
|
9 |
|
10 |
import evaluator
|
11 |
from yolo_dataset import YoloDataset
|
12 |
+
from yolo_model import YoloModel
|
13 |
+
|
14 |
+
fire_and_smoke = YoloModel("SHOU-ISD/fire-and-smoke", "yolov8n.pt")
|
15 |
+
crack = YoloModel("SHOU-ISD/yolo-cracks", "best.pt")
|
16 |
|
17 |
|
18 |
def main():
|
19 |
# Header & Page Config.
|
20 |
st.set_page_config(
|
21 |
+
page_title=f"Detection",
|
22 |
layout="centered")
|
23 |
+
|
24 |
+
model = None
|
25 |
+
model_choice = None
|
26 |
+
with st.sidebar:
|
27 |
+
model_choice = st.radio("Select Model", ["Fire&Smoke", "Crack"])
|
28 |
+
if model_choice == "Fire&Smoke":
|
29 |
+
model = fire_and_smoke
|
30 |
+
elif model_choice == "Crack":
|
31 |
+
model = crack
|
32 |
+
|
33 |
+
st.title(f"{model_choice} Detection:")
|
34 |
|
35 |
detect_tab, evaluate_tab = st.tabs(["Detect", "Evaluate"])
|
36 |
|
37 |
with evaluate_tab:
|
38 |
+
evaluate(model)
|
39 |
+
with detect_tab:
|
40 |
+
detect(model)
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
def evaluate(model: YoloModel):
|
44 |
+
buffer = st.file_uploader("Upload your Yolo Dataset here", type=["zip"])
|
45 |
+
|
46 |
+
if buffer:
|
47 |
+
with st.spinner('Wait for it...'):
|
48 |
+
# Slider for changing confidence
|
49 |
+
confidence = st.slider('Confidence Threshold', 0, 100, 30)
|
50 |
+
yolo_dataset = YoloDataset.from_zip_file(ZipFile(buffer))
|
51 |
+
capture_output(evaluator.evaluate)(model=model.model,
|
52 |
+
coco_gt=yolo_dataset.to_coco(), loader=yolo_dataset.load_image,
|
53 |
+
confidence_threshold=confidence / 100.0)
|
54 |
+
|
55 |
+
|
56 |
+
def detect(model: YoloModel):
|
57 |
+
# This will let you upload PNG, JPG & JPEG File
|
58 |
+
buffer = st.file_uploader("Upload your Image here", type=["jpg", "png", "jpeg"])
|
59 |
+
|
60 |
+
if buffer:
|
61 |
+
# Object Detecting
|
62 |
+
with st.spinner('Wait for it...'):
|
63 |
+
# Slider for changing confidence
|
64 |
+
confidence = st.slider('Confidence Threshold', 0, 100, 30)
|
65 |
+
|
66 |
+
# Calculating time for detection
|
67 |
+
t1 = time.time()
|
68 |
+
im = Image.open(buffer)
|
69 |
+
# im.save("saved_images/image.jpg")
|
70 |
+
res_img = model.preview_detect(im, confidence / 100.0)
|
71 |
+
t2 = time.time()
|
72 |
+
|
73 |
+
# Displaying the image
|
74 |
+
st.image(res_img, use_column_width=True)
|
75 |
+
|
76 |
+
# Printing Time
|
77 |
+
st.write("\n")
|
78 |
+
st.write("Time taken: ", t2 - t1, "sec.")
|
79 |
|
80 |
|
81 |
def capture_output(func):
|
evaluator.py
CHANGED
@@ -6,12 +6,12 @@ from pycocotools.coco import COCO
|
|
6 |
from tqdm import tqdm
|
7 |
|
8 |
from yolo_dataset import YoloDataset
|
9 |
-
from
|
10 |
|
11 |
image_loader = Callable[[str], Image]
|
12 |
|
13 |
|
14 |
-
def evaluate(coco_gt: COCO, loader: image_loader, confidence_threshold=0.6):
|
15 |
# initialize evaluator with ground truth (gt)
|
16 |
evaluator = CocoEvaluator(coco_gt=coco_gt, iou_types=["bbox"])
|
17 |
|
@@ -19,9 +19,10 @@ def evaluate(coco_gt: COCO, loader: image_loader, confidence_threshold=0.6):
|
|
19 |
for image_id, annotations in tqdm(coco_gt.imgToAnns.items()):
|
20 |
# get the inputs
|
21 |
image = coco_gt.imgs[image_id]
|
22 |
-
results = model(source=loader(image["file_name"]))
|
23 |
for result in results:
|
24 |
-
coco_anns = yolo_boxes_to_coco_annotations(image_id, result.boxes,
|
|
|
25 |
if len(coco_anns) == 0:
|
26 |
continue
|
27 |
evaluator.update(coco_anns)
|
@@ -47,7 +48,19 @@ def yolo_boxes_to_coco_annotations(image_id: int, yolo_boxes, confidence_thresho
|
|
47 |
]
|
48 |
|
49 |
|
50 |
-
|
51 |
-
yolo_dataset = YoloDataset.
|
52 |
coco_gt = yolo_dataset.to_coco()
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from tqdm import tqdm
|
7 |
|
8 |
from yolo_dataset import YoloDataset
|
9 |
+
from yolo_model import YoloModel
|
10 |
|
11 |
image_loader = Callable[[str], Image]
|
12 |
|
13 |
|
14 |
+
def evaluate(model: YoloModel, coco_gt: COCO, loader: image_loader, confidence_threshold=0.6):
|
15 |
# initialize evaluator with ground truth (gt)
|
16 |
evaluator = CocoEvaluator(coco_gt=coco_gt, iou_types=["bbox"])
|
17 |
|
|
|
19 |
for image_id, annotations in tqdm(coco_gt.imgToAnns.items()):
|
20 |
# get the inputs
|
21 |
image = coco_gt.imgs[image_id]
|
22 |
+
results = model.model(source=loader(image["file_name"]))
|
23 |
for result in results:
|
24 |
+
coco_anns = yolo_boxes_to_coco_annotations(image_id, result.boxes,
|
25 |
+
confidence_threshold=confidence_threshold)
|
26 |
if len(coco_anns) == 0:
|
27 |
continue
|
28 |
evaluator.update(coco_anns)
|
|
|
48 |
]
|
49 |
|
50 |
|
51 |
+
def main():
|
52 |
+
yolo_dataset = YoloDataset.from_path('tests/coco8.zip')
|
53 |
coco_gt = yolo_dataset.to_coco()
|
54 |
+
model = YoloModel("ultralyticsplus/yolov8s", "yolov8s.pt")
|
55 |
+
# model = YoloModel("SHOU-ISD/fire-and-smoke", "yolov8n.pt")
|
56 |
+
evaluate(model=model, coco_gt=coco_gt, loader=yolo_dataset.load_image, confidence_threshold=0.1)
|
57 |
+
# Validate the model
|
58 |
+
metrics = model.model.val() # no arguments needed, dataset and settings remembered
|
59 |
+
print(metrics.box.map) # map50-95
|
60 |
+
print(metrics.box.map50) # map50
|
61 |
+
print(metrics.box.map75) # map75
|
62 |
+
print(metrics.box.maps) # a list contains map50-95 of each category
|
63 |
+
|
64 |
+
|
65 |
+
if __name__ == '__main__':
|
66 |
+
main()
|
yolo_dataset.py
CHANGED
@@ -12,6 +12,9 @@
|
|
12 |
# ├── 1.txt
|
13 |
# ├── 2.txt
|
14 |
# └── ...
|
|
|
|
|
|
|
15 |
from datetime import datetime
|
16 |
from typing import Optional
|
17 |
from zipfile import ZipFile
|
@@ -198,8 +201,36 @@ class YoloDataset:
|
|
198 |
coco_ds.createIndex()
|
199 |
return coco_ds
|
200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
if __name__ == '__main__':
|
203 |
-
dataset = YoloDataset.
|
204 |
coco = dataset.to_coco()
|
205 |
print(coco)
|
|
|
|
12 |
# ├── 1.txt
|
13 |
# ├── 2.txt
|
14 |
# └── ...
|
15 |
+
import os
|
16 |
+
import pathlib
|
17 |
+
import shutil
|
18 |
from datetime import datetime
|
19 |
from typing import Optional
|
20 |
from zipfile import ZipFile
|
|
|
201 |
coco_ds.createIndex()
|
202 |
return coco_ds
|
203 |
|
204 |
+
def to_yaml(self):
|
205 |
+
"""
|
206 |
+
:return: Yaml format in
|
207 |
+
<code>
|
208 |
+
path: ../datasets/coco128 # dataset root dir
|
209 |
+
val: images/train2017 # val images (relative to 'path') 128 images
|
210 |
+
|
211 |
+
# Classes
|
212 |
+
names:
|
213 |
+
0: person
|
214 |
+
</code>
|
215 |
+
"""
|
216 |
+
try:
|
217 |
+
# recursively create dir
|
218 |
+
pathlib.Path(f'datasets/{self._zip_file.filename}').mkdir(parents=True, exist_ok=True)
|
219 |
+
self._zip_file.extractall(f'datasets/{self._zip_file.filename}')
|
220 |
+
with open(f'datasets/{self._zip_file.filename}/data.yaml', 'w') as f:
|
221 |
+
f.write(f'path: ../datasets/{self._zip_file.filename}\n')
|
222 |
+
f.write(f'val: images/train2017\n')
|
223 |
+
f.write(f'\n')
|
224 |
+
f.write(f'# Classes\n')
|
225 |
+
f.write(f'names:\n')
|
226 |
+
for i in range(len(self._classes)):
|
227 |
+
f.write(f' {i}: {self._classes[i]}\n')
|
228 |
+
finally:
|
229 |
+
shutil.rmtree(f'datasets/{self._zip_file.filename}')
|
230 |
+
|
231 |
|
232 |
if __name__ == '__main__':
|
233 |
+
dataset = YoloDataset.from_path('tests/coco8.zip')
|
234 |
coco = dataset.to_coco()
|
235 |
print(coco)
|
236 |
+
dataset.to_yaml()
|
yolo_fire.py → yolo_model.py
RENAMED
@@ -3,12 +3,17 @@ from PIL import ImageDraw, Image
|
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
from ultralytics import YOLO
|
5 |
|
6 |
-
# Load model
|
7 |
-
weight_file = hf_hub_download("SHOU-ISD/fire-and-smoke", "yolov8n.pt")
|
8 |
-
model = YOLO(weight_file) # pretrained YOLOv8n model
|
9 |
|
10 |
-
# Helper Functions for Plotting BBoxes
|
11 |
def plot_one_box(x, img, color=None, label=None, line_thickness=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
width, height = img.size
|
13 |
tl = line_thickness or round(0.002 * (width + height) / 2) + 1 # line/font thickness
|
14 |
color = color or (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
@@ -22,8 +27,14 @@ def plot_one_box(x, img, color=None, label=None, line_thickness=None):
|
|
22 |
img_draw.text((x1, y1), label, fill=(255, 255, 255))
|
23 |
|
24 |
|
25 |
-
# Ploting Bounding Box on img
|
26 |
def add_bboxes(pil_img, result, confidence=0.6):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
for box in result.boxes:
|
28 |
[cl] = box.cls.tolist()
|
29 |
[conf] = box.conf.tolist()
|
@@ -36,16 +47,61 @@ def add_bboxes(pil_img, result, confidence=0.6):
|
|
36 |
return pil_img
|
37 |
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
res_img = add_bboxes(res_img, result, confidence)
|
44 |
-
return res_img
|
45 |
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
im = Image.open("./tests/fire1.jpg")
|
48 |
-
results = model(source=im)
|
49 |
for result in results:
|
50 |
-
im = add_bboxes(im, result)
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
from ultralytics import YOLO
|
5 |
|
|
|
|
|
|
|
6 |
|
|
|
7 |
def plot_one_box(x, img, color=None, label=None, line_thickness=None):
|
8 |
+
"""
|
9 |
+
Helper Functions for Plotting BBoxes
|
10 |
+
:param x:
|
11 |
+
:param img:
|
12 |
+
:param color:
|
13 |
+
:param label:
|
14 |
+
:param line_thickness:
|
15 |
+
:return:
|
16 |
+
"""
|
17 |
width, height = img.size
|
18 |
tl = line_thickness or round(0.002 * (width + height) / 2) + 1 # line/font thickness
|
19 |
color = color or (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
|
|
27 |
img_draw.text((x1, y1), label, fill=(255, 255, 255))
|
28 |
|
29 |
|
|
|
30 |
def add_bboxes(pil_img, result, confidence=0.6):
|
31 |
+
"""
|
32 |
+
Plotting Bounding Box on img
|
33 |
+
:param pil_img:
|
34 |
+
:param result:
|
35 |
+
:param confidence:
|
36 |
+
:return:
|
37 |
+
"""
|
38 |
for box in result.boxes:
|
39 |
[cl] = box.cls.tolist()
|
40 |
[conf] = box.conf.tolist()
|
|
|
47 |
return pil_img
|
48 |
|
49 |
|
50 |
+
class YoloModel:
|
51 |
+
def __init__(self, repo_name: str, file_name: str):
|
52 |
+
weight_file = YoloModel.download_weight_file(repo_name, file_name)
|
53 |
+
self.model = YOLO(weight_file)
|
|
|
|
|
54 |
|
55 |
+
@staticmethod
|
56 |
+
def download_weight_file(repo_name: str, file_name: str):
|
57 |
+
return hf_hub_download(repo_name, file_name)
|
58 |
+
|
59 |
+
def detect(self, im):
|
60 |
+
return self.model(source=im)
|
61 |
+
|
62 |
+
def preview_detect(self, im, confidence):
|
63 |
+
results = self.model(source=im)
|
64 |
+
res_img = im
|
65 |
+
for result in results:
|
66 |
+
res_img = add_bboxes(res_img, result, confidence)
|
67 |
+
return res_img
|
68 |
+
|
69 |
+
|
70 |
+
def test():
|
71 |
+
model = YoloModel("SHOU-ISD/fire-and-smoke", "yolov8n.pt")
|
72 |
im = Image.open("./tests/fire1.jpg")
|
73 |
+
results = model.model(source=im)
|
74 |
for result in results:
|
75 |
+
im = add_bboxes(im, result, confidence=0.1)
|
76 |
+
print(result.boxes)
|
77 |
+
|
78 |
+
|
79 |
+
def argument_parser():
|
80 |
+
"""
|
81 |
+
Argument Parser
|
82 |
+
:return: args
|
83 |
+
"""
|
84 |
+
import argparse
|
85 |
+
parser = argparse.ArgumentParser(description='Help for YoloModel')
|
86 |
+
parser.add_argument('--test', '-t', action='store_true', help='Run test')
|
87 |
+
# list of repo_name&file_name
|
88 |
+
parser.add_argument('--weight_files', '-w', nargs='+', help='List of weight files')
|
89 |
+
return parser.parse_args()
|
90 |
+
|
91 |
+
|
92 |
+
def pre_cache_weight_files(weight_files: list[str]):
|
93 |
+
"""
|
94 |
+
Pre-cache weight files
|
95 |
+
:return: None
|
96 |
+
"""
|
97 |
+
for weight_file in weight_files:
|
98 |
+
weight_file = weight_file.split(":")
|
99 |
+
YoloModel.download_weight_file(weight_file[0], weight_file[1])
|
100 |
+
|
101 |
+
|
102 |
+
if __name__ == '__main__':
|
103 |
+
args = argument_parser()
|
104 |
+
if args.test:
|
105 |
+
test()
|
106 |
+
else:
|
107 |
+
pre_cache_weight_files(args.weight_files)
|