예제 #1
0
def readPicSaveFace(sourcePath, objectPath, *suffix):
    if not os.path.exists(objectPath):
        os.mkdir(objectPath)
    try:
        #读取照片,注意第一个元素是文件名
        resultArray, _ = readAllImg(sourcePath, *suffix)

        #对list中图片逐一进行检查,找出其中的人脸然后写到目标文件夹下
        count = 1
        face_cascade = cv2.CascadeClassifier(
            "haarcascade_frontalface_default.xml")
        for img in resultArray:
            # if type(img) != str:
            #     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            if img.ndim == 3:
                gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            else:
                gray = img
            faces = face_cascade.detectMultiScale(gray, 1.1, 5)
            for (x, y, w, h) in faces:
                listStr = [str(int(time.time())),
                           str(count)]  #以时间戳和读取的排序作为文件名称
                fileName = ''.join(listStr)
                f = cv2.resize(gray[y:(y + h), x:(x + w)], (128, 128))
                cv2.imwrite(objectPath + os.sep + '%s.jpg' % fileName, f)
                count += 1

    except IOError:
        print("Error")

    else:
        print('Already read ' + str(count - 1) + ' Faces to Destination ' +
              objectPath)
예제 #2
0
def readPicSaveFace(sourcePath, objectPath, *suffix):
    try:
        #Read image
        resultArray = readAllImg(sourcePath, *suffix)

        #Check the image in the list one by one. Find the face and put into the object path

        count = 1
        face_cascade = cv2.CascadeClassifier(
            'D:\proj\opencv-master\opencv-master\data\haarcascades\haarcascade_frontalface_alt.xml'
        )
        for i in resultArray:
            if type(i) != str:

                gray = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.3, 5)
                for (x, y, w, h) in faces:
                    listStr = [str(int(time.time())), str(count)]
                    fileName = ''.join(listStr)

                    f = cv2.resize(gray[y:(y + h), x:(x + w)], (200, 200))
                    cv2.imwrite(objectPath + os.sep + '%s.jpg' % fileName, f)
                    count += 1

    except IOError:
        print("Error")

    else:
        print('Already read ' + str(count - 1) + ' Faces to Destination ' +
              objectPath)
예제 #3
0
파일: pick_face.py 프로젝트: xiaotianxxx/ml
def readPicSaveFace(sourcePath, objectPath, *suffix):
    try:
        #读取照片,注意第一个元素是文件名
        resultArray = readAllImg(sourcePath, *suffix)

        #对list中图片逐一进行检查,找出其中的人脸然后写到目标文件夹下

        count = 1
        face_cascade = cv2.CascadeClassifier(
            '/Users/i037762/tf/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_alt.xml'
        )
        for i in resultArray:
            if type(i) != str:

                gray = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.3, 5)
                for (x, y, w, h) in faces:

                    listStr = [str(int(time.time())),
                               str(count)]  #以时间戳和读取的排序作为文件名称
                    fileName = ''.join(listStr)

                    f = cv2.resize(gray[y:(y + h), x:(x + w)], (200, 200))
                    cv2.imwrite(objectPath + os.sep + '%s.jpg' % fileName, f)
                    count += 1

    except IOError:
        print("Error")

    else:
        print('Already read ' + str(count - 1) + ' Faces to Destination ' +
              objectPath)
def readPicSaveFace(sourcePath, objectPath, *suffix):
    try:
        # 读取照片,注意第一个元素是文件名
        resultArray = readAllImg(sourcePath, *suffix)

        # 对list中图片逐一进行检查,找出其中的人脸然后写到目标文件夹下
        count = 1
        # 加载人脸特征库
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
                                             'haarcascade_frontalface_alt.xml')
        for i in resultArray:
            if type(i) != str:
                gray = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.3, 5)
                for (x, y, w, h) in faces:
                    # 以时间戳和读取的排序作为文件名称
                    listStr = [str(int(time.time())), str(count)]
                    print(listStr)
                    fileName = ''.join(listStr)

                    f = cv2.resize(gray[y:(y + h), x:(x + w)], (200, 200))
                    cv2.imwrite(objectPath + os.sep + '%s.jpg' % fileName, f)
                    count += 1

    except IOError:
        print("Error")

    else:
        print('已经读取 ' + str(count - 1) + ' 张照片到 ' + objectPath + '目录。')
예제 #5
0
def readPicSaveFace(sourcePath, objectPath, *suffix):
    try:
        # Read the photo, note that the first element is the file name
        resultArray = readAllImg(sourcePath, *suffix)

        # Check the pictures in the list one by one, find the faces in them and write them to the target folder
        count = 1
        # Load face feature library
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
                                             'haarcascade_frontalface_alt.xml')
        for i in resultArray:
            if type(i) != str:
                gray = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.3, 5)
                for (x, y, w, h) in faces:
                    # File name with timestamp and reading order
                    listStr = [str(int(time.time())), str(count)]
                    print(listStr)
                    fileName = ''.join(listStr)

                    f = cv2.resize(gray[y:(y + h), x:(x + w)], (200, 200))
                    cv2.imwrite(objectPath + os.sep + '%s.jpg' % fileName, f)
                    count += 1

    except IOError:
        print("Error")

    else:
        print('Already read ' + str(count - 1) + ' Photos to ' + objectPath +
              'directory')
