陇南市武都区住房和城乡建设网站seo网上培训
本博客为人脸识别系统的背景模糊代码解释
人脸识别系统博客汇总:人脸识别系统-博客索引
项目GitHub地址:Su-Face-Recognition: A face recognition for user logining
注意:阅读本博客前请先参考以下博客
工具安装、环境配置:人脸识别系统-简介
UI界面设计:人脸识别系统-UI界面设计
UI事件处理:人脸识别系统-UI事件处理
摄像头展示画面:人脸识别系统-摄像头画面展示
阅读完本博客后可以继续阅读:
用户端逻辑:
- 人脸识别:Python | 人脸识别系统 — 人脸识别
- 活体检测:Python | 人脸识别系统 — 活体检测
- 姿态检测:Python | 人脸识别系统 — 姿态检测
- 人脸比对:Python | 人脸识别系统 — 人脸比对
- 用户操作:Python | 人脸识别系统 — 用户操作
管理员端逻辑:
- 管理员操作:
- 用户操作:
一、判断器
首先判断是否打开摄像头,打开则判断 self.isFineSegmentation_flag 是否打开背景模糊。若否,则调用fine_segmentation方法,进行背景模糊。
# 主界面
class UserMainWindow(QMainWindow, UserMainUi):def __init__(self, parent=None):super(UserMainWindow, self).__init__(parent)self.setupUi(self)self.show_image = Noneself.cap = cv2.VideoCapture() # 相机self.source = CAPTURE_SOURCE # 相机标号self.WIN_WIDTH = 800 # 相机展示画面宽度self.WIN_HEIGHT = 500 # 相机展示画面高度self.isFineSegmentation_flag = False # 是否打开背景模糊标志... ...# 背景模糊判别器def fine_segmentation_judge(self):if not self.cap.isOpened():QMessageBox.information(self, "提示", self.tr("请先打开摄像头"))else:if not self.isFineSegmentation_flag:self.isFineSegmentation_flag = Trueself.fine_segmentation_button.setText("关闭背景模糊")self.fine_segmentation()self.fine_segmentation_button.setText("背景模糊")self.isFineSegmentation_flag = Falseelif self.isFineSegmentation_flag:self.isFineSegmentation_flag = Falseself.fine_segmentation_button.setText("背景模糊")self.show_camera()
二、背景模糊
# 背景模糊def fine_segmentation(self):mp_selfie_segmentation = mp.solutions.selfie_segmentationBG_COLOR = (192, 192, 192)with mp_selfie_segmentation.SelfieSegmentation(model_selection=1) as selfie_segmentation:while self.cap.isOpened():ret, frame = self.cap.read()QApplication.processEvents()# 将 BGR 图像转换为 RGBin_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)# 若要提高性能,可以选择将图像标记为不可写以通过引用传递。in_frame.flags.writeable = Falseresults = selfie_segmentation.process(in_frame)in_frame.flags.writeable = Truein_frame = cv2.cvtColor(in_frame, cv2.COLOR_RGB2BGR)# 在背景图像上绘制分割condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.1# 背景设置为高斯模糊gauss_image = cv2.GaussianBlur(in_frame, (85, 85), 0)if gauss_image is None:gauss_image = np.zeros(in_frame.shape, dtype=np.uint8)gauss_image[:] = BG_COLORout_frame = np.where(condition, in_frame, gauss_image)show_video = cv2.cvtColor(cv2.resize(out_frame, (self.WIN_WIDTH, self.WIN_HEIGHT)), cv2.COLOR_BGR2RGB)self.show_image = QImage(show_video.data, show_video.shape[1], show_video.shape[0],QImage.Format_RGB888)self.camera_label.setPixmap(QPixmap.fromImage(self.show_image))
阅读完本博客后可以继续阅读:
用户端逻辑:
- 人脸识别:Python | 人脸识别系统 — 人脸识别
- 活体检测:Python | 人脸识别系统 — 活体检测
- 姿态检测:Python | 人脸识别系统 — 姿态检测
- 人脸比对:Python | 人脸识别系统 — 人脸比对
- 用户操作:Python | 人脸识别系统 — 用户操作
管理员端逻辑:
- 管理员操作:
- 用户操作:
注:以上代码仅为参考,若需要运行,请参考项目GitHub完整源代码:Su-Face-Recognition: A face recognition for user logining