mingyang91 commited on
Commit
8892aa7
1 Parent(s): 684e6f5

add detection fast api

Browse files
Files changed (4) hide show
  1. .idea/workspace.xml +48 -12
  2. main.py +53 -6
  3. report.py +54 -0
  4. requirements.txt +1 -0
.idea/workspace.xml CHANGED
@@ -5,11 +5,10 @@
5
  </component>
6
  <component name="ChangeListManager">
7
  <list default="true" id="d7806539-b6d6-42e7-bb45-1565f5d54891" name="Changes" comment="add crack split.py">
8
- <change afterPath="$PROJECT_DIR$/models/tools/draw.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$/demo.py" beforeDir="false" afterPath="$PROJECT_DIR$/demo.py" afterDir="false" />
11
- <change beforePath="$PROJECT_DIR$/models/yolo_crack.py" beforeDir="false" afterPath="$PROJECT_DIR$/models/yolo_crack.py" afterDir="false" />
12
- <change beforePath="$PROJECT_DIR$/yolo_model.py" beforeDir="false" afterPath="$PROJECT_DIR$/yolo_model.py" afterDir="false" />
13
  </list>
14
  <option name="SHOW_DIALOG" value="false" />
15
  <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -60,39 +59,43 @@
60
  </component>
61
  <component name="PropertiesComponent"><![CDATA[{
62
  "keyToString": {
 
63
  "Python.demo.executor": "Debug",
64
  "Python.evaluator.executor": "Debug",
65
  "Python.extract.executor": "Run",
 
66
  "Python.streamlit.executor": "Run",
67
  "Python.yolo_crack.executor": "Run",
68
  "Python.yolo_dataset.executor": "Run",
69
  "RunOnceActivity.OpenProjectViewOnStart": "true",
70
  "RunOnceActivity.ShowReadmeOnStart": "true",
71
  "git-widget-placeholder": "main",
72
- "last_opened_file_path": "/Users/famer.me/PycharmProjects/detector/tools",
73
  "node.js.detected.package.eslint": "true",
74
  "node.js.detected.package.tslint": "true",
75
  "node.js.selected.package.eslint": "(autodetect)",
76
  "node.js.selected.package.tslint": "(autodetect)",
77
  "nodejs_package_manager_path": "npm",
78
- "settings.editor.selected.configurable": "settings.qodana",
79
  "vue.rearranger.settings.migration": "true"
80
  }
81
  }]]></component>
82
  <component name="RecentsManager">
83
  <key name="CopyFile.RECENT_KEYS">
 
84
  <recent name="$PROJECT_DIR$/tools" />
85
  <recent name="$PROJECT_DIR$/datasets" />
86
  <recent name="$PROJECT_DIR$/tests" />
87
  <recent name="$PROJECT_DIR$" />
88
  </key>
89
  <key name="MoveFile.RECENT_KEYS">
 
90
  <recent name="$PROJECT_DIR$/models" />
91
  <recent name="$PROJECT_DIR$/tests" />
92
  <recent name="$PROJECT_DIR$" />
93
  </key>
94
  </component>
95
- <component name="RunManager" selected="Python.yolo_crack">
96
  <configuration name="demo" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
97
  <module name="detector" />
98
  <option name="ENV_FILES" value="" />
@@ -139,6 +142,29 @@
139
  <option name="INPUT_FILE" value="" />
140
  <method v="2" />
141
  </configuration>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  <configuration name="streamlit" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
143
  <module name="detector" />
144
  <option name="ENV_FILES" value="" />
@@ -228,6 +254,7 @@
228
  <list>
229
  <item itemvalue="FastAPI.detector" />
230
  <item itemvalue="Python.streamlit" />
 
231
  <item itemvalue="Python.yolo_crack" />
232
  <item itemvalue="Python.demo" />
233
  <item itemvalue="Python.evaluator" />
@@ -235,17 +262,19 @@
235
  </list>
236
  <recent_temporary>
237
  <list>
 
 
238
  <item itemvalue="Python.yolo_crack" />
239
- <item itemvalue="Python.demo" />
240
  <item itemvalue="Python.evaluator" />
241
- <item itemvalue="Python.yolo_dataset" />
242
  </list>
243
  </recent_temporary>
244
  </component>
245
  <component name="SharedIndexes">
246
  <attachedChunks>
247
  <set>
248
- <option value="bundled-python-sdk-5a2391486177-2887949eec09-com.jetbrains.pycharm.pro.sharedIndexes.bundled-PY-233.13763.11" />
 
