1.初始化仓库\n2.添加同时推流mp4文件脚本\n3.添加批量添加摄像头
This commit is contained in:
commit
2412ebec3a
51
001测试模型.py
Normal file
51
001测试模型.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
from ultralytics import YOLO
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
def predict_and_visualize(model_path, image_path, output_path):
|
||||||
|
# 加载训练好的模型
|
||||||
|
model = YOLO(model_path)
|
||||||
|
|
||||||
|
# 进行预测
|
||||||
|
results = model.predict(source=image_path, conf=0.25) # conf设置置信度阈值
|
||||||
|
|
||||||
|
# 读取原始图片
|
||||||
|
img = cv2.imread(image_path)
|
||||||
|
|
||||||
|
# 获取预测结果
|
||||||
|
boxes = results[0].boxes
|
||||||
|
class_names = model.names # 获取类别名称字典
|
||||||
|
|
||||||
|
# 遍历每个检测结果
|
||||||
|
for box in boxes:
|
||||||
|
# 获取坐标和类别信息
|
||||||
|
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
||||||
|
cls_id = int(box.cls[0].item())
|
||||||
|
conf = box.conf[0].item()
|
||||||
|
|
||||||
|
# 绘制边界框
|
||||||
|
color = (0, 255, 0) # 绿色边框
|
||||||
|
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
|
||||||
|
|
||||||
|
# 准备显示文本
|
||||||
|
label = f"{class_names[cls_id]}: {conf:.2f}"
|
||||||
|
|
||||||
|
# 计算文本位置
|
||||||
|
(w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1)
|
||||||
|
|
||||||
|
# 绘制文本背景
|
||||||
|
cv2.rectangle(img, (x1, y1 - h - 5), (x1 + w, y1), color, -1)
|
||||||
|
# 绘制文本
|
||||||
|
cv2.putText(img, label, (x1, y1 - 5),
|
||||||
|
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 1)
|
||||||
|
|
||||||
|
# 保存结果
|
||||||
|
cv2.imwrite(output_path, img)
|
||||||
|
print(f"结果已保存至: {output_path}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 使用示例
|
||||||
|
model_path = "models/安全帽检测模型/yolo11n_safehat.pt" # 替换为你的模型路径
|
||||||
|
image_path = "images/mp4_509.jpg" # 替换为你的图片路径
|
||||||
|
output_path = "output/mp4_509.jpg" # 输出文件名
|
||||||
|
|
||||||
|
predict_and_visualize(model_path, image_path, output_path)
|
||||||
63
002批量测试模型图片.py
Normal file
63
002批量测试模型图片.py
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
from ultralytics import YOLO
|
||||||
|
import cv2
|
||||||
|
import os
|
||||||
|
|
||||||
|
def predict_and_visualize(model_path, image_path, output_path):
|
||||||
|
# 加载训练好的模型
|
||||||
|
model = YOLO(model_path)
|
||||||
|
|
||||||
|
all_image_file = os.listdir(image_path)
|
||||||
|
all_image_path = [os.path.join(image_path, t) for t in all_image_file]
|
||||||
|
|
||||||
|
for i in range(len(all_image_path)):
|
||||||
|
|
||||||
|
# 进行预测
|
||||||
|
results = model.predict(source=all_image_path[i], conf=0.25) # conf设置置信度阈值
|
||||||
|
|
||||||
|
# 读取原始图片
|
||||||
|
img = cv2.imread(all_image_path[i])
|
||||||
|
|
||||||
|
# 获取预测结果
|
||||||
|
boxes = results[0].boxes
|
||||||
|
class_names = model.names # 获取类别名称字典
|
||||||
|
|
||||||
|
|
||||||
|
# {0: 'head', 1: 'safehat'}
|
||||||
|
print(class_names)
|
||||||
|
|
||||||
|
# break
|
||||||
|
|
||||||
|
# 遍历每个检测结果
|
||||||
|
for box in boxes:
|
||||||
|
# 获取坐标和类别信息
|
||||||
|
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
||||||
|
cls_id = int(box.cls[0].item())
|
||||||
|
conf = box.conf[0].item()
|
||||||
|
|
||||||
|
# 绘制边界框
|
||||||
|
color = (0, 255, 0) # 绿色边框
|
||||||
|
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
|
||||||
|
|
||||||
|
# 准备显示文本
|
||||||
|
label = f"{class_names[cls_id]}: {conf:.2f}"
|
||||||
|
|
||||||
|
# 计算文本位置
|
||||||
|
(w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1)
|
||||||
|
|
||||||
|
# 绘制文本背景
|
||||||
|
cv2.rectangle(img, (x1, y1 - h - 5), (x1 + w, y1), color, -1)
|
||||||
|
# 绘制文本
|
||||||
|
cv2.putText(img, label, (x1, y1 - 5),
|
||||||
|
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 1)
|
||||||
|
|
||||||
|
# 保存结果
|
||||||
|
cv2.imwrite(output_path+f"{i}.jpg", img)
|
||||||
|
print(f"结果已保存至: {output_path}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 使用示例
|
||||||
|
model_path = "/home/admin-root/haotian/xcms/models/安全帽检测模型OpenVINO/best_s.xml" # 替换为你的模型路径
|
||||||
|
image_path = "/home/admin-root/haotian/锻8/tensorrtx/yolov8/images" # 替换为你的图片路径
|
||||||
|
output_path = "/home/admin-root/haotian/xcms/output/" # 输出文件名
|
||||||
|
|
||||||
|
predict_and_visualize(model_path, image_path, output_path)
|
||||||
35
003推流指定文件夹下所有mp4文件.sh
Normal file
35
003推流指定文件夹下所有mp4文件.sh
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
VIDEO_DIR="/home/admin-root/haotian/康达瑞贝斯机器狗/data_video" # 你的 .mp4 文件目录
|
||||||
|
RTSP_SERVER="rtsp://10.0.0.17:8554/camera_test"
|
||||||
|
|
||||||
|
|
||||||
|
# 捕获 SIGINT(Ctrl+C)和 SIGTERM,触发 exit(进而触发 EXIT trap)
|
||||||
|
trap "exit" INT TERM
|
||||||
|
|
||||||
|
# EXIT 触发时,终止当前进程组(包括所有子进程)
|
||||||
|
trap "kill 0" EXIT
|
||||||
|
|
||||||
|
for filepath in "$VIDEO_DIR"/*.mp4; do
|
||||||
|
[ -e "$filepath" ] || continue
|
||||||
|
filename=$(basename "$filepath" .mp4)
|
||||||
|
RTSP_URL="${RTSP_SERVER}/${filename}"
|
||||||
|
|
||||||
|
(
|
||||||
|
while true; do
|
||||||
|
echo "$(date): 推流 -> $RTSP_URL"
|
||||||
|
ffmpeg -re -stream_loop -1 -i "$filepath" \
|
||||||
|
-vf "scale=1920:1080" \
|
||||||
|
-c:v libx264 -preset veryfast -tune zerolatency \
|
||||||
|
-c:a aac -b:a 128k \
|
||||||
|
-muxdelay 0 -muxpreload 0 \
|
||||||
|
-f rtsp -rtsp_transport tcp \
|
||||||
|
"$RTSP_URL"
|
||||||
|
echo "$(date): 推流中断,5 秒后重启 -> $RTSP_URL"
|
||||||
|
sleep 5
|
||||||
|
done
|
||||||
|
) &
|
||||||
|
done
|
||||||
|
|
||||||
|
# 等待所有后台任务
|
||||||
|
wait
|
||||||
61
004批量添加摄像头.py
Normal file
61
004批量添加摄像头.py
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
import requests
|
||||||
|
import uuid
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
"""
|
||||||
|
批量向xcms中添加添加摄像头
|
||||||
|
"""
|
||||||
|
|
||||||
|
RSTP_URL = "rtsp://10.0.0.17:8554/camera_test/"
|
||||||
|
ADD_CAMERA_URL = "http://10.0.0.81:9001/open/addStream"
|
||||||
|
def add_camera(rtsp_path, nick_name, code ,method="POST"):
|
||||||
|
|
||||||
|
# code = uuid.uuid4().hex
|
||||||
|
|
||||||
|
# print(code)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"code": f"{code}",# 必填,摄像头编号(不能与已有摄像头编号重复)
|
||||||
|
"nickname": f"{nick_name}",#必填,名称
|
||||||
|
"pull_stream_type": 1, # 必填,数值类型,1:RTSP,2:RTMP,3:FVL,4:HLS,21:GB28181
|
||||||
|
"pull_stream_url": f"{rtsp_path}", # 必填,直播流地址
|
||||||
|
"pull_stream_ip":"10.0.0.17", # 必填,摄像头IP
|
||||||
|
"pull_stream_port":8554, # 必填,数值类型,摄像头拉流服务对用的端口
|
||||||
|
# "camera_name":"", # 非必填,摄像头名称
|
||||||
|
# "camera_manufacturer":"", # 非必填,摄像头厂商
|
||||||
|
# "camera_device_id":"group1",# (v4.638新增)非必填,摄像头所属分组编号,默认不填写时等于code
|
||||||
|
# "remark":"", # 非必填,备注
|
||||||
|
# "onvif_username":"", # 非必填,探测ONVIF username
|
||||||
|
# "onvif_password":"", # 非必填,探测ONVIF password
|
||||||
|
"is_audio":0 # 必填,数值类型,0:静音 1:原始音频
|
||||||
|
}
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Safe":"aqxY9ps21fyhyKNRyYpGvJCTp1JBeGOM"
|
||||||
|
}
|
||||||
|
|
||||||
|
response = requests.post(ADD_CAMERA_URL, json=data, headers=headers)
|
||||||
|
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
response_data = response.json()
|
||||||
|
|
||||||
|
print(response_data["code"])
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
video_path = "/home/admin-root/haotian/康达瑞贝斯机器狗/data_video"
|
||||||
|
video_name_list = os.listdir(video_path)
|
||||||
|
|
||||||
|
for i, video_name in enumerate(video_name_list):
|
||||||
|
video_name = video_name.split(".")[0]
|
||||||
|
add_camera(RSTP_URL+video_name, video_name, f"t{i}")
|
||||||
|
time.sleep(0.2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
15
docs/模型测试结果.md
Normal file
15
docs/模型测试结果.md
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
## 模型可用性
|
||||||
|
|
||||||
|
- [X] 人脸检测模型
|
||||||
|
- [X] 厨师帽检测模型
|
||||||
|
- [ ] 反光衣检测模型
|
||||||
|
AttributeError: Can't get attribute 'C3k2' on <module 'ultralytics.nn.modules.block' from '/home/admin-root/miniconda3/envs/trt/lib/python3.9/site-packages/ultralytics/nn/modules/block.py'>
|
||||||
|
- [X] 安全帽检测模型
|
||||||
|
报错同上.
|
||||||
|
- [X] 打架检测模型
|
||||||
|
- [X] 抽烟检测模型
|
||||||
|
- [X] 持械检测模型
|
||||||
|
- [X] 火焰烟火检测模型
|
||||||
|
- [X] 烟尘检测模型
|
||||||
|
- [X] 睡岗检测模型
|
||||||
|
|
||||||
BIN
models/安全帽检测模型OpenVINO/best_s.bin
Normal file
BIN
models/安全帽检测模型OpenVINO/best_s.bin
Normal file
Binary file not shown.
BIN
models/安全帽检测模型OpenVINO/best_s.xml
Normal file
BIN
models/安全帽检测模型OpenVINO/best_s.xml
Normal file
Binary file not shown.
16
models/安全帽检测模型OpenVINO/test.py
Normal file
16
models/安全帽检测模型OpenVINO/test.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from ultralytics import YOLO
|
||||||
|
import openvino as ov
|
||||||
|
'''
|
||||||
|
加载文件失败
|
||||||
|
'''
|
||||||
|
# 加载 OpenVINO 模型
|
||||||
|
core = ov.Core()
|
||||||
|
compiled_model = core.compile_model("best_s.xml", "CPU")
|
||||||
|
|
||||||
|
# 通过 YOLO 包装推理
|
||||||
|
model = YOLO("best_s.xml") # 使用 YOLO 接口
|
||||||
|
model.model = compiled_model # 替换为 OpenVINO 模型
|
||||||
|
|
||||||
|
|
||||||
|
# 执行推理
|
||||||
|
results = model.predict("image.jpg")
|
||||||
Loading…
Reference in New Issue
Block a user