ImanAmran commited on
Commit
37c21fd
·
1 Parent(s): 309a0ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -5
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import tensorflow as tf
2
- from tensorflow.keras.models import load_model
3
 
 
4
  def preprocess_image(filename, target_shape=(160, 160)):
5
  image_string = tf.io.read_file(filename)
6
  image = tf.image.decode_jpeg(image_string, channels=3)
@@ -8,9 +9,22 @@ def preprocess_image(filename, target_shape=(160, 160)):
8
  image = tf.image.resize(image, target_shape)
9
  return image
10
 
11
- embedding_model_path = 'facenet_siamese_embedding.h5'
12
- embedding_model = load_model(embedding_model_path)
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def generate_embedding(image_path, model):
15
  preprocessed_image = preprocess_image(image_path)
16
  preprocessed_image = tf.expand_dims(preprocessed_image, axis=0) # Add batch dimension
@@ -19,5 +33,5 @@ def generate_embedding(image_path, model):
19
 
20
  # Example usage
21
  image_path = 'iman.jpg' # Update with your image's path
22
- image_embedding = generate_embedding(image_path, embedding_model)
23
- print("Generated Embedding:", image_embedding.numpy())
 
1
  import tensorflow as tf
2
+ from tensorflow.keras.models import load_model, Model
3
 
4
+ # Function to preprocess the image
5
  def preprocess_image(filename, target_shape=(160, 160)):
6
  image_string = tf.io.read_file(filename)
7
  image = tf.image.decode_jpeg(image_string, channels=3)
 
9
  image = tf.image.resize(image, target_shape)
10
  return image
11
 
12
+ # Load the base FaceNet model
13
+ facenet_model = load_model('facenet_keras.h5', compile=False)
14
 
15
+ # Create the embedding model using the FaceNet model
16
+ embedding = Model(inputs=facenet_model.input,
17
+ outputs=facenet_model.layers[-2].output,
18
+ name="Embedding")
19
+
20
+ # Load the weights for your siamese or modified FaceNet model
21
+ embedding.load_weights('facenet_siamese_embedding.h5')
22
+
23
+ # Set all layers to non-trainable
24
+ for layer in embedding.layers:
25
+ layer.trainable = False
26
+
27
+ # Function to generate embedding
28
  def generate_embedding(image_path, model):
29
  preprocessed_image = preprocess_image(image_path)
30
  preprocessed_image = tf.expand_dims(preprocessed_image, axis=0) # Add batch dimension
 
33
 
34
  # Example usage
35
  image_path = 'iman.jpg' # Update with your image's path
36
+ image_embedding = generate_embedding(image_path, embedding)
37
+ print("Generated Embedding:", image_embedding.numpy())