249
  </set>
250
  </attachedChunks>
251
  </component>
@@ -272,6 +301,11 @@
272
  <workItem from="1706449264770" duration="4974000" />
273
  <workItem from="1706517295469" duration="2595000" />
274
  <workItem from="1706855634459" duration="14109000" />
 
 
 
 
 
275
  </task>
276
  <task id="LOCAL-00001" summary="init commit">
277
  <option name="closed" value="true" />
@@ -592,11 +626,13 @@
592
  </breakpoint-manager>
593
  </component>
594
  <component name="com.intellij.coverage.CoverageDataManagerImpl">
 
 
595
  <SUITE FILE_PATH="coverage/detector$yolo_dataset.coverage" NAME="yolo_dataset Coverage Results" MODIFIED="1705852113469" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
 
596
  <SUITE FILE_PATH="coverage/detector$evaluator.coverage" NAME="evaluator Coverage Results" MODIFIED="1706107083258" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
 
597
  <SUITE FILE_PATH="coverage/detector$demo.coverage" NAME="demo Coverage Results" MODIFIED="1706108414052" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
598
- <SUITE FILE_PATH="coverage/detector$streamlit.coverage" NAME="streamlit Coverage Results" MODIFIED="1706949799318" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
599
  <SUITE FILE_PATH="coverage/detector$yolo_crack.coverage" NAME="yolo_crack Coverage Results" MODIFIED="1706950034213" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
600
- <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$" />
601
  </component>
602
  </project>
 
5
  </component>
6
  <component name="ChangeListManager">
7
  <list default="true" id="d7806539-b6d6-42e7-bb45-1565f5d54891" name="Changes" comment="add crack split.py">
8
+ <change afterPath="$PROJECT_DIR$/report.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$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
11
+ <change beforePath="$PROJECT_DIR$/requirements.txt" beforeDir="false" afterPath="$PROJECT_DIR$/requirements.txt" afterDir="false" />
 
12
  </list>
13
  <option name="SHOW_DIALOG" value="false" />
14
  <option name="HIGHLIGHT_CONFLICTS" value="true" />
 
59
  </component>
60
  <component name="PropertiesComponent"><![CDATA[{
61
  "keyToString": {
62
+ "FastAPI.detector.executor": "Debug",
63
  "Python.demo.executor": "Debug",
64
  "Python.evaluator.executor": "Debug",
65
  "Python.extract.executor": "Run",
66
+ "Python.report.executor": "Run",
67
  "Python.streamlit.executor": "Run",
68
  "Python.yolo_crack.executor": "Run",
69
  "Python.yolo_dataset.executor": "Run",
70
  "RunOnceActivity.OpenProjectViewOnStart": "true",
71
  "RunOnceActivity.ShowReadmeOnStart": "true",
72
  "git-widget-placeholder": "main",
73
+ "last_opened_file_path": "/Users/famer.me/PycharmProjects/detector/datasets/coco8/images/val",
74
  "node.js.detected.package.eslint": "true",
75
  "node.js.detected.package.tslint": "true",
76
  "node.js.selected.package.eslint": "(autodetect)",
77
  "node.js.selected.package.tslint": "(autodetect)",
78
  "nodejs_package_manager_path": "npm",
79
+ "settings.editor.selected.configurable": "preferences.pluginManager",
80
  "vue.rearranger.settings.migration": "true"
81
  }
82
  }]]></component>
83
  <component name="RecentsManager">
84
  <key name="CopyFile.RECENT_KEYS">
85
+ <recent name="$PROJECT_DIR$/datasets/coco8/images/val" />
86
  <recent name="$PROJECT_DIR$/tools" />
87
  <recent name="$PROJECT_DIR$/datasets" />
88
  <recent name="$PROJECT_DIR$/tests" />
89
  <recent name="$PROJECT_DIR$" />
90
  </key>
91
  <key name="MoveFile.RECENT_KEYS">
92
+ <recent name="$PROJECT_DIR$/datasets/coco8/images/report" />
93
  <recent name="$PROJECT_DIR$/models" />
94
  <recent name="$PROJECT_DIR$/tests" />
95
  <recent name="$PROJECT_DIR$" />
96
  </key>
97
  </component>
98
+ <component name="RunManager" selected="FastAPI.detector">
99
  <configuration name="demo" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
100
  <module name="detector" />
101
  <option name="ENV_FILES" value="" />
 
142
  <option name="INPUT_FILE" value="" />
143
  <method v="2" />
144
  </configuration>
