网站建设的意义和作用,wordpress网易云音乐插件,支付宝小程序开发者工具,WordPress唯美个人博客主题对象检测是一种计算机视觉技术#xff0c;它使软件系统能够从给定的图像或视频中检测、定位并跟踪物体。对象检测的一个特殊属性是它能识别对象的类别#xff08;如人、桌子、椅子等#xff09;并在给定图像中指出其具体位置坐标。这个位置通常通过在物体周围绘制一个边界框…对象检测是一种计算机视觉技术它使软件系统能够从给定的图像或视频中检测、定位并跟踪物体。对象检测的一个特殊属性是它能识别对象的类别如人、桌子、椅子等并在给定图像中指出其具体位置坐标。这个位置通常通过在物体周围绘制一个边界框来指出。边界框可能会也可能不会准确地定位物体的位置。在图像内定位物体的能力定义了用于检测的算法的性能。人脸检测就是对象检测的一个例子。
通常对象检测任务分为三个步骤
生成输入的小片段如下图所示。你可以看到大量的边界框覆盖了整个图像。对每个分割的矩形区域进行特征提取以预测矩形是否包含有效物体。将重叠的框合并成一个单一的边界矩形非极大值抑制。
TensorFlow是一个用于数值计算和大规模机器学习的开源库它简化了获取数据、训练模型、提供预测和完善未来结果的过程。TensorFlow集合了机器学习和深度学习模型与算法使用Python作为方便的前端并在优化的C中高效运行。
使用TensorFlow进行对象检测如上所述使用这个API不一定需要了解神经网络和机器学习的知识因为我们主要使用API中提供的文件。我们需要的只是一些Python知识和完成这个项目的热情。
按照以下步骤进行
第1步创建一个名为ObjectDetection的文件夹并用VS Code打开。
第2步通过在VS Code的终端输入以下命令从Github仓库下载Tensorflow API
git clone https://github.com/tensorflow/models第3步设置虚拟环境
python -m venv --system-site-packages .\venv激活环境
.\venv\Scripts\activate将pip版本升级到最新
python -m pip install --upgrade --ignore-installed第4步安装依赖项
安装并升级tensorflow
pip install tensorflow
pip install --upgrade tensorflow安装matplotlib
pip install pillow Cython lxml jupyter matplotlib导航到models文件夹中的research子文件夹。
cd \models\research\第5步现在我们需要下载Protocol BuffersProtobuf这是谷歌的一种语言中立、平台中立的扩展机制用于序列化结构化数据可以想象成XML但更小、更快、更简单。
在models文件夹的research子文件夹中提取从上述链接下载的zip的内容并访问bin文件夹复制那里的protoc.exe的路径。然后打开“编辑系统环境变量”并点击“环境变量”。 (i) 在系统变量下选择’path’并点击编辑。 (ii) 点击新建并粘贴’protoc.exe’的复制路径。 第6步然后在VS Code的终端运行这个命令
protoc object_detection/protos/*.proto --python_out.第7步在同一文件夹中创建一个名为detect.py的新Python文件并粘贴下面给出的代码
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
import pathlib
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
from IPython.display import display
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util# 当前目录不是models时改变当前工作目录
while models in pathlib.Path.cwd().parts:os.chdir(..)# 加载模型函数
def load_model(model_name):base_url http://download.tensorflow.org/models/object_detection/model_file model_name .tar.gzmodel_dir tf.keras.utils.get_file(fnamemodel_name, originbase_url model_file,untarTrue)model_dir pathlib.Path(model_dir)/saved_modelmodel tf.saved_model.load(str(model_dir))return model# 路径到标签文件
PATH_TO_LABELS models/research/object_detection/data/mscoco_label_map.pbtxt
# 创建类别索引
category_index label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_nameTrue)# 模型名称
model_name ssd_inception_v2_coco_2017_11_17
# 加载检测模型
detection_model load_model(model_name)# 为单个图像运行推理的函数
def run_inference_for_single_image(model, image):image np.asarray(image)# 输入需要是张量使用tf.convert_to_tensor进行转换。input_tensor tf.convert_to_tensor(image)# 模型期望图像的批量所以使用tf.newaxis添加一个轴。input_tensor input_tensor[tf.newaxis,...]# 运行推理model_fn model.signatures[serving_default]output_dict model_fn(input_tensor)# 所有输出都是批量张量。# 转换为numpy数组并取索引[0]来去除批量维度。# 我们只对前num_detections个检测感兴趣。num_detections int(output_dict.pop(num_detections))output_dict {key:value[0, :num_detections].numpy() for key,value in output_dict.items()}output_dict[num_detections] num_detections# detection_classes应该是整数。output_dict[detection_classes] output_dict[detection_classes].astype(np.int64)# 处理带有掩模的模型if detection_masks in output_dict:# 将边框掩模调整到图像大小。detection_masks_reframed utils_ops.reframe_box_masks_to_image_masks(output_dict[detection_masks], output_dict[detection_boxes],image.shape[0], image.shape[1]) detection_masks_reframed tf.cast(detection_masks_reframed 0.5,tf.uint8)output_dict[detection_masks_reframed] detection_masks_reframed.numpy()return output_dict# 展示推理的函数
def show_inference(model, frame):# 从摄像头获取画面并将其转换为数组image_np np.array(frame)# 实际检测。output_dict run_inference_for_single_image(model, image_np)# 对检测结果进行可视化。vis_util.visualize_boxes_and_labels_on_image_array(image_np,output_dict[detection_boxes],output_dict[detection_classes],output_dict[detection_scores],category_index,instance_masksoutput_dict.get(detection_masks_reframed, None),use_normalized_coordinatesTrue,line_thickness5)return(image_np)# 现在我们打开摄像头并开始检测物体
import cv2
video_capture cv2.VideoCapture(0)
while True:# 逐帧捕获re,frame video_capture.read()Imagenpshow_inference(detection_model, frame)cv2.imshow(object detection, cv2.resize(Imagenp, (800,600)))if cv2.waitKey(1) amp; 0xFF ord(q):break video_capture.release()
cv2.destroyAllWindows()第7步对于实时对象检测我们还需要一个额外的依赖库OpenCV。因此要安装OpenCV请在终端运行以下命令。
pip install opencv-python第8步现在我们已经准备就绪可以执行设置并进行对象检测了。执行’detect.py’文件。
python detect.py