您的位置:首页 > 编程语言 > Python开发

python3+tensorflow实时读取rtmp的流+object_detection识别+ffmpeg推流直播

2020-02-01 20:19 405 查看

1.执行环境

win7(64) + GeForce940M 显卡+ cuda:10.0.130 + python3.7 + object_detection

2.下载Nginx

nginx 1.7.11.3 Gryphon下载连接
解压,在conf新增配置文件 。复制nginx-win.conf文件改名为nginx-win-rtmp.conf,编辑nginx-win-rtmp.conf新增rtmp的配置

rtmp {
server {
listen 8080;
chunk_size 4000;
application live {
live on;

# record first 1K of stream
record all;
record_path /tmp/av;
record_max_size 1K;

# append current timestamp to each flv
record_unique on;

# publish only from localhost
allow publish 127.0.0.1;
deny publish all;

#allow play all;
}
}
}

启动

nginx.exe -c conf\nginx-win-rtmp.conf

3.安装ffmpeg

下载地址为ffmpeg
配置好环境变量

4. 运行代码

import subprocess

import cv2
import numpy as np
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util

PATH_TO_CKPT = r"E:\AI\frozen_inference_graph.pb"
PATH_TO_LABELS = r"E:\AI\mscoco_label_map.pbtxt"

NUM_CLASSES = 90

label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(
label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
sess = tf.Session(graph=detection_graph)

rtsp = "rtmp://58.200.131.2:1935/livetv/gxtv"

cap = cv2.VideoCapture(rtsp)

rtmpUrl = 'rtmp://localhost:8080/live/home'
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
sizeStr = str(size[0]) + 'x' + str(size[1])
fps = cap.get(cv2.CAP_PROP_FPS)
command = ['ffmpeg',
'-y',
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', sizeStr,
'-r', str(fps),
'-i', '-',
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-preset', 'ultrafast',
'-f', 'flv',
rtmpUrl]
pipe = subprocess.Popen(command, stdin=subprocess.PIPE, shell=False)

def detect(image, sess, detection_graph):
image_np = np.array(image).astype(np.uint8)
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')

(boxes, scores, classes, num_detections) = sess.run([boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})

vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
min_score_thresh=0.15,
use_normalized_coordinates=True,
line_thickness=4)
return image_np
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(
label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
sess = tf.Session(graph=detection_graph)

while cap.isOpened():
success, frame = cap.read()
if success:
frame = detect(frame, sess, detection_graph)
pipe.stdin.write(frame.tostring())
cv2.imshow('object detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
  • 点赞
  • 收藏
  • 分享
  • 文章举报
代号0和1 发布了5 篇原创文章 · 获赞 0 · 访问量 381 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: