wordpress+站群插件,网站定制怎么选择,wordpress 文章添加字段,网站的微信推广怎么做图像分割与人脸识别 众所周知图像是由若干有意义的像素组成的#xff0c;图像分割作为计算机视觉的基础#xff0c;对具有现有目标和较精确边界的图像进行分割#xff0c;实现在图像像素级别上的分类任务。图像分割可分为语义分割和实例分割两类#xff0c;区别如下#x…图像分割与人脸识别 众所周知图像是由若干有意义的像素组成的图像分割作为计算机视觉的基础对具有现有目标和较精确边界的图像进行分割实现在图像像素级别上的分类任务。图像分割可分为语义分割和实例分割两类区别如下 语义分割将图像中每个像素赋予一个类别标签用不同的颜色来表示。例如能够区分人脸与头发、能够区分人与动物等 实例分割无需对每个像素进行标记只需要找到感兴趣物体的边缘轮廓。 图像分割通常应用如下所示 专业检测应用于专业场景的图像分析比如在卫星图像中识别建筑、道路、森林或在医学图像中定位病灶、测量面积等 智能交通识别道路信息包括车道标记、交通标志等。 我们使用pixellib库来进行图像分割。
import timefrom pixellib.instance import instance_segmentation
from pixellib.semantic import semantic_segmentationdef semantic_seg(inputfile, outputfile):对图像进行语义分割(semantic segmentation)Args:inputfile (string): 输入文件outputfile (string): 输出文件segment_image semantic_segmentation()segment_image.load_pascalvoc_model(rD:\test\deeplabv3_xception_tf_dim_ordering_tf_kernels.h5)start time.time()segment_image.segmentAsPascalvoc(inputfile, output_image_nameoutputfile,overlay True)end time.time()print(fInference Time: {end-start:.2f}seconds)def instance_seg(inputfile, outputfile):对图像进行实体分割Args:inputfile (string): 输入文件outputfile (string): 输出文件segment_image instance_segmentation()segment_image.load_model(rD:\test\mask_rcnn_coco.h5)start time.time()segment_image.segmentImage(inputfile, output_image_nameoutputfile,show_bboxes True)end time.time()print(fInference Time: {end-start:.2f}seconds)def test_semantic():targetimage rd:\test\horseandman.pngoutimage rd:\test\semantic.pngsemantic_seg(targetimage,outimage)def test_instance():targetimage rd:\test\food.jpgoutputfile rd:\test\box.pnginstance_seg(targetimage,outputfile)test_semantic()
test_instance()代码中test_semantic函数进行语义分割的演示。其中horseandman.png如下图所示是人、马、狗组合在一起的风景图人类是可以理解的。 经过人工智能训练后的代码进行语义分割后形成的图像如下所示。 上图表明计算机也能够理解。因为输出结果中不同物体的颜色表明了模型识别出的类型。颜色示意如下图所示。 其中棕色代表人、鲜红代表马、紫色代表狗形成了非常准确的判断。此外函数test_instance使用模型能够对目标进行识别并且使用矩形进行标注。下图是有名的互联网大佬聚餐图。 使用上述代码进行实体分割后的效果如下图所示。 效果还差强人意勉强可用。在上面的代码中我们加载了在load_pascalvoc_model上训练的用于分割对象的xception模型以及Mask RCNN模型。由于以上模型较大建议下载后本地化保存与部署。
在实际应用中还存在着人脸识别的需求。以下代码借助第三方库face_recognition轻松实现了人脸识别的基本能力。
import face_recognition
import cv2targetimage rd:\test\food.jpg# 加载待识别图片
image face_recognition.load_image_file(targetimage)face_location face_recognition.face_locations(image,number_of_times_to_upsample5, modelhog)for location in face_location:top, right, bottom, left locationprint(已检测到人脸部位像素区域为:top:{}, right:{}. bottom:{}, left:{}.format(top, right, bottom, left))start (left, top)end (right, bottom)cv2.rectangle(image, start, end, (0, 0, 255), thickness2)cv2.imshow(window, image)
cv2.waitKey(0)
依然使用前面的大佬聚餐图通过上述代码可以很轻松地完成人脸识别效果如下所示。 在face_locations函数中可以选择hog与cnn两种模型。hog模型更快cnn更精确。此外number_of_times_to_upsample缺省为1如果识别时效果不好增加这个值。最后将第三方库的安装过程列举如下。
pip install tensorflow2.2.0
pip install pixellib
pip install face_recognitiontensorflow推荐安装2.2.0版本否则上述代码可能跑不下来。