def post(): print("saving image...") print("base64 image") data = _child_parser.parse_args() image = data['photo'] img_name = str(uuid.uuid4()) + '.jpg' create_new_folder(os.path.join(real_path, 'images')) saved_path = os.path.join(os.path.join(real_path, 'images'), img_name) with open(saved_path, "wb") as fh: fh.write(base64.decodebytes(image.encode())) # section for saving child info into database child = ChildModel(data['name'], data['address'], data['parent_name'], data['phone'], img_name) child.save_to_db() print("cropping face...") id = child.id # get this value from database(child record id) if not crop_face(saved_path, os.path.join(os.getcwd(), 'croped_images/' + str(id))): return {"message": "face not found in image"}, 404 print("generating multiple images...") # generating multiple image from uploaded image datagen = ImageDataGenerator(rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') img = load_img( os.path.join(os.getcwd(), 'croped_images/' + str(id) + '/1.jpg')) # this is a PIL image x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150) x = x.reshape( (1, ) + x.shape) # this is a Numpy array with shape (1, 3, 150, 150) # the .flow() command below generates batches of randomly transformed images # and saves the results to the `preview/` directory if not os.path.exists(os.path.join(os.getcwd(), 'train/' + str(id))): os.makedirs(os.path.join(os.getcwd(), 'train/' + str(id))) i = 0 for batch in datagen.flow(x, batch_size=1, save_to_dir=os.path.join( os.getcwd(), 'train/' + str(id)), save_prefix='image', save_format='jpg'): i += 1 if i > 5: break # training print("training...") train(id) return {"message": "success"}
def post(self): data = attributes.parse_args() parents_id = list(data['parents'].split(",")) if len(parents_id) > 2: return {"message": "Child cannot have more than 2 Parents"} child = ChildModel(**data) child.save_child() return {"message": "Child cread successfully!"}, 201
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."}
def get(self): user_id = get_jwt_identity() children = [child.json() for child in ChildModel.find_all()] if user_id: return {'children': children}, 200 return { 'message': 'no data available unless you log in.', 'error': 'token expired' }, 401
def get(self, parents): childs = ChildModel.childs() data = [] if childs: for child in childs: if len(child.parents) == parents: data.appen(child) return data return {'message': 'There are no registered childs'}, 404
def put(self, id): data = attributes.parse_args() parents_id = list(data['parents'].split(",")) if len(parents_id) > 2: return {"message": "Child cannot have more than 2 Parents"} child = ChildModel.find_child(id) if child: child.update_child(data) return {'message': 'Updated child'} return {'message': 'Child not found'}, 404
def get(self): childs = ChildModel.childs() temp_childs = [] if childs: for child in childs: data = { 'id': child.id, 'name': child.name, 'child': child.child, 'createdAt': str(child.createdAt), 'updatedAt': str(child.updatedAt) } temp_childs.append(data) return temp_childs return {'message': 'There are no registered childs'}, 404
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
def post(self, child_id): data = parser.parse_args() if ChildModel.find_by_name(data['first_name'], data['last_name'], data['dob']): return {"message": "Child with that name already exists."}, 400 child = ChildModel(**data) try: child.save_to_db() except: return {"message": "An error occurred adding the child."}, 500 return child.json(), 201
def get(): children = ChildModel.find_all() return children, 200
def delete(cls, child_id): child = ChildModel.find_by_id(child_id) if not child: return {'message': 'Child not found.'}, 404 child.delete_from_db() return {'message': 'Child deleted.'}, 200
def get(cls, child_id): child = ChildModel.find_by_id(child_id) if not child: return {'message': 'Child not found.'}, 404 return child.json()
def delete(self, id): child = ChildModel.find_child(id) if child: child.delete_child() return {'message': 'Child deleted'} return {'message': 'Child not found'}, 404
def get(self, id): child = ChildModel.find_child(id) if child: return child.json() return {'message': 'Child not found'}, 404