用python玩玩人脸识别
<p>人脸识别,乍一听还是高大上的东西.</p> <p>今天我们就分享下怎么玩?</p> <p>不需要一大坨代码,只要你会装……包.</p> <p>当然,我们的重点不是装包,归根结底,还是怎么玩.</p> <p>Ok,那么,我们开始</p> <h2>一、需要安装什么?</h2> <p>1.OpenCV 一个图像处理的强大的包.官网 <a href="/misc/goto?guid=4959751276337147569" rel="nofollow,noindex">http://opencv.org/,最新的版本3.3.0</a></p> <ul> <li>不需要最新的版本直接brew install opencv3,我这里装好是3.2.0的</li> <li>如果需要最新的版本3.3.0的话,请按照这里安装 <a href="/misc/goto?guid=4959751276424772601" rel="nofollow,noindex">http://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/</a></li> </ul> <p>2.Python2.7或者Python3.x</p> <p>3.依赖包NumPY. 可以pip install numpy</p> <p>4.依赖包face <em>recognition.主角就是他了.pip install face</em> recognition</p> <p>注意事项参照地址 <a href="/misc/goto?guid=4959751276510775961" rel="nofollow,noindex">https://github.com/ageitgey/face_recognition</a></p> <h2>二、怎么玩</h2> <p>1.我们首先给定一张被照片</p> <p><img src="https://simg.open-open.com/show/bfb81de5f13de6705b340560b2d9a4d6.png"></p> <p>2.从摄像头捕捉的图像里获取检测出头像,跟上面的照片的图片进行对比.</p> <p>先看下结果:</p> <p><img src="https://simg.open-open.com/show/5ca1dd07ff354e2473057b8c2589932a.png"></p> <p>从截下来的图片来看. 我自己因为没有放图片上去进行识别,所以是unknown的. 手机上的图片被识别到了写上chu(名字) 标签.</p> <p>代码:</p> <pre> <code class="language-python">import face_recognition import cv2 import time #获取摄像头 video_capture = cv2.VideoCapture(0) # 加载图片 obama_image = face_recognition.load_image_file("IMG_20170723_213850R.jpg") # 图片先识别一遍人脸,后面用来比较 obama_face_encoding = face_recognition.face_encodings(obama_image)[0] # 初始化变量 face_locations = [] face_encodings = [] face_names = [] process_this_frame = True while True: # 从摄像头获取图像 ret, frame = video_capture.read() # 缩放1/4大小,方便快速处理 small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) # 保证只处理一遍 if process_this_frame: #找出所有的人脸 face_locations = face_recognition.face_locations(small_frame) face_encodings = face_recognition.face_encodings(small_frame, face_locations) face_names = [] for face_encoding in face_encodings: # 判断是否匹配给定的人脸 match = face_recognition.compare_faces([obama_face_encoding], face_encoding) name = "Unknown" if match[0]: name = "CHU<del>" face_names.append(name) process_this_frame = not process_this_frame # 把识别到和为识别到的人脸进行标记 for (top, right, bottom, left), name in zip(face_locations, face_names): # 原来是1/4的大小,现在放大4倍 top *= 4 right *= 4 bottom *= 4 left *= 4 # 给脸部画框 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # 脸部下面显示标签 cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.5, (255, 255, 255), 3) #识别到时候进行抓屏 for name in face_names: if name=="CHU</del>": cv2.imwrite('img-%d.png'%int(time.time()), frame) # 显示加了头像框视频 cv2.imshow('Video', frame) # Hit 'q' on the keyboard to quit! if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头句柄 video_capture.release() cv2.destroyAllWindows()</code></pre> <p> </p> <p>来自:http://tech.dianwoda.com/2017/08/11/yong-pythonwan-wan-ren-lian-shi-bie/</p> <p> </p>
本文由用户 lioncloud6 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!