在开发应用中,特别是视觉类应用中,很多时候需要输入本地视频或RTSP视频流,使用Python语言可以简单实现,注意这里的.mp4视频需要为H.264或H.265编码,如果不确定的话,建议用格式工厂转换一下编码。来来,直接看代码吧。这段代码来自社区例程中的头部姿势检测项目Python版本
# -*- coding: utf-8 -*-
import sys
import re
import cv2
from graph import *
import yolo3_resnet18_inference
import deepsort_inference
import client
import threading
import time
import datetime
import ChannelManager
# 获取命令输入中的视频地址或RTSP视频流地址
lenofUrl = len(sys.argv)
# The number of parameters is incorrect.
if lenofUrl <= 1:
print("[ERROR] Please input mp4/Rtsp URL")
sys.exit()
elif lenofUrl >= 3:
print("[ERROR] param input Error")
sys.exit()
# 正则匹配,找到.mp4视频或者RTSP视频流
URL = sys.argv[1]
# match Input parameter format
URL1 = re.match('rtsp://', URL)
URL2 = re.search('.mp4', URL)
# 判断是.mp4视频还是RTSP视频流
# Determine if it is a mp4 video based on matching rules
if URL1 is None:
if URL2 is None:
print("[ERROR] should input correct URL")
sys.exit()
else:
mp4_url = True
else:
mp4_url = False
# 以下代码为具体处理过程
# 实例化两个推理类
省略代码
clientsocket = client.PresenterSocketClient((presenter_ip, presenter_port), 5, None)
if clientsocket is None:
print('clientsocket is None')
sys.exit(1)
# Create a thread, bind a thread function.
# the thread function will block after establishing a connection
thread_1 = threading.Thread(target=clientsocket.start_connect)
# Set the thread as a background thread.
# After the main thread ends, the background thread will stop regardless of whether it has finished running
thread_1.setDaemon(True)
# Thread function execution, establish a connection
thread_1.start()
# Provide sufficient connection time for data transfer of the main thread
time.sleep(0.1)
# Instantiate Channel Manager
channel_manager = ChannelManager.ChannelManager()
# Get message data
data = channel_manager.OpenChannel()
# Send message data to presenter server
clientsocket.send_data(data)
cap = cv2.VideoCapture(URL)
ret, frame = cap.read()
print("视频是否打开成功:", ret)
# 开始计时, 从读取视频获得图片开始
starttime = time.time()
# According to the flag,Perform different processing methods
if mp4_url:
try:
while ret:
# Processing the detection results of a frame of pictures
ret = deepsort_inference.dowork(frame, clientsocket, channel_manager, yolo3_resnet18_app, deepsort_app)
if ret is None:
sys.exit(1)
# Loop through local video files
ret, frame = cap.read()
except Exception as e:
print("ERROR", e)
finally:
# Turn off the camera
cap.release()
else:
# 执行对RTSP视频流的处理
# Create a queue object for caching image data
rtsp_queue = client.Queue()
# Create a thread function, the thread reads a frame of image data from the queue
# does the inference and sends the result of the inference to presenter server
sub_thread = threading.Thread(target=deepsort_inference.sub_dowork, args=(rtsp_queue,clientsocket,channel_manager,face_app,head_pose))
# Set the thread as a background thread. After the main thread ends,
# the background thread stops regardless of whether it has finished running.
sub_thread.setDaemon(True)
# Start thread function
sub_thread.start()
try:
while ret:
# Put video frames into the queue
rtsp_queue.put(frame)
# Loop read video frame
ret, frame = cap.read()
except Exception as e:
print("ERROR", e)
finally:
# Turn off the camera
cap.release()
因为设备原因,暂时无法给出RTSP视频流的结果,详情可以参考官方社区例程 https://gitee.com/Atlas200DK/sample-headposeestimation-python ,在这个代码库,还有其他例程用到了RTSP视频流读取,欢迎大家查看,关注。