Exemplo n.º 1
0
    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"}
Exemplo n.º 2
0
    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