通过MediaPipe+MiDaS实现人脸单目测距

      MediaPipe:是Google开发的适用于直播和流媒体的开源、跨平台、可定制的机器学习解决方案。code地址:https://github.com/google/mediapipe ,最新发布版本v0.10.11,license为Apache-2.0。MediaPipe Solutions提供了一套库和工具,供你在应用程序中快速应用人工智能(AI)和机器学习(ML)技术,包括:目标检测、图像分类、图像分割、人脸检测等。

      MiDaS:开源的单目深度估计实现,地址:https://github.com/isl-org/MiDaS ,license为MIT。

      通过Anaconda搭建开发环境,依次执行如下命令:

conda create -n MediaPipe python=3.9
conda activate MediaPipe
pip install mediapipe
pip install requests
git clone https://github.com/fengbingchun/NN_Test
cd NN_Test/demo/Python

      以下为测试代码:

import sys
import os
import cv2
import requests
import mediapipe as mp

def download_onnx_model(url, model_name):
	if os.path.exists(model_name) and os.path.isfile(model_name):
		return

	response = requests.get(url, stream=True)
	if response.status_code == 200:
		print("Downloading ... ...")
		with open(model_name, "wb") as f:
			for chunk in response.iter_content(chunk_size=8192):  
				if chunk:  
					f.write(chunk)
		print("file downloaded successfully:", model_name)
	else:
		raise Exception("Error: unable to download file: {}".format(model_name))

def get_images(dir, img_suffix):
	#print("dir:{}, img suffix:{}".format(dir, img_suffix))
	imgs = []

	for img in os.listdir(dir):
		if img.endswith(img_suffix):
			imgs.append(dir+"/"+img)

	return imgs

def depth_to_distance(depth) -> float:
    return -1.5 * depth + 2

