Interface編集部
12-4FaceAutnClient.py
000: import cv2
001: import os
002: import numpy as np
003: import socket
004: import pickle
005:
006: def __main():
007: global infoFlag
008: global faces
009: global labels
010: global authCount
011: message = “”
012:
013: HOST = ‘192.168.101.27’
014: PORT = 2000
015: BUFFER_SIZE = 4096
016: ADDRE = (HOST, PORT)
017:
018: recognizer = cv2.face.LBPHFaceRecognizer_create()
019: recognizer.train(faces, np.array(labels))
020:
021: while True:
022: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socketTCP:
023: socketTCP.connect(ADDRE)
024: fullData = b”
025:
026: while True:
027: data = socketTCP.recv(BUFFER_SIZE)
028: if len(data) <= 0:
029: break
030:
031: fullData += data
032: # print('Data = {}'.format(len(fullData)))
033: img = pickle.loads(fullData)
034:
035: # ここに処理を書く
036: dst = setResultArea(img) # 認証可能エリアを切り取る
037: camFace = getCameraFace(dst) # 認証用顔画像を取得
038: if len(camFace) is not 0: # 認証画像が無い場合はサイズが0
039: infoFlag = True
040: camFace = resizeAuthImg(camFace, faces[0])
041: camFace = cv2.cvtColor(camFace, cv2.COLOR_BGR2GRAY)
042: labelNum, cofidence = recognizer.predict(camFace)
043: if cofidence > 50:
044: authCount = 0
045: else:
046: message = ‘Label={0} level={1}’.format(labelNum, int(cofidence))
047: infoFlag = False
048:
049: if infoFlag is True:
050: message = ‘During verification’
051: setInfo(img, message) # 認証中のメッセージ
052:
053: cv2.imshow(‘VideoStream’, img)
054: if cv2.waitKey(1) != -1:
055: break
056: cv2.destroyAllWindows()
057:
058: def init(src):
059: h = src.shape[0]
060: w = src.shape[1]
061: x = w / 2
062: y = h / 2
063: rectLength = w * 0.3
064: x1 = int(x – (rectLength / 2))
065: y1 = int(y – (rectLength / 2))
066: x2 = int(x + (rectLength / 2))
067: y2 = int(y + (rectLength / 2))
068: return x1, y1, x2, y2
069:
070: def setResultArea(src):
071: x1, y1, x2, y2 = init(src)
072: dst = src[y1:y2, x1:x2]
073: cv2.rectangle(src, (x1, y1), (x2, y2), (255, 255, 255), 2)
074: return dst
075:
076: def resizeAuthImg(src, baseImg):
077:
078: height = baseImg.shape[0]
079: width = baseImg.shape[1]
080: dst = cv2.resize(src, (width, height), interpolation=None)
081: return dst
082:
083: def getCameraFace(img):
084: global cascade
085: global authCount
086: face = []
087: gray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)
088: bodyRect = cascade.detectMultiScale(image=gray, scaleFactor=1.05, minNeighbors=10, flags=None, minSize=(100, 100))
089: for x, y, w, h in bodyRect:
090: authCount += 1
091: if authCount == 10: # 撮影開始直後の映像は失敗が多いので
092: # 10枚目を認証用に採用する
093: face = img[y:y+h, x:x+w]
094: cv2.rectangle(img=img, pt1=(x, y), pt2=(x + w, y + h), color=(0, 255, 255), thickness=3)
095: return face
096:
097: def loadFace():
098: faceImages = list()
099: fileCount = 0
100: baseImg = []
101: path = (‘1’, ‘2’, ‘3’) # 写真の保存してあるフォルダ名を列挙する
102: for i, folder in enumerate(path):
103: for fileName in os.listdir(folder):
104: print(fileName)
105: face = cv2.imread(folder + ‘/’ + fileName)
106: if fileCount == 0:
107: baseImg = face
108: face = resizeAuthImg(face, baseImg)
109: face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
110: faceImages.append(face)
111: labels.append(int(folder))
112: fileCount += 1
113: return faceImages
114:
115: def setInfo(img, msg):
116: cv2.rectangle(img, (50, 50), (600, 120), (0, 0, 0), -1)
117: cv2.putText(img=img, text=”{0}”.format(msg), org=(100, 100), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1.5, color=(255, 255, 255), lineType=cv2.LINE_AA)
118:
119: if __name__ == ‘__main__’:
120: cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)
121: authCount = 0
122: infoFlag = False
123: labels = []
124: faces = loadFace()
125: __main()