Example #1
0
 def create(self, analysisResult=AnalysisResult()):
     """
     (AnalysisResult) -> (AnalysisResult)
     Add analysis result to database
     """
     analysisResultDB = AnalysisResultDB(analysisResult=analysisResult)
     session = self.session_factory()
     session.add(analysisResultDB)
     session.flush()
     session.commit()
     session.refresh(analysisResultDB)
     return AnalysisResult(
         analysisResultDB.id,
         Analysis(
             analysisResultDB.analysis.id,
             Image(
                 analysisResultDB.analysis.image.id,
                 Disease(
                     analysisResultDB.analysis.image.disease.id,
                     Plant(
                         analysisResultDB.analysis.image.disease.plant.id,
                         analysisResultDB.analysis.image.disease.plant.
                         scientificName, analysisResultDB.analysis.image.
                         disease.plant.commonName),
                     analysisResultDB.analysis.image.disease.scientificName,
                     analysisResultDB.analysis.image.disease.commonName),
                 analysisResultDB.analysis.image.url,
                 analysisResultDB.analysis.image.description,
                 analysisResultDB.analysis.image.source,
                 analysisResultDB.analysis.image.size),
             Classifier(
                 analysisResultDB.analysis.classifier.id,
                 Plant(
                     analysisResultDB.analysis.classifier.plant.id,
                     analysisResultDB.analysis.classifier.plant.
                     scientificName,
                     analysisResultDB.analysis.classifier.plant.commonName),
                 analysisResultDB.analysis.classifier.tag,
                 analysisResultDB.analysis.classifier.path),
             user=User(id=analysisResultDB.analysis.user.id,
                       idType=analysisResultDB.analysis.user.idType,
                       email=analysisResultDB.analysis.user.email,
                       username=analysisResultDB.analysis.user.username)),
         Disease(
             analysisResultDB.disease.id,
             Plant(analysisResultDB.disease.plant.id,
                   analysisResultDB.disease.plant.scientificName,
                   analysisResultDB.disease.plant.commonName),
             analysisResultDB.disease.scientificName,
             analysisResultDB.disease.commonName), analysisResultDB.score,
         analysisResultDB.frame)
Example #2
0
    def searchByID(self, id):
        """
        (int) -> (AnalysisResult)
        Search analysis result by ID
        """
        session = self.session_factory()
        analysisResultDB = session.query(AnalysisResultDB).get(id)
        if (analysisResultDB is None):
            raise Exception("AnalysisResults not found!")

        return AnalysisResult(
            analysisResultDB.id,
            Analysis(
                analysisResultDB.analysis.id,
                Image(
                    analysisResultDB.analysis.image.id,
                    Disease(
                        analysisResultDB.analysis.image.disease.id,
                        Plant(
                            analysisResultDB.analysis.image.disease.plant.id,
                            analysisResultDB.analysis.image.disease.plant.
                            scientificName, analysisResultDB.analysis.image.
                            disease.plant.commonName),
                        analysisResultDB.analysis.image.disease.scientificName,
                        analysisResultDB.analysis.image.disease.commonName),
                    analysisResultDB.analysis.image.url,
                    analysisResultDB.analysis.image.description,
                    analysisResultDB.analysis.image.source,
                    analysisResultDB.analysis.image.size),
                Classifier(
                    analysisResultDB.analysis.classifier.id,
                    Plant(
                        analysisResultDB.analysis.classifier.plant.id,
                        analysisResultDB.analysis.classifier.plant.
                        scientificName,
                        analysisResultDB.analysis.classifier.plant.commonName),
                    analysisResultDB.analysis.classifier.tag,
                    analysisResultDB.analysis.classifier.path),
                user=User(id=analysisResultDB.analysis.user.id,
                          idType=analysisResultDB.analysis.user.idType,
                          email=analysisResultDB.analysis.user.email,
                          username=analysisResultDB.analysis.user.username)),
            Disease(
                analysisResultDB.disease.id,
                Plant(analysisResultDB.disease.plant.id,
                      analysisResultDB.disease.plant.scientificName,
                      analysisResultDB.disease.plant.commonName),
                analysisResultDB.disease.scientificName,
                analysisResultDB.disease.commonName), analysisResultDB.score,
            analysisResultDB.frame)
