Example #1
0
    def get(child_id):
        print("Deleting...")
        child = ChildModel.find_user_by_id(child_id)
        if child:
            try:
                print("removing from db...")
                child.remove_from_db()
            except:
                print("error occured")

            try:
                print("deleting from csv...")
                df = pd.read_csv("data.csv", header=None)
                df = df.set_index(0)
                df = df.drop(child_id)
                df.to_csv("data.csv", header=None)
            except:
                print("error occurred")

            try:
                print("deleting from images folder")
                child_image_name = child.image
                path = os.path.join(real_path, 'images', child_image_name)
                os.remove(path)
            except:
                print("error occurred")

            try:
                print("deleting from train folder")
                path = os.path.join(os.getcwd(), 'train', str(child_id))
                shutil.rmtree(path)
            except:
                print("error occurred")

            try:
                print("deleting from croped_images folder")
                path = os.path.join(os.getcwd(), 'croped_images',
                                    str(child_id))
                shutil.rmtree(path)
            except:
                print("error occurred")

            try:
                print("Training Model...")
                data = pd.read_csv(os.path.join(real_path, 'data.csv'),
                                   header=None)
                labels = data.loc[:, 0].to_numpy()
                data = data.loc[:, 1:].to_numpy()
                clf = svm.SVC(probability=True)
                clf.fit(data, labels)
                pickle.dump(clf, open("svm_model.sav", 'wb'))
            except:
                print("error occured")

            return {"message": "success"}
        else:
            print("child not found")
            return {"message": "Child Doesn't Exists."}
Example #2
0
 def post(self):
     print("getting image...")
     if request.files.get("image"):
         print("image file..")
         img = request.files['image']
         img_name = str(uuid.uuid4()) + '.jpg'
         create_new_folder(os.path.join(real_path, 'temp_images'))
         saved_path = os.path.join(os.path.join(real_path, 'temp_images'),
                                   img_name)
         img.save(saved_path)
     else:
         print("base64 image..")
         data = _child_search_parser.parse_args()
         img_name = str(uuid.uuid4()) + '.jpg'
         create_new_folder(os.path.join(real_path, 'temp_images'))
         saved_path = os.path.join(os.path.join(real_path, 'temp_images'),
                                   img_name)
         with open(saved_path, "wb") as fh:
             fh.write(base64.decodebytes(data['image'].encode()))
     print("cropping face...")
     if not crop_face(saved_path,
                      os.path.join(os.getcwd(), 'temp_croped_images')):
         return {"message": "face not found in image"}, 404
     print("finding matching image...")
     try:
         model_path = os.path.join(real_path, "model.h5")
         vgg_face_descriptor = load_model(model_path)
         svm_model = pickle.load(
             open(os.path.join(real_path, "svm_model.sav"), 'rb'))
         x = vgg_face_descriptor.predict(
             preprocess_image(
                 os.path.join(real_path, 'temp_croped_images/1.jpg')))
         print("Probability: " + str(np.max(svm_model.predict_proba(x))))
         if np.max(svm_model.predict_proba(x)) < 0.6:
             return {"message": "Child not found"}, 404
         res = svm_model.predict(x)
         child = ChildModel.find_user_by_id(res[0])
         print(child)
         if child:
             return {"message": "success", "data": child.json()}, 200
         else:
             return {"message": "Child not found"}, 404
     except:
         return {"message": "Child not found"}, 404