def delete(self, project, image): """ Deletes an image from a project. """ p = get_project(project) if p is None: return "project not found", 404 fn = "projects/{}/{}".format(p['id'], image) tfn = "projects/{}/{}".format(p['id'], image.split('.')[0] + '.tcapture') purl = "{}/{}".format(project, image) if 'activePhoto' in p: if p['activePhoto']['imgUrl'] == purl: del p['activePhoto'] photos = [] if 'photos' in p: for photo in p['photos']: if photo['imgUrl'] != purl: photos.append(photo) p['activePhoto'] = photo p["photos"] = photos persist(p) if not settings.storage.exists(fn): return 404 settings.storage.delete_cloud_file(fn) if settings.storage.exists(tfn): tcapture = json.loads(settings.storage.download_to_string(tfn)) logging.debug(tcapture) for img in tcapture["images"]: settings.storage.delete_cloud_file(img['path']) settings.storage.delete_cloud_file(tfn) return None, 204
def put(self, name): """ Updates a Project. Use this method to change attributes of a project. * Send a JSON object to turn on prediction for a project. ``` { "predict":true } ``` * Specify the name of the category to modify in the request URL path. """ data = request.json if settings.storage.exists("projects/projects.json"): pr = json.loads( settings.storage.download_to_string("projects/projects.json")) else: return {'error': 'no projects exist'}, 404 if name not in pr: return {'error': 'no project exists with that name'}, 404 if name in data: return { 'error': 'project name should not be included in json request' }, 422 pid = pr[name]['id'] opr = json.loads( settings.storage.download_to_string( "projects/{}/state.json".format(pid))) z = {**opr, **data} persist(z) return None, 204
def post(self): """ Creates a new project. Use this method to create a new project. Projects are used to determine path for bucket location and prediction engine path. """ data = request.json if settings.storage.exists("projects/projects.json"): pr = json.loads( settings.storage.download_to_string("projects/projects.json")) if data['name'] in projects: return 'object exists with that name', 409 logging.debug(data) persist(data) return 'Project successfully created.', 201
def put(self, cam, project): """ Produces snapshot(s) from a camera selecting additional paramaters """ data = request.json p = get_project(project) if p is None: return 404 if not "photos" in p: p["photos"] = [] image_name = uuid.uuid4() camera = settings.camera.get_cam(cam) img, _, _, _ = settings.camera.get_picture(cam) ofn = "projects/{}/{}{}".format(p['id'], image_name, settings.data["image_type"]) settings.storage.upload_data(img.tobytes(), ofn, contentType='image/jpeg') images = [] resp_obj = { "image": str(image_name) + settings.data["image_type"], "path": ofn, 'images': images } tcapture = { "request": data, "images": images, "base_image": ofn, "base_image_format": settings.data["image_type"], "camera": camera.__getstate__() } purl = "{}/{}{}".format(p['id'], image_name, settings.data["image_type"]) height, width = img.shape[:2] ptag = { "modified": 1548116137598, "dimensions": { "width": width, "height": height, "size": len(img.tobytes()) }, "photoId": str(image_name), "title": "photo:{} - camera:{}".format(len(p["photos"]), cam), "expanded": False, "imgUrl": purl, "children": [] } p["photos"].append(ptag) p["activePhoto"] = ptag persist(p) if 'base64' in data and data['base64'] == True: image = Image.open(io.BytesIO(img.tobytes())) image.thumbnail((128, 128), Image.ANTIALIAS) imgByteArr = io.BytesIO() image.save(imgByteArr, format='JPEG') imgByteArr = imgByteArr.getvalue() ncoded_string = base64.b64encode(imgByteArr).decode("utf-8") resp_obj['base64'] = ncoded_string tcapture["base_image_b64"] = ncoded_string if 'count' in data: for i in range(data['count'] - 1): img, _, _, _ = settings.camera.get_picture(cam) ofn = "projects/{}/{}/{}{}".format(p['id'], image_name, i, settings.data["image_type"]) settings.storage.upload_data(img.tobytes(), ofn, contentType='image/jpeg') images.append({ "image": str(i) + settings.data["image_type"], "path": ofn }) tofn = "projects/{}/{}{}".format(p['id'], image_name, ".tcapture") settings.storage.upload_data(json.dumps(tcapture), tofn, contentType='application/json') return resp_obj
def get(self, cam, project): """ Takes a snapshot and uploads it to the bucket/project_folder. """ p = get_project(project) if p is None: return 404 if not "photos" in p: p["photos"] = [] image_name = uuid.uuid4() camera = settings.camera.get_cam(cam) img, _, _, _ = settings.camera.get_picture(cam) ofn = "projects/{}/{}{}".format(p['id'], image_name, settings.data["image_type"]) settings.storage.upload_data(img.tobytes(), ofn, contentType='image/jpeg') tcapture = { "request": {}, "images": [], "base_image": ofn, "base_image_format": settings.data["image_type"], "camera": camera.__getstate__() } resp_obj = { "image": str(image_name) + settings.data["image_type"], "path": ofn } image = Image.open(io.BytesIO(img.tobytes())) image.thumbnail((128, 128), Image.ANTIALIAS) imgByteArr = io.BytesIO() image.save(imgByteArr, format='JPEG') imgByteArr = imgByteArr.getvalue() ncoded_string = base64.b64encode(imgByteArr).decode("utf-8") purl = "{}/{}{}".format(p['id'], image_name, settings.data["image_type"]) height, width = img.shape[:2] ptag = { "modified": 1548116137598, "dimensions": { "width": width, "height": height, "size": len(img.tobytes()) }, "photoId": str(image_name), "title": "photo:{} - camera:{}".format(len(p["photos"]), cam), "expanded": False, "imgUrl": purl, "children": [] } p["photos"].append(ptag) p["activePhoto"] = ptag resp_obj['base64'] = ncoded_string tcapture["base_image_b64"] = ncoded_string tofn = "projects/{}/{}{}".format(p['id'], image_name, ".tcapture") settings.storage.upload_data(json.dumps(tcapture), tofn, contentType='application/json') persist(p) return resp_obj