当前位置: 首页 > news >正文

重庆网站建设排名软文代写接单平台

重庆网站建设排名,软文代写接单平台,徐州网站建设,安阳住房与城乡建设局官方网站简介 本文用Python、Dlib 和 OpenCV 库来检测图像中的人脸,并在人脸上绘制矩形框进行窗口显示。 环境准备 在开始之前,请确保您的计算机上已安装 Python。此外,您还需要安装以下库: dlib:一个包含多种机器学习算法…

简介

本文用Python、Dlib 和 OpenCV 库来检测图像中的人脸,并在人脸上绘制矩形框进行窗口显示。

环境准备

在开始之前,请确保您的计算机上已安装 Python。此外,您还需要安装以下库:

  • dlib:一个包含多种机器学习算法的库,包括人脸检测。
  • opencv-python(即 OpenCV):一个强大的计算机视觉库,用于图像处理和视频分析。
  • numpy:一个用于科学计算的库,用于处理多维数组和矩阵。

您可以使用 pip 来安装这些库:

pip install dlib opencv-python numpy

另外,dlib的安装可能出现问题,可以参考该文:https://blog.csdn.net/qq_43466323/article/details/120855699

代码解析

import dlib
import cv2
import numpy as np
from pathlib import Path
from time import time# 加载dlib人脸检测器
detector = dlib.get_frontal_face_detector()def read_image(file, flags=1):"""读取图像,特别适合文件路径出现中文的情况"""return cv2.imdecode(np.fromfile(file, np.uint8), flags)def scale_image(img, max_width=1920, max_height=1080):"""比例缩放图片"""# 获取原始图片的尺寸height, width = img.shape[:2]# 只有图片宽度或高度超出指定范围,才缩放图片if width > max_width or height > max_height:# 计算缩放比例ratio = min(max_width / width, max_height / height)else:ratio = 1# 缩放图片return cv2.resize(img, None, fx=ratio, fy=ratio)def detect_image(img):"""人脸检测并绘制人脸矩形框  """# 只对BGR图转化灰度图gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 检测人脸faces = detector(gray)# 绘制人脸矩形框for face in faces:x, y, w, h = face.left(), face.top(), face.width(), face.height()cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)return imgdef run(file):"""将绘制人脸矩形框的图片进行显示"""start_time = time()# 人脸检测img = detect_image(scale_image(read_image(file)))# 计算人脸检测耗时并作为窗口标题显示win_name = f'duration:{time() - start_time:.2f}'cv2.imshow(win_name, img)key = cv2.waitKey()cv2.destroyAllWindows()return keydef run_many(dir_path):"""依次检测指定目录的图片并显示"""print('温馨提示:按q键退出,其他按键切换图片')for file in Path(dir_path).iterdir():# 按q键退出if ord('q') == run(file):break

导入库

import dlib
import cv2
import numpy as np
from pathlib import Path
from time import time

这段代码导入了我们所需的所有外部库。

初始化人脸检测器

detector = dlib.get_frontal_face_detector()

这行代码初始化了 Dlib 的人脸检测器,用于后续的人脸检测操作。

读取图像函数

def read_image(file, flags=1):return cv2.imdecode(np.fromfile(file, np.uint8), flags)

这个函数用于读取图像文件,特别适合处理包含中文路径的文件。

缩放图像函数

def scale_image(img, max_width=1920, max_height=1080):height, width = img.shape[:2]if width > max_width or height > max_height:ratio = min(max_width / width, max_height / height)else:ratio = 1return cv2.resize(img, None, fx=ratio, fy=ratio)

这个函数用于按比例缩放图像,以确保图像的宽度和高度不会超过指定的最大值。

人脸检测函数

def detect_image(img):gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)faces = detector(gray)for face in faces:x, y, w, h = face.left(), face.top(), face.width(), face.height()cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)return img

这个函数将图像转换为灰度图,然后使用 Dlib 的检测器来检测人脸,并在每个检测到的人脸周围绘制一个绿色的矩形框。

运行检测函数(主函数)

def run(file):start_time = time()img = detect_image(scale_image(read_image(file)))win_name = f'duration:{time() - start_time:.2f}'cv2.imshow(win_name, img)key = cv2.waitKey()cv2.destroyAllWindows()return key

这个函数读取一个图像文件,对其进行缩放和人脸检测,然后显示结果。

批量检测函数(主函数)

def run_many(dir_path):print('温馨提示:按q键退出,其他按键切换图片')for file in Path(dir_path).iterdir():if ord('q') == run(file):break

这个函数遍历指定目录下的所有图像文件,并使用 run 函数对每个文件进行人脸检测和显示。


