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
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Android学习——利用ffmpeg+nginx+Ijkplayer在自己的电脑和手机上搭建rtmp直播上
- TensorFlow框架做实时人脸识别小项目
- 使用ffmpeg-1.0内置RTMP协议实时解码H264视频流
- 直播系列1-ffmpeg捕捉摄像头发送rtmp
- BP神经网络之手写数据识别(python-tensorflow)
- FFmpeg实现监控摄像头的RTSP协议转RTMP协议直播
- Python(TensorFlow框架)实现手写数字识别系统
- 基于Python的Face_recognition来实现实时人脸识别
- python tensorflow基于cnn实现手写数字识别
- Python实战 | TensorFlow之softmax的实现——手写数字识别
- python3利用dlib实现摄像头实时人脸识别
- Python(TensorFlow框架)实现手写数字识别系统的方法
- 新 CentOS 6.5 6.9 环境下搭建直播模拟测试环境 ffmpeg+nginx=rtmp (傻瓜式环境搭建)
- Python 3 利用 Dlib 实现摄像头实时人脸识别
- Python读取实时数据流示例
- 深度学习TensorFlow:Object_Detection_API图像视频物体识别API在Windows7上的配置
- python 读取视频,处理后,实时计算帧数fps的方法
- TensorFlow实战,BelgiumTS交通数识别(一)(读取数据和显示图像)
- python:读取视频,处理后,实时计算帧数fps
- TensorFlow框架做实时人脸识别小项目