コード例 #1
0
 def get(self, cam, project):
     """
     Takes a snapshot and uploads it to the bucket/project_folder/predictions/ and
     returns a json document with the prediction info.
     * coords = relative coordinates of the capture at a percentage of image
     * image = Image name
     * path = Path to the image in the bucket
     * tags = Predition probability for each tag
     ```
     {
         "coords":{
             "c2_good":[0.209846, 0.0299885, 1, 0.637807],
             "emmc_good":[0.344515, 0.593382, 0.500586, 0.694372],
             "hdmi_good":[0.315496, 0.896179, 0.623717, 0.960438],
             "rpi3_cpu_good":[0.155855, 0.417758, 0.295672, 0.517903]
         },
         "image": "92b7245f-970a-4ec5-8fd5-4377b809ec07.jpg",
         "path": "project0/predictions/92b7245f-970a-4ec5-8fd5-4377b809ec07.jpg",
         "tags":{
             "c2_good": 0.422927,
             "emmc_good": 0.640976,
             "hdmi_good": 0.432281,
             "num_detections": "9",
             "rpi3_cpu_good": 0.535758
         }
     }
     ```
     """
     if not project in projects:
         pr = get_project(project)
         projects[project] = Project(project, pr)
         if pr is None:
             return 'Project not found.', 404
     p = projects[project]
     image_name = uuid.uuid4()
     img, tags, detections, ppmm = settings.camera.get_picture(cam,
                                                               predict=True,
                                                               project=p)
     ofn = "projects/{}/predictions/images/{}{}".format(
         p.id, image_name, settings.data["image_type"])
     dfn = "projects/{}/predictions/data/{}{}".format(
         p.id, image_name, 'json')
     p.s3.put_object(Body=img.tobytes(),
                     Bucket=p.bucket,
                     Key=ofn,
                     ContentType='image/jpeg')
     resp_obj = {
         "image": str(image_name) + settings.data["image_type"],
         "path": ofn,
         'tags': tags,
         'detections': detections,
         'ppmm': ppmm,
         'model': p.model,
         'model_version': p.version
     }
     p.s3.put_object(Body=json.dumps(resp_obj),
                     Bucket=p.bucket,
                     Key=dfn,
                     ContentType='application/json')
     return jsonify(resp_obj)
コード例 #2
0
ファイル: train.py プロジェクト: opendedup/visionbackend
    def post(self, project):
        """
        Uploads an image to the project training set.
        Images are uploaded as mutli-part \"images\" form
        element
        """
        if not project in projects:
            pr = get_project(project)
            projects[project] = Project(project, pr)
            if pr is None:
                return 'Project not found.', 404
        p = projects[project]
        fl = request.files['images']
        image_name = fl.filename.replace(',', '_')
        fn = secure_filename(p.tempdir + image_name)
        try:
            fl.save(fn)
            im = Image.open(fn)
            if not im.format == 'JPEG':
                im = im.convert("RGB")
                im.save(fn + '.jpg')
                os.remove(fn)
                fn = fn + '.jpg'

            ofn = "projects/{}/{}{}".format(p.id, image_name,
                                            settings.data["image_type"])
            with open(fn, 'rb') as file_t:
                blob_data = bytearray(file_t.read())
                settings.storage.upload_data(blob_data,
                                             ofn,
                                             contentType='image/jpeg')
                logging.info("uploaded {}".format(ofn))
        finally:
            os.remove(fn)
        return {'msg': 'Image uploaded'}, 200
コード例 #3
0
ファイル: train.py プロジェクト: opendedup/visionbackend
    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