Example #3
0
 def create(self, disease=Disease()):
     """
     (Disease) -> (Disease)
     """
     diseaseDB = DiseaseDB(disease=disease)
     session = self.session_factory()
     session.add(diseaseDB)
     session.flush()
     session.refresh(diseaseDB)
     session.commit()
     return Disease(
         diseaseDB.id,
         Plant(diseaseDB.plant.id, diseaseDB.plant.scientificName,
               diseaseDB.plant.commonName), diseaseDB.scientificName,
         diseaseDB.commonName)
Example #4
0
 def create(self, analysis=Analysis()):
     """
     (Analysis) -> (Analysis)
     Add analysis to database
     """
     analysisDB = AnalysisDB(analysis=analysis)
     session = self.session_factory()
     session.add(analysisDB)
     session.flush()
     session.refresh(analysisDB)
     session.commit()
     return Analysis(
         analysisDB.id,
         image=Image(
             analysisDB.image.id,
             Disease(
                 analysisDB.image.disease.id,
                 Plant(analysisDB.image.disease.plant.id,
                       analysisDB.image.disease.plant.scientificName,
                       analysisDB.image.disease.plant.commonName),
                 analysisDB.image.disease.scientificName,
                 analysisDB.image.disease.commonName), analysisDB.image.url,
             analysisDB.image.description, analysisDB.image.source,
             analysisDB.image.size),
         classifier=Classifier(
             analysisDB.classifier.id,
             Plant(analysisDB.classifier.plant.id,
                   analysisDB.classifier.plant.scientificName,
                   analysisDB.classifier.plant.commonName),
             analysisDB.classifier.tag, analysisDB.classifier.path),
         user=User(id=analysisDB.user.id,
                   idType=analysisDB.user.idType,
                   email=analysisDB.user.email,
                   username=analysisDB.user.username))
Example #5
0
    def update(self, image=Image()):
        """
        (Image) -> (Image)
        Update database image
        """
        session = self.session_factory()
        imageDB = session.query(ImageDB).filter_by(id=image.id).first()
        dic = {}
        if (imageDB.disease.id != image.disease.id):
            dic['idDisease'] = image.disease.id
        if (imageDB.url != image.url):
            dic['url'] = image.url
        if (imageDB.description != image.description):
            dic['description'] = image.description
        if (imageDB.source != image.source):
            dic['source'] = image.source
        if (imageDB.size != image.size):
            dic['size'] = image.size
        if (dic != {}):
            session.query(ImageDB).filter_by(id=image.id).update(dic)
            session.commit()
            session.flush()
            session.refresh(imageDB)

        return Image(
            imageDB.id,
            Disease(
                imageDB.disease.id,
                Plant(imageDB.disease.plant.id,
                      imageDB.disease.plant.scientificName,
                      imageDB.disease.plant.commonName),
                imageDB.disease.scientificName, imageDB.disease.commonName),
            imageDB.url, imageDB.description, imageDB.source, imageDB.size)
Example #6
0
 def search(self, image=Image(), pageSize=10, offset=0):
     """
     (Image, pageSize, offset) -> [Image]
     """
     session = self.session_factory()
     query = session.query(ImageDB).filter(
         or_(ImageDB.url.like('%' + image.url + '%'),
             ImageDB.description == image.description,
             ImageDB.source == image.source))
     content = query.slice(offset, pageSize).all()
     total = query.count()
     images = []
     for imageDB in content:
         images.append(
             Image(
                 imageDB.id,
                 Disease(
                     imageDB.disease.id,
                     Plant(imageDB.disease.plant.id,
                           imageDB.disease.plant.scientificName,
                           imageDB.disease.plant.commonName),
                     imageDB.disease.scientificName,
                     imageDB.disease.commonName), imageDB.url,
                 imageDB.description, imageDB.source, imageDB.size))
     return {'total': total, 'content': images}
