Exemplo n.º 1
0
def image_demo(ground_file, unknown_file, zoom, subject):
    '''
        function which enhances the resolution of image files
    '''
    # init image enhancer (Neural Enhance) object
    enhancer = neural_enhance()
    # intialize the ground, training image
    ground_img = init_img(ground_file)
    # init an unknown image
    # against which to compare the ground image
    unknown_img = init_img(unknown_file)
    # initialize image which will become doubled image
    unknown_img_x2 = init_img(unknown_file)
    # enhance image: double resolution of unknown image
    # convert image to np array
    unknown_img_x2 = np.array(enhancer.process(unknown_img_x2))
    # encode training image to ground encoding
    ground_encode = encode_face(ground_img)[0][0]
    # newline to console
    print('\n')
    # create filename based on input name of subject
    outpath_subject = '_'.join(subject)
    # same the ground image to the filepath
    scipy.misc.imsave('img/ground_img_' + outpath_subject + '.jpg', ground_img)
    # call function to encode and recognize unenhanced face image
    unknown_img = recognize_img(ground_encode, subject, unknown_img, 2)
    # same image with output information to directory
    scipy.misc.imsave('img/1x_img_' + outpath_subject + '.jpg', unknown_img)
    # call function to encode and recognize enhanced face image
    unknown_img_x2 = recognize_img(ground_encode, subject, unknown_img_x2, 1)
    # same image with output information to directory
    scipy.misc.imsave('img/2x_img_' + outpath_subject + '.jpg', unknown_img_x2)
Exemplo n.º 2
0
def compare_subject(grd, exp):
    '''
        compare the training image with an unknown image
    '''
    # return the encoding and locations of an unknown face
    encs, locs = encode_face(exp)
    # condition to determine if face was found in image
    # if no encoding, skip comparisons
    if encs:
        # get first encoding from list of face encodings
        encoding = encs[0]
        # make a comparison between training and experimental face
        # returns true if face is recognized, false if not
        # returns magnitude of difference between
        # training and unknown face
        results, distance = tolerance_face([grd], encoding)
        # unpack first values of lists of if face is recognized,
        # and location of face, and magnitude of similarity
        res, loc, dist = results[0], locs[0], distance[0]
        # unpack each corner of the rectangle
        top, bottom, left, right = loc.top(), loc.bottom(), loc.left(
        ), loc.right()
        # find area of face
        area = (right - left) * (bottom - top)
        # return tuple of
        # face is recognized
        # magnitude between faces
        # and area of recognized face
        return res, dist, area
    # condition if no face is found in image
    else:
        # return false, size of face is 0
        return False, 1.0, 0
