diff --git a/.github/workflows/python-package-conda.yml b/.github/workflows/python-package-conda.yml deleted file mode 100644 index 2e51f0c..0000000 --- a/.github/workflows/python-package-conda.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: Python Package using Conda - -on: [push] - -jobs: - build-linux: - runs-on: ubuntu-latest - strategy: - max-parallel: 5 - - steps: - - uses: actions/checkout@v4 - - name: Set up Python 3.8 - uses: actions/setup-python@v5 - with: - python-version: '3.8.12' - - name: Add conda to system path - run: | - # $CONDA is an environment variable pointing to the root of the miniconda directory - echo $CONDA/bin >> $GITHUB_PATH - - name: upgrade pip to avoid the AttributeError - run: | - python3 -m pip install --upgrade pip - - name: Install dependencies - run: | - pip install opencv-python numpy==1.23.0 onnx_simplifier==0.3.10 onnxruntime==1.16.0 pathlib argparse - - name: Lint with flake8 - run: | - conda install flake8 - # stop the build if there are Python syntax errors or undefined names - flake8 --count --select=E9,F63,F7,F82 --show-source --statistics ./detect.py - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics ./detect.py - - name: Test with pytest - run: | - conda install pytest - pytest ./detect.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0ce7a27 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,20 @@ +{ + "MicroPython.executeButton": [ + { + "text": "▶", + "tooltip": "运行", + "alignment": "left", + "command": "extension.executeFile", + "priority": 3.5 + } + ], + "MicroPython.syncButton": [ + { + "text": "$(sync)", + "tooltip": "同步", + "alignment": "left", + "command": "extension.execute", + "priority": 4 + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 5b753cc..b71af56 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,9 @@ ## 先把下面这些库装好,python不是3.8版本的话会有奇妙的bug **The dependency onnxruntime might be hard to be installed on Linux platforms.** -**Linux装onnxruntime是真的坐牢,慎用** +**Please install onnxruntime in conda virtual env.** + +**Linux装onnxruntime是真的坐牢,慎用,或者转用conda环境装** * Python(3.8 for recommended version) and pip * opencv-python * numpy==1.23.0 @@ -86,6 +88,9 @@ * Check the detect result through the new window created by opencv * 通过opencv打开的视频流窗口查看推理结果 +* The detect result will be saved in resultsave/video folder. +* 推理结果会被保留在resultsave/video目录下 + * Click "q" to end the detect process. * 按“q”键终止推理进程 @@ -113,13 +118,16 @@ * Check the detect result through the new window created by opencv * 通过opencv打开的视频流窗口查看推理结果 +* The detect result will be saved in resultsave/video folder. +* 推理结果会被保留在resultsave/video目录下 + * Click "q" to end the detect process. * 按“q”键终止推理进程 # Todo待完成 -* Save the VideoStream detection result as a file -* 以文件形式保存视频流推理结果 +* Save the VideoStream detection result as a file ✔ (done!) +* 以文件形式保存视频流推理结果 ✔(已完成!) * Write an UI to adapt most usage * 写一个能适用于大多数场景需求的UI * Multi CPU core Inferences diff --git a/detect.py b/detect.py index 9866121..10f86f4 100644 --- a/detect.py +++ b/detect.py @@ -259,13 +259,31 @@ def run(img): assert os.path.exists(opt.videofile), "视频文件不存在" cap = cv2.VideoCapture(opt.videofile) # 用opencv打开视频文件,转换为视频流 print("Opened VideoStream.") + print(f"Video width:{int(cap.get(3))} Video height:{int(cap.get(4))}") + # 查看结果文件夹最后的视频序号并生成此次结果视频文件 + folderChecker = os.listdir("resultsave/video/") + if len(folderChecker) == 0: + savePath = f"resultsave/video/0.mp4" + else: + savePath = f"resultsave/video/{int(sorted(folderChecker, reverse=True)[0].split(".")[0])+1}.mp4" + # 帧率调整 + fps = 25.0 + # 创建结果视频写入器以保存文件 + vw = cv2.VideoWriter( + savePath, + cv2.VideoWriter.fourcc(*"mp4v"), + fps, + (int(cap.get(3)), int(cap.get(4))), + ) while cap.isOpened(): # 按下q键退出推理 if cv2.waitKey(1) & 0xFF == ord("q"): + vw.release() break ret, img = cap.read() # 获取视频的开启状态和每一帧图片 if ret: run(img) + vw.write(resframe) cv2.imshow("Videostream", resframe) else: cv2.waitKey(1) # 防止推理速度不够快,有空白帧 @@ -280,13 +298,31 @@ def run(img): # 摄像头实时流源 cap = cv2.VideoCapture(opt.source) # 用opencv打开摄像头,0为默认摄像头 print("Opened Camera.") + print(f"Video width:{int(cap.get(3))} Video height:{int(cap.get(4))}") + # 查看结果文件夹最后的视频序号并生成此次结果视频文件 + folderChecker = os.listdir("resultsave/video/") + if len(folderChecker) == 0: + savePath = f"resultsave/video/0.mp4" + else: + savePath = f"resultsave/video/{int(sorted(folderChecker, reverse=True)[0].split(".")[0])+1}.mp4" + # 帧率调整 + fps = 25.0 + # 创建结果视频写入器以保存文件 + vw = cv2.VideoWriter( + savePath, + cv2.VideoWriter.fourcc(*"mp4v"), + fps, + (int(cap.get(3)), int(cap.get(4))), + ) while True: # 按下q键退出推理 if cv2.waitKey(1) & 0xFF == ord("q"): + vw.release() break ret, img = cap.read() # 获取视频的开启状态和每一帧图片 if ret: run(img) + vw.write(resframe) cv2.imshow("CameraStream", resframe) else: cv2.waitKey(1) # 防止推理速度不够快,有空白帧 @@ -294,4 +330,6 @@ def run(img): if "cap" in globals(): if cap.isOpened: cap.release() + if "vw" in globals(): + vw.release() cv2.destroyAllWindows()