Example #7
0
    def searchByID(self, id):
        """
        (int) -> (Analysis)
        Search analysis by ID
        """
        session = self.session_factory()
        analysisDB = session.query(AnalysisDB).get(id)
        if (analysisDB is None):
            raise Exception("Analysis not found!")

        results = []
        for result in analysisDB.analysis_results:
            results.append(
                AnalysisResult(
                    id=result.id,
                    disease=Disease(
                        id=result.disease.id,
                        scientificName=result.disease.scientificName,
                        commonName=result.disease.commonName),
                    frame=result.frame,
                    score=result.score))
        return Analysis(
            id=analysisDB.id,
            image=Image(
                analysisDB.image.id,
                Disease(
                    analysisDB.image.disease.id,
                    Plant(analysisDB.image.disease.plant.id,
                          analysisDB.image.disease.plant.scientificName,
                          analysisDB.image.disease.plant.commonName),
                    analysisDB.image.disease.scientificName,
                    analysisDB.image.disease.commonName), analysisDB.image.url,
                analysisDB.image.description, analysisDB.image.source,
                analysisDB.image.size),
            classifier=Classifier(
                analysisDB.classifier.id,
                Plant(analysisDB.classifier.plant.id,
                      analysisDB.classifier.plant.scientificName,
                      analysisDB.classifier.plant.commonName),
                analysisDB.classifier.tag, analysisDB.classifier.path),
            analysis_results=results,
            user=User(id=analysisDB.user.id,
                      idType=analysisDB.user.idType,
                      email=analysisDB.user.email,
                      username=analysisDB.user.username))
Example #8
0
 def searchByID(self, id):
     """
     (Int) -> (Disease)
     """
     session = self.session_factory()
     diseaseDB = session.query(DiseaseDB).get(id)
     return Disease(
         diseaseDB.id,
         Plant(diseaseDB.plant.id, diseaseDB.plant.scientificName,
               diseaseDB.plant.commonName), diseaseDB.scientificName,
         diseaseDB.commonName)
Example #9
0
 def search(self, disease=Disease(), pageSize=10, offset=0):
     """
     (Disease, pageSize, offset) -> {'total': int, 'content':[Disease]}
     """
     session = self.session_factory()
     query = session.query(DiseaseDB).filter(
         or_(
             DiseaseDB.scientificName.like('%' + disease.scientificName +
                                           '%'),
             DiseaseDB.commonName == disease.commonName))
     content = query.slice(offset, pageSize).all()
     total = query.count()
     diseases = []
     for diseaseDB in content:
         diseases.append(
             Disease(
                 diseaseDB.id,
                 Plant(diseaseDB.plant.id, diseaseDB.plant.scientificName,
                       diseaseDB.plant.commonName),
                 diseaseDB.scientificName, diseaseDB.commonName))
     dic = {'total': total, 'content': diseases}
     return dic
Example #10
0
 def __init__(self,
              id=0,
              disease=Disease(),
              url="",
              description="",
              source="",
              size=0):
     self.id = id
     self.disease = disease
     self.url = url
     self.description = description
     self.source = source
     self.size = size