Exemplo n.º 3
0
def recognize_img(ground_encode, subject, unknown_img, text_factor):
    '''
        function to recognize faces in images
        based on inputs of the training encoding
        the name of the subject
        the unknown image
    '''
    # create coding and location of unknown image
    unknown_encode, unknown_locations = encode_face(unknown_img)
    # iterate through encodings and locations of faces
    for unknown, rect in zip(unknown_encode, unknown_locations):
        # determine if training face is in unknown image
        results = recognize_face([ground_encode], unknown)
        # get first element of result list
        result = results[0]
        # condition to determine if face is recognized in unknown
        if result:
            # condition if result is a list
            if isinstance(subject, list):
                # join array into string representing name
                subtitle = ' '.join(subject)
            # condition if result is string
            else:
                # set string name to name
                subtitle = subject
        # condition if face is not recognized in unknown
        else:
            # subject 'name' is unknown
            subtitle = 'Unknown'
        # output the name of the subject if recognized
        # or unknown if the subject is not recognized
        print(subtitle)
        # get corner of the rectangle drawn around recognized face
        top, bottom, left, right = rect.top(), rect.bottom(), rect.left(
        ), rect.right()
        # set font for subtitle of subject or unknown
        font = cv2.FONT_HERSHEY_DUPLEX
        # draw rectangle around the face recognized
        cv2.rectangle(unknown_img, (left, top), (right, bottom), (0, 255, 0),
                      2 // text_factor)
        # draw name to the image, either subject or unknown
        cv2.putText(unknown_img, subtitle,
                    (40 // text_factor, len(unknown_img) - 10 // text_factor),
                    font, 1.0 / text_factor, (255, 255, 255), 1)
    # return the processed image
    return unknown_img
Exemplo n.º 4
0
def recognize_video(ground_file, unknown_file, subject):
    '''
        function to determine if the dlib face recognition
        is able to match the ground subject with the unknown
    '''
    # initialize ground image
    ground_img = init_img(ground_file)
    # an encoding of the ground subject
    # against which to match
    known = encode_face(ground_img)[0][0]
    # import the video stream
    vid = cv2.VideoCapture(unknown_file)
    # iterate through frames there are still frames
    while vid.isOpened():
        # read a single frame of the video
        ret, frame = vid.read()
        # condition if the current frame is readable
        if ret:
            # get face encoding of images 'in the wild'
            # including spatial positions of each face
            unknowns, locations = encode_face(frame)
            # iterate through each encoding and location
            for unknown, rect in zip(unknowns, locations):
                # return array of whether subject/known face
                # matches the unknown faces
                results = recognize_face([known], unknown)
                # get first element of results, true or false
                result = results[0]
                # if subject recognized, print subject name
                if result:
                    # join the subject name into single string
                    subtitle = ' '.join(subject)
                # if subject not recognized, print unknown
                else:
                    # output the string indicating
                    # that a person is not known
                    subtitle = 'Unknown person'
                # get the corners of the face locations
                top, bottom, left, right = rect.top(), rect.bottom(
                ), rect.left(), rect.right()
                # output green rectangle around
                # the area the face is recognized
                cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255),
                              2)
                # output placard at base of recognition rectangle
                cv2.rectangle(frame, (left, bottom - 35), (right, bottom),
                              (0, 0, 255), cv2.FILLED)
                # set font to predefined opencv font
                font = cv2.FONT_HERSHEY_DUPLEX
                # output text with subject name to bottom of rectangle
                cv2.putText(frame, subtitle, (left + 6, bottom - 6), font, 1.0,
                            (255, 255, 255), 1)
            # display the current frame to ffmpeg console
            cv2.imshow('', frame)
            # condition allows exiting of the loop
            # if 'q' is pressed or 'x' is pressed
            if cv2.waitKey(1) & 0xFF == ord('q'):
                # break out of loop if condition is true
                break
        # if frame is unreadable, break from while
        else:
            # break if frame is unreadable
            break
    # closes alread opened file or camera
    vid.release()
    # terminate the windows from opencv
    cv2.destroyAllWindows()
