Exemplo n.º 1
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))
Exemplo n.º 2
0
 def update(self, user=User()):
     """
     (User) -> (User)
     """
     session = self.session_factory()
     userDB = session.query(UserDB).filter_by(id=user.id).first()
     dic = {}
     if (userDB.idType != user.idType):
         dic['idType'] = user.idType
     if (userDB.email != user.email):
         dic['email'] = user.email
     if (userDB.username != user.username):
         dic['username'] = user.username
     if (userDB.password != user.password):
         dic['password'] = user.password
     if (userDB.salt != user.salt):
         dic['salt'] = user.salt
     if (userDB.dateInsertion != user.dateInsertion):
         dic['dateInsertion'] = user.dateInsertion
     if (userDB.dateUpdate != user.dateUpdate):
         dic['dateUpdate'] = user.dateUpdate
     if (dic != {}):
         session.query(UserDB).filter_by(id=user.id).update(dic)
         session.commit()
         session.flush()
         session.refresh(userDB)
     return User(userDB.id, userDB.idType, userDB.email, userDB.username,
                 userDB.password, userDB.salt, userDB.dateInsertion,
                 userDB.dateUpdate,
                 [Analysis(id=x.id) for x in userDB.analysis])
Exemplo n.º 3
0
 def searchByID(self, id):
     """
     (Int) -> (User)
     """
     session = self.session_factory()
     userDB = session.query(UserDB).get(id)
     return User(userDB.id, userDB.idType, userDB.email, userDB.username,
                 userDB.password, userDB.salt, userDB.dateInsertion,
                 userDB.dateUpdate,
                 [Analysis(id=x.id) for x in userDB.analysis])
Exemplo n.º 4
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))
Exemplo n.º 5
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}
Exemplo n.º 6
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)
Exemplo n.º 7
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)
Exemplo n.º 8
0
 def create(self, user=User()):
     """
     (User) -> (User)
     """
     userDB = UserDB(user=user)
     session = self.session_factory()
     session.add(userDB)
     session.flush()
     session.refresh(userDB)
     session.commit()
     return User(userDB.id, userDB.idType, userDB.email, userDB.username,
                 userDB.password, userDB.salt, userDB.dateInsertion,
                 userDB.dateUpdate,
                 [Analysis(id=x.id) for x in userDB.analysis])
Exemplo n.º 9
0
 def delete(self, analysis=Analysis()):
     """
     (Analysis) -> (bool)
     Delete analysis from database
     """
     status = False
     session = self.session_factory()
     analysisDB = session.query(AnalysisDB).filter_by(
         id=analysis.id).first()
     session.delete(analysisDB)
     session.commit()
     session.flush()
     if (not session.query(AnalysisDB).filter_by(id=analysisDB.id).count()):
         status = True
     session.close()
     return status
Exemplo n.º 10
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))
Exemplo n.º 11
0
    def authentication(self, user=User()):
        """
        (User) -> (User)
        """
        session = self.session_factory()
        userDB = session.query(UserDB).filter_by(
            username=user.username).first()
        crypto = Crypto()
        decriptedTest = crypto.decrypt(userDB.salt, userDB.password)
        # decriptedUser = crypto.decrypt(user.salt, user.password)
        print('decripted: %s' % decriptedTest)
        print('test: %s' % user.password)

        if (decriptedTest == user.password):
            return User(userDB.id, userDB.idType, userDB.email,
                        userDB.username, userDB.password, userDB.salt,
                        userDB.dateInsertion, userDB.dateUpdate,
                        [Analysis(id=x.id) for x in userDB.analysis])
        else:
            return User()
Exemplo n.º 12
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
Exemplo n.º 13
0
 def search(self, user=User(), pageSize=10, offset=0):
     """
     (User, pageSize, offset) -> {'total': int, 'content':[User]}
     """
     session = self.session_factory()
     query = session.query(UserDB).filter(
         and_(UserDB.email.like('%' + user.email + '%'),
              UserDB.username.like('%' + user.username + '%'),
              UserDB.dateInsertion.like('%' + user.dateInsertion + '%'),
              UserDB.dateUpdate.like('%' + user.dateUpdate + '%')))
     content = query.slice(offset, pageSize).all()
     users = []
     for userDB in content:
         users.append(
             User(userDB.id, userDB.idType, userDB.email, userDB.username,
                  userDB.password, userDB.salt, userDB.dateInsertion,
                  userDB.dateUpdate,
                  [Analysis(id=x.id) for x in userDB.analysis]))
     total = query.count()
     dic = {'total': total, 'content': users}
     return dic
Exemplo n.º 14
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)
Exemplo n.º 15
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}