Example #11
0
 def update(self, disease=Disease()):
     """
     (Disease) -> (Disease)
     """
     session = self.session_factory()
     diseaseDB = session.query(DiseaseDB).filter_by(id=disease.id).first()
     dic = {}
     if (diseaseDB.plant.id != disease.plant.id):
         dic['idPlant'] = disease.plant.id
     if (diseaseDB.scientificName != disease.scientificName):
         dic['scientificName'] = disease.scientificName
     if (diseaseDB.commonName != disease.commonName):
         dic['commonName'] = disease.commonName
     if (dic != {}):
         session.query(DiseaseDB).filter_by(id=disease.id).update(dic)
         session.commit()
         session.flush()
         session.refresh(diseaseDB)
     return Disease(
         diseaseDB.id,
         Plant(diseaseDB.plant.id, diseaseDB.plant.scientificName,
               diseaseDB.plant.commonName), diseaseDB.scientificName,
         diseaseDB.commonName)
Example #12
0
 def delete(self, disease=Disease()):
     """
     (Plant) -> (Boolean)
     """
     status = False
     session = self.session_factory()
     diseaseDB = session.query(DiseaseDB).filter_by(id=disease.id).first()
     session.delete(diseaseDB)
     session.commit()
     session.flush()
     if (not session.query(DiseaseDB).filter_by(id=diseaseDB.id).count()):
         status = True
     session.close()
     return status
Example #13
0
 def searchByID(self, plantId):
     """
     (Int) -> (Plant)
     """
     session = self.session_factory()
     plantDB = session.query(PlantDB).get(plantId)
     diseases = []
     for disease in plantDB.diseases:
         diseases.append(Disease(
             id=disease.id,
             scientificName=disease.scientificName,
             commonName=disease.commonName))
     return Plant(plantDB.id,
                  plantDB.scientificName,
                  plantDB.commonName,
                  diseases)
Example #14
0
 def searchByID(self, id):
     """
     (Int) -> (Image)
     Method used to get image object by ID
     """
     session = self.session_factory()
     imageDB = session.query(ImageDB).get(id)
     return Image(
         imageDB.id,
         Disease(
             imageDB.disease.id,
             Plant(imageDB.disease.plant.id,
                   imageDB.disease.plant.scientificName,
                   imageDB.disease.plant.commonName),
             imageDB.disease.scientificName, imageDB.disease.commonName),
         imageDB.url, imageDB.description, imageDB.source, imageDB.size)
Example #15
0
    def update(self, analysis=Analysis()):
        """
        (Analysis) -> (Analysis)
        update analysis table
        """
        session = self.session_factory()
        analysisDB = session.query(AnalysisDB).filter_by(
            id=analysis.id).first()
        dic = {}
        if (analysisDB.idImage != analysis.image.id):
            dic['idImage'] = analysis.image.id
        if (analysisDB.idClassifier != analysis.classifier.id):
            dic['idClassifier'] = analysis.classifier.id
        if (dic != {}):
            session.query(AnalysisDB).filter_by(id=analysis.id).update(dic)
            session.commit()
            session.flush()
            session.refresh(analysisDB)

        return Analysis(
            analysisDB.id,
            image=Image(
                analysisDB.image.id,
                Disease(
                    analysisDB.image.disease.id,
                    Plant(analysisDB.image.disease.plant.id,
                          analysisDB.image.disease.plant.scientificName,
                          analysisDB.image.disease.plant.commonName),
                    analysisDB.image.disease.scientificName,
                    analysisDB.image.disease.commonName), analysisDB.image.url,
                analysisDB.image.description, analysisDB.image.source,
                analysisDB.image.size),
            classifier=Classifier(
                analysisDB.classifier.id,
                Plant(analysisDB.classifier.plant.id,
                      analysisDB.classifier.plant.scientificName,
                      analysisDB.classifier.plant.commonName),
                analysisDB.classifier.tag, analysisDB.classifier.path),
            user=User(id=analysisDB.user.id,
                      idType=analysisDB.user.idType,
                      email=analysisDB.user.email,
                      username=analysisDB.user.username))
