def build_camera(self):
        # The location of the face cascade file in the opencv file, used to help identify faces in images or video streams
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
                                             'haarcascade_frontalface_alt.xml')
        # Read the subfolder name under the dataset
        name_list = read_name_list('./dataset')

        # Turn on the camera and start reading
        cameraCapture = cv2.VideoCapture(700)
        success, frame = cameraCapture.read()

        while success and cv2.waitKey(1) == -1:
            success, frame = cameraCapture.read()
            # Grayscale image
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # Face detection: scaleFactor is
            #the magnification ratio (this parameter must be greater than 1);
            #minNeighbors is the number of repeated identifications (this parameter is used to adjust the accuracy, the larger the more accurate)
            faces = face_cascade.detectMultiScale(gray,
                                                  scaleFactor=1.3,
                                                  minNeighbors=8)
            for (x, y, w, h) in faces:
                origin = gray[x:x + w, y:y + h]
                # origin is the original image, self.img_size is the size of the output image, and interpolation is the interpolation method
                origin = cv2.resize(origin, (self.img_size, self.img_size),
                                    interpolation=cv2.INTER_LINEAR)
                # Use the model to compare the faces recognized by cv2
                label, prob = self.model.predict(origin)
                # If the model believes that the probability is higher than 70%, it will be displayed as the existing label in the model
                if prob > 0.7:
                    show_name = name_list[label]

                else:
                    show_name = 'unknown'

                # Frame a face

                frame = cv2.rectangle(frame, (x, y), (x + w, y + h),
                                      (255, 0, 0), 1)
                if prob > 0.7:
                    #storing recognized folder in folder named with date of that day
                    today = datetime.now()
                    recpath = './Recognized/' + today.strftime("%d-%m-%Y")
                    if not os.path.exists(recpath):
                        os.makedirs(recpath)
                        #print(os.getcwd())
                    cv2.imwrite(
                        recpath + '/' + today.strftime("%H%M%S") + '.jpg',
                        frame[y:y + h, x:x + w])
                    #if not os.path.exists()

                # Show name
                cv2.putText(frame, show_name, (x, y - 20),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)

            cv2.imshow("Recognizing...", frame)
        # Release camera
        cameraCapture.release()
        # Close all windows
        cv2.destroyAllWindows()
def test_onePicture(path):
    # 加载模型
    model = Model()
    model.load()
    # 读取图片
    img = cv2.imread(path)
    # 重置图片尺寸为:128*128
    img = cv2.resize(img, (128, 128))
    # 图片灰度化
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # labelIndex为概率最高的label的索引号, prob为对应概率
    labelIndex, prob = model.predict(img)
    if labelIndex != -1:
        name_list = read_name_list('./dataset')
        print(name_list[labelIndex], prob)
    else:
        print("Don't know this person.")
def test_onBatch(path):
    # 加载模型
    model = Model()
    model.load()
    # 计数器
    index = 0
    # 读取所有图片;img_list是所有图片的集合,label_lsit是所有标签的集合,label_num是标签数量
    img_list, label_lsit, label_num = read_file(path)
    for img in img_list:
        # labelIndex为概率最高的label的索引号, prob为对应概率
        labelIndex, prob = model.predict(img)
        if labelIndex != -1:
            index += 1
            name_list = read_name_list('./dataset')
            print(name_list[labelIndex])
        else:
            print("Don't know this person.")
    return index
Beispiel #4
0
    def build_camera(self):
        # opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
                                             'haarcascade_frontalface_alt.xml')
        # 读取dataset数据集下的子文件夹名称,即标签名
        name_list = read_name_list('./dataset')

        # 打开摄像头并开始读取画面
        cameraCapture = cv2.VideoCapture('recognize_faces2.mp4')
        success, frame = cameraCapture.read()

        while success and cv2.waitKey(1) == -1:
            success, frame = cameraCapture.read()
            # 图像灰度化
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # 检测人脸:scaleFactor是放大比例(此参数必须大于1);minNeighbors是重复识别次数(此参数用来调整精确度,越大则越精确)
            faces = face_cascade.detectMultiScale(gray,
                                                  scaleFactor=1.3,
                                                  minNeighbors=8)
            for (x, y, w, h) in faces:
                origin = gray[x:x + w, y:y + h]
                # origin是原图,self.img_size是输出图像的尺寸,interpolation是插值方法
                origin = cv2.resize(origin, (self.img_size, self.img_size),
                                    interpolation=cv2.INTER_LINEAR)
                # 利用模型对cv2识别出的人脸进行比对
                label, prob = self.model.predict(origin)
                # 如果模型认为概率高于70%则显示为模型中已有的label
                if prob > 0.7:
                    show_name = name_list[label]
                else:
                    show_name = 'unknown'

                # 框出人脸
                frame = cv2.rectangle(frame, (x, y), (x + w, y + h),
                                      (255, 0, 0), 1)
                # 显示人名
                cv2.putText(frame, show_name, (x, y - 20),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)

            cv2.imshow("Recognizing...", frame)
        # 释放摄像头
        cameraCapture.release()
        # 关闭所有窗口
        cv2.destroyAllWindows()
Beispiel #5
0
model.load()
# 读取图片
image = cv2.imread(path)

# 重置图片尺寸为:128*128、512*512
small_image = cv2.resize(image, (128, 128))

# 图片灰度化
small_gray = cv2.cvtColor(small_image, cv2.COLOR_BGR2GRAY)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 检测人脸,探测图片中的人脸
faces = face_engine.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=15)

# 读取dataset数据集下的子文件夹名称
name_list = read_name_list('./dataset')
# 框出人脸
for (x, y, w, h) in faces:
    origin = gray[x:x + w, y:y + h]
    # origin是原图,self.img_size是输出图像的尺寸,interpolation是插值方法
    origin = cv2.resize(origin, (128, 128), interpolation=cv2.INTER_LINEAR)
    # 利用模型对cv2识别出的人脸进行比对
    # 第一个返回值为概率最高的label的index,第二个返回值为对应概率
    labelIndex, prob = model.predict(origin)
    # 如果模型认为概率高于70%则显示为模型中已有的label
    if prob > 0.7:
        show_name = name_list[labelIndex]
    else:
        show_name = 'unknown'
    # 1.原始图片 2.人脸坐标原点 3.标记的高度 4,线的颜色 5,线宽
    frame = cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 1)