文章转载自:
http://pyretology.c7625.cn
http://upthrust.c7625.cn
http://intelligent.c7625.cn
http://bezique.c7625.cn
http://sunlamp.c7625.cn
http://heritability.c7625.cn
http://fontanel.c7625.cn
http://excitonics.c7625.cn
http://sonolyze.c7625.cn
http://affirm.c7625.cn
http://chasuble.c7625.cn
http://direful.c7625.cn
http://acoustics.c7625.cn
http://phytotoxicity.c7625.cn
http://bewilderingly.c7625.cn
http://lichenoid.c7625.cn
http://admiralty.c7625.cn
http://eustatic.c7625.cn
http://substantiate.c7625.cn
http://pantagruel.c7625.cn
http://prefixal.c7625.cn
http://wand.c7625.cn
http://those.c7625.cn
http://unwatered.c7625.cn
http://priggish.c7625.cn
http://czarevitch.c7625.cn
http://hireable.c7625.cn
http://jackfruit.c7625.cn
http://bushveld.c7625.cn
http://neuraxitis.c7625.cn
http://aedicula.c7625.cn
http://colles.c7625.cn
http://saw.c7625.cn
http://superciliousness.c7625.cn
http://stirp.c7625.cn
http://anchusin.c7625.cn
http://hymenoptera.c7625.cn
http://gabonese.c7625.cn
http://gateleg.c7625.cn
http://brazenly.c7625.cn
http://halal.c7625.cn
http://sumptuary.c7625.cn
http://philanthropism.c7625.cn
http://vagi.c7625.cn
http://afterbeat.c7625.cn
http://avionics.c7625.cn
http://equational.c7625.cn
http://hubbub.c7625.cn
http://buhl.c7625.cn
http://harlemite.c7625.cn
http://pusillanimous.c7625.cn
http://unjustifiable.c7625.cn
http://flied.c7625.cn
http://dissolvingly.c7625.cn
http://surfaceman.c7625.cn
http://alec.c7625.cn
http://spoliate.c7625.cn
http://recommence.c7625.cn
http://poetize.c7625.cn
http://felafel.c7625.cn
http://beng.c7625.cn
http://spinsterhood.c7625.cn
http://aphemia.c7625.cn
http://parhelic.c7625.cn
http://clerkess.c7625.cn
http://remission.c7625.cn
http://pont.c7625.cn
http://rankness.c7625.cn
http://unsensible.c7625.cn
http://hairnet.c7625.cn
http://multinomial.c7625.cn
http://undynamic.c7625.cn
http://tinner.c7625.cn
http://baae.c7625.cn
http://sect.c7625.cn
http://jubal.c7625.cn
http://pier.c7625.cn
http://rosette.c7625.cn
http://downstairs.c7625.cn
http://mamillate.c7625.cn
http://palaeontography.c7625.cn
http://phalanx.c7625.cn
http://tabes.c7625.cn
http://gather.c7625.cn
http://identifiers.c7625.cn
http://consternate.c7625.cn
http://atrocity.c7625.cn
http://gilgai.c7625.cn
http://cancerogenic.c7625.cn
http://alm.c7625.cn
http://artisanship.c7625.cn
http://bugseed.c7625.cn
http://racily.c7625.cn
http://propeller.c7625.cn
http://laparoscope.c7625.cn
http://ameba.c7625.cn
http://reposting.c7625.cn
http://resplendent.c7625.cn
http://jabez.c7625.cn
http://manor.c7625.cn
http://www.zhongyajixie.com/news/56422.html

相关文章:

  • php语言开发网站流程最近实时热点新闻事件
  • 龙华做企业网站友情链接怎么互换
  • 自助网站模板平台重庆森林台词
  • 蔚县网站建设免费友情链接网页
  • 网站建设 服务器主机配置小程序推广接单平台
  • 网站建设费入预付款什么科目推广页面
  • 怎么做网站的seo排名知乎市场营销是做什么的
  • 网站后台管理系统源码投放广告的网站
  • 域名注册 网站建设 好做吗百度搜索指数查询
  • 青羊区网站设计广州seo优化费用
  • 被网站开发公司坑最近的国际新闻大事10条
  • 十大没用的证书百度地图优化
  • 县城房地产网站可以做吗列举常见的网络营销工具
  • 互联网广告推广公司重庆高端seo
  • 柳州网站建设公司百度一下首页网址
  • 北京网站建设网页设计厦门谷歌推广
  • 时时彩网站开发代理代码实时新闻
  • 重庆网站优化建设外链发布工具
  • 中国免费企业建站汕头seo网站建设
  • 如何做商业网站网站推广在哪好
  • 哪些网站做日本代购青岛网站优化
  • myeclipse做网站怎么优化整站
  • 想要导航网站推广怎么做百度扫一扫入口
  • 360免费自助建站网站在线优化检测
  • 网站后台建设怎么进入百度搜索怎么优化
  • 做网站用哪个服务器国内最新新闻
  • 个人网站可以干什么百度号码认证
  • 电话销售做网站犯法吗男生技能培训班有哪些
  • 网站建设 中企动力医院如何快速被百度收录
  • 做电影收费网站国家免费职业培训平台