Пример #1
0
    def post(self):

        # confirm that the X-Manual header was specified
        manual = checkXManual(request)

        prevId = -1
        if 'X-Crop-Id' in request.headers:
            prevId = request.headers.get('X-Crop-Id')
        else:
            abort(400, "Need to specify header 'X-Crop-Id'!")

        dao = OutgoingManualDAO(
            defaultConfigPath()) if manual else OutgoingAutonomousDAO(
                defaultConfigPath())

        outgoingIn = outgoing_manual(
            json=request.get_json()) if manual else outgoing_autonomous(
                json=request.get_json())
        outgoingIn.crop_id = prevId
        resultingId = dao.upsertClassification(outgoingIn)

        if resultingId == -1:
            return {
                'message':
                'Failed to insert classification into outgoing table'
            }, 500

        response = make_response(
            jsonify({
                'message': 'success!',
                'id': resultingId
            }))
        response.headers['X-Class-Id'] = resultingId
        return response
Пример #2
0
    def get(self):
        manual = checkXManual(request)
        dao = SubmittedTargetDAO(defaultConfigPath())

        resultingClassifications = dao.getAllTargets(not manual)
        if resultingClassifications is None or not resultingClassifications:
            return {
                'message':
                'Failed to retrieve any submitted targets (Have any been submitted?)'
            }, 404

        resultingClassifications = [
            classification.toDict()
            for classification in resultingClassifications
        ]

        croppedDao = CroppedManualDAO(
            defaultConfigPath()) if manual else CroppedAutonomousDAO(
                defaultConfigPath())

        for target in resultingClassifications:
            # for each of these targets we need to try and get the crop_id for it from the crop_path
            # crop_id is much more useful client-side than crop_path
            croppedImg = croppedDao.getImageWithCropPath(target['crop_path'])
            if croppedImg is not None:
                target['crop_id'] = croppedImg.crop_id

        return jsonify(resultingClassifications)
Пример #3
0
    def put(self, crop_id):

        # confirm that the X-Manual header was specified
        manual = checkXManual(request)

        content = request.get_json()
        if content is None:
            abort(400, 'Must specify values to update')
        if 'image_id' in content:
            abort(400, 'Updating image_id is forbidden!')
        if 'crop_id' in content:
            abort(400, "Updating the crop_id is forbidden")

        dao = CroppedManualDAO(
            defaultConfigPath()) if manual else CroppedAutonomousDAO(
                defaultConfigPath())
        result = dao.updateImage(crop_id, content)
        if result is None:
            return {
                'message':
                'No image with id {} found to update (or was there a server error?)'
                .format(crop_id)
            }, 404
        else:
            return jsonify(result.toJsonResponse())
Пример #4
0
    def get(self, crop_id):
        # confirm that the X-Manual header was specified
        manual = checkXManual(request)

        dao = CroppedManualDAO(
            defaultConfigPath()) if manual else CroppedAutonomousDAO(
                defaultConfigPath())
        image = dao.getImage(crop_id)

        if image is None:
            return {
                'message': 'Failed to locate cropped id {}'.format(crop_id)
            }, 404
        return jsonify(image.toJsonResponse())
Пример #5
0
    def get(self):
        # startTime = time.time()
        # Input Validation::
        manual = checkXManual(request)

        # Get Content
        dao = IncomingImageDAO(defaultConfigPath())
        image = dao.getNextImage(manual)

        # response validation
        if image is None:
            return {'message': 'Failed to locate untapped image'}, 404

        # print("Request fulfillment: {}".format(time.time()-startTime))
        return rawImageSender(image.image_id, image.image_path)
Пример #6
0
    def get(self, class_id):

        # confirm that the X-Manual header was specified
        manual = checkXManual(request)

        dao = OutgoingManualDAO(
            defaultConfigPath()) if manual else OutgoingAutonomousDAO(
                defaultConfigPath())
        result = dao.getClassification(class_id)
        if result is None:
            return {
                'message':
                'Failed to locate classification with id {}'.format(class_id)
            }, 404
        return jsonify(result.toDict())
Пример #7
0
    def get(self):

        # confirm that the X-Manual header was specified
        manual = checkXManual(request)

        dao = CroppedManualDAO(
            defaultConfigPath()) if manual else CroppedAutonomousDAO(
                defaultConfigPath())
        croppedList = dao.getAll()

        if croppedList == None or not croppedList:
            return {'message': 'Cropped table is empty!'}, 404

        exportable = [img.toJsonResponse() for img in croppedList]
        return jsonify(exportable)
Пример #8
0
    def get(self):
        # confirm that the X-Manual header was specified
        manual = checkXManual(request)

        # Get content:
        dao = CroppedManualDAO(
            defaultConfigPath()) if manual else CroppedAutonomousDAO(
                defaultConfigPath())
        image = dao.getNextImage()

        # response validation
        if image is None:
            return {'message': 'Failed to locate untapped image'}, 404

        # success!
        return cropImageSender(image, image.cropped_path)
