From 88fa4d7884b321bf309689beb9d7b557ceb62548 Mon Sep 17 00:00:00 2001 From: Harsimran Singh Dalal Date: Mon, 28 Oct 2024 20:21:53 +0530 Subject: [PATCH 01/14] test_face_recognition.py --- test_face_recognition.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test_face_recognition.py diff --git a/test_face_recognition.py b/test_face_recognition.py new file mode 100644 index 0000000..2ec522d --- /dev/null +++ b/test_face_recognition.py @@ -0,0 +1,19 @@ +import unittest +from face_recognition_module import detect_faces, generate_embeddings + +class TestFaceRecognition(unittest.TestCase): + def test_detect_faces(self): + # Test with a sample image + image_path = "test_images/sample.jpg" + faces = detect_faces(image_path) + self.assertGreater(len(faces), 0, "No faces detected in the sample image.") + + def test_generate_embeddings(self): + # Test with a dummy face data + face_data = "sample_face_data" + embedding = generate_embeddings(face_data) + self.assertIsNotNone(embedding, "Embedding generation failed for the given face data.") + self.assertEqual(len(embedding), 128, "Embedding length should be 128 dimensions.") # Example dimension + +if __name__ == "__main__": + unittest.main() From ed5b5820db727caceda53a26da8f6ab056d885ec Mon Sep 17 00:00:00 2001 From: Harsimran Singh Dalal Date: Mon, 28 Oct 2024 20:22:26 +0530 Subject: [PATCH 02/14] Update test_face_recognition.py --- test_face_recognition.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test_face_recognition.py b/test_face_recognition.py index 2ec522d..4d9973f 100644 --- a/test_face_recognition.py +++ b/test_face_recognition.py @@ -17,3 +17,4 @@ def test_generate_embeddings(self): if __name__ == "__main__": unittest.main() + From d0884e1a3e51ba613efd866a4ce09d9d44177d9c Mon Sep 17 00:00:00 2001 From: Harsimran Singh Dalal Date: Mon, 28 Oct 2024 20:27:49 +0530 Subject: [PATCH 03/14] Create model_optimization.py --- model_optimization.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 model_optimization.py diff --git a/model_optimization.py b/model_optimization.py new file mode 100644 index 0000000..5c4f18f --- /dev/null +++ b/model_optimization.py @@ -0,0 +1,22 @@ +from tensorflow.keras.applications import MobileNetV2 +from tensorflow.keras.preprocessing.image import img_to_array, load_img +import numpy as np + +def load_optimized_model(): + model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) + print("Loaded MobileNetV2 model for optimized face detection.") + return model + +def preprocess_image(image_path): + image = load_img(image_path, target_size=(224, 224)) + image = img_to_array(image) + image = image / 255.0 # Normalize image + image = np.expand_dims(image, axis=0) + return image + +# Example usage +if __name__ == "__main__": + model = load_optimized_model() + preprocessed_image = preprocess_image("path/to/sample_image.jpg") + features = model.predict(preprocessed_image) + print("Extracted features:", features) From 83dd9333953cd4e4cfe1a1d947af33d96baccc2b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:00:01 +0000 Subject: [PATCH 04/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- model_optimization.py | 9 +++++++-- test_face_recognition.py | 15 +++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/model_optimization.py b/model_optimization.py index 5c4f18f..31cb541 100644 --- a/model_optimization.py +++ b/model_optimization.py @@ -1,12 +1,16 @@ +import numpy as np from tensorflow.keras.applications import MobileNetV2 from tensorflow.keras.preprocessing.image import img_to_array, load_img -import numpy as np + def load_optimized_model(): - model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) + model = MobileNetV2( + weights="imagenet", include_top=False, input_shape=(224, 224, 3) + ) print("Loaded MobileNetV2 model for optimized face detection.") return model + def preprocess_image(image_path): image = load_img(image_path, target_size=(224, 224)) image = img_to_array(image) @@ -14,6 +18,7 @@ def preprocess_image(image_path): image = np.expand_dims(image, axis=0) return image + # Example usage if __name__ == "__main__": model = load_optimized_model() diff --git a/test_face_recognition.py b/test_face_recognition.py index 4d9973f..bd9ae64 100644 --- a/test_face_recognition.py +++ b/test_face_recognition.py @@ -1,20 +1,27 @@ import unittest + from face_recognition_module import detect_faces, generate_embeddings + class TestFaceRecognition(unittest.TestCase): def test_detect_faces(self): # Test with a sample image image_path = "test_images/sample.jpg" faces = detect_faces(image_path) - self.assertGreater(len(faces), 0, "No faces detected in the sample image.") + self.assertGreater( + len(faces), 0, "No faces detected in the sample image.") def test_generate_embeddings(self): # Test with a dummy face data face_data = "sample_face_data" embedding = generate_embeddings(face_data) - self.assertIsNotNone(embedding, "Embedding generation failed for the given face data.") - self.assertEqual(len(embedding), 128, "Embedding length should be 128 dimensions.") # Example dimension + self.assertIsNotNone( + embedding, "Embedding generation failed for the given face data." + ) + self.assertEqual( + len(embedding), 128, "Embedding length should be 128 dimensions." + ) # Example dimension + if __name__ == "__main__": unittest.main() - From 9f17d4eaf7e0ee9dd3ad9ae4a6c97c29e2800a36 Mon Sep 17 00:00:00 2001 From: Harsimran Singh Dalal Date: Mon, 28 Oct 2024 20:41:03 +0530 Subject: [PATCH 05/14] Create test_face_detection.py --- test_face_detection.py | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test_face_detection.py diff --git a/test_face_detection.py b/test_face_detection.py new file mode 100644 index 0000000..fe3441c --- /dev/null +++ b/test_face_detection.py @@ -0,0 +1,51 @@ +import unittest +import os +from face_recognition_module import detect_faces # Replace with the actual module name + +class TestFaceDetection(unittest.TestCase): + def test_invalid_image_path(self): + """Test case for an invalid image path.""" + invalid_path = "non_existent_directory/non_existent_image.jpg" + with self.assertRaises(FileNotFoundError): + detect_faces(invalid_path) + + def test_corrupted_image_file(self): + """Test case for a corrupted image file.""" + # Create a corrupted image file for testing + corrupted_image_path = "test_images/corrupted_image.jpg" + with open(corrupted_image_path, 'w') as f: + f.write("This is not a valid image content") + + with self.assertRaises(ValueError): + detect_faces(corrupted_image_path) + + # Clean up the corrupted image file after testing + os.remove(corrupted_image_path) + + def test_unsupported_image_format(self): + """Test case for an unsupported image format.""" + unsupported_image_path = "test_images/sample.txt" + # Create a text file to simulate an unsupported image format + with open(unsupported_image_path, 'w') as f: + f.write("This is a text file, not an image.") + + with self.assertRaises(ValueError): + detect_faces(unsupported_image_path) + + # Clean up the text file after testing + os.remove(unsupported_image_path) + + def test_empty_image_file(self): + """Test case for an empty image file.""" + empty_image_path = "test_images/empty_image.jpg" + # Create an empty file + open(empty_image_path, 'w').close() + + with self.assertRaises(ValueError): + detect_faces(empty_image_path) + + # Clean up the empty image file after testing + os.remove(empty_image_path) + +if __name__ == "__main__": + unittest.main() From 8ce87a2ba76f766ccc40e4007802c1d6b81a4383 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:11:21 +0000 Subject: [PATCH 06/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test_face_detection.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test_face_detection.py b/test_face_detection.py index fe3441c..6552b34 100644 --- a/test_face_detection.py +++ b/test_face_detection.py @@ -1,6 +1,9 @@ -import unittest import os -from face_recognition_module import detect_faces # Replace with the actual module name +import unittest + +from face_recognition_module import \ + detect_faces # Replace with the actual module name + class TestFaceDetection(unittest.TestCase): def test_invalid_image_path(self): @@ -13,7 +16,7 @@ def test_corrupted_image_file(self): """Test case for a corrupted image file.""" # Create a corrupted image file for testing corrupted_image_path = "test_images/corrupted_image.jpg" - with open(corrupted_image_path, 'w') as f: + with open(corrupted_image_path, "w") as f: f.write("This is not a valid image content") with self.assertRaises(ValueError): @@ -26,7 +29,7 @@ def test_unsupported_image_format(self): """Test case for an unsupported image format.""" unsupported_image_path = "test_images/sample.txt" # Create a text file to simulate an unsupported image format - with open(unsupported_image_path, 'w') as f: + with open(unsupported_image_path, "w") as f: f.write("This is a text file, not an image.") with self.assertRaises(ValueError): @@ -39,7 +42,7 @@ def test_empty_image_file(self): """Test case for an empty image file.""" empty_image_path = "test_images/empty_image.jpg" # Create an empty file - open(empty_image_path, 'w').close() + open(empty_image_path, "w").close() with self.assertRaises(ValueError): detect_faces(empty_image_path) @@ -47,5 +50,6 @@ def test_empty_image_file(self): # Clean up the empty image file after testing os.remove(empty_image_path) + if __name__ == "__main__": unittest.main() From b20a1f6dc64aae32e31790b018571019b40cdfa9 Mon Sep 17 00:00:00 2001 From: Harsimran Singh Dalal Date: Mon, 28 Oct 2024 21:03:08 +0530 Subject: [PATCH 07/14] Create test_generate_embeddings.py --- test_generate_embeddings.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test_generate_embeddings.py diff --git a/test_generate_embeddings.py b/test_generate_embeddings.py new file mode 100644 index 0000000..aa0e5d7 --- /dev/null +++ b/test_generate_embeddings.py @@ -0,0 +1,25 @@ +import unittest +from face_recognition_module import generate_embeddings # Replace with actual module name + +class TestGenerateEmbeddings(unittest.TestCase): + def test_invalid_face_data(self): + """Test case for invalid input data to `generate_embeddings`.""" + invalid_face_data = None + with self.assertRaises(TypeError): + generate_embeddings(invalid_face_data) + + def test_empty_face_data(self): + """Test case for empty face data input.""" + empty_face_data = [] + with self.assertRaises(ValueError): + generate_embeddings(empty_face_data) + + def test_large_face_data(self): + """Test case for overly large face data input.""" + # Simulate very large input + large_face_data = [0.1] * 10000 # Example of a large data array + with self.assertRaises(ValueError): + generate_embeddings(large_face_data) + +if __name__ == "__main__": + unittest.main() From a713e7169a567c9ee26f3bd6a3aed9b78691a8ef Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:33:22 +0000 Subject: [PATCH 08/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test_generate_embeddings.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test_generate_embeddings.py b/test_generate_embeddings.py index aa0e5d7..f6c0c9b 100644 --- a/test_generate_embeddings.py +++ b/test_generate_embeddings.py @@ -1,5 +1,8 @@ import unittest -from face_recognition_module import generate_embeddings # Replace with actual module name + +from face_recognition_module import \ + generate_embeddings # Replace with actual module name + class TestGenerateEmbeddings(unittest.TestCase): def test_invalid_face_data(self): @@ -21,5 +24,6 @@ def test_large_face_data(self): with self.assertRaises(ValueError): generate_embeddings(large_face_data) + if __name__ == "__main__": unittest.main() From 355adf839cae1c3409db428ce32118d4913e5370 Mon Sep 17 00:00:00 2001 From: Harsimran Singh Dalal Date: Mon, 28 Oct 2024 21:03:53 +0530 Subject: [PATCH 09/14] Create detect_faces.py --- detect_faces.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 detect_faces.py diff --git a/detect_faces.py b/detect_faces.py new file mode 100644 index 0000000..4789ac1 --- /dev/null +++ b/detect_faces.py @@ -0,0 +1,19 @@ +import os +from PIL import Image + +def detect_faces(image_path): + """Detect faces in the given image path.""" + # Validate input + if not os.path.exists(image_path): + raise FileNotFoundError(f"The file {image_path} does not exist.") + if not image_path.lower().endswith(('.png', '.jpg', '.jpeg')): + raise ValueError("Unsupported image format. Please use .png, .jpg, or .jpeg files.") + + try: + image = Image.open(image_path) + except IOError: + raise ValueError("The image file is corrupted or cannot be opened.") + + # Face detection logic here... + faces = [] # Replace with actual face detection logic + return faces From a0e6dedff23b301f42f0b517764b48494be2b936 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:34:07 +0000 Subject: [PATCH 10/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- detect_faces.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/detect_faces.py b/detect_faces.py index 4789ac1..bfb7fa0 100644 --- a/detect_faces.py +++ b/detect_faces.py @@ -1,19 +1,23 @@ import os + from PIL import Image + def detect_faces(image_path): """Detect faces in the given image path.""" # Validate input if not os.path.exists(image_path): raise FileNotFoundError(f"The file {image_path} does not exist.") - if not image_path.lower().endswith(('.png', '.jpg', '.jpeg')): - raise ValueError("Unsupported image format. Please use .png, .jpg, or .jpeg files.") - + if not image_path.lower().endswith((".png", ".jpg", ".jpeg")): + raise ValueError( + "Unsupported image format. Please use .png, .jpg, or .jpeg files." + ) + try: image = Image.open(image_path) except IOError: raise ValueError("The image file is corrupted or cannot be opened.") - + # Face detection logic here... faces = [] # Replace with actual face detection logic return faces From 72d863f6fdb4f8606555625ce43ed00d58f6f088 Mon Sep 17 00:00:00 2001 From: Harsimran Singh Dalal Date: Mon, 28 Oct 2024 21:04:20 +0530 Subject: [PATCH 11/14] Create test_model_loading.py --- test_model_loading.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test_model_loading.py diff --git a/test_model_loading.py b/test_model_loading.py new file mode 100644 index 0000000..0994028 --- /dev/null +++ b/test_model_loading.py @@ -0,0 +1,18 @@ +import unittest +from model_optimization import load_optimized_model # Replace with the actual module name + +class TestModelLoading(unittest.TestCase): + def test_model_loading(self): + """Test if the optimized model loads correctly.""" + model = load_optimized_model() + self.assertIsNotNone(model, "The model should not be None after loading.") + self.assertTrue(hasattr(model, 'predict'), "The loaded model should have a 'predict' method.") + + def test_model_input_shape(self): + """Test if the model input shape is as expected.""" + model = load_optimized_model() + input_shape = model.input_shape + self.assertEqual(input_shape, (None, 224, 224, 3), "Model input shape should be (None, 224, 224, 3).") + +if __name__ == "__main__": + unittest.main() From 5997b6d18cb7fbc74b5b1e7ee923ae83f7b73aa7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:34:37 +0000 Subject: [PATCH 12/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test_model_loading.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/test_model_loading.py b/test_model_loading.py index 0994028..e17d6d0 100644 --- a/test_model_loading.py +++ b/test_model_loading.py @@ -1,18 +1,30 @@ import unittest -from model_optimization import load_optimized_model # Replace with the actual module name + +from model_optimization import \ + load_optimized_model # Replace with the actual module name + class TestModelLoading(unittest.TestCase): def test_model_loading(self): """Test if the optimized model loads correctly.""" model = load_optimized_model() - self.assertIsNotNone(model, "The model should not be None after loading.") - self.assertTrue(hasattr(model, 'predict'), "The loaded model should have a 'predict' method.") + self.assertIsNotNone( + model, "The model should not be None after loading.") + self.assertTrue( + hasattr(model, "predict"), + "The loaded model should have a 'predict' method.", + ) def test_model_input_shape(self): """Test if the model input shape is as expected.""" model = load_optimized_model() input_shape = model.input_shape - self.assertEqual(input_shape, (None, 224, 224, 3), "Model input shape should be (None, 224, 224, 3).") + self.assertEqual( + input_shape, + (None, 224, 224, 3), + "Model input shape should be (None, 224, 224, 3).", + ) + if __name__ == "__main__": unittest.main() From 1d192bb47bef2fe0b64ab0d6588f8a2ed8265805 Mon Sep 17 00:00:00 2001 From: Harsimran Singh Dalal Date: Mon, 28 Oct 2024 21:05:23 +0530 Subject: [PATCH 13/14] Create test_preprocess_image.py --- test_preprocess_image.py | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test_preprocess_image.py diff --git a/test_preprocess_image.py b/test_preprocess_image.py new file mode 100644 index 0000000..b03c562 --- /dev/null +++ b/test_preprocess_image.py @@ -0,0 +1,41 @@ +import numpy as np +from tensorflow.keras.preprocessing.image import img_to_array, load_img + +def preprocess_image(image_path): + """Preprocess the image for model prediction.""" + try: + image = load_img(image_path, target_size=(224, 224)) + except FileNotFoundError: + raise FileNotFoundError(f"Image not found at path: {image_path}") + except Exception as e: + raise ValueError(f"Error loading image: {str(e)}") + + image = img_to_array(image) + if image.shape != (224, 224, 3): + raise ValueError(f"Image shape is invalid. Expected (224, 224, 3), got {image.shape}.") + + image = image / 255.0 # Normalize + image = np.expand_dims(image, axis=0) + return image +import unittest +from model_optimization import preprocess_image # Replace with actual module name + +class TestPreprocessImage(unittest.TestCase): + def test_nonexistent_image_path(self): + """Test case for a nonexistent image path.""" + with self.assertRaises(FileNotFoundError): + preprocess_image("invalid_path.jpg") + + def test_invalid_image_format(self): + """Test case for an invalid image format.""" + with self.assertRaises(ValueError): + preprocess_image("test_images/invalid_format.txt") + + def test_invalid_image_shape(self): + """Test case for an image with invalid shape.""" + # Simulate a grayscale image by resizing to (224, 224) and checking for failure. + with self.assertRaises(ValueError): + preprocess_image("test_images/grayscale_image.jpg") + +if __name__ == "__main__": + unittest.main() From 74846d97260c0b082e7f8265c4ee5679d8799a61 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:35:34 +0000 Subject: [PATCH 14/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test_preprocess_image.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test_preprocess_image.py b/test_preprocess_image.py index b03c562..e82ad44 100644 --- a/test_preprocess_image.py +++ b/test_preprocess_image.py @@ -1,6 +1,10 @@ +from model_optimization import \ + preprocess_image # Replace with actual module name +import unittest import numpy as np from tensorflow.keras.preprocessing.image import img_to_array, load_img + def preprocess_image(image_path): """Preprocess the image for model prediction.""" try: @@ -12,13 +16,14 @@ def preprocess_image(image_path): image = img_to_array(image) if image.shape != (224, 224, 3): - raise ValueError(f"Image shape is invalid. Expected (224, 224, 3), got {image.shape}.") + raise ValueError( + f"Image shape is invalid. Expected (224, 224, 3), got {image.shape}." + ) image = image / 255.0 # Normalize image = np.expand_dims(image, axis=0) return image -import unittest -from model_optimization import preprocess_image # Replace with actual module name + class TestPreprocessImage(unittest.TestCase): def test_nonexistent_image_path(self): @@ -37,5 +42,6 @@ def test_invalid_image_shape(self): with self.assertRaises(ValueError): preprocess_image("test_images/grayscale_image.jpg") + if __name__ == "__main__": unittest.main()