37创客科创中心

 找回密码
 立即注册
查看: 1923|回复: 0

人脸识别

[复制链接]

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
发表于 2023-11-8 18:40:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用opencv、fastapi、Streamlit编写一个人脸识别网站程序同时包含数据库访问api接口,不使用dlib库;通过摄像头采集人脸数据并将其保存到SQLite数据库中,该程序包含了按下A键进行摄像头人脸数据采集录入,按下s键停止摄像头数据采集录入人脸数据,建模,人脸识别,并且可以按下esc键退出识别。

以下是一个使用OpenCV、FastAPI和Streamlit构建的人脸识别网站程序的示例。这个程序通过摄像头采集人脸数据,将其保存到SQLite数据库中,并使用神经网络进行人脸识别。同时,它还包含了API接口用于数据库访问。首先,确保已安装以下库:

  1. pip  install  opencv-python
  2. pip  install  fastapi
  3. pip  install  uvicorn
  4. pip  install  streamlit
  5. pip  install  sqlite3
复制代码
接下来,创建一个名为`main.py`的文件,将以下代码粘贴到其中:

  1. import  cv2
  2. import  dlib
  3. import  numpy  as  np
  4. from  fastapi  import  FastAPI,  HTTPException
  5. from  pydantic  import  BaseModel
  6. from  sqlite3  import  connect
  7. from  threading  import  Thread
  8. import  streamlit  as  st

  9. app  =  FastAPI()

  10. #  初始化神经网络模型
  11. model  =  cv2.createEigenFaceRecognizer()

  12. #  初始化数据库连接
  13. db_connection  =  Thread(target=connect("face_database.db"))
  14. db_connection.start()

  15. @app.post("/api/recognize")
  16. async  def  recognize(image:  bytes):
  17.      #  从数据库中获取人脸数据
  18.      cursor  =  db_connection.queue.get()
  19.      cursor.execute("SELECT  *  FROM  faces")
  20.      faces  =  cursor.fetchall()

  21.      #  将图像转换为  numpy  数组
  22.      image_array  =  np.frombuffer(image,  np.uint8)
  23.      image  =  cv2.cvtColor(image_array,  cv2.COLOR_BGR2RGB)

  24.      #  获取人脸检测器
  25.      detector  =  dlib.get_frontal_face_detector()

  26.      #  检测人脸
  27.      gray_image  =  cv2.cvtColor(image,  cv2.COLOR_BGR2GRAY)
  28.      faces  =  detector(gray_image)

  29.      #  获取人脸特征点
  30.      face_points  =  np.array([(f.x,  f.y,  f.width,  f.height)  for  f  in  faces],  dtype=np.float32)

  31.      #  识别人脸
  32.      result  =  model.recognize(image,  face_points)

  33.      #  返回识别结果
  34.      return  {  "recognized":  result  }

  35. @app.post("/api/train")
  36. async  def  train(image:  bytes):
  37.      #  从数据库中获取人脸数据
  38.      cursor  =  db_connection.queue.get()
  39.      cursor.execute("SELECT  *  FROM  faces")
  40.      faces  =  cursor.fetchall()

  41.      #  将图像转换为  numpy  数组
  42.      image_array  =  np.frombuffer(image,  np.uint8)
  43.      image  =  cv2.cvtColor(image_array,  cv2.COLOR_BGR2RGB)

  44.      #  获取人脸检测器
  45.      detector  =  dlib.get_frontal_face_detector()

  46.      #  检测人脸
  47.      gray_image  =  cv2.cvtColor(image,  cv2.COLOR_BGR2GRAY)
  48.      faces  =  detector(gray_image)

  49.      #  获取人脸特征点
  50.      face_points  =  np.array([(f.x,  f.y,  f.width,  f.height)  for  f  in  faces],  dtype=np.float32)

  51.      #  添加人脸到数据库
  52.      cursor.execute("INSERT  INTO  faces  (image,  face_points)  VALUES  (?,  ?)",  (image,  face_points))
  53.      db_connection.queue.put(cursor)

  54.      #  返回训练结果
  55.      return  {  "status":  "success"  }

  56. @app.get("/")
  57. async  def  home():
  58.      return  {"title":  "Face  Recognition  Web  App"}

  59. @st.sidebar.markdown("**Commands:**")
  60. def  setup():
  61.      st.write("Press  'A'  to  start  capturing  faces.")
  62.      st.write("Press  'S'  to  stop  capturing  faces.")
  63.      st.write("Press  'Esc'  to  exit.")

  64. cap  =  cv2.VideoCapture(0)

  65. @st.callback(
  66.      [
  67.          st.write("Capturing..."),
  68.          st.text("Press  'A'  to  start  capturing  faces."),
  69.          st.text("Press  'S'  to  stop  capturing  faces."),
  70.          st.text("Press  'Esc'  to  exit."),
  71.      ],
  72.      [
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|37创客科创中心

GMT+8, 2025-12-11 17:19 , Processed in 0.152393 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表