Пример #9
0
    def get(self):
        manual = checkXManual(request)
        dao = getClassificationDAO(manual)

        results = dao.getPendingTargets()
        if results is None or not results or not results[0]:
            return {
                'message': 'Could not locate any targets pending submission'
            }, 404

        jsonible = []
        for target in results:
            jsonible.append(
                [classification.toDict() for classification in target])

        return jsonify(jsonible)
Пример #10
0
    def put(self, class_id):

        # confirm that the X-Manual header was specified
        manual = checkXManual(request)

        dao = OutgoingManualDAO(
            defaultConfigPath()) if manual else OutgoingAutonomousDAO(
                defaultConfigPath())
        result = dao.updateClassification(class_id, request.get_json())
        if result is None:
            return {
                'message':
                'No image with id {} found with a classification to update or your input was invalid (or was there a server error?)'
                .format(class_id)
            }, 404
        else:
            return jsonify(result.toDict())
Пример #11
0
    def get(self):
        # confirm that the X-Manual header was specified
        manual = checkXManual(request)

        outgoingList = []
        dao = OutgoingManualDAO(
            defaultConfigPath()) if manual else OutgoingAutonomousDAO(
                defaultConfigPath())
        outgoingList = dao.getAll()

        if not outgoingList:
            return {'message': 'Outgoing table is empty!'}, 404

        exportable = [
            classification.toDict() for classification in outgoingList
        ]
        return jsonify(exportable)
Пример #12
0
    def get(self, target_id):
        manual = checkXManual(request)
        dao = SubmittedTargetDAO(defaultConfigPath())

        resultTarget = dao.getTarget(target_id, (not manual))
        if resultTarget is None:
            return {
                'message': 'Failed to retrieve target {}'.format(target_id)
            }, 404

        # get the crop id from the path the target is using.
        # the crop_id is much more useful client-size than a crop_path
        outDict = resultTarget.toDict()
        croppedDao = CroppedManualDAO(
            defaultConfigPath()) if manual else CroppedAutonomousDAO(
                defaultConfigPath())
        croppedImg = croppedDao.getImageWithCropPath(outDict['crop_path'])
        if croppedImg is not None:
            outDict['crop_id'] = croppedImg.crop_id

        return jsonify(outDict)
Пример #13
0
    def post(self):
        manual = checkXManual(request)
        dao = getClassificationDAO(manual)

        # cant really do any fancy checks like above, just go for submission:
        resultingTargets = dao.submitAllPendingTargets()

        if resultingTargets is None:
            return {
                'message':
                'Either something went wrong, or there are not pending targets to submit'
            }, 404

        targetSubmitDao = SubmittedTargetDAO(defaultConfigPath())
        finalRet = []
        for result in resultingTargets:
            try:
                submittedTarget = writeTargetToODLCFile(result, manual)
                if submittedTarget is None:
                    dao.resetTargetSubmissionStatus(result.target)
                    print(
                        "ERR: Something went wrong trying to save the target to the ODLC format! Does the {} crop_id {} exist?"
                        .format('manual' if manual else 'autonomous',
                                result.crop_id))
                if submittedTarget is not None:
                    finalRet.append(submittedTarget.toAuvsiJson())
                    # add the target to our submitted_targets table where the ROS_handler
                    # will grab it and ship it over to interop. Our work here is done :)
                    submittedTarget.submitted = None  # get rid of classifications submission status
                    targetSubmitDao.upsertTarget(submittedTarget)
            except (Exception) as e:
                # something failed, make sure the target classifications are reset to 'unsubmitted'
                dao.resetTargetSubmissionStatus(result.target)
                targetSubmitDao.removeTarget(result.target, (not manual))
                raise  # rethrow the same exception

        return jsonify(finalRet)
Пример #14
0
    def delete(self, class_id):

        # confirm that the X-Manual header was specified
        manual = checkXManual(request)

        dao = OutgoingManualDAO(
            defaultConfigPath()) if manual else OutgoingAutonomousDAO(
                defaultConfigPath())

        if dao.getClassification(class_id) is None:
            return {
                'message':
                'Couldnt find classification with id {}'.format(class_id)
            }, 404

        result = dao.removeClassification(class_id)
        if not result:
            return {
                'message':
                'Something went wrong while trying to delete id {} (was it delete by someone else first??)'
                .format(class_id)
            }, 500

        return {'message': 'success!', 'id': class_id}