Exemplo n.º 5
0
def video_demo(ground_file, unknown_file, subject):
    '''
        create a video demo with
        face recognition for any video file,
        whether ehnahced or not
    '''
    # import training image as numpy array
    ground_img = init_img(ground_file)
    # return encoding scheme for training image
    # used to be compared to all other images
    # encoding is a 128D array of given face
    known = encode_face(ground_img)[0][0]
    # import full video as a set of frames
    # frames is array of unedited video images
    frames = full_video(unknown_file)
    # init empty list to be modified video images
    images = []
    # get the length of frames
    frame_num = len(frames)
    # iterate through each unedited frame
    for i in range(frame_num):
        # get current frame
        frame = frames[i]
        # create recognition and encoding frames
        # create face encodings and locations
        # encoding is a 128D array of given face
        unknowns, locations = encode_face(frame)
        # iterate through unknown encodings and locations
        for unknown, rect in zip(unknowns, locations):
            # recognize unknown face against the ground
            # return truthiness array of which faces are recognized
            results = recognize_face([known], unknown)
            # get first results
            # usually result array is 1 element list
            result = results[0]
            # condition if face is recognized
            if result:
                # make a name of the subject recognized
                # if they face is recognized
                # based on the above encoding
                subtitle = ' '.join(subject)
            # condition if face is not recognized
            else:
                # add a default, unknown subtitle
                # if face is not recognized
                subtitle = '???'
            # get each corner of the rectangle
            # which indicates if a face is recognized
            top, bottom, left, right = rect.top(), rect.bottom(), rect.left(
            ), rect.right()
            # draw the rectangle bordering the face with a red box
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
            # draw solid rectangle at bottom of face box
            cv2.rectangle(frame, (left, bottom + 20), (right, bottom),
                          (0, 0, 255), cv2.FILLED)
            # get the font of the text
            font = cv2.FONT_HERSHEY_DUPLEX
            # draw the text to the solid bordering
            # beneath the face if recognized
            cv2.putText(frame, subtitle, (left + 4, bottom + 16), font, 1.0,
                        (255, 255, 255), 1)
        # add processed frame to the array of processed frames
        images.append(frame)
        # print the frame number that is printed to console
        # this gives a primitive timescale
        print(i + 1, '/', frame_num)
    # get the dimensions of the processed image array
    # for use of resolution output of processed video
    h, w, l = images[0].shape
    # get filepath of videofile
    base = unknown_file.split('.')[0] + '-face-rec.mp4'
    # create VideoWriter object
    # at base video file with specified encoding scheme
    # at specific frame rate with specific frame size
    writer = cv2.VideoWriter(base, cv2.VideoWriter_fourcc(*"MJPG"), 23, (w, h))
    # iterate through array of images
    for img in images:
        # write processed file image by image with writer obj
        writer.write(img)
    # terminate the video writer
    writer.release()
Exemplo n.º 6
0
def run_experiment():
    '''
        the experiment to determine rate of similarity between
        low resolution and high resolution images and returns
        the stats related to rate of recognition
    '''
    # generate list of subjects in specified directory
    sub_dir = dir_list('img/')
    # get conditions of resolution (high,low)
    cond_dir = gen_cond()
    # get experiment conditions of various resolutions
    # including increased resolution and ground, etc...
    exp_dir = gen_exp()
    # instanciate object which enhances resolution of image
    enhancer = neural_enhance()
    # gen data structure which holds experimental results
    exp_res = gen_exp_res(sub_dir, exp_dir)
    # iterate through every file which represents subject
    # in specified file directory
    for sub in sub_dir:
        # print the subject number to the terminal
        print(sub)
        # initialize the ground truth training image
        grd_img = gen_img(sub, 'ground')
        # create dictionary of images at various exp conditions
        imgs = {c: gen_img(sub, c) for c in cond_dir}
        # create offset by which to query control test image
        off = (int(sub) + (len(sub_dir)) // 2) % len(sub_dir)
        # if subject is less than ten, append with zero digit
        # to make file dir string two chars long
        if off < 10:
            # add zero digit to front of string less than 10
            off = '0' + str(off)
        # condition if file less than 10
        else:
            # convert int of filepath to str
            off = str(off)
        # generate false positive, high resolution control image
        imgs['highf'] = gen_img(off, 'high')
        # generate false positive, low resolution control image
        imgs['lowf'] = gen_img(off, 'low')
        # generate enhanced resolution image from low res image
        imgs['x2'] = enhancer.process(imgs['low'])
        # generate enhanced resolution image from low res false positive
        imgs['x2f'] = enhancer.process(imgs['lowf'])
        # encode image from ground truth training image
        grd_enc = encode_face(grd_img)[0][0]
        # iterate through each image in experimental groups
        for key, img in imgs.items():
            # comparison between ground truth training image
            # and image in each experimental group
            match, dist, size = compare_subject(grd_enc, img)
            # add match to match experimental results
            # if true, adds 1 to sum
            # if false, adds 0 to sum
            exp_res[sub][key]['match'] += match
            # increment magnitude comparison
            # to determine 'closeness' of each face
            exp_res[sub][key]['dist'] += dist
            # add size of each face
            exp_res[sub][key]['size'] += size
    # return structure of data results contains
    # all data across experimental categories
    return exp_res