Example #1
0
def main():
    FaceAPI.facesetdelete(check_empty=0)
    create_table()
    get_ID_name('./data/import')
    cur.execute("delete from `face_data` where ID=0;")
    conn.commit()
    creat_faceset()
    cur.close()
    conn.close()
Example #2
0
def creat_faceset():
    cur.execute("select face_token from face_data")
    result = FaceAPI.facesetcreate()
    column = cur.fetchall()
    a = zip(*column)
    a = list(a[0])
    for i in a:
        print("******************************************")
        with open('./data/log/import.log', 'r') as f:
            f = f.read()
        if i in f:
            print "{}:------Has Been Exist!".format(i)
        if i not in f:
            with open("./data/log/import.log", 'a') as f:
                f.write(i)
            print i
            result = FaceAPI.facesetaddface(face_tokens=i)
            print result
Example #3
0
def detect(filename):
    global face_token
    count = 0
    faces = []
    face_cascade1 = cv2.CascadeClassifier(
        './data/cascades/haarcascade_frontalface_alt.xml')
    face_cascade2 = cv2.CascadeClassifier(
        './data/cascades/haarcascade_frontalface_default.xml')
    img = cv2.imread(filename)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces1 = face_cascade1.detectMultiScale(gray, 1.3, 5)
    faces2 = face_cascade2.detectMultiScale(gray, 1.3, 5)
    try:
        faces = faces1 if faces1.size <= faces2.size else faces2
    except AttributeError:
        print "HEllO"

    ft = cv2.freetype.createFreeType2()
    ft.loadFontData(fontFileName='./data/font/simhei.ttf', id=0)

    for (x, y, w, h) in faces:
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 3)
        f = cv2.resize(gray[y:y + h, x:x + w], (200, 200))
        cv2.imwrite(
            './data/search_img/{}/{}.pgm'.format(
                filesdir, time.strftime('%Y%m%d%H%M%S')), f)
        result = FaceAPI.searchItoI(
            image_file='./data/search_img/{}/{}.pgm'.format(
                filesdir, time.strftime('%Y%m%d%H%M%S')))
        if len(result) == 4:
            break
        if result["results"][0]["confidence"] >= 80.00:
            print result["results"][0]["confidence"]
            face_token = result["results"][0]["face_token"]
            print "face_token:{}".format(face_token)
            detail = get_detail()
            # if not os.path.exists('./data/at/{}/{}.pgm'.format(detail[0],filesdir)):
            # shutil.copyfile("./data/search_img/{}/{}.pgm".format(filesdir,time.strftime('%Y%m%d%H%M%S')),"./data/at/%s/%s.pgm"%(detail[0],filesdir))
            ft.putText(img=img,
                       text=detail[1],
                       org=(x, y - 10),
                       fontHeight=30,
                       line_type=cv2.LINE_AA,
                       color=(0, 255, 165),
                       thickness=1,
                       bottomLeftOrigin=True)
        else:
            print "Unknow face"
            cv2.putText(img, "Unknow", (x, y - 10),
                        cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 225), 2)
        count += 1
    cv2.imwrite(i, img)
Example #4
0
def video():
    global face_token
    ft = cv2.freetype.createFreeType2()
    ft.loadFontData(fontFileName='./data/font/simhei.ttf', id=0)
    face_cascade = cv2.CascadeClassifier(
        './data/cascades/haarcascade_frontalface_alt.xml')
    camera = cv2.VideoCapture(0)
    count = 0
    while (True):
        ret, frame = camera.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in faces:
            img = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255),
                                2)
            if count % 10 == 1:

                f = cv2.resize(gray[y:y + h, x:x + w], (200, 200))
                cv2.imwrite('./data/temp/temp.pgm', f)
                result = FaceAPI.searchItoI('./data/temp/temp.pgm')
                if len(result) == 4:
                    break
                if result["results"][0]["confidence"] >= 80.00:
                    print result["results"][0]["confidence"]
                    face_token = result["results"][0]["face_token"]
                    detail = get_detail()
                    # shutil.copyfile("./data/temp/temp.pgm","./data/at/%s/%s.pgm"%(detail,time.strftime('%Y%m%d%H%M%S')))
                    print detail
                    ft.putText(img=img,
                               text=detail[1],
                               org=(x, y - 10),
                               fontHeight=60,
                               line_type=cv2.LINE_AA,
                               color=(0, 255, 165),
                               thickness=2,
                               bottomLeftOrigin=True)
                    # count+=1
                    insert_data()
                else:
                    print "Unknow face"
                    cv2.putText(img, "Unknow", (x, y - 10),
                                cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 225),
                                2)
            count += 1
            print count
        cv2.namedWindow("image", cv2.WINDOW_NORMAL)
        cv2.imshow("image", frame)
        if cv2.waitKey(1000 / 12) & 0xff == ord("q"):
            break
    camera.release()
    cv2.destroyAllWindows()
