def predict(self, img, dtype='float'): self.load_models() img_bbox, img_landmarks = img.copy(), img.copy() # predict bounding box img_bbox, _ = frederic.utils.image.resize( img_bbox, 0, sampling_method=Image.LANCZOS) x = np.expand_dims(mobilenet_v2.preprocess_input(np.asarray(img_bbox)), axis=0) bbox = self.bbox_model.predict(x, verbose=0)[0, :4] # scale and translate predicted bounding box bbox = frederic.utils.image.postprocess_bounding_box(bbox, img.size) # predict landmarks inside predicted bounding box img_landmarks, _ = frederic.utils.image.crop(img_landmarks, 0, bbox) img_landmarks, _ = frederic.utils.image.resize( img_landmarks, 0, sampling_method=Image.LANCZOS) x = np.expand_dims(mobilenet_v2.preprocess_input( np.asarray(img_landmarks)), axis=0) y_pred = self.landmarks_model.predict(x, verbose=0)[0] # scale and translate predicted landmarks bb_size = np.max((bbox[2] - bbox[0], bbox[3] - bbox[1])) ratio = bb_size / frederic.utils.general.IMG_SIZE predicted_landmarks = (y_pred * ratio).reshape((-1, 2)) + bbox[:2] if 'int' in dtype: predicted_landmarks = np.round(predicted_landmarks) return predicted_landmarks.astype(dtype)
def cat_dog_image(): # Note that 'jpg' images can have RGB data # which is fine in the case of this model (requires three channels) img_path = 'tests/images/cat_dog.jpg' im = keras.preprocessing.image.load_img(img_path, target_size=(224, 224)) doc = keras.preprocessing.image.img_to_array(im) doc = np.expand_dims(doc, axis=0) mobilenet_v2.preprocess_input( doc) # because we our classifier is mobilenet_v2 return doc
def prepar_image(img): # prepar image before DNN model x = cv2.resize(img, (224, 224)) x = image.img_to_array(x) x = np.expand_dims(x,axis=0) x = preprocess_input(x) return x
def prepare_image(image, target): image = Image.open(io.BytesIO(image)) image = image.resize(target) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = preprocess_input(image) return image
def evaluate_face(model, image_path, output_dir): image = Image.open(image_path) origin_width, origin_heigth = image.size rescale_factor_width =origin_width/WIDTH_model rescale_factor_heigth = origin_heigth/ HEIGTH_model #Resizing into 128x128 because we trained the model with this image size. im = image.resize((WIDTH_model,HEIGTH_model)) img_array = np.array(im) #Our keras model used a 4D tensor, (images x height x width x channel) #So changing dimension 128x128x3 into 1x128x128x3 img_array = np.expand_dims(img_array, axis=0) img_array = preprocess_input(img_array) #Calling the predict method on model to predict 'me' on the image prediction = model.predict([img_array,img_array]) #print(prediction) face, box = round(prediction[0][0][0],2), prediction[1][0] print("There is a face? ", "Yes" if face>=confidence else "No") print("Box guess: " + str(box)) #if prediction is 0, which means I am missing on the image, then show the frame in gray color. if face >= confidence: #rescale box to original size x_1, x_2 = int(box[0]*rescale_factor_width),int(box[1]*rescale_factor_heigth) x_3, x_4 = int(box[0]*rescale_factor_width+box[2]*rescale_factor_width), int(box[1]*rescale_factor_heigth+box[3]*rescale_factor_heigth) img1 = ImageDraw.Draw(image) img1.rectangle([(x_1,x_2),(x_3,x_4)], outline="red") filename = os.path.basename(image_path).split(".")[0] image.save(output_dir + filename + "_evalued.jpg")
def detect_and_predict_mask(frame, faceNet, maskNet): (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 1.0, (224, 224), (104.0, 177.0, 123.0)) faceNet.setInput(blob) detections = faceNet.forward() print(detections.shape) faces = [] locs = [] preds = [] for i in range(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > 0.5: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") (startX, startY) = (max(0, startX), max(0, startY)) (endX, endY) = (min(w - 1, endX), min(h - 1, endY)) face = frame[startY:endY, startX:endX] face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) face = cv2.resize(face, (224, 224)) face = img_to_array(face) face = preprocess_input(face) faces.append(face) locs.append((startX, startY, endX, endY)) if len(faces) > 0: faces = np.array(faces, dtype="float32") preds = maskNet.predict(faces, batch_size=32) return (locs, preds)
def find_clusters(image_path, save_cluster_path): mobilenet_feature_list = [] for images in os.listdir(image_path): img = image.load_img(os.path.join(image_path, images), target_size=(224, 224)) img_data = image.img_to_array(img) img_data = np.expand_dims(img_data, axis=0) img_data = preprocess_input(img_data) mobilenet_feature = model.predict(img_data) mobilenet_feature_np = np.array(mobilenet_feature) mobilenet_feature_list.append(mobilenet_feature_np.flatten()) mobilenet_feature_list_np = np.array(mobilenet_feature_list) #kmeans = KMeans(n_clusters=50, random_state=0).fit(mobilenet_feature_list_np) dbscan = DBSCAN(eps=0.5, metric='euclidean', min_samples=2).fit(mobilenet_feature_list_np) y_kmeans = dbscan.labels_ #print(y_kmeans) max_labels = max(y_kmeans) for i in range(0, len(y_kmeans)): if y_kmeans[i] == -1: y_kmeans[i] = max_labels + 1 for i, j in zip(os.listdir(save_icon_path), y_kmeans): #print(i, j) if not os.path.exists(save_cluster_path): os.makedirs(save_cluster_path) #image2 = Image.open(os.path.join(save_icon_path, i)) icon = cv2.imread(os.path.join(save_icon_path, i)) if not os.path.exists(os.path.join(save_cluster_path, str(j))): os.makedirs(os.path.join(save_cluster_path, str(j))) cv2.imwrite(save_cluster_path + '/' + str(j) + '/' + str(i), icon)
def detect_and_predict_mask(frame, faceNet, maskNet): # grab the dimensions of the frame and then construct a blob # from it (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), (104.0, 177.0, 123.0)) # pass the blob through the network and obtain the face detections faceNet.setInput(blob) detections = faceNet.forward() # initialize our list of faces, their corresponding locations, # and the list of predictions from our face mask network faces = [] locs = [] preds = [] # loop over the detections for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with # the detection confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the confidence is # greater than the minimum confidence if confidence > 0.5: # compute the (x, y)-coordinates of the bounding box for # the object box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # ensure the bounding boxes fall within the dimensions of # the frame (startX, startY) = (max(0, startX), max(0, startY)) (endX, endY) = (min(w - 1, endX), min(h - 1, endY)) # extract the face ROI, convert it from BGR to RGB channel # ordering, resize it to 224x224, and preprocess it face = frame[startY:endY, startX:endX] face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) face = cv2.resize(face, (224, 224)) face = img_to_array(face) face = preprocess_input(face) face = np.expand_dims(face, axis=0) # add the face and bounding boxes to their respective # lists faces.append(face) locs.append((startX, startY, endX, endY)) # only make a predictions if at least one face was detected if len(faces) > 0: # for faster inference we'll make batch predictions on *all* # faces at the same time rather than one-by-one predictions # in the above `for` loop preds = maskNet.predict(faces) # return a 2-tuple of the face locations and their corresponding # locations return (locs, preds)
def load_dataset(self): imagePaths = list(paths.list_images(config.BASE_PATH)) data = [] labels = [] for imagePath in imagePaths: label = imagePath.split(os.path.sep)[-2] image = load_img(imagePath, target_size=config.INPUT_DIMS) image = img_to_array(image) image = preprocess_input(image) data.append(image) labels.append(label) data = np.array(data, dtype="float32") labels = np.array(labels) self.lb = LabelBinarizer() labels = self.lb.fit_transform(labels) labels = to_categorical(labels) (self.trainX, self.testX, self.trainY, self.testY) = train_test_split(data, labels, test_size=0.20, stratify=labels, random_state=42) return self
def img_preprocessing(pillowed_img: Image) -> np.ndarray: """ given a PIL.Image object resize, convert to RGB and return as np.array """ img = pillowed_img.resize((IMG_SHAPE[:-1]), Image.LANCZOS) img = img.convert("RGB") img_arr = np.asarray(img, dtype="float32") img_arr = preprocess_input(img_arr) # pixel scaling/color normalization return img_arr
def classify(input_folder, output_folder, model_name): """ Classify images as free or occupied and copy them to the right folders. :param input_folder: path to input folder with cropped images :param output_folder: path to output folder with classified images :param model_name: model name """ free_folder = os.path.join(output_folder, 'free') occupied_folder = os.path.join(output_folder, 'occupied') for subdir, _, files in os.walk(input_folder): for filename in files: input_path = os.path.join(subdir, filename) if input_path.endswith('.png'): img = load_img(input_path, target_size=(WIDTH, HEIGHT)) x = img_to_array(img) x = np.expand_dims(x, axis=0) is_occupied = ( models[model_name].predict(preprocess_input(x)) > 0.5) if is_occupied[0][0]: output_path = os.path.join(occupied_folder, filename) else: output_path = os.path.join(free_folder, filename) copyfile(input_path, output_path)
def test_model(model_path, test_folder, misclassified_folder): """ Test how well the model performs on a new data. :param model_path: path to the model folder :param test_folder: path to the classified images :param misclassified_folder: path to output folder with misclassified images """ model = load_inference_model(os.path.join(model_path, 'model.json'), os.path.join(model_path, 'weights.h5')) for subdir, _, files in os.walk(test_folder): for src_name in files: maybe_img = os.path.join(subdir, src_name) if maybe_img.endswith(('.png', '.jpg', '.bmp')): x = img_to_array(load_img(maybe_img, target_size=(WIDTH, HEIGHT))) x = np.expand_dims(x, axis=0) is_occupied = model.predict(preprocess_input(x)) > 0.5 is_occupied_gt = "occupied" in subdir if is_occupied[0][0]: if not is_occupied_gt: dest_folder = os.path.join(misclassified_folder, 'free') os.makedirs(dest_folder, exist_ok=True) dest_path = os.path.join(dest_folder, src_name) copyfile(maybe_img, dest_path) else: if is_occupied_gt: dest_folder = os.path.join(misclassified_folder, 'occupied') os.makedirs(dest_folder, exist_ok=True) dest_path = os.path.join(dest_folder, src_name) copyfile(maybe_img, dest_path)
def loaddata(imgfolderpath=".\\sample"): ''' Loads the images in the imgfolderpath to run the checks on the model. Parameters : imgfolderpath(string) : The path of the folder from where the image data has to be loaded. Returns: (np.array(batchsize, height, width, channels)) : The images to be fed into the model (list of images) : The list of original image to use while printing the results (list of string) : The names of the images to use while saving results ''' imgsname = os.listdir(imgfolderpath) imagespath = [os.path.join(imgfolderpath, imgpath) for imgpath in imgsname] _imglist = [] imglist = [] for imgpath in imagespath: _img = load_img(imgpath, target_size=(224, 224)) img = img_to_array(_img) img = preprocess_input(img) _imglist.append(_img) imglist.append(img) imgs = np.array(imglist) return imgs, _imglist, imgsname
def predict(folder): """ Predict state (free/occupied) for each image in the folder. :param folder: path to a folder with cropped images (assumes a separate subfolder for each model) :return: dict in the following format: {<slot id>: (<state>, <probability>), ...} <slot id>: int <state>: either 0 (means free) or 1 (means occupied) <probability>: probability that the parking slot is occupied (float in range [0..1]) """ res = {} for dirpath, _, filenames in os.walk(folder): model_name = os.path.basename(dirpath) if model_name not in models: continue model = models[model_name] for filename in filenames: if not filename.endswith('.png'): continue slot_id = int(filename.split('.')[-2].split(' - ')[-1]) filepath = os.path.join(dirpath, filename) img = load_img(filepath, target_size=(WIDTH, HEIGHT)) x = img_to_array(img) x = np.expand_dims(x, axis=0) prob_occupied = model.predict(preprocess_input(x))[0][0] res[slot_id] = (1 if prob_occupied > 0.5 else 0, str(prob_occupied)) return res
def detect_and_predict_mask(frame, faceNet, maskNet): (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), (104.0, 177.0, 123.0)) faceNet.setInput(blob) detection = faceNet.forward() faces = [] locs = [] preds = [] for i in range(0, detection.shape[2]): confidence = detection[0, 0, i, 2] if confidence > args["confidence"]: box = detection[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") (startX, startY) = (max(0, startX), max(0, startY)) (endX, endY) = (min(w - 1, endX), min(h - 1, endY)) face = frame[startY:endY, startX:endY] face = cv2.resize(face, (224, 224)) face = img_to_array(face) face = preprocess_input(face) face = np.expand_dims(face, axis=0) faces.append(face) locs.append((startX, startY, endX, endY)) if (len(faces) > 0): preds = maskNet.predict(faces) return (locs, preds)
def detect_face(frame, faceNet, model): #getting dimensions of the frame h, w = frame.shape[:2] #construct a blob that can be passed through the pre-trained network blob = cv2.dnn.blobFromImage(frame, 1, (224, 224), (104, 177, 123)) # pass the blob through the network and obtain the face detections faceNet.setInput(blob) detections = faceNet.forward() faces = [] locs = [] preds = [] for i in range(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > 0.6: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startx, starty, endx, endy) = box.astype('int') (startx, statry) = (max(0, startx), max(0, starty)) (endx, endy) = (min(w - 1, endx), min(h - 1, endy)) face = frame[starty:endy, startx:endx] face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) face = cv2.resize(face, (224, 224)) face = img_to_array(face) face = preprocess_input(face) faces.append(face) faces.append(face) locs.append((startx, starty, endx, endy)) if len(faces) > 0: faces = np.array(faces, dtype='float32') preds = model.predict(faces, batch_size=10) return (locs, preds)
def __init__(self, csv_file, batch_size=32, inmemory=False, number_of_elements_to_be_output=1): self._number_of_elements_to_be_output = number_of_elements_to_be_output assert (self._number_of_elements_to_be_output == 1) self.paths = [] self.batch_size = batch_size self.inmemory = inmemory with open(csv_file, "r") as file: self.y = np.zeros((sum(1 for line in file), ), dtype=int) file.seek(0) reader = csv.reader(file, delimiter=",") for index, (scaled_img_path, _, _, _, label) in enumerate(reader): if self._number_of_elements_to_be_output == 1: self.y[index] = label self.paths.append(scaled_img_path) self.y = lb.fit_transform(self.y) print(self.y) print(str(self.paths)) if self.inmemory: self.x = self.__load_images(self.paths) self.x = preprocess_input(self.x)
def extract_pic_features_inception(pics_data_file, pics_folder, path_to_save): # load the model model = InceptionV3(weights='imagenet', input_shape=(299, 299, 3), pooling='avg', include_top=False) img_dir = pics_folder with pics_data_file.open('r') as csvreader: data = csv.reader(csvreader, delimiter='\t') with path_to_save.open('w') as fw: for row in data: img_name = row[0] + '.jpg' img = img_dir / img_name try: image = load_img(img, target_size=(299, 299)) except Exception as e: print(e) continue image = img_to_array(image) image = image.reshape( (1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) feature = model.predict(image, verbose=0)[0] feature_str = (',').join([str(f) for f in feature]) res = (',').join([row[0], feature_str]) fw.write(res + '\n')
def predict(self, webcam_image): image = process_webcam_image(webcam_image) image = preprocess_input(image) x = np.expand_dims(image, axis=0) result = self.model.predict(x) # TODO: convert result to dict referencing class names return float(result[0][0])
def generate_class_activation_map(model, image_path, image_size, kernel_size): # Calculate full image result image = k_image.load_img(image_path, target_size=(image_size, image_size)) x = k_image.img_to_array(image) x_ = np.expand_dims(x, axis=0) x_ = k_mobilenet_v2.preprocess_input(x_) predictions = model.predict(x_) result = k_mobilenet_v2.decode_predictions(predictions, top=5)[0][0][2] # Checking by how much does the result drop by covering all parts of the image one at a time heatmap_x = 0 heatmap_y = 0 image_orig = k_image.load_img(image_path) x_orig = k_image.img_to_array(image_orig) height = x_orig.shape[0] width = x_orig.shape[1] heatmap = np.zeros((height // kernel_size + 1, width // kernel_size + 1)) for i in range(0, height, kernel_size): for j in range(0, width, kernel_size): print((heatmap_x, heatmap_y)) saved_rgb = np.zeros((kernel_size, kernel_size, 3)) for k in range(0, kernel_size): for l in range(0, kernel_size): index_y = i+k if i+k < height else height-1 index_x = j+l if j+l < width else width-1 for m in range(0, 3): saved_rgb[k][l][m] = x_orig[index_y][index_x][m] x_orig[index_y][index_x][m] = 0.5 img = k_image.array_to_img(x_orig) img = img.resize((224, 224)) arr = k_image.img_to_array(img) arr_ = np.expand_dims(arr, axis=0) arr_ = k_mobilenet_v2.preprocess_input(arr_) predictions = model.predict(arr_) diff = result - \ k_mobilenet_v2.decode_predictions(predictions, top=5)[0][0][2] heatmap[heatmap_y][heatmap_x] = diff if diff > 0 else 0 for k in range(0, kernel_size): for l in range(0, kernel_size): index_y = i+k if i+k < height else height-1 index_x = j+l if j+l < width else width-1 for m in range(0, 3): x_orig[index_y][index_x][m] = saved_rgb[k][l][m] heatmap_x += 1 heatmap_x = 0 heatmap_y += 1 plt.imshow(heatmap, cmap='hot', interpolation='nearest') plt.show()
def preprocess_image(self, img): img = image.array_to_img(img) print('Sum of pixels before resize:', np.array(img).sum()) img = img.resize((self.img_nrows, self.img_ncols)) img = image.img_to_array(img) print('Sum of pixels after resize:', np.array(img).sum()) img = preprocess_input(img) print('Sum of pixels after preprocess_input():', np.array(img).sum()) return np.array([img])
def get_embeddings(paths, model=model): imgs = np.array([ preprocess_input( image.img_to_array(load_img(path, target_size=(224, 224, 3)))) for path in paths ]) with graph.as_default(): embs = model.predict(imgs) return embs
def predict(input): images = mobilenet_v2.preprocess_input(input) with graph.as_default(): probas = mobile_net.predict(images) classes = probas.argmax(axis=1) return { "classes": classes.astype("int64"), "probabilities": probas.astype("double") }
def exercise_five(): # na koniec skorzystamy z gotowej, wytrenowanej juz sieci glebokiej # TODO: uruchom przyklad pobierajacy gotowa siec, wytrenowana na zbiorze ImageNet # pobranie gotowego modelu zlozonej sieci konwolucyjnej z odpowiednimi wagami # include_top=True oznacza ze pobieramy wszystkie warstwy - niektore zastosowania korzystaja tylko z dolnych model = k_mobilenet_v2.MobileNetV2(weights='imagenet', include_top=True) # podejrzenie tego z ilu i jakich warstw sie sklada layers = dict([(layer.name, layer.output) for layer in model.layers]) print("Network containts following layers:") for i, (name, layer) in enumerate(layers.items()): print("Layer {0} : {1}".format(i, (name, layer))) # oraz ile parametrow musialo zostac wytrenowanych print("Together: {0} parameters\n".format(model.count_params())) # TODO: odpowiedz, o ile wieksza jest ta siec od wytrenowanej przez nas? # powyzsza siec jest i tak wyjatkowo mala - sluzy do zastosowan mobilnych, wiec jest silnie miniaturyzowana # otworzmy przykladowe zdjecie i dostosujemy jego rozmiar i zakres wartosci do wejscia sieci image_path = 'nosacz.jpg' image = k_image.load_img(image_path, target_size=(224, 224)) # TODO: zastap None powyzej zmieniajac rozmiar na taki, jaki przyjmuje wejscie sieci (skorzystaj z wypisanego info) x = k_image.img_to_array( image) # kolejne linie dodatkowo dostosowuja obraz pod dana siec x = np.expand_dims(x, axis=0) x = k_mobilenet_v2.preprocess_input(x) # sprawdzmy jaki wynik przewidzi siec predictions = model.predict(x) # i przetlumaczmy uzywajac etykiet zrozumialych dla czlowieka (5 najbardziej prawdopodobnych klas zdaniem sieci) print('Predicted class:', k_mobilenet_v2.decode_predictions(predictions, top=5)[0]) # TODO: czy skonczylo sie sukcesem? # TODO: pobaw sie z innymi zdjeciami z Internetu - jak radzi sobie siec? kiedy sie myli? w jaki sposob? # TODO: (c.d.) pamietaj, ze siec rozumie tylko wymienione w ponizszym JSONIE klasy: # https://github.com/raghakot/keras-vis/blob/master/resources/imagenet_class_index.json # finalnie podgladamy aktywacje jakie wysylaja neurony sieci w trakcie dzialania # w wypisanych wczesniej informacjach mozna latwo spradzic ile kanalow ma warstwa o danym numerze (i ktora to) # layer_to_preview = 4 # numer warstwy, ktorej aktywacje podgladamy layer_to_preview = 65 # numer warstwy, ktorej aktywacje podgladamy # channel_to_preview = 16 # numer kanalu w tejze warstwie channel_to_preview = 0 # numer kanalu w tejze warstwie get_activations = k.function([model.layers[0].input], [model.layers[layer_to_preview].output]) activations = get_activations([x]) plt.imshow(activations[0][0, :, :, channel_to_preview], cmap="viridis") plt.show() # TODO: podejrzyj aktywacje w kolejnych warstwach; czym roznia sie te w poczatkowych od tych w koncowych? # o. ..0000.00 0.0 o.0 # colory znikajo, i rozdzielozcosc znika return model
def home_two(): model = k_mobilenet_v2.MobileNetV2(weights='imagenet', include_top=True) image_path = 'RTR6S1V.jpg' #224=7*2^5 image = k_image.load_img(image_path, target_size=(224, 224)) print("running") # TODO: zastap None powyzej zmieniajac rozmiar na taki, jaki przyjmuje wejscie sieci (skorzystaj z wypisanego info) x = k_image.img_to_array( image) # kolejne linie dodatkowo dostosowuja obraz pod dana siec x = np.expand_dims(x, axis=0) x = k_mobilenet_v2.preprocess_input(x) out_array = np.zeros( (224, 224, 2) ) # first is sum of % outcomes, second is number of tries later they will be divided size = 7 * 2 # 227/size must be int for i1 in range(224 - size): for k1 in range(224 - size): x_clone = np.copy(x) add_dark_square(x_clone, i1, k1, size) predictions = model.predict([x_clone]) add_to_all( out_array, i1, k1, size, k_mobilenet_v2.decode_predictions( predictions, top=5)[0][0][2]) # TODO cahange 0.5 to actual outcome heat_map = np.zeros((224, 224)) min_val = 1 max_val = 0 for i in range(224): for k in range(224): heat_map[i][k] = out_array[i][k][0] / out_array[i][k][1] if heat_map[i][k] < min_val: min_val = heat_map[i][k] if heat_map[i][k] > max_val: max_val = heat_map[i][k] plt.imshow(heat_map, cmap="hot", vmin=min_val, vmax=max_val) plt.show() print(heat_map) print(max_val) print(min_val) print("\n\n\n\n") print(type(x)) print(x) print(x.shape) predictions = model.predict(x) print('Predicted class:', k_mobilenet_v2.decode_predictions(predictions, top=5)[0]) print('Predicted class:', k_mobilenet_v2.decode_predictions(predictions, top=5)[0][0][1])
def prepare_image(path): """ This function returns the numpy array of an image """ img = image.load_img(path, target_size=(224, 224)) img_array = image.img_to_array(img) img_array_expanded_dims = np.expand_dims(img_array, axis=0) return preprocess_input(img_array_expanded_dims)
def detect_and_predict_mask(frame, faceNet, maskNet): # grab the dimensions of the frame and then construct a blob from it # a blob is just a of image with the same spatial dimensions (i.e., width and height), same depth (number of channels), that have all be preprocessed in the same manner. (h, w) = frame.shape[:2] # blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size, RGBmean, swapRB=True) # The mean RGB values are the means for each individual RGB channel across all images in your training set blob = cv2.dnn.blobFromImage(frame, 1, (224, 224), (104, 177, 123)) # blob.shape = (num_images=1, num_channels=3, width=224, height=224) # pass the blob through the network and obtain the face detections faceNet.setInput(blob) detections = faceNet.forward() # initialize our list of faces, their corresponding locations, and the list of predictions from our face mask network faces = [] locs = [] preds = [] # loop over the detections for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with the detection confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the confidence is greater than the minimum confidence if confidence > 0.5: # compute the (x, y)-coordinates of the bounding box for the object box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # ensure the bounding boxes fall within the dimensions of the frame (startX, startY) = (max(0, startX), max(0, startY)) (endX, endY) = (min(w - 1, endX), min(h - 1, endY)) # extract the face ROI, convert it from BGR to RGB channel ordering, resize it to 224x224, and preprocess it face = frame[startY:endY, startX:endX] face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) face = cv2.resize(face, (224, 224)) face = img_to_array(face) face = preprocess_input(face) # add the face and bounding boxes to their respective lists faces.append(face) locs.append((startX, startY, endX, endY)) # only make a predictions if at least one face was detected if len(faces) > 0: # for faster inference we'll make batch predictions on *all* # faces at the same time rather than one-by-one predictions # in the above `for` loop faces = np.array(faces, dtype="float32") preds = maskNet.predict(faces, batch_size=32) # return a 2-tuple of the face locations and their corresponding prediction return (locs, preds)
def weather_augment(image): """ Apply weather augmentation. :param image: original image :return: augmented image """ rand = np.random.randint(5) if rand == 4: image = augment_random(image.astype(np.uint8), aug_types=['add_snow', 'add_rain', 'add_fog', 'add_shadow']) return preprocess_input(image.astype(np.float32))
def detect_mask(face_crop): face_crop = cv2.cvtColor(face_crop, cv2.COLOR_BGR2RGB) face_crop = cv2.resize(face_crop, (224, 224)) face_crop = img_to_array(face_crop) face_crop = preprocess_input(face_crop) face_crop = np.expand_dims(face_crop, axis=0) (mask, withoutMask) = mask_detection.predict(face_crop)[0] label = "Mask" if mask > withoutMask else "No Mask" return label,(mask)
def prediction(test_img, model): test_img = cv2.resize(test_img, (224, 224)) test_img = img_to_array(test_img) test_img = preprocess_input(test_img) test_array = [] test_array.append(test_img) test_array = np.array(test_array) test_result = model.predict(test_array) return test_result