This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def pre_process(face, required_size=(160, 160)): | |
| ret = cv2.resize(face, required_size) | |
| #ret = cv2.cvtColor(ret, cv2.COLOR_BGR2RGB) | |
| ret = ret.astype('float32') | |
| # standardize pixel values across channels (global) | |
| mean, std = ret.mean(), ret.std() | |
| ret = (ret - mean) / std | |
| return ret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import matplotlib.pyplot as plt | |
| from mtcnn.mtcnn import MTCNN | |
| def read_image(file): | |
| img = cv2.imread(file) | |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
| return img | |
| def crop_bb(image, detection, margin): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // changes made to the model implementation | |
| public class TFLiteObjectDetectionAPIModel | |
| implements SimilarityClassifier { | |
| ... | |
| // the output size is 192 | |
| private static final int OUTPUT_SIZE = 192; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Changes made to the model interface | |
| // This interface was renamed from Classifier to SimilarityClassifier | |
| public interface SimilarityClassifier { | |
| // added method for registering samples into the dataset | |
| void register(String name, Recognition recognition); | |
| ... | |
| public class Recognition { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow.lite as lite | |
| input_file = "models/sandberg/facenet_keras.h5" | |
| output_file = "models/sandberg/facenet.tflite" | |
| # Converts the Keras model to TensorFlow Lite | |
| converter = lite.TocoConverter.from_keras_model_file(input_file) | |
| converter.post_training_quantize = True | |
| tflite_model = converter.convert() | |
| open(output_file, "wb").write(tflite_model) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| from tensorflow.keras import models | |
| # Loads Hiroki's FaceNet model | |
| model_file = 'models/hiroki/facenet_keras.h5' | |
| hiroki_model = models.load_model(model_file) | |
| # Creates a random image | |
| img = np.random.randint(0, 255, (160, 160, 3), dtype='uint8') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow.lite as lite | |
| input_file = "models/hiroki/facenet_keras.h5" | |
| output_file = "models/hiroki/facenet.tflite" | |
| # Converts the Keras model to TensorFlow Lite | |
| converter = lite.TocoConverter.from_keras_model_file(input_file) | |
| converter.post_training_quantize = True | |
| tflite_model = converter.convert() | |
| open(output_file, "wb").write(tflite_model) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Override | |
| public List<Recognition> recognizeImage(final Bitmap bitmap) { | |
| ... | |
| Object[] inputArray = {imgData}; | |
| // Here outputMap is changed to fit the Face Mask detector | |
| Map<Integer, Object> outputMap = new HashMap<>(); | |
| output = new float[1][2]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DetectorActivity | |
| extends CameraActivity | |
| implements OnImageAvailableListener { | |
| ... | |
| // Face Mask | |
| private static final int TF_OD_API_INPUT_SIZE = 224; | |
| private static final boolean TF_OD_API_IS_QUANTIZED = false; | |
| private static final String TF_OD_API_MODEL_FILE = "mask_detector.tflite"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void onFacesDetected(long currTimestamp, List<Face> faces) { | |
| final List<Classifier.Recognition> mappedRecognitions = | |
| new LinkedList<Classifier.Recognition>(); | |
| ... | |
| final Canvas cvFace = new Canvas(faceBmp); |
NewerOlder