예제 #1
0
def data_save(name,card):
    path = r'./users/' + str(card) +'/User.' + str(card) + '.txt'
    file = open(path,'w')             
    file.write(name)
    file.write(' ')
    file.write(str(card))
    file.close()
    send.socket_client(path)
예제 #2
0
def FaceRecognitionCollect():
    cam = cv2.VideoCapture(0)
    minW = 0.1*cam.get(3)
    minH = 0.1*cam.get(4)
    
    count = 0
    index = ''.join(random.sample('zyxwvutsrqponmlkjihgfedcba',6))   #生成6位索引码
    print('请按ESC键退出')
    
    #start = time.perf_counter()
    
    while True:
        #end = time.perf_counter()
        
        ret, img = cam.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = detector.detectMultiScale(
            gray,
            scaleFactor=1.2,
            minNeighbors=5,
            minSize=(int(minW), int(minH))
        )

        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
            count += 1
            path = r"./askers/Asker." + str(index) + '.' + str(count) + '.jpg'
            cv2.imwrite(path, gray[y: y + h, x: x + w])   # 保存图像
            send.socket_client(path)   #上传图像
            cv2.imshow('image', img)

        k = cv2.waitKey(1)   # 保持画面的持续
        
        if k == 27:   # 通过esc键退出摄像
            FaceRecognitionCollectSuccess_flag = 0
            print('您已按下ESC键退出录入!')
            break
        #elif (end - start) > 15:   # 超过15s退出摄像
            #FaceRecognitionCollectSuccess_flag = 0
            #print('识别超时!请检查您当前光线是否良好后重新录入。')
            #break
        elif count >= 20:   # 得到20个样本后退出摄像
            FaceRecognitionCollectSuccess_flag = 1
            print('数据已上传,正在识别 ...')
            break
        
    cam.release()   # 关闭摄像头
    cv2.destroyAllWindows()
    
    flag_save(index,FaceRecognitionCollectSuccess_flag)   #识别数据是否异常
예제 #3
0
def face_save():

    face_name = input('\n 请输入你的姓名:')   #外接小程序
    face_card = input('\n 请输入你的校园卡号:')   #外接小程序
    while len(face_card)!=12:
        face_card = input('\n 校园卡号必须为12位,请重新输入:')
    card_mkdir(face_card)
    data_save(face_name, face_card)
    
    cap = cv2.VideoCapture(0)   # 调用摄像头
    print('\n 请看向摄像头并等待一会儿...')
    
    count = 0
    start = time.time()
    
    while True:
        
        end = time.time()
        
        sucess, img = cap.read()   # 从摄像头读取图片
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)   # 转为灰度图片
        faces = detector.detectMultiScale(gray, 1.3, 5)   # 检测人脸
        
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x+w, y+w), (255, 0, 0))
            count += 1
            path = r"./users/" + str(face_card) + '/User.' + str(face_card)+ '.' + str(count) + '.jpg'
            cv2.imwrite(path, gray[y: y + h, x: x + w])   # 保存图像
            send.socket_client(path)   #上传图像
            cv2.imshow('image', img)
            
        k = cv2.waitKey(1)   # 保持画面的持续
        
        if k == 27:   # 通过esc键退出摄像
            FaceDataCollectSuccess_flag = 0
            print('您已按下ESC键退出录入!')
            break
        elif (end - start) > 30:   # 超过30s退出摄像
            FaceDataCollectSuccess_flag = 0
            print('录入超时!请检查您当前光线是否良好后重新录入。')
            break
        elif count >= 200:   # 得到200个样本后退出摄像
            FaceDataCollectSuccess_flag = 1
            print('录入成功!')
            break
        
    cap.release()   # 关闭摄像头
    cv2.destroyAllWindows()

    flag_save(face_card,FaceDataCollectSuccess_flag)   #录入数据是否异常