Пример #15
0
    def post(self):
        # confirm that the X-Manual header was specified
        manual = checkXManual(request)

        # basic setup pieces for this taken from :
        # http://flask.pocoo.org/docs/1.0/patterns/fileuploads/
        # Input Validation:
        if 'cropped_image' not in request.files:
            abort(400, "Need to pass an image with the key 'cropped_image'")
        imageFile = request.files.get('cropped_image', '')

        # use the right model object depending on if this is a manual or autonomous request
        cropped = cropped_manual() if manual else cropped_autonomous()
        # confirm that we got an image_id in the headers
        if 'X-Image-Id' in request.headers:
            cropped.image_id = request.headers.get('X-Image-Id')
        else:
            abort(400, "Need to specify header 'X-Image-Id'!")

        # make sure the filename wont make our computer explode:
        if imageFile.filename == '' or imageFile.filename == 'cropped_image':
            # if we didnt get a filename we assume jpg. why not....
            extension = 'jpg'
        elif not allowedFileType(imageFile.filename):
            abort(400, "Filetype is not allowed!")
        else:
            extension = getFileExtension(imageFile.filename)

        imageFile.filename = str(int(time.time())) + "-" + str(
            randint(0, 10000)) + '.' + extension

        filename = secure_filename(imageFile.filename)
        # save image
        full_path = os.path.join(defaultCroppedImgPath(), filename)
        imageFile.save(full_path)
        cropped.cropped_path = full_path

        # check for the optional crop coordinate form data
        if 'crop_coordinate_tl' in request.form:
            cropped.crop_coordinate_tl = point(
                ptStr=request.form['crop_coordinate_tl'])
        if 'crop_coordinate_br' in request.form:
            cropped.crop_coordinate_br = point(
                ptStr=request.form['crop_coordinate_br'])

        # get the timestamp from incoming_image table to copy into this guy....
        # meh, but functional /shrug
        dao = IncomingImageDAO(defaultConfigPath())
        img = dao.getImage(cropped.image_id)
        if img is not None:
            cropped.time_stamp = img.time_stamp
        else:
            print(
                "WARN:: failed to find incoming_image model at id {} X-Image-Id"
                .format(cropped.image_id))
            cropped.time_stamp = int(time.time())

        # add to db
        dao = CroppedManualDAO(
            defaultConfigPath()) if manual else CroppedAutonomousDAO(
                defaultConfigPath())
        # resultingId is the cropped_manual.id value given to this image (abstracted from client)
        resultingId = dao.upsertCropped(cropped)

        if resultingId == -1:
            return {
                'message':
                'Failed to insert image into manual_cropped! (If youre trying to update information on an image_id that already exists, you should use PUT)'
            }, 500

        # done!
        response = make_response(
            jsonify({
                'message': 'success!',
                'id': resultingId
            }))
        response.headers['X-Crop-Id'] = resultingId
        return response
Пример #16
0
    def post(self, target_id):
        manual = checkXManual(request)
        dao = getClassificationDAO(manual)
        content = request.get_json(
            silent=True)  #silence error throwing if no json present

        # do a bunch of checks before we even think about submitting a target
        allTargetIds = dao.getAllTargetIDs()

        if allTargetIds is None or not allTargetIds:
            # is there even stuff in the table?
            return {
                'message': 'No classifications in outgoing (table empty)!'
            }, 404
        elif target_id not in allTargetIds:
            # is the target_id we are directed to submit in the table?
            return {
                'message':
                'Failed to find any targets with id {} to submit'.format(
                    target_id)
            }, 404

        if dao.isTargetSubmitted(target_id):
            # have we submitted said target already?
            return {
                'message':
                'Target {} has already been submitted'.format(target_id)
            }, 400

        # if all the above checks pass, we know the specified target has yet to be
        # submitted, and has data we can use for submission. Lets do it
        resultTarget = None
        try:
            resultTarget = dao.submitPendingTarget(target_id, content)
        except Exception as e:
            # something failed, make sure the target classifications are reset to 'unsubmitted'
            dao.resetTargetSubmissionStatus(target_id)
            raise  # rethrow the same exception

        if resultTarget is None:
            dao.resetTargetSubmissionStatus(
                target_id)  # something failed, reset to unsubmitted state
            return {
                'message':
                'Something went wrong while trying to submit target {}'.format(
                    target_id)
            }, 500

        submittedTarget = None
        targetSubmitDao = SubmittedTargetDAO(defaultConfigPath())
        try:
            submittedTarget = writeTargetToODLCFile(resultTarget, manual)
        except Exception as e:
            # something failed, make sure the target classifications are reset to 'unsubmitted'
            dao.resetTargetSubmissionStatus(target_id)
            raise  # rethrow the same exception

        if submittedTarget is None:
            dao.resetTargetSubmissionStatus(target_id)
            print(
                "ERR: Something went wrong trying to save the target to the ODLC format! Does the {} crop_id {} exist?"
                .format('manual' if manual else 'autonomous',
                        resultTarget.crop_id))
            return {
                'message':
                'Something went wrong trying to save the target to the ODLC format! Does the {} crop_id {} exist?'
                .format('manual' if manual else 'autonomous',
                        resultTarget.crop_id)
            }, 404
        else:
            # send to interop via the submitted_target table
            try:
                submittedTarget.submitted = None  # get rid of classifications submission status
                targetSubmitDao.upsertTarget(submittedTarget)
            except Exception as e:
                dao.resetTargetSubmissionStatus(target_id)
                targetSubmitDao.removeTarget(target_id, (not manual))
                raise

        # we did it!
        return jsonify(submittedTarget.toAuvsiJson())