def calc_distance(imgs, model_name):
	for img in imgs:
		bgr = cv2.imread(img, 1)
		if bgr is None:
			print("Error: image {} can't be read".format(bgr))
			continue

		rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
		height, width, channels = rgb.shape

		# define mediapipe face detection model
		face_detection_model = mp.solutions.face_detection.FaceDetection(min_detection_confidence=0.5, model_selection=0)

		# load monocular depth estimation model
		mono_model = cv2.dnn.readNet(model_name)

		# detect faces
		face_results = face_detection_model.process(rgb)
		if face_results.detections:
			for face in face_results.detections:
				# draw bounding boxes around the detected faces
				mp.solutions.drawing_utils.draw_detection(rgb, face)
				# in 0-1 scale
				boundary_box = face.location_data.relative_bounding_box
				# scale up to the image size
				boundary_box_scaled = int(boundary_box.xmin * width), int(boundary_box.ymin * height), int(boundary_box.width * width), int(boundary_box.height * height)
				# display the face detection score
				cv2.putText(rgb, f'{int(face.score[0]*100)}%', (boundary_box_scaled[0], boundary_box_scaled[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 2)

				# interest point of depth in a face. The center will be measured.
				interest_point = (boundary_box_scaled[0] + boundary_box_scaled[2] / 2, boundary_box_scaled[1] + boundary_box_scaled[3] / 2)

			# MiDaS v2.1 Small (Scale: 1/255, Size: 256x256, Mean Subtraction: (123.675, 116.28, 103.53), Channels Order: RGB,swapRB=True, crop=False)
			blob = cv2.dnn.blobFromImage(rgb, 1/255., (256,256), (123.675, 116.28, 103.53), True, False)

			# set the input into the model
			mono_model.setInput(blob)

			# get depth map
			depth_map = mono_model.forward()

			# resize it to the real world
			depth_map = depth_map[0,:,:]
			depth_map = cv2.resize(depth_map, (width, height))
			depth_map = cv2.normalize(depth_map, None, 0, 1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)

			# change colors to display it in OpenCV
			bgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR)

			# get the depth of the point of interest
			depth = depth_map[int(interest_point[0]), int(interest_point[1])]

			depth_distance = depth_to_distance(depth)
			cv2.putText(bgr, f"Depth to face: {str(round(depth_distance,2)*100)} cm", (40,600), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 2)
			cv2.imwrite("../../data/result_"+os.path.basename(img), bgr)

if __name__ == "__main__":
	if len(sys.argv) != 3:
		raise Exception("Usage: requires two parameters, for example: python {} directory_name image_suffix_name".format(sys.argv[0]))

	model_name = "model-small.onnx"
	download_onnx_model("https://github.com/isl-org/MiDaS/releases/download/v2_1/model-small.onnx", model_name)

	imgs = get_images(sys.argv[1], sys.argv[2])
	#print("imgs:", imgs)
 
	calc_distance(imgs, model_name)

	print("test finish")

      说明

      1.测试代码参考:https://levelup.gitconnected.com

      2.mp.solutions.face_detection.FaceDetection函数中,model_selection默认为0;距离相机2米以内的脸部检测模型设置为0,即short_range;距离相机5米以内的脸部检测模型设置为1,即full_range。

      3.model-small.onnx为预训练的单目深度估计模型,从https://github.com/isl-org/MiDaS/releases/tag/v2_1 下载;如果有cuda,也可以使用更大的模型获得更真实的结果。

      4.depth_to_distance函数用于将深度图值转换为以厘米为单位的真实世界的距离,此转换的公式根据你的网络摄像头配置而有所不同。注:还不清楚此公式怎么来的

      5.测试代码接收2个参数,第一个参数指定存放图像的路径,第二个参数指定图像后缀名;首次运行会自动下载onnx模型。

      运行结果如下图所示:

      测试图像执行结果如下图所示:原始图像来自于网络

      GitHub:https://github.com/fengbingchun/NN_Test

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/607967.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Linux网络】HTTPS【上】{运营商劫持/加密方式/数据摘要/https的诞生}

文章目录 1.引入1.1http与https1.2SSL/TLS1.3VPN1.4认识1.5密码学1.6为什么要加密?运营商 1.7常见的加密方式对称加密非对称加密 2.加密与解密3.数据摘要 && 数据指纹MD5 数字 签名理解三者数据摘要(Digital Digest):数字…

数据结构与算法之树和二叉树的一些概念和性质

目录 前言 一、树的定义 二、树的若干术语 1.结点的度 2.叶子 3.双亲与孩子 4.兄弟 5.祖先 6.树的度 7.结点的层次 8.树的深度 9.有序树和无序树 10.森林 三、树的逻辑结构 四、树的存储结构 1.顺序存储 2.链式存储 五、二叉树 1.定义 2.二叉树的五种状态 …

美食推荐网站设计

**中文摘要:**在当今信息化、网络化的时代背景下,美食文化正逐渐融入人们的日常生活,而网络平台成为人们获取美食信息、分享美食体验的重要途径。为了满足广大美食爱好者对美食信息的探索和推荐需求,本文提出了一种创新的美食推荐…

OS复习笔记ch5-3

引言 上一节我们学习了关于信号量机制的一些内容,包括信号量的含义,对应的PV操作等。 如图所示,上一节主要是针对信号量的互斥,其实信号量机制还可以做很多事情,比如实现进程同步和前驱关系,这一节我们先复…

Selenium 自动化 —— 常用的定位器(Locator)

什么是定位器 定位器(Locator)是识别DOM中一个或多个特定元素的方法。 也可以叫选择器 Selenium 通过By类,提供了常见的定位器。具体语法如下: By.xxx("");我们选择单个元素时可以使用findByElement: Web…

ICode国际青少年编程竞赛- Python-2级训练场-坐标与列表练习

ICode国际青少年编程竞赛- Python-2级训练场-坐标与列表练习 1、 for i in range(6):Spaceship.step(Item[i].x - Spaceship.x)Dev.step(Item[i].y - Dev.y)Dev.step(Spaceship.y - Dev.y)2、 for i in range(5):Spaceship.step(Item[i].x - Spaceship.x)Flyer[i].step(Item[…

【MySQL数据库开发设计规范】之基础规范

欢迎点开这篇文章,自我介绍一下哈,本人笔名姑苏老陈,是一个JAVA开发老兵。 本文收录于 《MySQL数据库开发设计规范》专栏中,该专栏主要分享一些关于MySQL数据库开发设计相关的技术规范文章,定期更新,欢迎关…

《ESP8266通信指南》11-Lua开发环境配置

往期 《ESP8266通信指南》10-MQTT通信(Arduino开发)-CSDN博客 《ESP8266通信指南》9-TCP通信(Arudino开发)-CSDN博客 《ESP8266通信指南》8-连接WIFI(Arduino开发)(非常简单)-CSD…

机器学习(三) ----------线性回归算法(梯度下降+正则化)

目录 1 定义 2 损失函数(回归) 2.1 最小二乘函数(Least Squares Function) 2.2 均方误差(Mean Squared Error, MSE) 2.3 均方根误差(Root Mean Squared Error, RMSE) 2.4 平均绝…

自动驾驶纵向控制算法

本文来源——b站忠厚老实的老王,链接:忠厚老实的老王投稿视频-忠厚老实的老王视频分享-哔哩哔哩视频 (bilibili.com),侵删。 功率和转速之间的关系就是:功率P等于转矩M乘以转速ω。并不是油门越大加速度就越大。 发动机和电机的转…

GDAL:Warning 1: All options related to creation ignored in update mode

01 警告说明 首先贴出相关代码: out_file_name Rs_{:4.0f}{:02.0f}.tiff.format(year, month) out_path os.path.join(out_dir, out_file_name) mem_driver gdal.GetDriverByName(MEM) mem_ds mem_driver.Create(, len(lon), len(lat), 1, gdal.GDT_Float32) …

掌握用户全生命周期数据,Xinstall让App投放更科学

在数字化时代,App已成为企业与用户互动的重要窗口。然而,想要让App在众多竞争者中脱颖而出,吸引并留住用户,有效的广告投放策略至关重要。这就需要对广告投放效果进行精准分析,以便及时调整策略,实现最大化…

Kubernetes的基本概念

目录 一.基本内容 1.定义 2.作用 二.特性 1.弹性伸缩 2.自我修复 3.服务发现和负载均衡 4.自动发布(默认滚动发布模式)和回滚 5.集中化配置管理和密钥管理 6.存储编排,支持外挂存储并对外挂存储资源进行编排 7.任务批处理运行 三…

clickhouse mergeTree表引擎解析

参照 https://clickhouse.com/docs/zh/engines/table-engines/mergetree-family/mergetree https://clickhouse.com/docs/en/optimize/skipping-indexes Clickhouse中最强大的表引擎当属MergeTree(合并树)引擎及该系列(*MergeTree&#xff…

Springboot项目使用redis实现session共享

1.安装redis,并配置密码 这里就不针对于redis的安装约配置进行说明了,直接在项目中使用。 redis在windows环境下安装:Window下Redis的安装和部署详细图文教程(Redis的安装和可视化工具的使用)_redis安装-CSDN博客 2…

图片公式识别@文档公式识别@表格识别@在线和离线OCR工具

文章目录 abstract普通文字识别本地软件识别公式扩展插件下载小结 在线识别网站/API👺Quicker整合(推荐)可视化编辑和识别公式其他多模态大模型识别图片中的公式排版 开源模型 abstract 本文介绍免费图片文本识别(OCR)工具,包括普通文字识别,公式识别,甚至是手写公…

Linux网络——自定义序列化与反序列化

前言 之前我们学习过socket之tcp通信,知道了使用tcp建立连接的一系列操作,并通过write与read函数能让客户端与服务端进行通信,但是tcp是面向字节流的,有可能我们write时只写入了部分数据,此时另一端就来read了&#x…

ZYNQ MPSoC zcu102 PS端运行helloworld

文章目录 一、参考资料二、需要注意的步骤三、运行结果 一、参考资料 1.zcu102 zynq Mpsoc uart hello world——CSDN博客 2.zcu102自学 —— 第一个实验 (纯PS 串口打印 Hello world)——CSDN博客 3.【02】ALINX Zynq MPSoC XILINX FPGA视频教程 SDK 裸…

Linux:进程信号(一)信号的产生

目录 一、信号是什么? 二、Linux信号 三、信号处理方式 四、信号的产生 1、 通过终端按键产生信号 2、调用系统函数向进程发信号 3、 硬件异常产生信号 一、信号是什么? 在生活中,有许多信号,比如红绿灯,下课铃声…

如何使用Transformer-TTS语音合成模型

1、技术原理及架构图 ​ Transformer-TTS主要通过将Transformer模型与Tacotron2系统结合来实现文本到语音的转换。在这种结构中,原始的Transformer模型在输入阶段和输出阶段进行了适当的修改,以更好地处理语音数据。具体来说,Transformer-TT…
最新文章