Face Recognition Unlock System for Real-World Game Scenes Using Raspberry Pi and Tencent Cloud
The article details how a low‑cost, compact face‑recognition lock for an escape‑room game was built using a Raspberry Pi, camera, ultrasonic sensor, relay‑controlled electromagnetic lock, and Tencent Cloud’s Face Recognition API, with about 200 lines of Python code handling parallel sensor monitoring, video streaming, watermark overlay, and cloud authentication.
The article describes a complete solution for implementing a face‑recognition based lock in a real‑world game scenario. The author, a product manager at Tencent Cloud, explains how the system was built with only about 200 lines of code, covering system architecture, hardware selection, cloud service configuration, and detailed software implementation.
Background
A friend needed a face‑recognition lock for an escape‑room‑style game. The requirements were simple in functionality but constrained by cost, space, reliability, and network conditions.
Player Experience
When a player enters the area, a screen shows a live view of the player.
Approaching the lock triggers a frame capture; a watermark "Authenticating" appears.
If recognition fails, the watermark changes to "Authentication Failed" for 2 seconds.
If recognition succeeds, the watermark changes to "Authentication Successful" and the safe opens.
Product Requirements
Low cost (hardware and maintenance).
Easy maintenance – staff can recover from failures quickly.
High reliability – accurate recognition, low fault rate.
Compact size (max 20 cm × 15 cm × 15 cm after removing display and lock).
Limited lighting, weak network, and parallel processing of authentication and feedback.
Functional Design
The final architecture consists of a Raspberry Pi as the controller, a camera for video input, an ultrasonic sensor for proximity detection, a display for video output, a relay to drive an electromagnetic lock, and a 5 V power supply. The system runs two processes in parallel: one for sensor monitoring and authentication, the other for video capture and watermark overlay.
Cost Analysis
Hardware: ¥500‑600.
Spare parts (1:1): ¥500‑600.
Cloud usage: free tier, negligible electricity cost.
Technical Implementation
1. System Architecture
Raspberry Pi 3B+ (main controller).
Camera module (CSI interface).
Ultrasonic distance sensor (VCC, Trig, Echo, GND).
Display (HDMI output).
Relay module (5 V, dual‑side wiring) to control the electromagnetic lock.
2. System Setup
a. Tencent Cloud Configuration
Register a Tencent Cloud account, create a Face Recognition person group, and obtain the API secret ID/key.
b. Raspberry Pi Configuration
Install the OS (desktop version), configure Wi‑Fi, and set a static IP:
# 具体内容请参考你的本地网络规划
interface wlan0
static ip_address=192.168.0.xx/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 192.168.0.2Install the Tencent Cloud SDK and OpenCV:
sudo apt-get install python-pip -y
pip install tencentcloud-sdk-python sudo apt-get install libopencv-dev -y
sudo apt-get install python-opencv -yClone the source code from GitHub and copy it to /home/pi/faceid . Edit config.json to insert the secret ID/key and person‑group ID.
c. Auto‑Start Configuration
Type=Application
Exec=python /home/pi/faceid/main.pyd. Hardware Wiring
Camera connected via CSI.
Ultrasonic sensor: Trig → BCM‑24, Echo → BCM‑23, VCC → 5 V, GND → GND.
Relay: VCC → 5 V, GND → GND, CH1 → BCM‑12 (controls the lock).
3. Code Logic and Technologies
a. Pseudocode for the monitoring/authentication process
# 监测鉴权进程-主进程
获取应用配置(API ID/Key 等)
初始化GPIO引脚(准备控制 传感器、继电器)
启动视频管理进程(辅进程)
循环开始:
if not 测距达到触发标准:
continue
与辅进程通信(捕获当前帧,并存入指定路径,并添加“认证中”水印)
调用云API,使用该帧图片人脸识别
if 识别成功:
与辅进程通信(变更水印为“认证成功”)
等待5秒
关机 或 继续运行(由config.json中 su2halt 字段指定)
else:
与辅进程通信(变更水印为“认证失败”)
等待2秒
与辅进程通信(清除水印)
# 视频管理进程-辅进程
初始化摄像头
循环开始:
取帧
取进程间共享队列
按消息进行不同操作(帧图像保存/加不同水印/不处理)
输出帧b. Watermark processing with OpenCV
# img1为当前视频帧(底图),img2为已读取水印图
def addpic(img1,img2):
# 关注区域ROI-取底图中将被水印图编辑的图像
rows, cols = img2.shape[:2]
roi = img1[:rows, :cols]
# 图片灰化-避免水印图非纯黑纯白情况
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# 生成掩膜-过滤浅色,位运算取非
ret, mask = cv2.threshold(img2gray, 220, 255, 3) #cv2.THRESH_BINARY
mask_inv = cv2.bitwise_not(mask)
# 生成水印区图像-底图裁出字体部分,生成水印区最终图像,替换原图水印区
img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
dst = cv2.add(img1_bg, img2)
img1[:rows, :cols] = dst
return img14. Hardware Details
Ultrasonic sensor works by sending a >10 µs high pulse on Trig, then measuring the high duration on Echo. Distance (m) = Echo duration × 340 / 2.
The 5 V relay module has a dual‑side interface: one side for power/signal (compatible with 3.3 V), the other side for the load (normally‑closed contact to the electromagnetic lock). Driving CH1 high opens the lock.
GPIO on the Raspberry Pi can be accessed via the RPi.GPIO Python library.
5. Solution Comparison
Local A : ESP‑EYE + C – low hardware cost, high development cost, hard to maintain.
Local B : Raspberry Pi + local inference – moderate cost, high CPU load, limited FPS.
Local C : Edge AI board (BM1880) – high hardware cost, high development cost.
Cloud A : Tencent Cloud Video AI – low hardware cost, low operating cost, but depends on stable network.
The chosen solution (cloud‑based face recognition with a Raspberry Pi Zero streaming video) balances cost, maintainability, and reliability for the given constraints.
References
API key acquisition: https://cloud.tencent.com/document/product/598/40488?from=10680
Tencent Cloud SDK installation: https://cloud.tencent.com/document/sdk/Python?from=10680
GitHub source code: https://github.com/eckygao/FaceRecognitionInRealGame
Face recognition product: https://cloud.tencent.com/product/facerecognition?from=10680
Video AI product: https://aivideo.cloud.tencent.com/list.html
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.