Keras之CNN:基于Keras利用cv2建立训练存储卷积神经网络模型(2+1)并调用摄像头进行实时人脸识别


吐司帅气
吐司帅气 2022-09-20 09:57:24 48745
分类专栏: 资讯

Keras之CNN:基于Keras利用cv2建立训练存储卷积神经网络模型(2+1)并调用摄像头进行实时人脸识别

目录

输出结果

设计思路

核心代码


输出结果

 

设计思路

核心代码

  1. -*- coding:utf-8 -*-
  2. import cv2
  3. from train_model import Model
  4. from read_data import read_name_list
  5. from timeit import default_timer as timer to calculate FPS
  6. class Camera_reader(-title class_ inherited__">object):
  7. def __init__(self):
  8. self.model = Model()
  9. self.model.load()
  10. self.img_size = 128
  11. def build_camera(self):
  12. face_cascade = cv2.CascadeClassifier('F:\\Program Files\\Python\\Python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt.xml')
  13. print(face_cascade) 输出<CascadeClassifier 000002240244CC70>
  14. name_list = read_name_list('F:\\File_Python\\Python_example\\face_recognition_name\\After_cut_picture')
  15. print(name_list)
  16. cameraCapture = cv2.VideoCapture(0)
  17. success, frame = cameraCapture.read()
  18. while success and cv2.waitKey(1) == -1:
  19. success, frame = cameraCapture.read()
  20. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  21. faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  22. ROI = gray[x:x + w, y:y + h]
  23. ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR)
  24. label,prob = self.model.predict(ROI)
  25. if prob >0.7:
  26. show_name = name_list[label]
  27. else:
  28. show_name = 'Stranger'
  29. cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)
  30. frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
  31. cv2.imshow("Camera", frame)
  32. cameraCapture.release()
  33. cv2.destroyAllWindows()
  34. def detect_video(self):
  35. face_cascade = cv2.CascadeClassifier('F:\\Program Files\\Python\\Python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt.xml')
  36. name_list = read_name_list('F:\\File_Python\\Python_example\\face_recognition_name\\After_cut_picture')
  37. video = cv2.VideoCapture(video_path) -doctag">TODO: will video path other than 0 be used?
  38. success, frame = video.read()
  39. accum_time = 0
  40. curr_fps = 0
  41. fps = "FPS: ??" fps = "FPS: ??"
  42. prev_time = timer()
  43. while success and cv2.waitKey(1) == -1:
  44. success, frame = video.read()
  45. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  46. faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  47. curr_time = timer()
  48. exec_time = curr_time - prev_time
  49. prev_time = curr_time
  50. accum_time = accum_time + exec_time
  51. curr_fps = curr_fps + 1 1
  52. if accum_time > 1:
  53. accum_time = accum_time - 1 1
  54. fps = "FPS: " + str(curr_fps)
  55. curr_fps = 0 0
  56. for (x, y, w, h) in faces:
  57. ROI = gray[x:x + w, y:y + h]
  58. ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) cv2.INTER_LINEAR图像尺寸变换的方法,默认的双线性插值
  59. label,prob = self.model.predict(ROI)
  60. if prob >0.7:
  61. show_name = name_list[label]
  62. else:
  63. show_name = 'Stranger'
  64. cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)
  65. frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
  66. cv2.namedWindow("result", cv2.WINDOW_NORMAL)
  67. cv2.imshow("result",frame)
  68. if __name__ == '__main__':
  69. camera = Camera_reader()
  70. camera.build_camera()
  71. video_path='F:/File_Python/Python_example/YOLOv3_use_TF/RunMan1.mp4'
  72. camera.detect_video()

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=3985
赞同 0
评论 0 条
吐司帅气L0
粉丝 0 发表 9 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2941
【软件正版化】软件正版化工作要点  2860
统信UOS试玩黑神话:悟空  2819
信刻光盘安全隔离与信息交换系统  2712
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1246
grub引导程序无法找到指定设备和分区  1213
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  163
点击报名 | 京东2025校招进校行程预告  162
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  160
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  157
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!