face_register.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #coding=utf-8
3 
4 import time,cv2
5 import os
6 import base64
7 import urllib2
8 import sys
9 import os
10 from Tkinter import *
11 import tkFileDialog
12 from json import *
13 
14 result = {0:"成功", 1:"请求超时",2:"识别/核身解析结果错误",3:"识别解析结果错误",4:"识别解析相似度错误",
15  5:"输入信息错误",6:"无该Userid对应信息",7:"获取注册人脸图片数据错误",
16  8:"Base64解码错误",9:"人脸数据提取失败",10:"删除人脸失败",11:"图片过大,不超过初始配置大小(1280,720",
17  12:"文件不存在",13:"人脸图片打开失败",14:"人脸已存在(注册人脸时method=normal情况下,userid重复",
18  15:"未检测到网卡",16:"输入信息不合法",17:"一键开门失败",18:"文件读取失败",2019:"恢复出厂设置失败",
19  2010:"数据清除失败",2021:"获取日志列表失败",22:"MAC地址不匹配"}
21  cmd = "ls /dev/video* > vs.out"
22  os.system(cmd)
23  vf = open('vs.out','r')
24  vs = vf.readlines()
25  vf.close()
26  vn = len(vs)
27  # print vs
28  for dev in vs:
29  vd = dev[:-1]
30  cmd = "udevadm info "+vd+" |grep 'ID_VENDOR_ID=0bda' > vs.info"
31  os.system(cmd)
32  infof = open("vs.info",'r')
33  info = infof.readlines()
34  infof.close()
35  # print info
36  if len(info)!=0:
37  return int(vd[-1])
38 
39  return -1
40 
41 class face_register():
42  def __init__(self):
43  try:
44  mode = sys.argv[1]
45  if mode =="image":
46  user_name = sys.argv[1]
47  image_path = sys.argv[2]
48  self.image_register(user_name)
49  if mode =="camera":
50  camera_index = getCameraId()
51  self.camera_register(camera_index)
52  except KeyboardInterrupt:
53  print(" Has Exited or Finished!")
54  except IndexError:
55  print("Input ERROR!")
56  print("Please input the correct command and try again!")
57  print("使用摄像头交互进行注册:")
58  print("rosrun xbot_face 01face_register.py camera [camera_index]")
59  print("使用照片进行注册:")
60  print("rosrun xbot_face 01face_register.py image [user_name] ")
61 
62  # 使用照片进行注册
63  def image_register(self,user_name):
64  print("使用照片进行注册:")
65  url = "http://192.168.8.141:8000/management/register?method=force"
66  body = str()
67  post_data = dict()
68  post_data["Userid"] = user_name
69  image_path = tkFileDialog.askopenfilename()
70 
71  with open(image_path, "rb") as fp:
72  image_binary = fp.read()
73  image_binary = base64.b64encode(image_binary)
74  post_data["Image"] = image_binary
75  if post_data:
76  body = JSONEncoder().encode(post_data)
77  req = urllib2.Request(url, body)
78  response = urllib2.urlopen(req)
79  buf = response.read()
80  body = JSONDecoder().decode(buf)
81  ret = body["Ret"]
82  print("返回状态:"),
83  print body["Ret"],
84  print result[ret]
85 
86 
87  # 使用摄像头交互进行注册
88  def camera_register(self,camera_index):
89  print("使用摄像头交互进行注册:")
90  url = "http://192.168.8.141:8000/management/register?method=normal"
91  cap = cv2.VideoCapture(camera_index)
92  body = str()
93  post_data = dict()
94  while(True):
95 
96  ret, frame = cap.read()
97 
98 
99  cv2.imshow('frame',frame)
100  if cv2.waitKey(1) & 0xFF == ord('r'):
101  cv2.imwrite('tmp.jpg',frame)
102  tip = '请用拼音输入你的名字:\n'
103  name = input(tip)
104  post_data["Userid"] = name
105  with open('tmp.jpg', "rb") as fp:
106  image_binary = fp.read()
107  image_binary = base64.b64encode(image_binary)
108  post_data["Image"] = image_binary
109  if post_data:
110  body = JSONEncoder().encode(post_data)
111  req = urllib2.Request(url, body)
112  response = urllib2.urlopen(req)
113  buf = response.read()
114  body = JSONDecoder().decode(buf)
115  ret = body["Ret"]
116  print("返回状态:"),
117  print body["Ret"],
118  print result[ret]
119  elif cv2.waitKey(1) & 0xFF == ord('q'):
120  break
121 
122  # When everything done, release the capture
123  cap.release()
124  cv2.destroyAllWindows()
125 
126 
127 if __name__ == '__main__':
128  face_register()
129 
def camera_register(self, camera_index)
def image_register(self, user_name)


xbot_face
Author(s):
autogenerated on Sat Oct 10 2020 03:27:46