145
+ <configuration name="report" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
146
+ <module name="detector" />
147
+ <option name="ENV_FILES" value="" />
148
+ <option name="INTERPRETER_OPTIONS" value="" />
149
+ <option name="PARENT_ENVS" value="true" />
150
+ <envs>
151
+ <env name="PYTHONUNBUFFERED" value="1" />
152
+ </envs>
153
+ <option name="SDK_HOME" value="" />
154
+ <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
155
+ <option name="IS_MODULE_SDK" value="true" />
156
+ <option name="ADD_CONTENT_ROOTS" value="true" />
157
+ <option name="ADD_SOURCE_ROOTS" value="true" />
158
+ <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
159
+ <option name="SCRIPT_NAME" value="$PROJECT_DIR$/report.py" />
160
+ <option name="PARAMETERS" value="" />
161
+ <option name="SHOW_COMMAND_LINE" value="false" />
162
+ <option name="EMULATE_TERMINAL" value="false" />
163
+ <option name="MODULE_MODE" value="false" />
164
+ <option name="REDIRECT_INPUT" value="false" />
165
+ <option name="INPUT_FILE" value="" />
166
+ <method v="2" />
167
+ </configuration>
168
  <configuration name="streamlit" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
169
  <module name="detector" />
170
  <option name="ENV_FILES" value="" />
 
254
  <list>
255
  <item itemvalue="FastAPI.detector" />
256
  <item itemvalue="Python.streamlit" />
257
+ <item itemvalue="Python.report" />
258
  <item itemvalue="Python.yolo_crack" />
259
  <item itemvalue="Python.demo" />
260
  <item itemvalue="Python.evaluator" />
 
262
  </list>
263
  <recent_temporary>
264
  <list>
265
+ <item itemvalue="Python.report" />
266
+ <item itemvalue="Python.yolo_dataset" />
267
  <item itemvalue="Python.yolo_crack" />
 
268
  <item itemvalue="Python.evaluator" />
269
+ <item itemvalue="Python.demo" />
270
  </list>
271
  </recent_temporary>
272
  </component>
273
  <component name="SharedIndexes">
274
  <attachedChunks>
275
  <set>
276
+ <option value="bundled-js-predefined-1d06a55b98c1-74d2a5396914-JavaScript-PY-241.14494.241" />
277
+ <option value="bundled-python-sdk-0509580d9d50-28c9f5db9ffe-com.jetbrains.pycharm.pro.sharedIndexes.bundled-PY-241.14494.241" />
278
  </set>
279
  </attachedChunks>
280
  </component>
 
301
  <workItem from="1706449264770" duration="4974000" />
302
  <workItem from="1706517295469" duration="2595000" />
303
  <workItem from="1706855634459" duration="14109000" />
304
+ <workItem from="1708327016792" duration="4898000" />
305
+ <workItem from="1708856137024" duration="98000" />
306
+ <workItem from="1708856451866" duration="718000" />
307
+ <workItem from="1714034677595" duration="2647000" />
308
+ <workItem from="1714110004313" duration="6341000" />
309
  </task>
310
  <task id="LOCAL-00001" summary="init commit">
311
  <option name="closed" value="true" />
 
626
  </breakpoint-manager>
627
  </component>
628
  <component name="com.intellij.coverage.CoverageDataManagerImpl">
629
+ <SUITE FILE_PATH="coverage/detector$streamlit.coverage" NAME="streamlit Coverage Results" MODIFIED="1706949799318" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
630
+ <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$" />
631
  <SUITE FILE_PATH="coverage/detector$yolo_dataset.coverage" NAME="yolo_dataset Coverage Results" MODIFIED="1705852113469" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
632
+ <SUITE FILE_PATH="coverage/detector$detector.coverage" NAME="detector Coverage Results" MODIFIED="1714119714139" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
633
  <SUITE FILE_PATH="coverage/detector$evaluator.coverage" NAME="evaluator Coverage Results" MODIFIED="1706107083258" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
634
+ <SUITE FILE_PATH="coverage/detector$report.coverage" NAME="report Coverage Results" MODIFIED="1708530028219" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
635
  <SUITE FILE_PATH="coverage/detector$demo.coverage" NAME="demo Coverage Results" MODIFIED="1706108414052" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
 
636
  <SUITE FILE_PATH="coverage/detector$yolo_crack.coverage" NAME="yolo_crack Coverage Results" MODIFIED="1706950034213" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
 
637
  </component>
638
  </project>
