TensorFlow:将自己训练好的模型迁移到电脑摄像头和外置海康摄像头上,并在视频中实时检测

本文主要是介绍TensorFlow:将自己训练好的模型迁移到电脑摄像头和外置海康摄像头上,并在视频中实时检测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

有了训练好的模型之后,可以将模型迁移到电脑或者手机上

电脑:

# -*- coding: utf-8 -*-
"""@author: Terry n
"""
# Imports
import numpy as np
import os
import sys
import tensorflow as tf
import cv2# if tf.__version__ < '1.4.0':
#     raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')os.chdir('D:\\object_detection_api\\models-master\\research\\object_detection')# Env setup
# This is needed to display the images.
# %matplotlib inline# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")# Object detection imports
from utils import label_map_utilfrom utils import visualization_utils as vis_util# Model preparation
# What model to download.
#MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'  # [30,21]  best
# MODEL_NAME = 'ssd_inception_v2_coco_2017_11_17'            #[42,24]
# MODEL_NAME = 'faster_rcnn_inception_v2_coco_2017_11_08'         #[58,28]
# MODEL_NAME = 'faster_rcnn_resnet50_coco_2017_11_08'     #[89,30]
# MODEL_NAME = 'faster_rcnn_resnet50_lowproposals_coco_2017_11_08'   #[64, ]
# MODEL_NAME = 'rfcn_resnet101_coco_2017_11_08'    #[106,32]
# MODEL_NAME = 'faster_rcnn_inception_resnet_v2_atrous_coco_2018_01_28'
# MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09'
MODEL_NAME = 'fod_detection'# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'# List of the strings that is used to add correct label for each box.
#PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
PATH_TO_LABELS = os.path.join('data', 'fod.pbtxt')#NUM_CLASSES = 90
NUM_CLASSES = 1
# Load a (frozen) Tensorflow model into memory.
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='')# Loading label map
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)# Helper code
def load_image_into_numpy_array(image):(im_width, im_height) = image.sizereturn np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)# Size, in inches, of the output images.
# IMAGE_SIZE = (12, 8)with detection_graph.as_default():with tf.Session(graph=detection_graph) as sess:# Definite input and output Tensors for detection_graphimage_tensor = detection_graph.get_tensor_by_name('image_tensor:0')# Each box represents a part of the image where a particular object was detected.detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')# Each score represent how level of confidence for each of the objects.# Score is shown on the result image, together with the class label.detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')num_detections = detection_graph.get_tensor_by_name('num_detections:0')# the video to be detected, eg, "test.mp4" herevidcap = cv2.VideoCapture(0)# Default resolutions of the frame are obtained.The default resolutions are system dependent.# We convert the resolutions from float to integer.frame_width = int(vidcap.get(3))frame_height = int(vidcap.get(4))while (True):ret, image = vidcap.read()if ret == True:# image_np = load_image_into_numpy_array(image)image_np = image# Expand dimensions since the model expects images to have shape: [1, None, None, 3]image_np_expanded = np.expand_dims(image_np, axis=0)# Actual detection.(boxes, scores, classes, num) = sess.run([detection_boxes, detection_scores, detection_classes, num_detections],feed_dict={image_tensor: image_np_expanded})# Visualization of the results of a detection.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,use_normalized_coordinates=True,line_thickness=8)print(scores)cv2.imshow("capture",image_np)if cv2.waitKey(1) & 0xFF == ord('q'):ret = False# Break the loopelse:break
vidcap.release()
cv2.destroyAllWindows()

注意:1,第十八行定位到你的object_detection文件夹下。

2,43行,47行定位到模型位置。50,51行相继修改。54行num_classes为1

3,注意,将此model_video的python文件定位到object_detection下,再在anaconda下运行。

海康摄像头:

model_video.py

# -*- coding: utf-8 -*-
"""@author: Terry n
"""
# Imports
import numpy as np
import os
import sys
import tensorflow as tf
import cv2# if tf.__version__ < '1.4.0':
#     raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')os.chdir('D:\\object_detection_api\\models-master\\research\\object_detection')# Env setup
# This is needed to display the images.
# %matplotlib inline# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")# Object detection imports
from utils import label_map_utilfrom utils import visualization_utils as vis_util# Model preparation
# What model to download.
#MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'  # [30,21]  best
# MODEL_NAME = 'ssd_inception_v2_coco_2017_11_17'            #[42,24]
# MODEL_NAME = 'faster_rcnn_inception_v2_coco_2017_11_08'         #[58,28]
# MODEL_NAME = 'faster_rcnn_resnet50_coco_2017_11_08'     #[89,30]
# MODEL_NAME = 'faster_rcnn_resnet50_lowproposals_coco_2017_11_08'   #[64, ]
# MODEL_NAME = 'rfcn_resnet101_coco_2017_11_08'    #[106,32]
# MODEL_NAME = 'faster_rcnn_inception_resnet_v2_atrous_coco_2018_01_28'
# MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09'
# MODEL_NAME = 'fod_detection'
MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'# List of the strings that is used to add correct label for each box.
# PATH_TO_LABELS = os.path.join('data', 'fod.pbtxt')
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')NUM_CLASSES = 90
# NUM_CLASSES = 1
# Load a (frozen) Tensorflow model into memory.
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='')# Loading label map
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)# Helper code
def load_image_into_numpy_array(image):(im_width, im_height) = image.sizereturn np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)# Size, in inches, of the output images.
# IMAGE_SIZE = (12, 8)with detection_graph.as_default():with tf.Session(graph=detection_graph) as sess:# Definite input and output Tensors for detection_graphimage_tensor = detection_graph.get_tensor_by_name('image_tensor:0')# Each box represents a part of the image where a particular object was detected.detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')# Each score represent how level of confidence for each of the objects.# Score is shown on the result image, together with the class label.detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')num_detections = detection_graph.get_tensor_by_name('num_detections:0')# the video to be detected, eg, "test.mp4" hereurl = 'rtsp://admin:ha515515@192.168.1.64:554/11'# vidcap = cv2.VideoCapture(0)# Default resolutions of the frame are obtained.The default resolutions are system dependent.# We convert the resolutions from float to integer.while (True):vidcap = cv2.VideoCapture(url)ret, image = vidcap.read()frame_width = int(vidcap.get(3))frame_height = int(vidcap.get(4))if ret == True:# image_np = load_image_into_numpy_array(image)image_np = image# Expand dimensions since the model expects images to have shape: [1, None, None, 3]image_np_expanded = np.expand_dims(image_np, axis=0)# Actual detection.(boxes, scores, classes, num) = sess.run([detection_boxes, detection_scores, detection_classes, num_detections],feed_dict={image_tensor: image_np_expanded})# Visualization of the results of a detection.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,use_normalized_coordinates=True,line_thickness=8)print(scores)cv2.imshow("capture",image_np)if cv2.waitKey(20) & 0xFF == ord('q'):ret = False# Break the loopelse:break
vidcap.release()
cv2.destroyAllWindows()

