@人脸识别代码和一些常见错误

基于opencv的视频人脸识别(中文显示)以及 index 480 is out of bounds for axis 0 with size 480错误的解决

参考了
https://github.com/niehen6174/face-recognition-and-put-in-chinese
上述代码中采用的是海康的摄像头,这里我用的是自己笔记本的摄像头。
直接使用会显示数组溢出,这里会有一些修改的部分
报错 index 480 is out of bounds for axis 0 with size 480的解决见代码后面的修改部分
直接运行下述代码应该不会报错

ft2(处理中文识别)

# -*- coding: utf-8 -*-
# http://blog.csdn.net/zizi7/article/details/70145150'''
##################################################
# tools                                          #
#------------------------------------------------#
# draw chinese text using freetype on python2.x  #                  #
# 2017.4.12                                      #
##################################################
'''import numpy as np
import freetype
import copy
import pdbclass put_chinese_text(object):def __init__(self, ttf):self._face = freetype.Face(ttf)def draw_text(self, image, x, y,text, text_size, text_color):'''draw chinese(or not) text with ttf:param image:     image(numpy.ndarray) to draw text:param pos:       where to draw text:param text:      the context, for chinese should be unicode type:param text_size: text size:param text_color:text color:return:          image'''self._face.set_char_size(text_size * 64)metrics = self._face.sizeascender = metrics.ascender/64.0#descender = metrics.descender/64.0#height = metrics.height/64.0#linegap = height - ascender + descenderypos = int(ascender)if not isinstance(text, str):#对于Python 2中的unicode和Python 3中的str,对于Python 2中的str/bytes和Python 3中的bytes的二进制文件text = text.decode('utf-8')img = self.draw_string(image,x,y+ypos, text, text_color)return imgdef draw_string(self, img, x_pos, y_pos, text, color):'''draw string:param x_pos: text x-postion on img:param y_pos: text y-postion on img:param text:  text (unicode):param color: text color:return:      image'''prev_char = 0pen = freetype.Vector()pen.x = x_pos << 6   # div 64pen.y = y_pos << 6hscale = 1.0matrix = freetype.Matrix(int((hscale)*0x10000), int(0.2*0x10000),\int(0.0*0x10000), int(1.1*0x10000))cur_pen = freetype.Vector()pen_translate = freetype.Vector()image = copy.deepcopy(img)for cur_char in text:self._face.set_transform(matrix, pen_translate)self._face.load_char(cur_char)kerning = self._face.get_kerning(prev_char, cur_char)pen.x += kerning.xslot = self._face.glyphbitmap = slot.bitmapcur_pen.x = pen.xcur_pen.y = pen.y - slot.bitmap_top * 64self.draw_ft_bitmap(image, bitmap, cur_pen, color)pen.x += slot.advance.xprev_char = cur_charreturn imagedef draw_ft_bitmap(self, img, bitmap, pen, color):'''draw each char:param bitmap: bitmap:param pen:    pen:param color:  pen color e.g.(0,0,255) - red:return:       image'''x_pos = pen.x >> 6y_pos = pen.y >> 6cols = bitmap.widthrows = bitmap.rowsglyph_pixels = bitmap.bufferfor row in range(rows):for col in range(cols):if glyph_pixels[row*cols + col] != 0:img[y_pos + row][x_pos + col][0] = color[0]img[y_pos + row][x_pos + col][1] = color[1]img[y_pos + row][x_pos + col][2] = color[2]if __name__ == '__main__':# just for testimport cv2line = '你好'img = np.zeros([300,300,3])color_ = (0,255,0) # Greenpos = (3, 3)text_size = 24#ft = put_chinese_text('wqy-zenhei.ttc')ft = put_chinese_text('msyh.ttf')image = ft.draw_text(img, pos, line, text_size, color_)cv2.imshow('diplay', image)cv2.waitKey(0)

修改的部分是第44行的,就是这里的数组出现了问题。

img = self.draw_string(image,x,y+ypos, text, text_color)

face_mobilevideo实现人脸识别,按下S键可拍照并保存。

# -*- coding: utf-8 -*-
# 摄像头头像识别
import face_recognition
import cv2
import ft2
from PIL import Image, ImageDraw, ImageFont
import numpy as npcam = cv2.VideoCapture(0)
# 本地图像
kxs_image = face_recognition.load_image_file("kxs.jpg")
kxs_face_encoding = face_recognition.face_encodings(kxs_image)[0]# 本地图像二
wsp_image = face_recognition.load_image_file("wsp.jpg")
wsp_face_encoding = face_recognition.face_encodings(wsp_image)[0]# 本地图片三2
pr_image = face_recognition.load_image_file("pr.jpg")
pr_face_encoding = face_recognition.face_encodings(pr_image)[0]# Create arrays of known face encodings and their names
# 脸部特征数据的集合
known_face_encodings = [kxs_face_encoding,wsp_face_encoding,pr_face_encoding
]# 人物名称的集合
"""""
known_face_names = ["kxs","wsp","panrui"
]
"""""
known_face_names = ["匡","王","潘"
]
face_locations = []
face_encodings = []
face_names = []
process_this_frame = Truewhile(cam.isOpened()):# 读取摄像头画面ret, frame = cam.read()if not ret:#等同于 if ret is not nonebreaksucess, img = cam.read()k = cv2.waitKey(1)if k == 27:# 通过esc键退出摄像cv2.destroyAllWindows()breakelif k == ord("s"):# 通过s键保存图片,并退出。num = 1cv2.imwrite("image%s.jpg" % num, img)num+=1;print("ok")#cv2.destroyAllWindows()break# 改变摄像头图像的大小,图像小,所做的计算就少small_frame = cv2.resize(frame, (0, 0), fx=0.33, fy=0.33)# opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。rgb_small_frame = small_frame[:, :, ::-1]# Only process every other frame of video to save timeif process_this_frame:# 根据encoding来判断是不是同一个人,是就输出true,不是为flaseface_locations = face_recognition.face_locations(rgb_small_frame)face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)face_names = []for face_encoding in face_encodings:# 默认为unknownmatches = face_recognition.compare_faces(known_face_encodings, face_encoding,tolerance=0.48)#阈值太低容易造成无法成功识别人脸,太高容易造成人脸识别混淆 默认阈值tolerance为0.6#print(matches)name = "Unknown"# if match[0]:#     name = "michong"# If a match was found in known_face_encodings, just use the first one.if True in matches:first_match_index = matches.index(True)name = known_face_names[first_match_index]face_names.append(name)process_this_frame = not process_this_frame# 将捕捉到的人脸显示出来for (top, right, bottom, left), name in zip(face_locations, face_names):# Scale back up face locations since the frame we detected in was scaled to 1/4 size#由于我们检测到的帧被缩放到1/4大小,所以要缩小面位置top *= 3right *= 3bottom *= 3left *= 3cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 3)print(name)ft = ft2.put_chinese_text('msyh.ttf')#引入ft2中的字体#加上标签xpos=left+10ypos=bottomframe = ft.draw_text(frame,xpos,ypos,name, 20, (255, 255, 255))cv2.imshow('monitor', frame)if cv2.waitKey(1) & 0xFF == 27:breakcam.release()
cv2.destroyAllWindows()

修改的部分是120行左右的

     xpos=left+10ypos=bottomframe = ft.draw_text(frame,xpos,ypos,name, 20, (255, 255, 255))

只输出英文(把上面的输出文字改为下面的代码即可)

cv2.putText(frame, name, (left + 6, bottom - 6), 000, 0.8, (255, 255, 255), 1)  # 这是不输入汉字时可以用的代码

报错


** index 480 is out of bounds for axis 0 with size 480**
就按我上面的修改就不会出错了。

代码文件下载

https://github.com/horsein/-python_face_recognition–/tree/main

调用笔记本的摄像头实现基于opencv的视频人脸识别(中文显示和英文显示)以及 index 480 is out of bounds for axis 0 with size 480错误的解决相关推荐

  1. 基于openCV的视频人脸识别——演员的诞生视频人脸识别

    1.准备训练数据 网络上下载(训练数据量大时,通过爬虫获取)目标的图片: 运用以下代码将原图中的人脸头像识别.提取.调整大小(这里是150*200),并分别保存. 运行环境:win7 64+VS201 ...

  2. 基于 OpenCV + Python 的人脸识别上课签到系统

    目录 前言 安装第三方库 第一步:采集人脸图像 (1)修改姓名学号 (2)运行capture_face.py (3)采集人脸图像 (4)查看采集到的人脸图像 第二步:训练模型 第三步:识别签到 (1) ...

  3. 【优秀课设】基于OpenCV的Python人脸识别、检测、框选(遍历目录下所有照片依次识别 视频随时标注)

    基于OpenCV的Python人脸识别.检测.框选 (遍历目录下所有照片依次识别 视频随时标注) 移步: https://blog.csdn.net/weixin_53403301/article/d ...

  4. 【毕业设计/课程设计】基于opencv的高精度人脸识别考勤系统设计与实现

    文章目录 0 项目说明 1 需求分析 2 总体设计 3 详细设计 4 程序运行结果测试与分析 5 实验心得 6 项目源码 0 项目说明 基于opencv的高精度人脸识别考勤系统设计与实现 提示:适合用 ...

  5. 使用openCV进行视频人脸识别

    视频人脸识别系列 第一篇 使用openCV进行视频人脸识别 第二篇 使用虹软SDK进行视频人脸识别 第三篇 使用虹软SDK进行视频人脸比对 文章目录 视频人脸识别系列 前言 一.环境搭建 开发环境 配 ...

  6. C++ OpenCV相片视频人脸识别统计人数

    C++ OpenCV相片视频人脸识别统计人数 如需远程调试,可加QQ905733049由专业技术人员远程协助! 运行代码如下: #include <iostream> #include&l ...

  7. 基于Opencv快速实现人脸识别(图片识别)

    两个文件夹,一个为训练数据集,一个为测试数据集,训练数据集中有两个文件夹0和1,之前看一些资料有说这里要遵循"slabel"命名规则,但后面处理起来比较麻烦,因为目前opencv接 ...

  8. 基于Opencv快速实现人脸识别(完整版)

    上篇博客:https://blog.csdn.net/beyond9305/article/details/92844258严格来说标题是有误的,只是单纯地对人脸进行了检测,而并非识别,opencv内 ...

  9. 基于OpenCV的简单人脸识别系统

    目录 1. 调用库函数 2. 调用摄像头并设置窗口 3. 设置图片正负样本数据集的路径 4. 调用人脸检测器 5. 正负样本载入 6.提取人脸区域 7. 建立LBPH人脸识别模型 8. 实时检测 9. ...

最新文章

  1. 只有2GB内存在20亿个整数中找到出现次数最多的数
  2. 让Team Exploer自动登录TFS
  3. Java在开发中应注意的问题_Java设计编程应该注意的几个问题
  4. mybatis实现多对多
  5. JeecgCloud 微服务开发平台-部署文档
  6. cad经典工作空间_最实用的CAD界面的设置
  7. SpringMVC一路总结(一)
  8. MongoDB副本集学习(三):性能和优化相关
  9. Atitit 动态调用webservice与客户端代理方式调用
  10. easyui 删除数据表格
  11. SN1SLD16 华为SDH全新原包装2xSTM-16光接口板
  12. 一些好用的免费的截屏、GIF制作的PC端小工具
  13. python收取126或163邮件
  14. android rgb接口,Android RGB颜色查询对照表
  15. IP地址中A类、B类、C类地址的区别
  16. 《我是谁:没有绝对安全的系统》观影感受
  17. 树莓派3B安装openwrt19.07.04
  18. 好用的在线二维码生成器网站PHP源码
  19. ERP生产管理软件系统的主要功能模块是什么?
  20. js百度地图鼠标绘制工具条库

热门文章

  1. 小米网关控制空调伴侣_米家智能家居组建之绿米aqara空调伴侣升级版网关
  2. 今日份安利:什么软件可以录音翻译英语?
  3. 如何才能做到学以致用
  4. Spring框架自学笔记
  5. 解决eclipse中打开xml文件时不显示namespace标签的问题
  6. 计算机excel中所占比怎么算,excel表格中数据比重计算公式-excel所占比例怎么算...
  7. 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java校外实习管理平台6tu82
  8. 不做互联网大厂编外人:外包员工的煎熬、逃离与逆袭
  9. 未检测到ca设备或ca驱动异常_混凝土碳化深度异常原因的探讨
  10. MPLS总部分部共用AS架构案例