Example #16
0
    def __init__(self,
                 id=0,
                 analysis=Analysis(),
                 disease=Disease(),
                 score=0,
                 frame='0,0,0,0'):
        """AnalisysResult model constructor

        args:
            id: Integer ID number
            analysis: Analysis object that gave this result
            disease: Disease object that was predicted by the Analysis classifier
            score: Score of result certainty
            frame: peace of image that went submited to prediction
        """
        self.id = id
        self.analysis = analysis
        self.disease = disease
        self.score = score
        self.frame = frame
Example #17
0
 def create(self, image=Image()):
     """
     (Image) -> (Image)
     Add image to database
     """
     imageDB = ImageDB(image=image)
     session = self.session_factory()
     session.add(imageDB)
     session.flush()
     session.refresh(imageDB)
     session.commit()
     return Image(
         imageDB.id,
         Disease(
             imageDB.disease.id,
             Plant(imageDB.disease.plant.id,
                   imageDB.disease.plant.scientificName,
                   imageDB.disease.plant.commonName),
             imageDB.disease.scientificName, imageDB.disease.commonName),
         imageDB.url, imageDB.description, imageDB.source, imageDB.size)
Example #18
0
    def search(self, analysis=Analysis(), pageSize=10, offset=0):
        """
        (Analysis, pageSize, offset) -> [Analysis]
        search by analysis
        """
        session = self.session_factory()
        query = session.query(AnalysisDB).filter(
            and_(AnalysisDB.idImage == analysis.image.id,
                 AnalysisDB.idClassifier == analysis.classifier.id))
        content = query.slice(offset, pageSize).all()
        total = query.count()
        analyses = []
        for analysisDB in content:
            analyses.append(
                Analysis(
                    analysisDB.id,
                    image=Image(
                        analysisDB.image.id,
                        Disease(
                            analysisDB.image.disease.id,
                            Plant(
                                analysisDB.image.disease.plant.id,
                                analysisDB.image.disease.plant.scientificName,
                                analysisDB.image.disease.plant.commonName),
                            analysisDB.image.disease.scientificName,
                            analysisDB.image.disease.commonName),
                        analysisDB.image.url, analysisDB.image.description,
                        analysisDB.image.source, analysisDB.image.size),
                    classifier=Classifier(
                        analysisDB.classifier.id,
                        Plant(analysisDB.classifier.plant.id,
                              analysisDB.classifier.plant.scientificName,
                              analysisDB.classifier.plant.commonName),
                        analysisDB.classifier.tag, analysisDB.classifier.path),
                    user=User(id=analysisDB.user.id,
                              idType=analysisDB.user.idType,
                              email=analysisDB.user.email,
                              username=analysisDB.user.username)))

        return {'total': total, 'content': analyses}
