示例#1
0
 def testTHPrediction(self):
     keras.backend.set_image_dim_ordering('th')
     model = VGGFace()
     img = image.load_img('image/ak.jpg', target_size=(224, 224))
     x = image.img_to_array(img)
     x = np.expand_dims(x, axis=0)
     x = utils.preprocess_input(x)
     preds = model.predict(x)
     print('Predicted:', utils.decode_predictions(preds))
     self.assertIn(utils.decode_predictions(preds)[0][0][0], 'Aamir_Khan')
     self.assertAlmostEqual(
         utils.decode_predictions(preds)[0][0][1], 0.94938219)
示例#2
0
 def testRESNET50(self):
     keras.backend.set_image_dim_ordering('tf')
     model = VGGFace(model='resnet50')
     img = image.load_img('image/ajb.jpg', target_size=(224, 224))
     x = image.img_to_array(img)
     x = np.expand_dims(x, axis=0)
     x = utils.preprocess_input(x, version=2)
     preds = model.predict(x)
     #print ('\n',"RESNET50")
     #print('\n',preds)
     #print('\n','Predicted:', utils.decode_predictions(preds))
     self.assertIn('A._J._Buckley', utils.decode_predictions(preds)[0][0][0])
     self.assertAlmostEqual(utils.decode_predictions(preds)[0][0][1], 0.91819614,places=3)
示例#3
0
 def testRESNET50(self):
     keras.backend.set_image_dim_ordering('tf')
     model = VGGFace(model='resnet50')
     img = image.load_img('image/ajb.jpg', target_size=(224, 224))
     x = image.img_to_array(img)
     x = np.expand_dims(x, axis=0)
     x = utils.preprocess_input(x, version=2)
     preds = model.predict(x)
     #print ('\n',"RESNET50")
     #print('\n',preds)
     #print('\n','Predicted:', utils.decode_predictions(preds))
     self.assertIn('A._J._Buckley', utils.decode_predictions(preds)[0][0][0])
     self.assertAlmostEqual(utils.decode_predictions(preds)[0][0][1], 0.91819614)
示例#4
0
 def testSENET50(self):
     image_data_format()
     model = VGGFace(model='senet50')
     img = image.load_img('image/ajb.jpg', target_size=(224, 224))
     x = image.img_to_array(img)
     x = np.expand_dims(x, axis=0)
     x = utils.preprocess_input(x, version=2)
     preds = model.predict(x)
     # print ('\n', "SENET50")
     # print('\n',preds)
     # print('\n','Predicted:', utils.decode_predictions(preds))
     self.assertIn('A._J._Buckley', utils.decode_predictions(preds)[0][0][0])
     self.assertAlmostEqual(utils.decode_predictions(preds)[0][0][1], 0.9993529, places=3)
 def testVGG16(self):
     keras.backend.image_data_format()
     model = VGGFace(model='vgg16')
     img = image.load_img('image/ajb.jpg', target_size=(224, 224))
     x = image.img_to_array(img)
     x = np.expand_dims(x, axis=0)
     x = utils.preprocess_input(x, version=1)
     preds = model.predict(x)
     # print ('\n', "VGG16")
     # print('\n',preds)
     # print('\n','Predicted:', utils.decode_predictions(preds))
     self.assertIn('A.J._Buckley', utils.decode_predictions(preds)[0][0][0])
     self.assertAlmostEqual(utils.decode_predictions(preds)[0][0][1],
                            0.9790116,
                            places=3)
示例#6
0
def who_is_this(img, vgg_face_descriptor):
    face_array, face = extract_face(img)
    face_array = face_array.astype('float32')
    input_sample = np.expand_dims(face_array, axis=0)
    img_prediction = vgg_face_descriptor.predict(
        preprocess_input(input_sample))
    results = decode_predictions(img_prediction)
    prediction = results[0][0][0].replace("b'", "").replace("'", "")

    # plt.imshow(cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2RGB))
    # plt.title(results[0][0][0].replace("b'", "").replace("'",""))

    # plt.figure(figsize=(12, 12))
    # ax1 = plt.subplot(121)
    # ax2 = plt.subplot(122)

    # ax1.imshow(cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2RGB))
    # ax1.set_title(results[0][0][0].replace("b'", "").replace("'","") \
    #              + " original image".upper(),\
    #                 fontdict={'fontsize': 18, 'fontweight': 'bold'})
    # ax2.imshow(cv2.cvtColor(face, cv2.COLOR_BGR2RGB))
    # ax2.set_title(results[0][0][0].replace("b'", "").replace("'","")\
    #             + " face".upper(),\
    #             fontdict={'fontsize': 18, 'fontweight': 'bold'})

    # plt.show()

    return prediction
