用face_recognition写一个简单的人脸识别分类的程序

基于:python2.7
前几天在研究机器学习,发现了一个face_recognition的人脸识别包,于是想利用这个包做一个简易的照片按人脸分类的程序。
思路如下

1.把文件内的图片重新命名

def rename():
    path="{}/people/".format(global_path)
    filelist = os.listdir(path)
    global count
    count=1
    for files in filelist:
        Olddir = os.path.join(path,files)
        if not (files[-3:] == "JPG" or files[-3:] == "jpg"):
            continue
        filename=os.path.splitext(files)
        Newdir=os.path.join(path,"{}{}.jpg".format(new_name,count))
        os.rename(Olddir,Newdir)
        count+=1

利用的是通过遍历循环目录,将目录中jpg格式的图片按照“1、2、3…”的形式改名

2.用face_recognition取出特征值,并序列化写入文件

def code_file(start):
    for i in range(start,count):
        if os.path.exists("{}/{}".format(global_path,i)):
            shutil.rmtree("{}/{}".format(global_path,i))
        #读文件
        locals()['test%s_image'%i]=face_recognition.load_image_file("{}/people/test{}.jpg".format(global_path,i))
        #创建目录
        os.mkdir("{}/{}".format(global_path,i))
        #转移图片
        shutil.copy("{}/people/test{}.jpg".format(global_path,i),"{}/{}".format(global_path,i))
        #解码
        locals()['test%s_face_encoding'%i]=face_recognition.face_encodings(locals()['test%s_image'%i])
        #放入文件
        w = 0
        for char in locals()['test%s_face_encoding'%i]:
            w+=1
            code_file = open("{}/{}/code_file{}".format(global_path,i,w),'w+')
            code_file.write(pickle.dumps(char))
            code_file.close()

因为之前已经把图片的名字按顺序从新命名了,现在只要按照顺序迭代,利用face_recognition.face_encodings函数将图片解码,pickle序列化后写入文件。用os.mkdir,shutil.copy同时将文件和图片放在同一个文件夹,方便之后的分类。

3.图片的分类

def categorize(start):
    for i in range(start,count):
        for j in range(1,5):
            if os.path.exists("{}/{}/code_file{}".format(global_path,i,j)):
                code_file=open("{}/{}/code_file{}".format(global_path,i,j),'r')
            else:
                continue
            for x in range(start,count):
                if i >= x:
                    continue
                for y in range(1,5):
                    if os.path.exists("{}/{}/code_file{}".format(global_path,x,y)):
                        code_file2=open("{}/{}/code_file{}".format(global_path,x,y),"r")
                        code_file.seek(0, os.SEEK_SET)
                        #读码
                        code_rev = pickle.loads(code_file.read())
                        code_rev2 = pickle.loads(code_file2.read())
                        list_code=[]
                        list_code.append(code_rev)
                        #比较
                        result = face_recognition.compare_faces(list_code,code_rev2,tolerance=0.4)
                        print(result[0])
                        if result[0]:
                            #创建人物文件夹
                            if not os.path.exists("{}/people_{}_{}".format(global_path,i,j)):
                                os.mkdir("{}/people_{}_{}".format(global_path,i,j))
                            #转移图片
                            shutil.copy("{}/people/test{}.jpg".format(global_path,x),"{}/people_{}_{}".format(global_path,i,j))
                            shutil.copy("{}/people/test{}.jpg".format(global_path,i),"{}/people_{}_{}".format(global_path,i,j))
                            #删除编码文件
                            os.remove("{}/{}/code_file{}".format(global_path,x,y))


                    else:
                        continue

思路:
这里写图片描述

4. 组装函数

def main(g_path,n_name):
    global global_path
    global_path=g_path
    global new_name
    new_name=n_name

    rename()
    code_file(1)
    categorize(1)

main("/home","test")

不足之处:
1.python刚入门不久,对相关的一些语法书写不能做到尽可能整洁,有逻辑性,代码的可读性比较差。
2.经过测试,如果图片文件比较大的话会导致识别率降低,建议先压缩文件。
3.本代码只作为一个简单小量性的人脸识别分类,目的是锻炼一下自己的思维逻辑能力,不适用大型的计算分类。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值