3,在视频中实时检测

video_detection.py

# By Terry_n
# https://space.bilibili.com/275177832
# 可以放在任何文件夹下运行(前提正确配置API[环境变量])
# 输出视频没有声音,pr可解决一切import numpy as np
import os
import sys
import tensorflow as tf
import cv2
import timefrom object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_utilstart = time.time()
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
cv2.setUseOptimized(True)  # 加速cv# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")# 可能要改的内容
######################################################
PATH_TO_CKPT = 'D:\\object_detection_api\\models-master\\research\\object_detection\\fod_detection\\fod_frozen_inference_graph.pb'  # 模型及标签地址PATH_TO_LABELS = 'D:\\object_detection_api\\models-master\\research\\object_detection\\data\\fod.pbtxt'video_PATH = "D:\\object_detection_api\\models-master\\research\\object_detection\\test_video\\cycling.mp4"  # 要检测的视频
out_PATH = "D:\\object_detection_api\\models-master\\research\\object_detection\\output_video\\out_cycling1.mp4"  # 输出地址NUM_CLASSES = 1  # 检测对象个数fourcc = cv2.VideoWriter_fourcc(*'DIVX')  # 编码器类型(可选)
# 编码器: DIVX , XVID , MJPG , X264 , WMV1 , WMV2####################################################### Load a (frozen) Tensorflow model into memory.
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='')# Loading label map
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)# 读取视频
video_cap = cv2.VideoCapture(video_PATH)
fps = int(video_cap.get(cv2.CAP_PROP_FPS))  # 帧率width = int(video_cap.get(3))  # 视频长,宽
hight = int(video_cap.get(4))videoWriter = cv2.VideoWriter(out_PATH, fourcc, fps, (width, hight))config = tf.ConfigProto()
config.gpu_options.allow_growth = True  # 减小显存占用
with detection_graph.as_default():with tf.Session(graph=detection_graph, config=config) as sess:# Definite input and output Tensors for detection_graphimage_tensor = detection_graph.get_tensor_by_name('image_tensor:0')# Each box represents a part of the image where a particular object was detected.detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')# Each score represent how level of confidence for each of the objects.# Score is shown on the result image, together with the class label.detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')num_detections = detection_graph.get_tensor_by_name('num_detections:0')num = 0while True:ret, frame = video_cap.read()if ret == False:  # 没检测到就跳出breaknum += 1print(num)  # 输出检测到第几帧了# print(num/fps) # 检测到第几秒了image_np = frameimage_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')# Actual detection.(boxes, scores, classes, num_detections) = sess.run([boxes, scores, classes, num_detections],feed_dict={image_tensor: image_np_expanded})# Visualization of the results of a detection.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,use_normalized_coordinates=True,line_thickness=4)# 写视频videoWriter.write(image_np)videoWriter.release()
end = time.time()
print("Execution Time: ", end - start)

 

这篇关于TensorFlow:将自己训练好的模型迁移到电脑摄像头和外置海康摄像头上,并在视频中实时检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/844817

相关文章

如何关闭 Mac 触发角功能或设置修饰键? mac电脑防止误触设置技巧

《如何关闭Mac触发角功能或设置修饰键?mac电脑防止误触设置技巧》从Windows换到iOS大半年来,触发角是我觉得值得吹爆的MacBook效率神器,成为一大说服理由,下面我们就来看看mac电... MAC 的「触发角」功能虽然提高了效率,但过于灵敏也让不少用户感到头疼。特别是在关键时刻,一不小心就可能触

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

Spring AI Alibaba接入大模型时的依赖问题小结

《SpringAIAlibaba接入大模型时的依赖问题小结》文章介绍了如何在pom.xml文件中配置SpringAIAlibaba依赖,并提供了一个示例pom.xml文件,同时,建议将Maven仓... 目录(一)pom.XML文件:(二)application.yml配置文件(一)pom.xml文件:首

Java如何获取视频文件的视频时长

《Java如何获取视频文件的视频时长》文章介绍了如何使用Java获取视频文件的视频时长,包括导入maven依赖和代码案例,同时,也讨论了在运行过程中遇到的SLF4J加载问题,并给出了解决方案... 目录Java获取视频文件的视频时长1、导入maven依赖2、代码案例3、SLF4J: Failed to lo