示例#7
0
def identify_faces(detected_faces):
    faceArray1D = expand_dims(detected_faces, axis=0)
    faceArray1D = preprocess_input(faceArray1D, version=2)
    model = VGGFace(model='resnet50')
    yhat = model.predict(faceArray1D)
    results = decode_predictions(yhat)
    return results
示例#8
0
def extract_and_display_results(prediction, img):
  # convert predictions into names & probabilities
  results = decode_predictions(prediction)
  # Display results
  cv2_imshow(img)
  for result in results[0]:
    print ('%s: %.3f%%' % (result[0], result[1]*100))
  return results
示例#9
0
 def testVGG16(self):
     keras.backend.set_image_dim_ordering('tf')
     model = VGGFace(include_top=False,
                     input_shape=(224, 224, 3),
                     pooling='avg')  # pooling: None, avg or max
     #model = VGGFace(model='vgg16')
     img = image.load_img('image/ajb.jpg', target_size=(224, 224))
     x = image.img_to_array(img)
     x = np.expand_dims(x, axis=0)
     x = utils.preprocess_input(x, version=2)
     preds = model.predict(x)
     print('\n', "VGG16")
     print('\n', preds)
     print('\n', 'Predicted:', utils.decode_predictions(preds))
     self.assertIn('A.J._Buckley', utils.decode_predictions(preds)[0][0][0])
     self.assertAlmostEqual(utils.decode_predictions(preds)[0][0][1],
                            0.9790116,
                            places=3)
示例#10
0
def detect(path):
    pixels = pyplot.imread(path)
    pixels = pixels.astype("float32")
    samples = expand_dims(pixels, axis=0)
    model = VGGFace(model="resnet50")
    yhat = model.predict(samples)
    results = decode_predictions(yhat)
    for result in results[0]:
        print(result[0], ": ", result[1] * 100)