Example #19
0
def organize(database, workdir, output, size):
    """ (str, str, str, str) -> Bool

        Method used to execute organization of images
    """
    filehandler = open(output, "a")
    logging.info("CONNECTING DATABASE {}".format(database))
    conn = sqlite3.connect(database)
    cursor = conn.cursor()
    plants = []

    scriptPlants = ""
    scriptDiseases = ""
    scriptImages = ""

    for row in cursor.execute(
            "SELECT id, crop_common_name, crop_scientific_name, disease_common_name, disease_scientific_name, url, description, metadata FROM ANNOTATIONS;"
    ):
        oldAnnotation = OldAnnotation(row[0], row[1], row[2], row[3], row[4],
                                      row[5], row[6], row[7])

        if (oldAnnotation.url == "" or oldAnnotation.cropScientificName == ""):
            continue

        indexPlant = searchPlantByScientificName(plants,
                                                 oldAnnotation.cropCommonName)
        plant = Plant()
        if (indexPlant == -1):
            plant = Plant(scientificName=oldAnnotation.cropScientificName,
                          commonName=oldAnnotation.cropCommonName,
                          diseases=[])
            logging.info("CREATING {}".format(plant.commonName).replace(
                " ", "_").replace(";", "").replace("(", "_").replace(
                    ")", "_").replace("<i>", "").replace("</i>", ""))
            os.system("mkdir -p " + workdir + "/" + plant.commonName.replace(
                " ", "_").replace(";", "").replace("(", "_").replace(
                    ")", "_").replace("<i>", "").replace("</i>", ""))
            #filehandler.write("INSERT INTO PLANTS(scientific_name, common_name) VALUES ('{}', '{}')\n".format(plant.scientificName, plant.commonName))
            scriptPlants += "INSERT INTO PLANTS(scientific_name, common_name) VALUES ('{}', '{}');\n".format(
                plant.scientificName, plant.commonName)
        else:
            logging.info("index: {} - plant: {}".format(
                str(indexPlant), plants[indexPlant].scientificName))
            plant = plants[indexPlant]

        disease = Disease(plant=plant,
                          commonName=oldAnnotation.diseaseCommonName,
                          scientificName=oldAnnotation.diseaseScientificName)

        if (oldAnnotation.diseaseCommonName == ""
                or oldAnnotation.diseaseScientificName == ""
                or "Healthy" in oldAnnotation.description
                or "healthy" in oldAnnotation.description):
            disease.scientificName = "healthy"
            disease.commonName = "healthy"

        indexDisease = searchDiseaseByScientificName(disease.plant,
                                                     disease.scientificName)
        logging.info("DISEASE: {} - {} - {}".format(disease.scientificName,
                                                    plant.scientificName,
                                                    indexDisease))
        if (indexDisease == -1):
            logging.info("CREATING {}/{}".format(
                plant.commonName.replace(" ", "_").replace(";", "").replace(
                    "(", "_").replace(")",
                                      "_").replace("<i>",
                                                   "").replace("</i>", ""),
                disease.scientificName.replace(" ", "_").replace(
                    ";", "").replace("(", "_").replace(")", "_").replace(
                        "<i>", "").replace("</i>", "")))
            os.system("mkdir -p " + workdir + "/" + plant.commonName.replace(
                " ", "_").replace(";", "").replace("(", "_").replace(
                    ")", "_").replace("<i>", "").replace("</i>", "") + "/" +
                      disease.scientificName.replace(" ", "_").replace(
                          ";", "").replace("(", "_").replace(")", "_").replace(
                              "<i>", "").replace("</i>", ""))
            scriptDiseases += "INSERT INTO DISEASES(id_plant, scientific_name, common_name) VALUES ((SELECT id FROM PLANTS WHERE scientific_name = '{}' LIMIT 1),'{}', '{}');\n".format(
                plant.scientificName, disease.scientificName,
                disease.commonName)
            #filehandler.write("INSERT INTO DISEASES(id, scientific_name, common_name) VALUES ((SELECT id FROM PLANTS WHERE scientific_name = '{}' LIMIT 1),'{}', '{}')\n".format(disease.plant.scientificName, disease.scientificName, disease.commonName))
        else:
            disease = plant.diseases[indexDisease]

        image = Image(disease=disease,
                      url=oldAnnotation.url.replace("<i>",
                                                    "").replace("</i>", ""),
                      description=oldAnnotation.description,
                      source=oldAnnotation.metadata)

        regex = re.compile("[\w]+/[\w,;\-]*\/([\w\.;\-,]+)+")
        logging.info(image.url)
        if (not regex.match(image.url) == None):
            logging.info(image.url)
            image.url = regex.match(image.url).group(1)
            if ("large" in image.url):
                continue
        else:
            logging.info(image.url)
            continue

        if ("large" in disease.scientificName):
            continue
        logging.info("CREATING {}/{}/{} ".format(
            plant.commonName.replace(" ", "_"),
            disease.scientificName.replace(" ", "_"),
            image.url.replace(" ", "_")))
        os.makedirs(workdir + "/" + plant.commonName.replace(" ", "_").replace(
            ";", "").replace("(", "_").replace(")", "_").replace(
                "<i>", "").replace("</i>", "") +
                    "/" + disease.scientificName.replace(" ", "_").replace(
                        ";", "").replace("(", "_").replace(")", "_").replace(
                            "<i>", "").replace("</i>", "") + "/",
                    exist_ok=True)
        dir1 = workdir + "/" + plant.commonName.replace(" ", "_").replace(
            "(", "_").replace(")", "_").replace("<i>", "").replace(
                "</i>", "") + "/" + image.url
        dir2 = workdir + "/" + plant.commonName.replace(" ", "_").replace(
            ";", "").replace(
                "(", "_").replace(")", "_").replace("<i>", "").replace(
                    "</i>", "") + "/" + disease.scientificName.replace(
                        " ", "_").replace(";", "").replace("(", "_").replace(
                            ")", "_").replace("<i>", "").replace(
                                "</i>", "") + "/" + image.url
        logging.info(dir1)
        logging.info(dir2)

        if ("large" in dir2):
            continue

        try:
            shutil.copyfile(dir1, dir2)
        except FileNotFoundError:
            continue

        #filehandler.write("INSERT INTO IMAGES(id_disease, url, description, source) VALUES ((SELECT id FROM DISEASES WHERE scientific_name = '{}' AND id_plant = (SELECT id FROM PLANTS WHERE scientific_name = '{}' LIMIT 1) LIMIT 1), '{}', '{}', '{}')\n".format(image.disease.scientificName, image.disease.plant.scientificName, image.url, image.description, image.source))
        scriptImages += "INSERT INTO IMAGES(id_disease, url, description, source, size) VALUES ((SELECT id FROM DISEASES WHERE scientific_name = '{}' AND id_plant = (SELECT id FROM PLANTS WHERE scientific_name = '{}' LIMIT 1) LIMIT 1), '{}', '{}', '{}', (SELECT id FROM TYPES WHERE value='{}' AND description='image-size' LIMIT 1));\n".format(
            disease.scientificName, plant.scientificName, image.url,
            image.description, image.source, size)

        disease.images.append(image)

        if (indexDisease == -1):
            plant.diseases.append(disease)
        else:
            plant.diseases[indexDisease] = disease

        if (indexPlant == -1):
            plants.append(plant)
        else:
            plants[indexPlant] = plant

    filehandler.write(scriptPlants)
    filehandler.write(scriptDiseases)
    filehandler.write(scriptImages)

    filehandler.close()
    return True
