python 人脸检测初步实现

人脸检测已经有现成的库类实现,这里只是简单的调用python的face_recognition进行检测,后面有时间会进行重构。

以下是源码部分

import wx
import os
class MainWindow(wx.Frame):
    def __init__(self,parent,title):
        self.dirname=''
        wx.Frame.__init__(self,parent,title=title,size=(600,400))
        self.control=wx.TextCtrl(self,style=wx.TE_MULTILINE)
        self.CreateStatusBar()
        filemenu=wx.Menu()
        menuOpen=filemenu.Append(wx.ID_OPEN,u'face_recognition',u'face_recognition')
        filemenu.AppendSeparator()
        menuBar=wx.MenuBar()
        menuBar.Append(filemenu,'face_recognition')
        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU,self.OnOpen,menuOpen)
        self.Show(True)
    def OnOpen(self,e):
        import face_recognition as fr
        import cv2
        cap=cv2.VideoCapture(0)
        while 1:
            ret,frame=cap.read()
            if ret:
                fl=fr.face_locations(frame)
                b=[]
                for i in fl:
                    for j in i:
                        b.append(j)
                temp=int(len(b)/4)
                for i in range(0,temp):
                    cv2.rectangle(frame,(b[i*4+1],b[i*4]),(b[i*4+3],b[i*4+2]),(0,255,0))
                cv2.imshow('face_recognition and q|Q quit',frame)
            if cv2.waitKey(1)==ord('q'):
                break

        cap.release()
        cv2.destroyAllWindows()
app=wx.App(False)
frame=MainWindow(None,title='face_recognition')
app.MainLoop()

用了简单的wxpython做界面(特别丑),打开电脑前置摄像头进行拍摄,理论上支持多人检测,后面有时间会加上其他功能,如人脸的具体部位检测等。

猜你喜欢

转载自www.cnblogs.com/kangpenglin/p/9363500.html