Skip to content

Commit 27a35fb

Browse files
committed
face++,人脸识别考勤机第一版本
1 parent 7441edf commit 27a35fb

File tree

209 files changed

+521
-168
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

209 files changed

+521
-168
lines changed

1.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

\

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import subprocess
2+
import time
3+
4+
file_number = 0
5+
dir = "/home/pi/images/"
6+
7+
while True:
8+
file_name = dir + format(file_number,"05d")+".jpg"
9+
file_number = file_number + 1
10+
subprocess.call(["raspistill","-w","400","-h","400","-e","jpg","-n","-t","1","-o",file_name])
11+
subprocess.call(["cp","-f",file_name,dir + "live.jpg"])
12+
time.sleep(2)
13+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import tornado.ioloop
2+
import tornado.web
3+
import subprocess
4+
5+
class MainHandler(tornado.web.RequestHandler):
6+
def get(self):
7+
self.write('<! DOCTYPE html><head>'+
8+
'<META HTTP_EQUIV="refresh"' +
9+
'CONTENT="5"></head><body>' +
10+
'<img src="/images/live.jpg"></body>')
11+
class ImageHandler(tornado.web.StaticFileHandler):
12+
def set_extra_headers(self, path):
13+
self.set_header('Cache-Control',
14+
'no-store, no-cache,must-revalidate, max-age=0')
15+
16+
application = tornado.web.Application([
17+
(r"/",MainHandler),
18+
(r"/images/(.*)", ImageHandler,{"path":"/home/pi/images"})])
19+
20+
if __name__== "__main__":
21+
subprocess.Popen(["python","lianxuzhibo.py"])
22+
application.listen(8899)
23+
tornado.ioloop.IOLoop.instance().start()
24+
25+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import tornado.ioloop
2+
import tornado.web
3+
import subprocess
4+
import time
5+
6+
class MainHandler(tornado.web.RequestHandler):
7+
def get(self):
8+
subprocess.call(["raspistill", "-w", "200", "-h", "200","-e", "jpg", "-n", "-t", "1", "-o", "/home/pi/images/live.jpg"])
9+
time.sleep(2)
10+
self.write('<! DOCTYPE html><head>'+
11+
'<META HTTP_EQUIV="refresh"' +
12+
'CONTENT="5"></head><body>' +
13+
'<img src="/images/live.jpg"></body>')
14+
class ImageHandler(tornado.web.StaticFileHandler):
15+
def set_extra_headers(self, path):
16+
self.set_header('Cache-Control',
17+
'no-store, no-cache,must-revalidate,'+
18+
' max-age=0')
19+
application = tornado.web.Application([
20+
(r"/",MainHandler),
21+
(r"/images/(.*)", ImageHandler,{"path":"/home/pi/images"})])
22+
23+
if __name__== "__main__":
24+
application.listen(8889)
25+
tornado.ioloop.IOLoop.instance().start()
26+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
之前的人脸识别考勤系统,已经依靠face++和opencv基本完成了功能初步测试。最后调试下的情况是:
2+
3+
4+
管理员:使用python进行文件夹遍历,获取每个目录下的每个图片,以及name.txt文件获得名字。。。然后统一先行上传到服务进行识别训练。这时,我需要一份返回的识别组的id。
5+
6+
摄像头监听:使用python调用建立新进程指令。从而反复进行拍摄命令,获取的每一张图片均调用opencv进行人脸识别判定,如果有人脸则暂停进程,发出语音沟通,再次拍照进行二次识别。
7+
8+
活取到二次照片,进行文件沟通,首先移动到end.jpg 然后将end.txt 设定为1。。在循环的主文件中,再将文件上传进行人脸准确识别。。返回的人脸名字,将当前时间记录下来,也存储到签到表中。
9+
10+
漏洞说明:使用文件沟通,可能引发读写故障,实用face++考虑出try的情况,可能返回错误信息,那时候的处理比较复杂。
11+
12+
现在重新整理文件情况。
13+
14+
15+
程序文件的结构:
16+
admin.py   管理员
17+
18+
11.5 KB
Binary file not shown.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python2
2+
# -*- coding: utf-8 -*-
3+
# $File: admin.py
4+
5+
API_KEY = '187aa2ee8fc784972b7302f3b31becab'
6+
API_SECRET = 'F1czaFXaRJ5VzRcdVNZppUwJpDg4zjUY'
7+
8+
# 导入系统库并定义辅助函数
9+
import time
10+
from pprint import pformat
11+
def print_result(hint, result):
12+
def encode(obj):
13+
if type(obj) is unicode:
14+
return obj.encode('utf-8')
15+
if type(obj) is dict:
16+
return {encode(k): encode(v) for (k, v) in obj.iteritems()}
17+
if type(obj) is list:
18+
return [encode(i) for i in obj]
19+
return obj
20+
print hint
21+
result = encode(result)
22+
print '\n'.join([' ' + i for i in pformat(result, width = 75).split('\n')])
23+
24+
# 首先,导入SDK中的API类,还有本地文件读取所需要的facepp类
25+
from facepp import API
26+
import facepp
27+
import os
28+
29+
api = API(API_KEY, API_SECRET)
30+
#face = api.detection.detect(img = facepp.File('3.jpg'))
31+
32+
# 步骤0:人名及其脸部图片,此部分,读取目录下picture文件夹,中通过获取每文件夹的路径和名字,完成PERSONS字典的构建。
33+
34+
dir = os.getcwd()+'/picture'
35+
a = os.listdir(dir)
36+
print(a)
37+
PERSONS = []
38+
for name in a:
39+
print (name)
40+
dir_temp = dir +'/'+name
41+
b = os.listdir(dir_temp)
42+
print(b)
43+
for img_name in b:
44+
if img_name[-1] != 'g':
45+
continue
46+
dir_img =dir_temp+'/'+img_name
47+
print (dir_img)
48+
f = open(dir_temp+'/name.txt')
49+
a = f.read()
50+
f.close()
51+
dir_temp
52+
yuan = (a,facepp.File(dir_img))
53+
PERSONS.append(yuan)
54+
print (PERSONS)
55+
56+
# IMAGE_DIR 此部分则是选择出需要进行分析的图片。。管理员模式下不用设置实用。(可能有文件打开错误的bug,如果本身没有end.jpg的话。)
57+
TARGET_IMAGE = facepp.File('end.jpg')
58+
59+
# 步骤1:检测出输入图片中的Face,找出图片中Face的位置及属性
60+
61+
FACES = {name: api.detection.detect(img = url)
62+
for name, url in PERSONS}
63+
64+
for name, face in FACES.iteritems():
65+
print_result(name, face)
66+
67+
68+
# 步骤2:引用face_id,创建新的person
69+
for name, face in FACES.iteritems():
70+
rst = api.person.create(
71+
person_name = name, face_id = face['face'][0]['face_id'])
72+
print_result('create person {}'.format(name), rst)
73+
74+
75+
# 步骤3:.创建Group,将之前创建的Person加入这个Group
76+
77+
rst = api.group.create(group_name = 'test')
78+
print_result('创建一个群组--test', rst)
79+
rst = api.group.add_person(group_name = 'test', person_name = FACES.iterkeys())
80+
print_result('添加人到群组中', rst)
81+
82+
83+
# 步骤4:训练模型
84+
rst = api.train.identify(group_name = 'test')
85+
print_result('开始训练请等待', rst)
86+
87+
# 等待训练完成
88+
rst = api.wait_async(rst['session_id'])
89+
print_result('等待异步训练完成', rst)
90+
91+
'''
92+
# 步骤5:识别新图中的Face
93+
rst = api.recognition.identify(group_name = 'test', img = TARGET_IMAGE)
94+
print_result('recognition result', rst)
95+
print '=' * 60
96+
print 'The person with highest confidence:', \
97+
rst['face'][0]['candidate'][0]['person_name']
98+
'''
99+
# 最终,删除无用的person和group
100+
101+
#api.group.delete(group_name = 'test')
102+
#api.person.delete(person_name = FACES.iterkeys())
103+
104+
105+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python2
2+
# -*- coding: utf-8 -*-
3+
# $File: chaxun.py
4+
5+
API_KEY = '187aa2ee8fc784972b7302f3b31becab'
6+
API_SECRET = 'F1czaFXaRJ5VzRcdVNZppUwJpDg4zjUY'
7+
8+
# 导入系统库并定义辅助函数
9+
import time
10+
from pprint import pformat
11+
from facepp import API
12+
import facepp
13+
import os
14+
#此部分是为了中文的处理
15+
import sys
16+
reload(sys)
17+
sys.setdefaultencoding( "utf-8" )
18+
19+
def print_result(hint, result):
20+
def encode(obj):
21+
if type(obj) is unicode:
22+
return obj.encode('utf-8')
23+
if type(obj) is dict:
24+
return {encode(k): encode(v) for (k, v) in obj.iteritems()}
25+
if type(obj) is list:
26+
return [encode(i) for i in obj]
27+
return obj
28+
print hint
29+
result = encode(result)
30+
print '\n'.join([' ' + i for i in pformat(result, width = 75).split('\n')])
31+
32+
# 首先,导入SDK中的API类,还有本地文件读取所需要的facepp类
33+
34+
35+
api = API(API_KEY, API_SECRET)
36+
#face = api.detection.detect(img = facepp.File('3.jpg'))
37+
38+
# IMAGE_DIR 此部分则是选择出需要进行分析的图片。。管理员模式下不用设置实用。(可能有文件打开错误的bug,如果本身没有end.jpg的话。)
39+
TARGET_IMAGE = facepp.File('end.jpg')
40+
print ("已经读取图片,正在进行人脸识别,根据网络环境速度不定。")
41+
#识别end.jpg图中的Face
42+
rst = api.recognition.identify(group_name = 'test', img = TARGET_IMAGE)
43+
#print_result('识别结果:', rst)
44+
45+
print '=' * 60
46+
print '识别结果最匹配的对象为:', \
47+
rst['face'][0]['candidate'][0]['person_name']
48+
a = rst['face'][0]['candidate'][0]['person_name']
49+
50+
print ("正在写入kaoqin.txt中,当前时间为:"+" "+ time.asctime())
51+
print '=' * 60
52+
fp = open("kaoqin.txt",'a')
53+
fp.write("姓名:"+" "+rst['face'][0]['candidate'][0]['person_name']+"签到时间:"+" "+time.asctime()+"\n")
54+
fp.close()
55+
56+
57+
# 最终,删除无用的person和group
58+
#api.group.delete(group_name = 'test')
59+
#api.person.delete(person_name = FACES.iterkeys())
60+
61+
62+

0 commit comments

Comments
 (0)