Example #20
0
    def update(self, analysisResult=AnalysisResult()):
        """
        (AnalysisResult) -> (AnalysisResult)
        update analysis_result table
        """
        session = self.session_factory()
        analysisResultDB = session.query(AnalysisResultDB).filter_by(
            id=analysisResult.id).first()
        dic = {}
        if (analysisResultDB.idAnalysis != analysisResult.analysis.id):
            dic['idAnalysis'] = analysisResult.analysis.id
        if (analysisResultDB.idDisease != analysisResult.disease.id):
            dic['idDisease'] = analysisResult.disease.id
        if (analysisResultDB.score != analysisResult.score):
            dic['score'] = analysisResult.score
        if (analysisResultDB.frame != analysisResult.frame):
            dic['frame'] = analysisResult.frame
        if (dic != {}):
            session.query(AnalysisResultDB).filter_by(
                id=analysisResult.id).update(dic)
            session.commit()
            session.flush()
            session.refresh(analysisResultDB)

        return AnalysisResult(
            analysisResultDB.id,
            Analysis(
                analysisResultDB.analysis.id,
                Image(
                    analysisResultDB.analysis.image.id,
                    Disease(
                        analysisResultDB.analysis.image.disease.id,
                        Plant(
                            analysisResultDB.analysis.image.disease.plant.id,
                            analysisResultDB.analysis.image.disease.plant.
                            scientificName, analysisResultDB.analysis.image.
                            disease.plant.commonName),
                        analysisResultDB.analysis.image.disease.scientificName,
                        analysisResultDB.analysis.image.disease.commonName),
                    analysisResultDB.analysis.image.url,
                    analysisResultDB.analysis.image.description,
                    analysisResultDB.analysis.image.source,
                    analysisResultDB.analysis.image.size),
                Classifier(
                    analysisResultDB.analysis.classifier.id,
                    Plant(
                        analysisResultDB.analysis.classifier.plant.id,
                        analysisResultDB.analysis.classifier.plant.
                        scientificName,
                        analysisResultDB.analysis.classifier.plant.commonName),
                    analysisResultDB.analysis.classifier.tag,
                    analysisResultDB.analysis.classifier.path),
                user=User(id=analysisResultDB.analysis.user.id,
                          idType=analysisResultDB.analysis.user.idType,
                          email=analysisResultDB.analysis.user.email,
                          username=analysisResultDB.analysis.user.username)),
            Disease(
                analysisResultDB.disease.id,
                Plant(analysisResultDB.disease.plant.id,
                      analysisResultDB.disease.plant.scientificName,
                      analysisResultDB.disease.plant.commonName),
                analysisResultDB.disease.scientificName,
                analysisResultDB.disease.commonName), analysisResultDB.score,
            analysisResultDB.frame)
