You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python人脸识别如何对接数据库实现(无需使用网络摄像头)

实现方案说明

1. 不调用网络摄像头的功能实现

你当前编写的代码本身就没有调用网络摄像头,是直接读取本地静态图片完成人脸识别的,完全满足需求。如果需要扩展能力,可以按以下方式调整:

  • 批量识别本地图片:遍历指定文件夹下的所有图片文件,循环调用现有识别逻辑即可
  • 识别本地视频文件:使用opencv读取本地视频逐帧提取画面,复用现有人脸比对逻辑处理每帧画面即可,全程不需要调用摄像头

2. 对接数据库实现人脸识别的可行路径

核心逻辑是将提前计算好的人脸128维特征向量序列化后存入数据库,识别时从数据库取出所有特征做比对即可,具体实现步骤如下:

  • 数据库表设计:至少存储3个字段,自增主键ID、人员姓名、人脸特征序列化后的二进制/字符串数据
  • 特征入库:提前遍历所有人脸底库图片,计算特征后序列化存入数据库
  • 识别流程:识别时先从数据库拉取所有已存的人脸特征和对应姓名,反序列化后得到特征列表,再和待识别图片的人脸特征做比对,和你现有逻辑完全一致

如果使用MySQL、PostgreSQL等其他关系型数据库,仅需修改数据库连接逻辑,特征序列化、比对的逻辑完全通用。

参考实现代码

首先安装依赖:pip install numpy sqlite3

第一步:人脸特征入库代码

import face_recognition
import sqlite3
import numpy as np

# 初始化数据库
conn = sqlite3.connect('face_db.db')
cursor = conn.cursor()
# 创建人脸表
cursor.execute('''
CREATE TABLE IF NOT EXISTS faces (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    encoding BLOB NOT NULL
)
''')
conn.commit()

# 批量将底库人脸存入数据库
face_info = [
    ("Keanu Reeves", "KeanuR.jpg"),
    ("Shakira", "Shakira.jpg"),
    ("Donald Trump", "DTrump.jpg"),
    ("Beyonce", "Beyonce.jpg")
]
for name, img_path in face_info:
    # 计算人脸特征
    img = face_recognition.load_image_file(img_path)
    encoding = face_recognition.face_encodings(img)[0]
    # 序列化特征为二进制
    encoding_blob = encoding.tobytes()
    # 存入数据库
    cursor.execute("INSERT INTO faces (name, encoding) VALUES (?, ?)", (name, encoding_blob))
conn.commit()
conn.close()

第二步:对接数据库的人脸识别代码

import face_recognition
from PIL import Image, ImageDraw
import sqlite3
import numpy as np

# 从数据库加载所有人脸特征和姓名
conn = sqlite3.connect('face_db.db')
cursor = conn.cursor()
cursor.execute("SELECT name, encoding FROM faces")
results = cursor.fetchall()
conn.close()

known_face_encodings = []
known_face_names = []
for name, encoding_blob in results:
    # 反序列化二进制为特征数组
    encoding = np.frombuffer(encoding_blob, dtype=np.float64)
    known_face_encodings.append(encoding)
    known_face_names.append(name)

# 以下是原有识别逻辑,无需改动
test_image = face_recognition.load_image_file('unknown6.jpg')
face_locations = face_recognition.face_locations(test_image)
face_encodings = face_recognition.face_encodings(test_image, face_locations)

pil_image = Image.fromarray(test_image)
draw = ImageDraw.Draw(pil_image)

for(top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
    matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
    name = "Unknown person"
    if True in matches:
        first_match_index = matches.index(True)
        name = known_face_names[first_match_index]
        
    draw.rectangle(((left, top), (right, bottom)), outline=(5,5,5))
    text_width, text_height = draw.textsize(name)
    draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill = (0,0,0), outline=(0,0,0))
    draw.text((left + 6, bottom - text_height - 5), name, fill=(255,255,255,255))
        
del draw     
    
pil_image.show()

内容的提问来源于stack exchange,提问作者Koko009856

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 23:09:03