コード例 #4
0
    def post(self, project):
        """
        Predicts results based on uploaded file.
        """
        print('uploading')
        if not project in projects:
            pr = get_project(project)
            projects[project] = Project(project, pr)
            if pr is None:
                return 'Project not found.', 404
        p = projects[project]
        f = request.files['images']
        p = projects[project]
        fl = request.files['images']
        image_name = fl.filename
        fn = secure_filename(p.tempdir + image_name)
        try:
            fl.save(fn)
            im = Image.open(fn)
            if not im.format == 'JPEG':
                im = im.convert("RGB")
                im.save(fn + '.jpg')
                os.remove(fn)
                fn = fn + '.jpg'

            image_name = str(uuid.uuid4())
            img, bimg, tags, detections, ppmm = settings.camera.read_picture(
                fn, predict=True, project=p)
            with open(fn, 'rb') as file_t:
                blob_data = bytearray(file_t.read())
                ofn = "projects/{}/predictions/images/{}{}".format(
                    p.id, image_name, settings.data["image_type"])
                settings.storage.upload_data(blob_data,
                                             ofn,
                                             contentType='image/jpeg')
            ofn = "projects/{}/predictions/data/{}.{}".format(
                project, image_name, 'json')
            image = Image.open(io.BytesIO(bimg.tobytes()))
            image.thumbnail((800, 800), Image.ANTIALIAS)
            imgByteArr = io.BytesIO()
            image.save(imgByteArr, format='JPEG')
            imgByteArr = imgByteArr.getvalue()
            ncoded_string = base64.b64encode(imgByteArr).decode("utf-8")
            resp_obj = {
                "image": str(image_name) + settings.data["image_type"],
                "path": ofn,
                'tags': tags,
                'detections': detections,
                'ppmm': ppmm,
                'model': p.model,
                'model_version': p.version,
                'base64': ncoded_string
            }
            settings.storage.upload_data(json.dumps(resp_obj),
                                         ofn,
                                         contentType='application/json')
        finally:
            os.remove(fn)
        return jsonify(resp_obj)
コード例 #5
0
ファイル: projects.py プロジェクト: opendedup/visionbackend
 def get(self, name):
     """
     Returns a project.
     """
     pr = get_project(name)
     if pr is None:
         return 'Project not found.', 404
     return pr
コード例 #6
0
ファイル: train.py プロジェクト: opendedup/visionbackend
 def get(self, project):
     """
     Returns a list of all images available for training.
     """
     p = get_project(project)
     if p is None:
         return 404
     fn = "projects/{}/".format(p['id'])
     lst = settings.storage.list_files(fn, delimiter='/', metadata=True)
     rl = []
     for t in lst:
         print(t)
         if t['name'].endswith('.jpg'):
             t['name'] = t['name'][len(fn):]
             t['imgUrl'] = project + '/' + t['name']
             rl.append(t)
     return rl
コード例 #7
0
ファイル: train.py プロジェクト: opendedup/visionbackend
 def get(self, project, image):
     """
     Returns an existing image in the project.
     """
     p = get_project(project)
     if p is None:
         return 404
     fn = "projects/{}/{}".format(p['id'], image)
     if not settings.storage.exists(fn):
         return 404
     image_data = settings.storage.download_to_data(fn)
     response = make_response(image_data)
     response.headers.set('Content-Type', 'image/jpeg')
     response.headers.set('Content-Disposition',
                          'attachment',
                          filename='%s.jpg' % fn)
     return response
コード例 #8
0
 def get(self, project, image):
     """
     Returns an existing image in the project.
     """
     if not project in projects:
         pr = get_project(project)
         projects[project] = Project(project, pr)
         if pr is None:
             return 'Project not found.', 404
     p = projects[project]
     fn = "projects/{}/predictions/images/{}".format(p.id, image)
     obj = p.s3.get_object(Bucket=data["bucket"], Key=fn)
     image_data = obj['Body'].read()
     response = make_response(image_data)
     response.headers.set('Content-Type', 'image/jpeg')
     response.headers.set('Content-Disposition',
                          'attachment',
                          filename='%s.jpg' % fn)
     return response
コード例 #9
0
    def get(self, project, cam):
        """
        Streams the video from the designated camera.
        """
        if not project in projects:
            pr = get_project(project)
            projects[project] = Project(project, pr)
            if pr is None:
                return 'Project not found.', 404
        p = projects[project]

        def gen(cam, project):
            """Video streaming generator function."""
            while True:
                frame = settings.camera.getstream(cam,
                                                  predict=True,
                                                  project=project)
                yield (b'--jpgboundary\r\n'
                       b'Content-Type: image/jpeg\r\n\r\n' + bytearray(frame) +
                       b'\r\n')

        return Response(
            gen(cam, p),
            mimetype='multipart/x-mixed-replace; boundary=--jpgboundary')
コード例 #10
0
ファイル: train.py プロジェクト: opendedup/visionbackend
    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
コード例 #11
0
ファイル: train.py プロジェクト: opendedup/visionbackend
    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