Example #21
0
    def search(self, analysisResult=AnalysisResult(), pageSize=10, offset=0):
        """
        (AnalysisResult, pageSize, offset) -> [AnalysisResult]
        search by analysisResult
        """
        session = self.session_factory()
        query = session.query(AnalysisResultDB).filter(
            or_(AnalysisResultDB.idAnalysis == analysisResult.analysis.id,
                AnalysisResultDB.idDisease == analysisResult.disease.id,
                AnalysisResultDB.score == analysisResult.score,
                AnalysisResultDB.frame == analysisResult.frame))
        content = query.slice(offset, pageSize).all()
        total = query.count()
        analysisResults = []
        for analysisResultDB in content:
            analysisResults.append(
                AnalysisResult(
                    analysisResultDB.id,
                    Analysis(
                        analysisResultDB.analysis.id,
                        Image(
                            analysisResultDB.analysis.image.id,
                            Disease(
                                analysisResultDB.analysis.image.disease.id,
                                Plant(
                                    analysisResultDB.analysis.image.disease.
                                    plant.id, analysisResultDB.analysis.image.
                                    disease.plant.scientificName,
                                    analysisResultDB.analysis.image.disease.
                                    plant.commonName), analysisResultDB.
                                analysis.image.disease.scientificName,
                                analysisResultDB.analysis.image.disease.
                                commonName),
                            analysisResultDB.analysis.image.url,
                            analysisResultDB.analysis.image.description,
                            analysisResultDB.analysis.image.source,
                            analysisResultDB.analysis.image.size),
                        Classifier(
                            analysisResultDB.analysis.classifier.id,
                            Plant(
                                analysisResultDB.analysis.classifier.plant.id,
                                analysisResultDB.analysis.classifier.plant.
                                scientificName, analysisResultDB.analysis.
                                classifier.plant.commonName),
                            analysisResultDB.analysis.classifier.tag,
                            analysisResultDB.analysis.classifier.path),
                        user=User(
                            id=analysisResultDB.analysis.user.id,
                            idType=analysisResultDB.analysis.user.idType,
                            email=analysisResultDB.analysis.user.email,
                            username=analysisResultDB.analysis.user.username)),
                    Disease(
                        analysisResultDB.disease.id,
                        Plant(analysisResultDB.disease.plant.id,
                              analysisResultDB.disease.plant.scientificName,
                              analysisResultDB.disease.plant.commonName),
                        analysisResultDB.disease.scientificName,
                        analysisResultDB.disease.commonName),
                    analysisResultDB.score, analysisResultDB.frame))

        return {'total': total, 'content': analysisResults}