Example #5
0
def check_stuID(ID):
    global face_token, gender, facequality
    cur.execute("select ID from face_data")
    column = cur.fetchall()
    a = zip(*column)
    if ID in a[0]:
        print "{}:------Has been Exist!".format(ID)
    if ID not in a[0]:
        result = FaceAPI.detect(image_file=imagedir)
        face_token = result["faces"][0]["face_token"]
        gender = result["faces"][0]["attributes"]["gender"]["value"]
        facequality = result["faces"][0]["attributes"]["facequality"]["value"]
        print "ID:{}".format(ID)
        print "Name:{}".format(name)
        print "face_token:{}".format(face_token)
        print "gender:{}".format(gender)
        print "facequality:{}".format(facequality)
        insert_data()
Example #6
0
def detect(filename):
    global face_token
    count = 0
    faces = []
    face_cascade = cv2.CascadeClassifier(
        './data/cascades/haarcascade_frontalface_alt.xml')
    img = cv2.imread(filename)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    ft = cv2.freetype.createFreeType2()
    ft.loadFontData(fontFileName='./data/font/simhei.ttf', id=0)

    for (x, y, w, h) in faces:
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 3)
        f = cv2.resize(gray[y:y + h, x:x + w], (200, 200))
        cv2.imwrite('./data/search/{}/{}.pgm'.format(filesdir, count), f)
        result = FaceAPI.searchItoI(
            image_file='./data/search/{}/{}.pgm'.format(filesdir, count))
        if len(result) == 4:
            break
        if result["results"][0]["confidence"] >= 80.00:
            print result["results"][0]["confidence"]
            face_token = result["results"][0]["face_token"]
            print "face_token:{}".format(face_token)
            detail = get_detail()
            ft.putText(img=img,
                       text=detail[1],
                       org=(x, y - 10),
                       fontHeight=30,
                       line_type=cv2.LINE_AA,
                       color=(0, 255, 165),
                       thickness=1,
                       bottomLeftOrigin=True)
        else:
            print "Unknow face"
            cv2.putText(img, "Unknow", (x, y - 10),
                        cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 225), 2)
        count += 1
    cv2.imwrite(i, img)
Example #7
0
def video():
    global db_face_token
    count = 0
    names = ['100000000001', '100000000002', '100000000003', '100000000004']
    [X, y] = read_images()
    y = np.asarray(y, dtype=np.int32)
    model = cv2.face.createLBPHFaceRecognizer()
    ft = cv2.freetype.createFreeType2()
    ft.loadFontData(fontFileName='./data/font/simhei.ttf', id=0)
    # model = cv2.face.createEigenFaceRecognizer()
    model.train(np.asarray(X), np.asarray(y))
    camera = cv2.VideoCapture(0)
    face_cascade = cv2.CascadeClassifier(
        './data/cascades/haarcascade_frontalface_default.xml')

    while (True):
        read, img = camera.read()
        faces = face_cascade.detectMultiScale(img, 1.3, 5)

        if (count % 20) < 1:
            for (x, y, w, h) in faces:
                img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 245, 0),
                                    2)
                gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                f = cv2.resize(gray[y:y + h, x:x + w], (200, 200))
                cv2.imwrite('./data/temp/temp.pgm', f)
                result = FaceAPI.searchItoI(image_file='./data/temp/temp.pgm')
                if len(result) == 4:
                    break
                if result["results"][0]["confidence"] >= 80.00:
                    print result["results"][0]["confidence"]
                    face_token = result["results"][0]["face_token"]
                    detail = get_detail()
                    # shutil.copyfile("./data/temp/temp.pgm","./data/at/%s/%s.pgm"%(detail,time.strftime('%Y%m%d%H%M%S')))
                    print detail
                    ft.putText(img=img,
                               text=detail[1],
                               org=(x, y - 10),
                               fontHeight=60,
                               line_type=cv2.LINE_AA,
                               color=(0, 255, 165),
                               thickness=2,
                               bottomLeftOrigin=True)
                    count += 1
                else:
                    print "Unknow face"
                    cv2.putText(img, "Unknow", (x, y - 10),
                                cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 225),
                                2)
        else:
            for (x, y, w, h) in faces:
                img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255),
                                    2)
                gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                roi = gray[x:x + w, y:y + h]
                try:
                    roi = cv2.resize(roi, (200, 200),
                                     interpolation=cv2.INTER_LINEAR)
                    # print roi.shape
                    params = model.predict(roi)
                    print "Label: %s, Confidence: %.2f" % (params[0],
                                                           params[1])
                    # ft.putText(img=img, text=names[params[0]-1], org=(x, y - 20), line_type=cv2.LINE_AA, color=(0,255,255), thickness=2,bottomLeftOrigin=True)
                    cv2.putText(img, names[params[0] - 1], (x, y - 20),
                                cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)

                    if (params[0] == 0):
                        cv2.imwrite('face_rec.jpg', img)
                except:
                    continue
        count += 1
        print count
        cv2.namedWindow("camera", cv2.WINDOW_NORMAL)
        cv2.imshow("camera", img)
        if cv2.waitKey(1000 / 12) & 0xff == ord("q"):
            break
    camera.release()
    cv2.destroyAllWindows()