单路视频分析三种实现方式
1、截帧
通过截取图像帧的形式,获取检测图片,来通过图像实现指定时间内图像识别
1pic-extraction.py
import cv2
import os
import time
import gc
rtsp_url = "rtsp://admin:xysz2022@192.168.0.100:554/h264/1/main/av_stream"
save_path = r'/home/smoke/test-1205/'
interval = 5
frame_count = 0
if not os.path.exists(save_path):
os.makedirs(save_path)
start_time = time.time()
cap = cv2.VideoCapture(rtsp_url)
while True:
ret, frame = cap.read()
if time.time() - start_time > 30: # Check if 20 seconds have passed
# print("time:", time.time())
print("time.time() - start_time��", time.time() - start_time)
break
if ret:
timestamp = int(time.time())
if timestamp % interval == 0:
img_path = save_path + str(timestamp) + ".jpg"
cv2.imwrite(img_path, frame)
frame_count += 1
# print("frame_count:", frame_count)
# print("save:", img_path)
gc.collect()
if gc.get_count()[2] > 500:
gc.collect()
else:
break
cap.release()
1pic_run.sh
#!/bin/bash
echo "0117test"
source /root/miniconda3/bin/activate py38bjrec
echo "Successfully activated conda environment py38bjrec"
total_iterations=30
current_iteration=0
python_command1="python 1pic-extraction.py"
python_command2="yolo detect predict model=/home/smoke/generatrix/1st2/weights/best.pt source=./test-1205"
clear_folder() {
rm -rf ./test-1205/*
echo "deleted test-1205"
}
execute_and_clear() {
echo "start 1pic-extraction.py"
eval "$python_command1"
sleep 35
echo "start yolo"
eval "$python_command2"
echo "Python command2: $python_command2"
sleep 10
clear_folder
mkdir -p ./test-1205
}
while [ $current_iteration -lt $total_iterations ]; do
execute_and_clear
((current_iteration++))
echo "Waiting for next iteration..."
sleep 60
done
echo "Script has finished executing."
2、 截取视频段
2video-extraction.py
import os
import subprocess
import time
# ffmpeg命令
ffmpeg_command = f'ffmpeg -i "rtsp://admin:xysz2022@192.168.0.100:554/h264/1/main/av_stream" -c:v copy -c:a aac -strict experimental -b:a 128k -ar 44100 -f segment -segment_time 5 -reset_timestamps 1 -strftime 1 /home/smoke/test-1205/output_%Y%m%d_%H%M%S.mp4'
# 指定要运行的总秒数
max_duration_seconds = 30
process = subprocess.Popen(ffmpeg_command, shell=True)
# 开始计时
start_time = time.time()
while True:
# 检查是否达到最大运行时间
current_time = time.time()
if current_time - start_time >= max_duration_seconds:
print("已达到最大运行时间,将终止ffmpeg进程")
process.send_signal(subprocess.signal.SIGINT) # 发送中断信号(模拟Ctrl+C)
break
# 等待一小段时间再检查(例如每秒检查一次)
time.sleep(1)
# 确保进程退出
process.wait()
2video_run.sh
#!/bin/bash
echo "0117test"
source /root/miniconda3/bin/activate py38bjrec
echo "Successfully activated conda environment py38bjrec"
total_iterations=30
current_iteration=0
python_command1="python 2video-extraction.py"
python_command2="yolo detect predict model=/home/smoke/generatrix/1st2/weights/best.pt source=./test-1205 save=True save_crop=True augment=True"
clear_folder() {
rm -rf ./test-1205/*
echo "deleted test-1205"
}
execute_and_clear() {
echo "start 2video-extraction.py"
eval "$python_command1"
sleep 10
echo "start yolo"
eval "$python_command2"
echo "Python command2: $python_command2"
sleep 30
clear_folder
mkdir -p ./test-1205
}
while [ $current_iteration -lt $total_iterations ]; do
execute_and_clear
((current_iteration++))
echo "Waiting for next iteration..."
sleep 40
done
echo "Script has finished executing."
3、接入rtsp全部识别
#!/bin/bash
# 检查并确保在指定目录下运行
if [[ $(pwd) != "/home/smoke" ]]; then
echo "Changing directory to /home/smoke"
cd /home/smoke || exit 1 # 如果切换失败,则退出脚本
fi
echo "0122test"
source /root/miniconda3/bin/activate py38bjrec
echo "Successfully activated conda environment py38bjrec"
total_iterations=30
current_iteration=0
python_command="yolo detect predict model=/home/smoke/generatrix/1st2/weights/best.pt source=rtsp://admin:xysz2022@192.168.0.100:554/h264/1/main/av_stream save=True save_crop=True augment=True"
clear_folder() {
rm -rf /root/runs/detect/*
echo "deleted runs/detect"
}
execute_and_clear() {
start_time=$(date +%s)
# 使用子shell来执行命令以获取PID
( $python_command ) &
python_pid=$!
echo "Current PID: $python_pid"
while true; do
current_time=$(date +%s)
time_diff=$((current_time - start_time))
if [[ $time_diff -ge 120 ]]; then # 检查是否已过去2分钟(120秒)
echo "The process has been running for 2 minutes. Terminating..."
# 尝试发送SIGTERM信号以正常关闭进程
kill -TERM $python_pid
sleep 1 # 给进程一些时间响应SIGTERM
# 如果进程仍然存在,则强制结束
if ps -p $python_pid > /dev/null; then
kill -KILL $python_pid
sleep 1 # 确保进程已被终止
fi
break
fi
sleep 1 # 每秒检查一次
done
clear_folder
mkdir -p /root/runs/detect/
}
while [ $current_iteration -lt $total_iterations ]; do
((current_iteration++))
echo "..........current iteration....$current_iteration..................................."
execute_and_clear
echo "..........current iteration....$current_iteration..................................."
echo "Waiting for next iteration......................................................"
sleep 20
done
echo "Script has finished executing."