Skip to content

Commit

Permalink
save video detection result as file
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscarcncn committed Feb 3, 2025
1 parent aff7ae3 commit 99fff49
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 40 deletions.
37 changes: 0 additions & 37 deletions .github/workflows/python-package-conda.yml

This file was deleted.

20 changes: 20 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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”键终止推理进程

Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) # 防止推理速度不够快,有空白帧
Expand All @@ -280,18 +298,38 @@ 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) # 防止推理速度不够快,有空白帧

if "cap" in globals():
if cap.isOpened:
cap.release()
if "vw" in globals():
vw.release()
cv2.destroyAllWindows()

0 comments on commit 99fff49

Please sign in to comment.