예제 #4
0
def deal_data(conn, addr):
    # Received a new connection request from
    print('\n收到了从 {0}:{1} 发来的一个新的连接请求'.format(addr[0], addr[1]))
    FaceDataCollectSuccess_flag = 1
    while True:
        try:
            # 首先接收文件名和大小信息(这里不接收文件的其它属性)
            # Firstly receive the file name and size information(no other attributes of the file are received here)
            # 128si: 定义这些信息由128位长度字符串和int字符组成
            # 128si: Defines that this information consists of 128-bit length strings and int characters
            fileinfo_size = struct.calcsize('128si')
            buf = conn.recv(fileinfo_size)
            if buf:
                # 解包发送过来的文件信息 Unpack file information
                filename, filesize = struct.unpack('128si', buf)
                # 解码文件名称 Decode file name
                fn = filename.decode().strip('\00')

                recvd_size = 0
                # 定义已接收文件的大小 Define the size of the received file
                if fn[0:5] == "Asker" and fn[-3:] == "txt":
                    isExists = os.path.exists('./Query/' + fn[6:12])
                    if not isExists:
                        os.makedirs('./Query/' + fn[6:12])
                    fp = open("./Query/" + fn[6:12] + "/" + fn, 'wb')
                    print('开始接收...')  # Begin receiving
                    # 接收文件主体数据 Receiving file body data
                    while not recvd_size == filesize:
                        if filesize - recvd_size > 1024:
                            data = conn.recv(1024)
                            recvd_size += len(data)
                        else:
                            data = conn.recv(filesize - recvd_size)
                            recvd_size = filesize
                        fp.write(data)
                    fp.close()
                    if GetFaceRecognitionCollectSuccessFlag.get_FaceRecognitionCollectSuccess_flag(
                            fn[6:12]) == "1":
                        send.socket_client(
                            str(
                                FaceRecognition.face_recognition(
                                    fn[6:12], FaceDataCollectSuccess_flag)))
                    else:
                        send.socket_client("0")
                elif fn[0:4] == "User" and fn[-3:] == "txt":
                    if fn[-8:-4] == 'flag':
                        fp = open("./Facedata_flag/" + fn, 'wb')
                        print('开始接收...')  # Begin receiving
                        # 接收文件主体数据 Receiving file body data
                        while not recvd_size == filesize:
                            if filesize - recvd_size > 1024:
                                data = conn.recv(1024)
                                recvd_size += len(data)
                            else:
                                data = conn.recv(filesize - recvd_size)
                                recvd_size = filesize
                            fp.write(data)
                        fp.close()
                        if GetFaceDataCollectSuccessFlag.get_FaceDataCollectSuccess_flag(
                                int(fn[5:-9])) == 0:
                            pass
                    else:
                        fp = open("./Data/" + fn, 'wb')
                        print('开始接收...')  # Begin receiving
                        # 接收文件主体数据 Receiving file body data
                        while not recvd_size == filesize:
                            if filesize - recvd_size > 1024:
                                data = conn.recv(1024)
                                recvd_size += len(data)
                            else:
                                data = conn.recv(filesize - recvd_size)
                                recvd_size = filesize
                            fp.write(data)
                        fp.close()
                elif fn[0:4] == "User" and fn[-3:] == "jpg":
                    fp = open("./Facedata/" + fn, 'wb')
                    print('开始接收...')  # Begin receiving
                    # 接收文件主体数据 Receiving file body data
                    while not recvd_size == filesize:
                        if filesize - recvd_size > 1024:
                            data = conn.recv(1024)
                            recvd_size += len(data)
                        else:
                            data = conn.recv(filesize - recvd_size)
                            recvd_size = filesize
                        fp.write(data)
                    fp.close()
                    if fn[-7:-4] == "200":
                        FaceTraining.face_training()
                elif fn[0:5] == "Asker" and fn[-3:] == "jpg":
                    isExists = os.path.exists('./Query/' + fn[6:12])
                    if not isExists:
                        os.makedirs('./Query/' + fn[6:12])
                    fp = open("./Query/" + fn[6:12] + '/' + fn, 'wb')
                    print('开始接收...')  # Begin receiving
                    # 接收文件主体数据 Receiving file body data
                    while not recvd_size == filesize:
                        if filesize - recvd_size > 1024:
                            data = conn.recv(1024)
                            recvd_size += len(data)
                        else:
                            data = conn.recv(filesize - recvd_size)
                            recvd_size = filesize
                        fp.write(data)
                    fp.close()
                else:
                    fp = open(fn, 'wb')
                    print('开始接收...')  # Begin receiving
                    # 接收文件主体数据 Receiving file body data
                    while not recvd_size == filesize:
                        if filesize - recvd_size > 1024:
                            data = conn.recv(1024)
                            recvd_size += len(data)
                        else:
                            data = conn.recv(filesize - recvd_size)
                            recvd_size = filesize
                        fp.write(data)
                    fp.close()
                print('接收结束!')  # Receiving finished
                # The received file name is ... and the file size is ... bytes.
                print('接收到的文件名为 {0}, 文件大小为 {1} 字节'.format(fn, filesize))
                f = open("addr.txt", 'w')
                f.write(addr[0])
                f.close()
                print('\n等待连接中...')  # Waiting for the connection...
            conn.close()
        except Exception:
            traceback.print_exc()
            print(buf)
            print("请求操作失败!")  # The request operation failed!
            conn.close()
            print('\n等待连接中...')  # Waiting for the connection...
        break
예제 #5
0
def flag_save(index,flag):
    path = r'./askers/Asker.' + str(index) + '.txt'
    file = open(path,'w')             
    file.write(str(flag))
    file.close()
    send.socket_client(path)
예제 #6
0
def flag_save(card,flag):
    path = r'./users/' + str(card) + '/User.' + str(card) + '.flag.txt'
    file = open(path,'w')             
    file.write(str(flag))
    file.close()
    send.socket_client(path)