单路视频分析三种实现方式

本文介绍了三种实现单路视频分析的方法:通过截取图像帧进行图像识别、使用ffmpeg截取视频片段和直接接入RTSP流进行实时识别,同时提到使用Python脚本配合Yolo模型进行对象检测。
摘要由CSDN通过智能技术生成

单路视频分析三种实现方式

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."



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值