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)
def get_embeddings(self, pixls): samples = asarray(pixls, 'float32') samples = preprocess_input(samples, version=2) yhat = self.model.predict(samples) return yhat
def detect_and_predict_mask(frame, faceNet, version): # 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 > args["confidence"]: # 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)) pixels_expanded = np.expand_dims(face.astype(np.float64), axis=0) face = utils.preprocess_input(pixels_expanded, version=version)[0] print(face.shape) # add the face and bounding boxes to their respective # lists faces.append(face / 255) # print(face) locs.append((startX, startY, endX, endY)) faces = np.array(faces) #np.expand_dims(faces[0], axis=0) # only make a predictions if at least one face was detected if len(faces) > 0: for face in faces: preds.append(classify_image(interpreter, face)) # print(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_image_as_array(filepath: str) -> np.ndarray: img = image.load_img(filepath, target_size=(224, 224)) img = np.array(img).astype(np.float) return preprocess_input(img, version=2)
# CROP IMAGE start3=time() if img.shape > (width,height): #downsampling img_small=cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA) #resize the image to desired dimensions e.g., 256x256 elif img.shape < (width,height): #upsampling img_small=cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC) #resize the image to desired dimensions e.g., 256x256 cv2.imshow('frame',img_small) cv2.waitKey(1) #hit any key end3=time() print('face crop', end3-start3) #CREATE FACE EMBEDDINGS start4=time() pixels = img_small.astype('float32') samples = expand_dims(pixels, axis=0) samples = preprocess_input(samples, version=2) EMBEDDINGS = tflite_model.predict(samples) #print('.') end4=time() print('create face embeddings' , end4-start4) # READ CELEB EMBEDDINGS AND COMPARE start_EU=time() EuDist=[] with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: ergebniss_1=executor.submit(faceembeddingNP,EMBEDDINGS,np.array(celeb_embeddings[0].Embedding)) ergebniss_2=executor.submit(faceembeddingNP,EMBEDDINGS,np.array(celeb_embeddings[1].Embedding)) ergebniss_3=executor.submit(faceembeddingNP,EMBEDDINGS,np.array(celeb_embeddings[2].Embedding)) ergebniss_4=executor.submit(faceembeddingNP,EMBEDDINGS,np.array(celeb_embeddings[3].Embedding)) if ergebniss_1.done() & ergebniss_2.done() & ergebniss_3.done() & ergebniss_4.done(): EuDist.extend(ergebniss_1.result())
import numpy as np from keras_vggface.vggface import VGGFace from keras.preprocessing import image from keras_vggface import utils from sklearn.neighbors import NearestNeighbors if __name__ == "__main__": model = VGGFace(model='vgg16', include_top=False) # or VGGFace() as default img = image.load_img('matt.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(preds) print(preds.shape)
def read_img(path): img = image.load_img(path, target_size=(224, 224)) img = np.array(img).astype(np.float) return preprocess_input(img, version=2)
def callback(data): try: img = bridge.imgmsg_to_cv2(data, desired_encoding="passthrough") except CvBridgeError as e: print(e) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) global body if not body: faces = face_detector.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in faces: global sess global graph with graph.as_default(): tf.keras.backend.set_session(sess) cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2) arr = cv2.resize(img[y:y+h,x:x+w],(224,224)) arr = image.img_to_array(arr) arr = np.expand_dims(arr,axis=0) arr = utils.preprocess_input(arr,version=1) prediction = model.predict(arr) prediction = prediction.reshape(1,1,2048) finalPred = modelLin.predict(prediction) if (names[np.argmax(finalPred)] == navigateTo): if((2*x+w)/2 > 2*(img.shape[0]/3)): vel_msg.angular.z = -0.5 vel_msg.linear.x = 0.2 #0 elif((2*x+w)/2 < (img.shape[0]/3)): vel_msg.angular.z = 0.5 vel_msg.linear.x = 0.2 #0 else: vel_msg.angular.z = 0 vel_msg.linear.x = 0.2 vel_pub.publish(vel_msg) cv2.putText(img, names[np.argmax(finalPred)], (x+5,y-5), font, 1, (255,255,255), 2) else: if not BodyFound: gray = cv2.resize(gray, (320, 240)) bodies = body_detector.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in bodies: cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2) if((2*x+w)/2 > 2*(img.shape[0]/3)): vel_msg.angular.z = -0.5 vel_msg.linear.x = 0.2 #0 elif((2*x+w)/2 < (img.shape[0]/3)): vel_msg.angular.z = 0.5 vel_msg.linear.x = 0.2 #0 else: vel_msg.angular.z = 0 vel_msg.linear.x = 0.2 initBB = (x,y,w,h) tracker.init(img, initBB) global BodyFound BodyFound = True else: t_s = time.time() h, w = frame.shape[:2] #resize frame frame_re = cv2.resize(frame, (320, 240)) h_r, w_r = frame_re.shape[:2] (success, box) = tracker.update(frame_re) if success: (x,y,w,h) = [int(v) for v in box] cv2.rectangle(frame_re, (x, y), (x + w, y + h), (0, 255, 0), 2) if((2*x+w)/2 > 2*(img.shape[0]/3)): vel_msg.angular.z = -0.5 vel_msg.linear.x = 0.2 #0 elif((2*x+w)/2 < (img.shape[0]/3)): vel_msg.angular.z = 0.5 vel_msg.linear.x = 0.2 #0 else: vel_msg.angular.z = 0 vel_msg.linear.x = 0.2 t_end = time.time() # loop over the info tuples and draw them on our frame vel_pub.publish(vel_msg) vel_pub.publish(vel_msg) vel_pub.publish(vel_msg)
def elaborate_image(self, b_64_image): image = self.deserializer_image(b_64_image) return preprocess_input(image, version=2)
def main(): args = get_args() # appa_dir = args.appa_dir # utk_dir = args.utk_dir model_name = args.model_name batch_size = args.batch_size nb_epochs = args.nb_epochs lr = args.lr opt_name = args.opt image_size = args.img_size train_dir = args.db import ipdb ipdb.set_trace() # if model_name == "ResNet50": # image_size = 224 # elif model_name == "InceptionResNetV2": # image_size = 299 checkpoint_path = args.output_dir + "_seface_agerange" + str( fc_num) + '_' + str(unfrozen_num) + '_' + str( train_batchsize) + '_' + 'lr' + str(lr) + datetime.now().strftime( "%Y%m%d-%H%M%S") + '.ckpt' # train_gen = FaceGenerator(appa_dir, utk_dir=utk_dir, batch_size=batch_size, image_size=image_size) # val_gen = ValGenerator(appa_dir, batch_size=batch_size, image_size=image_size) train_generator = train_datagen.flow_from_directory( train_dir, target_size=(image_size, image_size), batch_size=nb_epochs, class_mode='categorical', shuffle=True, seed=2, subset='training') validation_generator = valid_datagen.flow_from_directory( train_dir, target_size=(image_size, image_size), batch_size=nb_epochs, class_mode='categorical', shuffle=True, seed=2, subset='validation') model = get_model(model_name=model_name) opt = get_optimizer(opt_name, lr) # Compile the model model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['acc']) model.summary() # output_dir = Path(__file__).resolve().parent.joinpath(args.output_dir) # output_dir.mkdir(parents=True, exist_ok=True) # cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, # # save_weights_only=True, # save_best_only=True, # # Save weights, every 5-epochs. # # period=5, # monitor='val_acc', # verbose=1, # mode='max') # callbacks = [ # keras.callbacks.LearningRateScheduler(lr_scheduler, verbose=1) # ] cp_callbacks = [ LearningRateScheduler(schedule=Schedule(nb_epochs, initial_lr=lr)), ModelCheckpoint( checkpoint_path + "/weights.{epoch:03d}-{val_loss:.3f}-{val_age_mae:.3f}.hdf5", monitor="val_age_mae", verbose=1, save_best_only=True, mode="min") ] history = model.fit_generator( train_generator, steps_per_epoch=train_generator.samples / train_generator.batch_size, epochs=nb_epochs, validation_data=validation_generator, validation_steps=validation_generator.samples / validation_generator.batch_size, # callbacks=[tensorboard_callback], callbacks=[cp_callback, tensorboard_callback], verbose=1, workers=4) print("Average validation loss: ", np.average(history.history['loss'])) np.savez(str(output_dir.joinpath("history.npz")), history=hist.history) fnames = validation_generator.filenames # valid_imglabels = validation_generator.labels ground_truth = validation_generator.classes best_model = load_model(checkpoint_path) true_y = [] pred_y = [] error = 0 for i in range(len(fnames)): y = ground_truth[i] true_y.append(y) title = 'Original label:{}'.format(fnames[i]) img = load_img('{}/{}'.format(train_dir, fnames[i]), target_size=(image_size, image_size)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) pred = best_model.predict(x) predlabel = np.argmax(pred, axis=1) pred_y.append(predlabel) if predlabel != y: error = error + 1 print('acc rate', (len(pred_y) - error) / len(pred_y)) print('error rate', error / len(pred_y)) import sklearn.metrics as metrics from sklearn.metrics import classification_report, confusion_matrix print(confusion_matrix(ground_truth, pred_y))
def get_embedding(model, face_image): face_image = preprocess_input(face_image, version=2) face_image = expand_dims(face_image, axis=0) embedding = model.predict(face_image) return embedding
def buttonClick(): """ handle button click event and output text from entry area""" print('hello') # do here whatever you want root.filename = filedialog.askopenfilename( initialdir="/home/namvh/Documents/do_an_cuoi_cung/file/imageTest", title="Select file", filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*"))) im = cv2.imread(root.filename) gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5) #luu anh files = glob.glob( '/home/namvh/Documents/do_an_cuoi_cung/file/pridict/image/*') for f in files: os.remove(f) files = glob.glob( '/home/namvh/Documents/do_an_cuoi_cung/file/pridict/feature/*') for f in files: os.remove(f) sampleNum = 0 for (x, y, w, h) in faces: sampleNum = sampleNum + 1 cv2.imwrite( "/home/namvh/Documents/do_an_cuoi_cung/file/pridict/image/img." + '1.' + str(sampleNum) + ".jpg", im[y:y + h, x:x + w]) #extra feature path = "/home/namvh/Documents/do_an_cuoi_cung/file/pridict/image" imgs = [] for f in os.listdir(path): imgs.append(Image.open(os.path.join(path, f))) for img in imgs: img_path = img.filename print(img_path) save_path = img_path.replace("image", "feature").replace(".jpg", ".npy") imga = image.load_img(img_path, target_size=(224, 224)) img_data = image.img_to_array(imga) img_data = np.expand_dims(img_data, axis=0) img_data = utils.preprocess_input(img_data, version=1) # or version=2 feature = model.predict(img_data) #print("[+] Extract feature from image : ", img_path) save_feature(save_path, feature) #load feature path = "/home/namvh/Documents/do_an_cuoi_cung/file/pridict/feature" imgs = [] results = {} #xac dinh danh tÃnh for f in os.listdir(path): filePath = os.path.join(path, f) test = np.load(filePath) result = clf.predict(np.reshape(test, (1, -1))) #chi so i i = f[6:] i = i[:i.find('.')] print(filePath, i, result) results[int(i)] = result[0] print(results) i = 1 #hiển thị danh tÃnh for (x, y, w, h) in faces: cv2.rectangle(im, (x, y), (x + w, y + h), (225, 0, 0), 2) cv2.putText(im, results[i], (x, y + h), font, 1, fontcolor) i = i + 1 plt.imshow(im) plt.show()
def read_img_vgg(path): """this function will read image from specified path and convert it into size of 224 for VGGFace""" img = cv2.imread(path) img = cv2.resize(img,(IMG_SIZE_VGG,IMG_SIZE_VGG)) img = np.array(img).astype(np.float) return preprocess_input(img, version=2)
def extract(self, img, face_records): '''Extract a template that allows the face to be matched.''' # Compute the 128D vector that describes the face in img identified by # shape. In general, if two face descriptor vectors have a Euclidean # distance between them less than 0.6 then they are from the same # person, otherwise they are from different people. Here we just print # the vector to the screen. im = pv.Image(img[:, :, ::-1]) for face_record in face_records.face_records: rect = pt.rect_proto2pv(face_record.detection.location) x, y, w, h = rect.asTuple() # Extract view rect = pv.Rect() cx, cy = x + 0.5 * w, y + 0.5 * h tmp = 1.5 * max(w, h) cw, ch = tmp, tmp crop = pv.AffineFromRect(pv.CenteredRect(cx, cy, cw, ch), (256, 256)) pvim = pv.Image(img[:, :, ::-1]) # convert rgb to bgr pvim = crop(pvim) view = pt.image_pv2proto(pvim) face_record.view.CopyFrom(view) # Extract landmarks l, t, r, b = [int(tmp) for tmp in [x, y, x + w, y + h]] d = dlib.rectangle(l, t, r, b) shape = self.shape_pred(img, d) for i in range(len(shape.parts())): loc = shape.parts()[i] landmark = face_record.landmarks.add() landmark.landmark_id = "point_%02d" % i landmark.location.x = loc.x landmark.location.y = loc.y # Get detection rectangle and crop the face #rect = pt.rect_proto2pv(face_record.detection.location).rescale(1.5) #tile = im.crop(rect) tile = pvim.resize((224, 224)) #tile.show(delay=1000) face_im = tile.asOpenCV2() face_im = face_im[:, :, ::-1] # Convert BGR to RGB #mat_ = cv2.cvtColor(mat,cv2.COLOR_RGB2GRAY) #mat = cv2.cvtColor(mat_,cv2.COLOR_GRAY2RGB) #img = image.load_img('../image/ajb.jpg', target_size=(224, 224)) from keras_vggface import utils from keras.preprocessing import image face_im = image.img_to_array(face_im) face_im = np.expand_dims(face_im, axis=0) face_im = utils.preprocess_input(face_im, version=2) # or version=2 # Needed in multithreaded applications with self.graph.as_default(): tmp = self.recognizer.predict(face_im) face_descriptor = pv.meanUnit(tmp.flatten()) #print('shape:',face_records.face_records[0].landmarks) #face_descriptor = self.face_rec.compute_face_descriptor(img, shape, JITTER_COUNT) #face_descriptor = np.array(face_descriptor) #vec = face_descriptor.flatten() face_record.template.data.CopyFrom( pt.vector_np2proto(face_descriptor))
def extract_video(self, id, x, y): embeddings = np.zeros((4096)) if not os.path.isfile(self.videos + id + ".mp4"): if self.verbose: print("--------Video {} not found-----------".format(self.videos + id + ".mp4")) return 1 if (not os.path.isfile(self.destination_dir + id + ".pkl")): if self.verbose: print("Resampling video", id) resample = "ffmpeg -nostats -loglevel 0 -y -i {1}{2}.mp4 -r {0} -t {3} '{4}{2}.mp4'".format(self.fps, self.videos, id, self.duration, self.destination_dir) res2 = subprocess.Popen(resample, stdout = FNULL, shell=True).communicate() if not os.path.isfile(self.destination_dir + id + ".mp4"): if self.verbose: print("--------Fault in video {}--------".format(id)) return 1 extract_frames = "ffmpeg -nostats -loglevel 0 -i '{0}{1}.mp4' {2}/%02d.jpg".format(self.destination_dir, id, self.frames_dir) rs = subprocess.Popen(extract_frames, stdout = FNULL, shell = True).communicate() for j in range(1, 7): if not os.path.isfile(self.frames_dir + "%02d" % j + ".jpg"): if self.verbose: print("------MISSING FRAME DETECTED FOR {} FRAME NO {}----".format(id, j)) continue if self.verbose: print("reading frame - {0}".format(j)) frame = Image.open(self.frames_dir + "%02d" % j + ".jpg") face_boxes = face_recognition.face_locations(np.array(frame), model= self.face_extraction_model) if(len(face_boxes) > 1): if self.verbose: print("-----2 faces detected in {0} frame {1}-----".format(id, j)) return 1 elif len(face_boxes) == 0: if self.verbose: print("-----No face detected in {} frame {}-----".format(id, j)) return 1 top, right, bottom, left = np.squeeze(face_boxes) frame_cropped = frame.crop(box = (left, top, right, bottom)) frame_resized = scipy.misc.imresize(np.array(frame_cropped), size = (160,160)) Image.fromarray(frame_resized).save(self.frame_cropped + id + '.jpg') frame_resized = np.expand_dims(np.array(frame_resized, dtype=np.float64), 0) frame_resized = utils.preprocess_input(frame_resized, version=1) embeddings = self.vgg_model.predict(frame_resized) break pickle.dump(embeddings, open(self.destination_dir + id + ".pkl", "wb")) delete_frames = "rm {0}*".format(self.frames_dir) delete_video = "rm '{0}'".format(self.destination_dir + id + ".mp4") rs = subprocess.Popen(delete_frames, stdout = subprocess.PIPE, shell = True).communicate() rs = subprocess.Popen(delete_video, stdout = subprocess.PIPE, shell = True).communicate() return 0
vgg_features = VGGFace(include_top=False, input_shape=(224, 224, 3), pooling='avg') # pooling: None, avg or max # LIST FILES files = sorted( glob.glob( '/home/cristianopatricio/Documents/Datasets/LFW/lfwcrop_color/faces/*.ppm' )) print(files) feats_list = [] # CALCULATE FEATURES for imagepath in files: print(imagepath) im = cv2.imread(imagepath) im = cv2.resize(im, (224, 224)) im_np = np.expand_dims(im, axis=0) im_np = im_np.astype('float32') im_preproc = utils.preprocess_input(im_np, version=1) feats = vgg_features.predict(im_preproc) feats_list.append(feats[0]) if len(feats_list) % 1000 == 0: np.save('lfw_feats_512.npy', feats_list) np.save('lfw_feats_512.npy', feats_list)
def preprocess_image(image_end): img = load_img(image_end, target_size=(224, 224)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = utils.preprocess_input(img, version=2) return img
def callback(data): try: img = bridge.imgmsg_to_cv2(data, desired_encoding="passthrough") except CvBridgeError as e: print(e) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) global body if not body: faces = face_detector.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in faces: global sess global graph with graph.as_default(): tf.keras.backend.set_session(sess) cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2) arr = cv2.resize(img[y:y+h,x:x+w],(224,224)) arr = image.img_to_array(arr) arr = np.expand_dims(arr,axis=0) arr = utils.preprocess_input(arr,version=1) prediction = model.predict(arr) prediction = prediction.reshape(1,1,2048) finalPred = modelLin.predict(prediction) if (names[np.argmax(finalPred)] == navigateTo): if((2*x+w)/2 > 2*(img.shape[0]/3)): vel_msg.angular.z = -0.4 vel_msg.linear.x = 0.2 #0 elif((2*x+w)/2 < (img.shape[0]/3)): vel_msg.angular.z = 0.4 vel_msg.linear.x = 0.2 #0 else: vel_msg.angular.z = 0 vel_msg.linear.x = 0.25 vel_pub.publish(vel_msg) cv2.putText(img, names[np.argmax(finalPred)], (x+5,y-5), font, 1, (255,255,255), 2) else: #img = cv2.resize(img, (320, 240)) if BodyFound == False: #gray = cv2.resize(gray, (320, 240)) #bodies = body_detector.detectMultiScale(gray, 1.3, 5) (H, W) = img.shape[:2] # determine only the *output* layer names that we need from YOLO ln = net.getLayerNames() ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] # construct a blob from the input image and then perform a forward # pass of the YOLO object detector, giving us our bounding boxes and # associated probabilities blob = cv2.dnn.blobFromImage(img, 1 / 255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) layerOutputs = net.forward(ln) # initialize our lists of detected bounding boxes, confidences, and # class IDs, respectively boxes = [] confidences = [] classIDs = [] # loop over each of the layer outputs for output in layerOutputs: # loop over each of the detections for detection in output: # extract the class ID and confidence (i.e., probability) of # the current object detection scores = detection[5:] classID = np.argmax(scores) if classID != HumanClass: continue confidence = scores[classID] # filter out weak predictions by ensuring the detected # probability is greater than the minimum probability if confidence > CONFIDENCE: # scale the bounding box coordinates back relative to the # size of the image, keeping in mind that YOLO actually # returns the center (x, y)-coordinates of the bounding # box followed by the boxes' width and height box = detection[0:4] * np.array([W, H, W, H]) (centerX, centerY, width, height) = box.astype("int") # use the center (x, y)-coordinates to derive the top and # and left corner of the bounding box x = int(centerX - (width / 2)) y = int(centerY - (height / 2)) # update our list of bounding box coordinates, confidences, # and class IDs boxes.append([x, y, int(width), int(height)]) confidences.append(float(confidence)) classIDs.append(classID) # apply non-maxima suppression to suppress weak, overlapping bounding # boxes idxs = cv2.dnn.NMSBoxes(boxes, confidences, CONFIDENCE, THRESHOLD) # ensure at least one detection exists if len(idxs) > 0: # loop over the indexes we are keeping for i in idxs.flatten(): # extract the bounding box coordinates (x, y) = (boxes[i][0], boxes[i][1]) (w, h) = (boxes[i][2], boxes[i][3]) cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2) if((2*x+w)/2 > 2*(img.shape[0]/3)): vel_msg.angular.z = -0.2 vel_msg.linear.x = 0.1 #0 elif((2*x+w)/2 < (img.shape[0]/3)): vel_msg.angular.z = 0.2 vel_msg.linear.x = 0.1 #0 else: vel_msg.angular.z = 0 vel_msg.linear.x = 0.3 global tracker tracker = cv2.TrackerKCF_create() initBB = (x,y,w,h) tracker.init(img, initBB) global BodyFound BodyFound = True else: h, w = img.shape[:2] (success, box) = tracker.update(img) if success: (x,y,w,h) = [int(v) for v in box] cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) if((2*x+w)/2 > 2*(img.shape[0]/3)): vel_msg.angular.z = -0.2 vel_msg.linear.x = 0.1 #0 elif((2*x+w)/2 < (img.shape[0]/3)): vel_msg.angular.z = 0.2 vel_msg.linear.x = 0.1 #0 else: vel_msg.angular.z = 0 vel_msg.linear.x = 0.3 #x_center = (2*x + w)/2 #msg = rospy.wait_for_message("/camera/depth_registered/image", Image) #depth_data = bridge.imgmsg_to_cv2(msg, desired_encoding="16UC1") #if depth_data[y,x_center] > 500: vel_pub.publish(vel_msg) else: global BodyFound BodyFound = False cv2.imshow('image', img) cv2.waitKey(3)
def process_image(image): img = cv.resize(image, (224, 224)) img = img.astype(np.float32) img = np.expand_dims(img, axis=0) img = utils.preprocess_input(img, version=2) return img
def preprocess_dataset(idx=0, total_workers=1): train_limit = len(x_train_all) // total_workers test_limit = len(x_test_all) // total_workers x_train = x_train_all[idx * train_limit:(idx + 1) * train_limit] y_train = y_train_all[idx * train_limit:(idx + 1) * train_limit] x_test = x_test_all[idx * test_limit:(idx + 1) * test_limit] y_test = y_test_all[idx * test_limit:(idx + 1) * test_limit] print("[Worker%d]: Processing training images..." % idx) x_train_labels = [] x_train_features = [] x_train_crashed = [] for i, fname in enumerate(x_train): try: feature = extract_face(fname) except AssertionError as e: print(">>>> [Worker%d]: %s" % (idx, str(e))) x_train_crashed.append((fname, y_train[i])) continue except Exception as e: print(">>>> [Worker%d]: Exception: %s" % (idx, str(e))) print(fname) x_train_crashed.append((fname, y_train[i])) continue x_train_labels.append(y_train[i]) x_train_features.append(feature) print("[Worker%d]: Processing test images..." % idx) x_test_labels = [] x_test_features = [] x_test_crashed = [] for i, fname in enumerate(x_test): try: feature = extract_face(fname) except AssertionError as e: print(">>>> [Worker%d]: %s" % (idx, str(e))) x_test_crashed.append((fname, y_train[i])) continue except Exception as e: print(">>>> [Worker%d]: Exception: %s" % (idx, str(e))) print(fname) x_test_crashed.append((fname, y_train[i])) continue x_test_labels.append(y_test[i]) x_test_features.append(feature) crashed = (x_train_crashed, x_test_crashed) print("[Worker%d]: Preprocessing inputs..." % idx) x_train_processed = preprocess_input(x_train_features, version=2) x_test_processed = preprocess_input(x_test_features, version=2) print("[Worker%d]: Creating bottleneck features..." % idx) p_train = model.predict(x_train_processed) p_test = model.predict(x_test_processed) try: print("[Worker%d]: Removing dataset dir..." % idx) shutil.rmtree(DATASET_DIR) except: pass print("[Worker%d]: Saving features..." % idx) np.save(os.path.join(CURR_DIR, "features-train-worker%d" % idx), p_train) np.save(os.path.join(CURR_DIR, "features-test-worker%d" % idx), p_test) np.save(os.path.join(CURR_DIR, "labels-train-worker%d" % idx), x_train_labels) np.save(os.path.join(CURR_DIR, "labels-test-worker%d" % idx), x_test_labels)
def image2x(image_path): img = image.load_img(image_path, 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 return x
def get_model_scores(faces, model): samples = np.asarray(faces, 'float32') # prepare the data for the model samples = preprocess_input(samples, version=2) # perform prediction return model.predict(samples)
def read_img(path): img = cv2.imread(path) img = np.array(img).astype(np.float) return preprocess_input(img, version=2)
plt.ylabel('Cross Entropy') plt.title('Custom Model Training and Validation Loss') plt.xlabel('epoch') plt.show() #my image after converting to size 224*224 test1_img = load_img('/Data/Prediction_Images/Test_1.jpg', target_size=(224, 224)) plt.imshow(test1_img) #preprocess image img = img_to_array(test1_img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) #prediction using custom model all_predictions = custom_final_model.predict(img) predicted_class = np.argmax(all_predictions, axis=1) #getting labels labels = (train_data.class_indices) labels = dict((indx, lab) for lab, indx in labels.items()) predictions = [labels[indx] for indx in predicted_class] #show label after predictions print("[OUT]:The person in the image is : ", predictions) #Dwayne Johnson(The Rock) Image after converting to size 224*224
proposed_model.add(Dense(1024, input_dim=2048, activation='linear')) #resnet proposed_model.add(Dropout(0.1)) proposed_model.add(Lambda(l2_norm)) proposed_model.load_weights( dirpath + '/weights/male_resnet.h5') #load appropriate weights # first image: img_1 = cv2.imread(img_url_1) img_1 = img_1 / 255 img_1 = img_1[bbox_1[1]:bbox_1[1] + bbox_1[3], bbox_1[0]:bbox_1[0] + bbox_1[2], :] img_1 = misc.imresize(img_1, (224, 224), interp='bilinear') img_1 = image.img_to_array(img_1) img_1 = np.expand_dims(img_1, axis=0) img_1 = utils.preprocess_input( img_1, version=version) # version=1 for vgg16, version=2 for resnet50 predict_1 = model.predict(img_1) # predict with baseline model baseline_features_1 = preprocessing.normalize(predict_1, norm='l2', axis=1, copy=True, return_norm=False) proposed_features_1 = proposed_model.predict( predict_1) # predict with proposed model # second image: img_2 = cv2.imread(img_url_2) img_2 = img_2 / 255 img_2 = img_2[bbox_2[1]:bbox_2[1] + bbox_2[3], bbox_2[0]:bbox_2[0] + bbox_2[2], :] img_2 = misc.imresize(img_2, (224, 224), interp='bilinear')
startX = bounding_box[0] endX = bounding_box[0] + bounding_box[0] + bounding_box[2] startY = bounding_box[1] endY = bounding_box[1] + bounding_box[3] # crop the detected face region face_crop = frame[bounding_box[1]:bounding_box[1] + bounding_box[3], bounding_box[0]:bounding_box[0] + bounding_box[2], :] # if (face_crop.shape[0]) < 10 or (face_crop.shape[1]) < 10: # continue face_crop = cv2.resize(face_crop, (224, 224)) face_crop = img_to_array(face_crop) face_crop = np.expand_dims(face_crop, axis=0) face_crop = preprocess_input(face_crop) cv2.rectangle(frame, (bounding_box[0], bounding_box[1]), (bounding_box[0] + bounding_box[2], bounding_box[1] + bounding_box[3]), (0, 155, 255), 2) Y = startY - 10 if startY - 10 > 10 else startY + 10 # # # apply gender detection on face conf = model.predict(face_crop)[0] # print(conf) # get label with max accuracy idx_l = np.argmax(conf) mxconf = np.max(conf) conf_age = age_model.predict(face_crop)[0] age_idx = np.argmax(conf_age) mxconf_age = np.max(conf_age) # if age_idx == 0 :