示例#11
0
def get_classification_face_match(target_image):
    nb_class = 2
    hidden_dim = 512
    vggmodel = VGGFace(include_top=False, input_shape=(224, 224, 3))
    last_layer = vggmodel.get_layer('pool5').output
    x = Flatten(name='flatten')(last_layer)
    x = Dense(hidden_dim, activation='relu', name='fc6')(x)
    x = Dense(hidden_dim, activation='relu', name='fc7')(x)
    out = Dense(nb_class, activation='softmax', name='fc8')(x)
    identity_model = Model(vggmodel.input, out)
    ## Train the model
    ##Predict
    img = image.load_img(target_image, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = utils.preprocess_input(x, version=1)
    preds = identity_model.predict(x)
    print('\n', "VGG16")
    print('\n', preds)
    print('\n', 'Predicted:', utils.decode_predictions(preds))
    return utils.decode_predictions(preds)[0][0][0]
示例#12
0
def face_process():
    #vgg_features = VGGFace(include_top=False, input_shape=(224, 224, 3), pooling='avg') ## doesn't seem to be required.
    print("* Loading model")
    model = VGGFace(model='vgg16')
    print("* Model loaded")

    while True:
        queue = db.lrange(settings.FACE_QUEUE, 0, settings.BATCH_SIZE - 1)
        imageIDs = []
        batch = None

        for q in queue:
            # deserialize the object and obtain the input image
            q = json.loads(q.decode("utf-8"))
            image = helpers.base64_decode_image(
                q["image"], settings.IMAGE_DTYPE,
                (1, settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH,
                 settings.IMAGE_CHANS))

            # check to see if the batch list is None
            if batch is None:
                batch = image

            # otherwise, stack the data
            else:
                batch = np.vstack([batch, image])

            # update the list of image IDs
            imageIDs.append(q["id"])

        # check to see if we need to process the batch
        if len(imageIDs) > 0:
            # classify the batch
            print("* Batch size: {}".format(batch.shape))
            preds = model.predict(batch)
            results = utils.decode_predictions(preds)
            #print(results) ## this comes back with something so its the below structure giving me an issue.
            # [[["b'A.J._Buckley'", 0.9768057], ["b'David_Denman'", 0.0013909286], ["b'Carmine_Giovinazzo'", 0.0010687601], ["b'Robert_Buckley'", 0.00093060045], ["b'Eddie_Cahill'", 0.00044030472]]]

            for (imageID, resultSet) in zip(imageIDs, results):
                print("imageID", imageID, resultSet)
                ## imageID 63350aef-0ec3-4e1d-be99-3db36013a6d7
                output = []
                for (label, prob) in resultSet:
                    r = {"label": label, "probability": float(prob)}
                    output.append(r)
                db.set(imageID, json.dumps(output))

            # remove the set of images from our queue
            db.ltrim(settings.FACE_QUEUE, len(imageIDs), -1)

        # sleep for a small amount
        time.sleep(settings.SERVER_SLEEP)
示例#13
0
def predict():
    message = request.get_json(force=True)  # this gets all the request data
    imgsrcstring = message['image']
    imgdata = imgsrcstring.split(',')[1]
    decoded = base64.b64decode(imgdata)
    img = np.array(Image.open(BytesIO(decoded)))
    detector = MTCNN()
    target_size = (224, 224)
    border_rel = 0
    detections = detector.detect_faces(img)
    # creating the model to predict the celebrity
    x1, y1, width, height = detections[0]['box']
    dw = round(width * border_rel)
    dh = round(height * border_rel)
    x2, y2 = x1 + width + dw, y1 + height + dh
    face = img[y1:y2, x1:x2]
    face = PIL.Image.fromarray(face)
    face = face.resize((224, 224))
    face = np.asarray(face)
    # convert to float32
    face_pp = face.astype('float32')
    face_pp = np.expand_dims(face_pp, axis=0)
    from keras_vggface.utils import preprocess_input
    face_pp = preprocess_input(face_pp, version=2)
    # Create the resnet50 Model
    model = VGGFace(model='resnet50')
    # predict the face with the input
    prediction = model.predict(face_pp)
    # convert predictions into names & probabilities
    from keras_vggface.utils import decode_predictions
    results = decode_predictions(prediction)
    results = decode_predictions(prediction, top=1)
    s = results[0][0][0]
    # regex to get everything between the '' and remove the underscore
    b = (s.split("' "))[1].split("':")[0]
    # replace the underscore with a space:
    c = b.replace("_", " ")
    response = c.replace("'", '')
    response = 'You look like ' + response + '!'
    return (response)
示例#14
0
def predict(img_path, classify=False):
    model = VGGFace(model='resnet50',
                    include_top=False,
                    input_shape=(224, 224, 3),
                    pooling='avg')  # pooling: None, avg or max
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = utils.preprocess_input(x, version=2)
    preds = model.predict(x)
    if classify:
        # print('Predicted:', utils.decode_predictions(preds))
        return utils.decode_predictions(preds)
    else:
        # print("Features: ", preds.shape)
        return preds
示例#15
0
def get_predictions_from_png_image_example():
    """Example usage to get predictions (human identity) from image"""
    from tensorflow.keras.preprocessing import image
    import numpy as np
    import keras_vggface.utils as libutils

    image_preprocessor = create_preprocessing_model()
    model = VGGFace(model='senet50')
    img = image.load_img('image/ajb-resized.jpg',
                         target_size=(224, 224),
                         interpolation="bilinear")
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    preprocessed = image_preprocessor.predict(x)
    predictions = model.predict(preprocessed)
    print('Predicted:', libutils.decode_predictions(predictions))
示例#16
0
def hello(filepath):
    # load the photo and extract the face
    pixels = extract_face(filepath)
    # convert one face into samples
    pixels = pixels.astype('float32')
    samples = expand_dims(pixels, axis=0)
    # prepare the face for the model, e.g. center pixels
    samples = preprocess_input(samples, version=2)
    # create a vggface model
    model = VGGFace(model='resnet50')
    # perform prediction
    yhat = model.predict(samples)
    # convert prediction into names
    results = decode_predictions(yhat)
    # display most likely results

    # for result in results[0]:
    # 	print('%s: %.3f%%' % (result[0], result[1]*100))
    #

    return results[0]
示例#17
0
	x1, y1, width, height = results[0]['box']
	x2, y2 = x1 + width, y1 + height

	face = pixels[y1:y2, x1:x2]

	image = Image.fromarray(face)
	image = image.resize(required_size)

	face_array = np.asarray(image)
	return face_array

pixels = extract_face(args['filename'])
# plt.imshow(pixels)
# plt.savefig('test.jpg')
print(type(pixels))

pixels = pixels.astype('float32')
samples = np.expand_dims(pixels, axis=0)

# Preprocess input same way as training data i.e. centering mean pixel values
samples = preprocess_input(samples, version=2)

# create VGGFace model
model = VGGFace(model='resnet50')

yhat = model.predict(samples)

results = decode_predictions(yhat)

for result in results[0]:
	print('{}, {:.3f}'.format(result[0][3:-1], result[1] * 100))
示例#18
0
def decoder(yhat):
    results = decode_predictions(yhat)
# display most likely results
    for result in results[0]:
        print('The likely candidate is %s with confidence level of %.3f%%' % (result[0], result[1]*100))
示例#19
0
 def predict(self, img_path):
     pixels = self.__process_img(img_path)
     preds = self.vgg.predict(pixels)
     return decode_predictions(preds)
示例#20
0
import numpy as np
from keras.preprocessing import image
from keras_vggface.vggface import VGGFace
from keras_vggface import utils

# tensorflow
model = VGGFace() # default : VGG16 , you can use model='resnet50' or 'senet50'

# Change the image path with yours.
img = image.load_img('face1.jpg', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = utils.preprocess_input(x, version=1) # or version=2
preds = model.predict(x)
print('Predicted:', utils.decode_predictions(preds))
示例#21
0
 def predict(self, X, y=None):
     preds = self._model.predict(X)
     if self._verbose_msg:
         print('Predicted: ', utils.decode_predictions(preds))
     return preds
示例#22
0
    """Load and preprocess image."""
    x = image.load_img(img_path, target_size=shape)
    x = image.img_to_array(x)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x, version=1)  # or version=2
    return x


# load model (with person classifier)
vggface = VGGFace(model='resnet50')

# classify image
img = load_preprocess('src/faces/chen2.jpg')
preds = vggface.predict(img)
print('Predicted: {};\nConfidence: {}'.format(
    decode_predictions(preds)[0][0][0],
    decode_predictions(preds)[0][0][1]))

# load model (feature extraction)
vggface_topless = VGGFace(model='resnet50', include_top=False)
face_chen1 = 'src/faces/chen.jpg'
face_chen2 = 'src/faces/chen2.jpg'
face_sheldon1 = 'src/faces/sheldon.jpg'
face_sheldon2 = 'src/faces/sheldon2.jpg'
chen1 = vggface_topless.predict(load_preprocess(face_chen1))
chen2 = vggface_topless.predict(load_preprocess(face_chen2))
sheldon1 = vggface_topless.predict(load_preprocess(face_sheldon1))
sheldon2 = vggface_topless.predict(load_preprocess(face_sheldon2))

# check correlation
np.correlate(chen1.flatten(), chen2.flatten())
示例#23
0
"""# Step 4: Predict"""

# Create the resnet50 Model
model = VGGFace(model= 'resnet50')
# Check what the required input of the model is & output
print('Inputs: {input}'.format(input = model.inputs))
print('Output: {output}'.format(output = model.outputs))

# predict the face with the input
prediction = model.predict(face_pp)

"""# Step 5: Extract results"""

# convert predictions into names & probabilities
results = decode_predictions(prediction)
# Display results
cv2_imshow(img)
for result in results[0]:
  print ('%s: %.3f%%' % (result[0], result[1]*100))

"""# Lets put everything together"""

def get_img(url):
  # Open the link and save the image to res
  res = request.urlopen(url)
  # Read the res object and convert it to an array
  img = np.asarray(bytearray(res.read()), dtype='uint8')
  # Add the color variable
  img = cv2.imdecode(img, cv2.IMREAD_COLOR)
  return img