1 说明:
=====
1.1 吸烟有害健康!!纯属娱乐和学习python的相关知识。
1.2 虽然是娱乐,但是opencv、dlib和python在人工智能、人脸识别、自动化等有很大作用,目前已经或者未来会有更多的应用,作为一名普通人,学习一点知识很有必要。
1.3 我的文章通俗易懂,小白都会,而且亲测过,没有编程经验的,一看就会!可以培养自己和孩子的学习计算机和编程兴趣!
1.4 效果图:
2 准备工作:
========
2.1 环境:
华为笔记本电脑、深度deepin-linux操作系统、python3.8和微软vscode编辑器。
2.2 要求:
2.2.1 PIL、numpy、imutils模块安装,安装简单,省略。
2.2.2 cv2模块安装:
pip install opencv-python#下面是我的cv2模块相关初步介绍的文章https://www.toutiao.com/i6833713845005976067/
2.2.3 dlib模块安装:有坑!!!
我是这么安装的,从github下载包,解压。
https://github.com/davisking/dlib
我是在桌面上,终端打开,输入:
python setup.py install #推荐#下面是我的安装#xgj@xgj-PC:~/Desktop/dlib/dlib-master$ sudo python3.8 setup.py install #本机安装
3 代码来源:
========
3.1 源代码:
https://github.com/tomoncle/face-detection-induction-course
下载,解压,找到:input_video_stream_paste_mask.py,进行修改。
3.2 shape_predictor_68_face_landmarks.dat下载
官网,网速慢,还有坑,建议下面地址下载。
https://jaist.dl.sourceforge.net/project/dclib/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2
3.3 修改后的代码:为了简洁,删除不必要的注释和代码:
# @Author : tomoncle# @Site : https://github.com/tomoncle/face-detection-induction-course#导出模块from time import sleepimport cv2import numpy as npfrom PIL import Imagefrom imutils import face_utils, resizefrom dlib import get_frontal_face_detector, shape_predictorclass DynamicStreamMaskService(object): #动态黏贴面具服务 def __init__(self, saved=False): self.saved = saved # 是否保存图片 self.listener = True # 启动参数 self.video_capture = cv2.VideoCapture(0) # 调用本地摄像头 self.doing = False # 是否进行面部面具 self.speed = 0.1 # 面具移动速度 self.detector = get_frontal_face_detector() # 面部识别器 #下载的shape_predictor_68_face_landmarks.dat,解压放在指定路径下,因为我是启动微软vscode编辑器 self.predictor = shape_predictor("/home/xgj/Desktop/dlib/xxx/face-detection-induction-course-master/shape_predictor_68_face_landmarks.dat") # 面部分析器 self.fps = 4 # 面具存在时间基础时间 self.animation_time = 0 # 动画周期初始值 self.duration = self.fps * 4 # 动画周期最大值 self.fixed_time = 4 # 画图之后,停留时间 self.max_width = 500 # 图像大小 self.deal, self.text, self.cigarette = None, None, None # 面具对象 def read_data(self): _, data = self.video_capture.read() return data def save_data(self, draw_img): if not self.saved: return #指定路径 draw_img.save("/home/xgj/Desktop/dlib/xxx/face-detection-induction-course-master/images/%05d.png" % self.animation_time) def init_mask(self): self.console("加载面具...") self.deal, self.text, self.cigarette = ( #修改指定路径,因为我是启动微软vscode编辑器 Image.open(x) for x in ["/home/xgj/Desktop/dlib/xxx/face-detection-induction-course-master/images/deals.png", "/home/xgj/Desktop/dlib/xxx/face-detection-induction-course-master/images/text.png", "/home/xgj/Desktop/dlib/xxx/face-detection-induction-course-master/images/cigarette.png"] ) def get_glasses_info(self, face_shape, face_width): left_eye = face_shape[36:42] right_eye = face_shape[42:48] left_eye_center = left_eye.mean(axis=0).astype("int") right_eye_center = right_eye.mean(axis=0).astype("int") y = left_eye_center[1] - right_eye_center[1] x = left_eye_center[0] - right_eye_center[0] eye_angle = np.rad2deg(np.arctan2(y, x)) deal = self.deal.resize( (face_width, int(face_width * self.deal.size[1] / self.deal.size[0])), resample=Image.LANCZOS) deal = deal.rotate(eye_angle, expand=True) deal = deal.transpose(Image.FLIP_TOP_BOTTOM) left_eye_x = left_eye[0, 0] - face_width // 4 left_eye_y = left_eye[0, 1] - face_width // 6 return {"image": deal, "pos": (left_eye_x, left_eye_y)} def get_cigarette_info(self, face_shape, face_width): mouth = face_shape[49:68] mouth_center = mouth.mean(axis=0).astype("int") cigarette = self.cigarette.resize( (face_width, int(face_width * self.cigarette.size[1] / self.cigarette.size[0])), resample=Image.LANCZOS) x = mouth[0, 0] - face_width + int(16 * face_width / self.cigarette.size[0]) y = mouth_center[1] return {"image": cigarette, "pos": (x, y)} def orientation(self, rects, img_gray): #人脸定位 faces = [] for rect in rects: face = {} face_shades_width = rect.right() - rect.left() predictor_shape = self.predictor(img_gray, rect) face_shape = face_utils.shape_to_np(predictor_shape) face['cigarette'] = self.get_cigarette_info(face_shape, face_shades_width) face['glasses'] = self.get_glasses_info(face_shape, face_shades_width) faces.append(face) return faces def start(self): self.console("程序启动成功.") self.init_mask() while self.listener: frame = self.read_data() frame = resize(frame, width=self.max_width) img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = self.detector(img_gray, 0) faces = self.orientation(rects, img_gray) draw_img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) if self.doing: self.drawing(draw_img, faces) self.animation_time += self.speed self.save_data(draw_img) if self.animation_time > self.duration: self.doing = False self.animation_time = 0 else: frame = cv2.cvtColor(np.asarray(draw_img), cv2.COLOR_RGB2BGR) cv2.imshow("hello mask", frame) self.listener_keys() def listener_keys(self): key = cv2.waitKey(1) & 0xFF #按字母q是退出 if key == ord("q"): self.listener = False self.console("程序退出") sleep(1) self.exit() #按字母d是开始戴墨镜和叼烟 if key == ord("d"): self.doing = not self.doing def exit(self): self.video_capture.release() cv2.destroyAllWindows() def drawing(self, draw_img, faces): for face in faces: if self.animation_time < self.duration - self.fixed_time: current_x = int(face["glasses"]["pos"][0]) current_y = int(face["glasses"]["pos"][1] * self.animation_time / (self.duration - self.fixed_time)) draw_img.paste(face["glasses"]["image"], (current_x, current_y), face["glasses"]["image"]) cigarette_x = int(face["cigarette"]["pos"][0]) cigarette_y = int(face["cigarette"]["pos"][1] * self.animation_time / (self.duration - self.fixed_time)) draw_img.paste(face["cigarette"]["image"], (cigarette_x, cigarette_y), face["cigarette"]["image"]) else: draw_img.paste(face["glasses"]["image"], face["glasses"]["pos"], face["glasses"]["image"]) draw_img.paste(face["cigarette"]["image"], face["cigarette"]["pos"], face["cigarette"]["image"]) draw_img.paste(self.text, (75, draw_img.height // 2 + 128), self.text) @classmethod def console(cls, s): print("{} !".format(s))if __name__ == '__main__': ms = DynamicStreamMaskService() ms.start()
3.4 效果:
4 小插曲:
=======
4.1 本机是华为笔记本电脑,预装深度deepin-linux操作系统,原因众所周知!使用起来还是很方便的!
4.2 在我调用摄像头时,起初竟然一片漆黑!!于是,我私下“骂了华为”,什么“高科技”笔记本电脑,竟然没有安装摄像头!!!
4.3 后来网上一查,还真是高科技,华为牛!摄像头藏在键盘7上面,如下图,按一下弹出来,牛!
自己整理并分享出来,喜欢的点赞、转发和收藏。