예제 #6
0
def readPicSaveFace(sourcePath, objectPath, *suffix):
    try:
        resultArray = readAllImg(sourcePath, *suffix)
        count = 1
        face_cascade = cv2.CascadeClassifier(
            'models/haarcascade_frontalface_default.xml')
        for i in resultArray:
            if type(i) != str:

                gray = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.3, 5)
                for (x, y, w, h) in faces:

                    listStr = [str(int(time.time())),
                               str(count)]  #以时间戳和读取的排序作为文件名称
                    fileName = ''.join(listStr)

                    f = cv2.resize(gray[y:(y + h), x:(x + w)], (200, 200))
                    cv2.imwrite(objectPath + os.sep + '%s.png' % fileName, f)
                    count += 1

    except IOError:
        print("Error")

    else:
        print('Already read ' + str(count - 1) + ' Faces to Destination ' +
              objectPath)
예제 #7
0
def readPicSaveFace(sourcePath, objectPath, *suffix):
    try:
        # 读取照片,注意第一个元素是文件名
        resultArray = readAllImg(sourcePath, *suffix)

        # 对list中图片逐一进行检查,找出其中的人脸然后写到目标文件夹下

        count = 1
        face_cascade = cv2.CascadeClassifier(
            './haarcascade_frontalface_alt.xml')
        for i in resultArray:
            if type(i) != str:

                # gray = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)

                # 我也不知道为什么 。。。。。注释掉这行就可以了

                faces = face_cascade.detectMultiScale(i, 1.3, 5)
                for (x, y, w, h) in faces:
                    listStr = [str(int(time.time())),
                               str(count)]  # 以时间戳和读取的排序作为文件名称
                    fileName = ''.join(listStr)

                    f = cv2.resize(i[y:(y + h), x:(x + w)], (92, 112))
                    cv2.imwrite(objectPath + os.sep + '%s.pgm' % fileName, f)
                    count += 1
    except IOError:
        print("Error")

    else:
        print('Already read ' + str(count - 1) + ' Faces to Destination ' +
              objectPath)
def readPicSaveFace(sourcePath, objectPath, *suffix):
    try:
        #读取照片,注意第一个元素是文件名
        resultArray = readAllImg(sourcePath, *suffix)
        # print(resultArray)
        #对list中图片逐一进行检查,找出其中的人脸然后写到目标文件夹下
        count = 1
        # face_cascade = cv2.CascadeClassifier('E:\openCV\opencv\sources\data\haarcascades\
        # D:\Program Files (x86)\opencv\sources\data\haarcascades
        # face_cascade = cv2.CascadeClassifier(r'D:/Program Files(x86)/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml')
        # pathf = 'D:\\Program Files(x86)\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml'
        # pathf = r'D:/Program Files(x86)/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml'
        # face_cascade = cv2.CascadeClassifier(pathf)
        # another
        # face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
        # face_cascade = cv2.CascadeClassifier(r'D:/my_laboratory/face_detection20180516/source - cynthia/haarcascade_frontalface_alt.xml')
        # face_cascade.load(r'D:/Program Files(x86)/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml')
        face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
        # print(len(resultArray))
        for i in resultArray:
            print(type(i))
            if type(i) != str:
                gray = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.3, 5)
                for (x, y, w, h) in faces:
                    listStr = [str(int(time.time())),
                               str(count)]  #以时间戳和读取的排序作为文件名称
                    fileName = ''.join(listStr)
                    # f = cv2.resize(gray[y:(y + h), x:(x + w)], (200, 200))
                    f = cv2.resize(gray[y:(y + h), x:(x + w)], (250, 250))
                    # # print(type(f))
                    # cv2.imshow("Output1",f)
                    # cv2.waitKey(0)
                    # cv2.destroyAllWindows()
                    # cv2.imwrite(objectPath+os.sep+'%s.jpg' % fileName, f)
                    cv2.imwrite(objectPath + '%s.jpg' % fileName, f)
                    count += 1

    except IOError:
        print("Error")

    else:
        print('Already read ' + str(count - 1) + ' Faces to Destination ' +
              objectPath)
예제 #9
0
def test_onBatch(path):
    model= Model(read_save=True)
    model.load()
    index = 0
    img_list,img_name_list = readAllImg(path,'.jpg')
    face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    for img in img_list:
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray,1.1,5)
        for (x, y, w, h) in faces:
            face = cv2.resize(gray[y:(y + h), x:(x + w)], (128, 128))
        picType,prob = model.predict(face)
        name_list = read_name_list('G:\Python/FaceRecognition\datasets')
        if picType != -1:
        # if picType>=0 and picType<len(name_list):
            print (img_name_list[index],":",name_list[picType],",",prob)
            index += 1
        else:
            print (" Don't know this person")

    return index