main.py CHANGED
@@ -1,13 +1,60 @@
1
- from fastapi import FastAPI
 
 
 
2
 
3
  app = FastAPI()
4
 
 
 
5
 
6
  @app.get("/")
7
- async def root():
8
- return {"message": "Hello World"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
 
11
- @app.get("/hello/{name}")
12
- async def say_hello(name: str):
13
- return {"message": f"Hello {name}"}
 
 
 
 
 
 
 
 
 
 
1
+ import ultralytics.engine.results
2
+ from fastapi import FastAPI, UploadFile
3
+ from PIL import Image
4
+ from yolo_model import YoloModel
5
 
6
  app = FastAPI()
7
 
8
+ model = YoloModel("SHOU-ISD/fire-and-smoke", "yolov8n.pt")
9
+
10
 
11
  @app.get("/")
12
+ async def info():
13
+ return {
14
+ "model": f"Fire and Smoke Detection",
15
+ "version": "1.0",
16
+ }
17
+
18
+
19
+ @app.post("/predict")
20
+ async def predict(image: UploadFile):
21
+ if image.content_type not in ["image/jpeg", "image/png"]:
22
+ return {"error": "Invalid file type"}
23
+
24
+ im = Image.open(image.file)
25
+ res = model.detect(im)
26
+ return {
27
+ "list": [
28
+ detect_item(box, res_item.names)
29
+ for res_item in res
30
+ for box in res_item.boxes
31
+ ]
32
+ }
33
+
34
+
35
+ CXYWH = {
36
+ "cx": float,
37
+ "cy": float,
38
+ "w": float,
39
+ "h": float,
40
+ }
41
+
42
+ DetectItem = {
43
+ "category": str,
44
+ "bbox": CXYWH,
45
+ "score": float,
46
+ }
47
 
48
 
49
+ def detect_item(box: ultralytics.engine.results.Boxes, mapping: dict[int, str]) -> DetectItem:
50
+ cx, cy, w, h = box.xywhn.tolist()[0]
51
+ return {
52
+ "category": mapping[int(box.cls)],
53
+ "bbox": {
54
+ "cx": cx,
55
+ "cy": cy,
56
+ "w": w,
57
+ "h": h,
58
+ },
59
+ "score": float(box.conf),
60
+ }
report.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+
4
+ def render(kernel, epochs, train_datasets, valid_datasets,
5
+ val_images, accuracy, precision, recall):
6
+ return f"""
7
+ # Model Info
8
+ * Kernel: {kernel}
9
+ * Epochs: {epochs}
10
+ * TrainSet: {train_datasets}
11
+ * ValidSet: {valid_datasets}
12
+ # Metrics
13
+ * Accuracy: {accuracy}
14
+ * Precision: {precision}
15
+ * Recall: {recall}
16
+ # Confusion Matrix
17
+ ![confusion_matrix](./confusion_matrix.png)
18
+ # Valid Image Example
19
+ {render_list(val_images)}
20
+ """.strip()
21
+
22
+
23
+ def render_list(lst):
24
+ return "".join(render_list_iter(lst))
25
+
26
+
27
+ def render_list_iter(lst):
28
+ for item in lst:
29
+ yield f"""{render_item(item)}\n"""
30
+
31
+
32
+ def render_item(item: tuple[str, str]):
33
+ original, pred = item
34
+ return f"""
35
+ ### Original Image
36
+ ![original_image]({original})
37
+ ### Predicted Image
38
+ ![predicted_image]({pred})
39
+ """.strip()
40
+
41
+
42
+ def main(val_images: list[tuple[str, str]]):
43
+
44
+ return render(kernel='yolov5s', epochs=100, train_datasets='coco', valid_datasets='coco',
45
+ val_images=val_images,
46
+ accuracy=0.9, precision=0.8, recall=0.7)
47
+
48
+
49
+ if __name__ == '__main__':
50
+ out = 'datasets/coco8/images/report/report.md'
51
+ imgs = list(map(lambda x: (f"../val/{x}", f"../val/{x}"), os.listdir('datasets/coco8/images/val')))
52
+
53
+ with open(out, 'w') as f:
54
+ f.write(main(val_images=imgs))
requirements.txt CHANGED
@@ -131,3 +131,4 @@ websockets==12.0
131
  wheel==0.42.0
132
  widgetsnbextension==4.0.9
133
  zipp==3.17.0
 
 
131
  wheel==0.42.0
132
  widgetsnbextension==4.0.9
133
  zipp==3.17.0
134
+ python-multipart==0.0.9