Example #1
0
    def checkDummy(self,raidpic, x1, x2, y1, y2, hash, raidNo, radius):
        foundgym = None
        log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'checkDummy: Check for dummy Gym Image')
        
        template = ("mon_img/dummy_nearby.jpg")

        find_gym = mt.fort_image_matching(raidpic, template, True, 0.9, raidNo, hash, True, radius, x1, x2, y1, y2)
        
        if find_gym >= 0.9:
            return True
        return False
Example #2
0
    def detectRaidBoss(self, raidpic, lvl, hash, raidcount):
        foundmon = None
        monID = None
        log.debug('Extracting Raidboss')
        lower = np.array([80, 60, 30], dtype="uint8")
        upper = np.array([110, 90, 70], dtype="uint8")
        kernel = np.ones((3, 3), np.uint8)
        kernel2 = np.ones((6, 6), np.uint8)
        raidMonZoom = cv2.resize(raidpic, (0, 0), fx=2, fy=2)
        mask = cv2.inRange(raidMonZoom, lower, upper)
        output = cv2.bitwise_and(raidMonZoom, raidMonZoom, mask=mask)
        monAsset = cv2.inRange(output, np.array([0, 0, 0]),
                               np.array([15, 15, 15]))
        monAsset = cv2.morphologyEx(monAsset, cv2.MORPH_CLOSE, kernel)
        monAsset = cv2.morphologyEx(monAsset, cv2.MORPH_OPEN, kernel2)

        picName = self.tempPath + "/" + str(hash) + "_raidboss" + str(
            raidcount) + ".jpg"
        cv2.imwrite(picName, monAsset)

        log.debug('detectRaidBoss: Scanning Raidboss')
        monHash = self.imageHashExists(
            self.tempPath + "/" + str(hash) + "_raidboss" + str(raidcount) +
            ".jpg", False, 'mon-' + str(lvl))
        log.debug('detectRaidBoss: Monhash: ' + str(monHash))

        if monHash is None:
            for file in glob.glob("mon_img/_mon_*_" + str(lvl) + ".png"):
                find_mon = mt.fort_image_matching(file, picName, False, 0.75)
                if foundmon is None or find_mon > foundmon[0]:
                    foundmon = find_mon, file

                if foundmon and foundmon[0] > 0.75:
                    monSplit = foundmon[1].split('_')
                    monID = monSplit[3]

            #we found the mon that's most likely to be the one that's in the crop
            log.debug('detectRaidBoss: Found mon in mon_img: ' + str(monID))

        else:
            os.remove(picName)
            return monHash, monAsset

        if monID:
            self.imageHash(picName, monID, False, 'mon-' + str(lvl))
            os.remove(picName)
            return monID, monAsset

        log.debug('No Mon found!')

        os.remove(picName)
        return False, monAsset
Example #3
0
    def detectGym(self, raidpic, hash, raidcount, monId=None):
        foundgym = None
        gymId = None
        x1 = 90
        x2 = 125
        y1 = 135
        y2 = 200

        #if gymHash is none, we haven't seen the gym yet, otherwise, gymHash == gymId we are looking for
        if monId:
            log.debug('Got Mon-ID for Gym-Detection %s' % monId)
            with open('monsspec.json') as f:
                data = json.load(f)

            if str(monId) in data:
                crop = data[str(monId)]["Crop"]
                log.debug('Found other Crops for Mon %s' % monId)
                log.debug(str(crop))
                x1 = crop['X1']
                x2 = crop['X2']
                y1 = crop['Y1']
                y2 = crop['Y2']

        gymHash = self.imageHashExists(raidpic, True, 'gym', x1, x2, y1, y2)

        if gymHash is None:
            for file in glob.glob("gym_img/*.jpg"):
                find_gym = mt.fort_image_matching(raidpic, file, True, 0.7, x1,
                                                  x2, y1, y2)
                if foundgym is None or find_gym > foundgym[0]:
                    foundgym = find_gym, file

                if foundgym and foundgym[0] >= 0.7:
                    #okay, we very likely found our gym
                    gymSplit = foundgym[1].split('_')
                    gymId = gymSplit[2]
                    #if we are looking by coords (TODO), we will likely get additional checks somewhere around here and before the for-loop

        else:
            return gymHash

        if gymId:
            self.imageHash(raidpic, gymId, True, 'gym', x1, x2, y1, y2)
            return gymId
        else:
            #we could not find the gym...
            return None
Example #4
0
    def detectLevel(self, raidpic, hash, raidNo):
        foundlvl = None
        lvl = None
        lvlTypes = [
            'mon_img/_raidlevel_5_.jpg', 'mon_img/_raidlevel_4_.jpg',
            'mon_img/_raidlevel_3_.jpg', 'mon_img/_raidlevel_2_.jpg',
            'mon_img/_raidlevel_1_.jpg'
        ]

        raidlevel = raidpic[230:260, 0:170]
        #raidlevel = cv2.resize(raidlevel, (0,0), fx=2, fy=2)

        cv2.imwrite(
            os.path.join(self.tempPath,
                         str(hash) + '_raidlevel' + str(raidNo) + '.jpg'),
            raidlevel)

        log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                  ') ] ' + 'Scanning Level')
        for file in lvlTypes:
            find_lvl = mt.fort_image_matching(
                file,
                os.path.join(self.tempPath,
                             str(hash) + '_raidlevel' + str(raidNo) + '.jpg'),
                False, 0.7, raidNo, hash)

            if foundlvl is None or find_lvl > foundlvl[0]:
                foundlvl = find_lvl, file

            if not foundlvl is None:
                lvlSplit = foundlvl[1].split('_')
                lvl = lvlSplit[3]

        os.remove(
            os.path.join(self.tempPath,
                         str(hash) + '_raidlevel' + str(raidNo) + '.jpg'))

        if lvl:
            log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                      ') ] ' + 'detectLevel: found level %s' % str(lvl))
            return lvl
        else:
            log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                     ') ] ' + 'detectLevel: could not find level')
            return None
Example #5
0
    def detectEgg(self, raidpic, hash, raidcount):
        foundegg = None
        eggID = None
        for file in glob.glob("mon_img/_egg_*.png"):
            find_egg = mt.fort_image_matching(file, raidpic, True, 0.9)
            if foundegg is None or find_egg > foundegg[0]:
                foundegg = find_egg, file

            if not foundegg is None and foundegg[0] > 0.9:
                eggSplit = foundegg[1].split('_')
                eggID = eggSplit[3]

        log.debug('Eggfound: ' + str(eggID))

        if eggID:
            return eggID

        return False
Example #6
0
    def detectLevel(self, raidpic, hash, raidcount):
        foundlvl = None
        lvl = None
        lvlTypes = [
            'mon_img/_raidlevel_5_.jpg', 'mon_img/_raidlevel_4_.jpg',
            'mon_img/_raidlevel_3_.jpg', 'mon_img/_raidlevel_2_.jpg',
            'mon_img/_raidlevel_1_.jpg'
        ]

        raidlevel = raidpic[230:260, 0:170]
        raidlevel = cv2.resize(raidlevel, (0, 0), fx=2, fy=2)

        cv2.imwrite(
            self.tempPath + "/" + str(hash) + "_raidlevel" + str(raidcount) +
            ".jpg", raidlevel)

        log.debug('Scanning Level')
        for file in lvlTypes:
            find_lvl = mt.fort_image_matching(
                file, self.tempPath + "/" + str(hash) + "_raidlevel" +
                str(raidcount) + ".jpg", False, 0.8)
            if foundlvl is None or find_lvl > foundlvl[0]:
                foundlvl = find_lvl, file

            if not foundlvl is None and foundlvl[0] > 0.8:
                lvlSplit = foundlvl[1].split('_')
                lvl = lvlSplit[3]

        os.remove(self.tempPath + "/" + str(hash) + "_raidlevel" +
                  str(raidcount) + ".jpg")

        if lvl:
            log.debug("detectLevel: found level '%s'" % str(lvl))
            return lvl
        else:
            log.info("detectLevel: could not find level")
            return None
Example #7
0
    def start_detect(self, filename, hash):
        log.error("Starting detection")
        if not os.path.isfile(filename):
            log.error("File does not exist")
            log.error(filename)
            return

        log.error("Starting analisys")

        img = cv2.imread(filename)
        img = cv2.resize(img, (750, 1334), interpolation=cv2.INTER_CUBIC)
        ###raid1
        raid1 = img[Y1 - 70:Y1 + 200, X1 - 80:X1 + 80]
        cv2.imwrite(self.tempPath + "/" + str(hash) + "_raid1.jpg", raid1)

        ###raid2
        raid2 = img[Y1 - 70:Y1 + 200, X2 - 80:X2 + 80]
        cv2.imwrite(self.tempPath + "/" + str(hash) + "_raid2.jpg", raid2)

        ###raid3
        raid3 = img[Y1 - 70:Y1 + 200, X3 - 80:X3 + 80]
        cv2.imwrite(self.tempPath + "/" + str(hash) + "_raid3.jpg", raid3)

        ###raid4
        raid4 = img[Y2 - 70:Y2 + 200, X1 - 80:X1 + 80]
        cv2.imwrite(self.tempPath + "/" + str(hash) + "_raid4.jpg", raid4)

        ###raid5
        raid5 = img[Y2 - 70:Y2 + 200, X2 - 80:X2 + 80]
        cv2.imwrite(self.tempPath + "/" + str(hash) + "_raid5.jpg", raid5)

        ###raid3
        raid6 = img[Y2 - 70:Y2 + 200, X3 - 80:X3 + 80]
        cv2.imwrite(self.tempPath + "/" + str(hash) + "_raid6.jpg", raid6)

        i = 1
        foundgym = None
        foundmon = None
        foundegg = None
        foundlvl = None

        while i < 7:
            gymfound = 0
            monfound = 0
            eggfound = 0
            lvlfound = 0
            log.debug('Generating Pic of Raidboss')
            image1 = cv2.imread(self.tempPath + "/" + str(hash) + "_raid" +
                                str(i) + ".jpg")
            raidpic = image1[0:165, 0:160]
            cv2.imwrite(
                self.tempPath + "/" + str(hash) + "_raidpic" + str(i) + ".jpg",
                raidpic)

            log.debug('Generating Pic of Timer')
            image2 = cv2.imread(self.tempPath + "/" + str(hash) + "_raid" +
                                str(i) + ".jpg")
            raidtimer = image2[200:230, 0:297]
            raidtimer = cv2.resize(raidtimer, (0, 0), fx=3, fy=3)
            cv2.imwrite(
                self.tempPath + "/" + str(hash) + "_raidtimer" + str(i) +
                ".jpg", raidtimer)

            log.debug('Reading Raidtext')
            emptyraid = image2[195:225, 0:160]
            cv2.imwrite(self.tempPath + "/" + str(hash) + "_emptyraid.png",
                        raidtimer)
            rt = Image.open(self.tempPath + "/" + str(hash) + "_emptyraid.png")
            gray = rt.convert('L')
            bw = gray.point(lambda x: 0 if x < 210 else 255, '1')
            bw.save(self.tempPath + "/" + str(hash) +
                    "_cropped_emptyraid_bw.png")
            raidtext = pytesseract.image_to_string(
                Image.open(self.tempPath + "/" + str(hash) +
                           "_cropped_emptyraid_bw.png"),
                config='-psm 7')
            log.debug(raidtext)

            if len(raidtext) > 0:

                log.debug('Generating Pic of Timer')
                raidlevel = image2[235:265, 0:297]
                raidlevel = cv2.resize(raidlevel, (0, 0), fx=3, fy=3)
                cv2.imwrite(
                    self.tempPath + "/" + str(hash) + "_raidlevel" + str(i) +
                    ".jpg", raidlevel)

                log.debug('Extracting Raidboss')
                lower = np.array([80, 60, 30], dtype="uint8")
                upper = np.array([110, 90, 70], dtype="uint8")
                kernel = np.ones((3, 3), np.uint8)
                kernel2 = np.ones((6, 6), np.uint8)
                raidMonZoom = cv2.resize(image1, (0, 0), fx=2, fy=2)
                mask = cv2.inRange(raidMonZoom, lower, upper)
                output = cv2.bitwise_and(raidMonZoom, raidMonZoom, mask=mask)
                cv2.imwrite(
                    self.tempPath + "/" + str(hash) + "_raidboss" + str(i) +
                    ".jpg", output)
                monAsset = cv2.imread(
                    self.tempPath + "/" + str(hash) + "_raidboss" + str(i) +
                    ".jpg", 3)
                monAsset = cv2.inRange(monAsset, np.array([0, 0, 0]),
                                       np.array([15, 15, 15]))
                monAsset = cv2.morphologyEx(monAsset, cv2.MORPH_CLOSE, kernel)
                monAsset = cv2.morphologyEx(monAsset, cv2.MORPH_OPEN, kernel2)
                cv2.imwrite(
                    self.tempPath + "/" + str(hash) + "_raidboss" + str(i) +
                    ".jpg", monAsset)

                log.debug('Reading Timer')
                raidtime = Image.open(self.tempPath + "/" + str(hash) +
                                      "_raidtimer" + str(i) + ".jpg")
                gray = raidtime.convert('L')
                bw = gray.point(lambda x: 0 if x < 185 else 255, '1')
                bw.save(self.tempPath + "/" + str(hash) + "_raidtimer" +
                        str(i) + ".jpg")
                timer = pytesseract.image_to_string(
                    Image.open(self.tempPath + "/" + str(hash) + "_raidtimer" +
                               str(i) + ".jpg"),
                    config='--psm=7').replace(' ', '').replace(
                        '~', '').replace('o',
                                         '0').replace('O',
                                                      '0').replace('-', '')
                log.debug(timer)

                log.debug('Scanning Level')
                for file in glob.glob("mon_img/_raidlevel_*.jpg"):
                    find_lvl = mt.fort_image_matching(
                        file, self.tempPath + "/" + str(hash) + "_raidlevel" +
                        str(i) + ".jpg", False, 0.5)
                    if foundlvl is None or find_lvl > foundlvl[0]:
                        foundlvl = find_lvl, file

                if not foundlvl is None and foundlvl[0] > 0.5 and len(
                        raidtext) > 0:
                    lvlfound = 1

                lvlSplit = foundlvl[1].split('_')
                lvl = lvlSplit[3]

                log.debug('Level: ' + str(lvl))
                log.debug('Scanning Egg')

                for file in glob.glob("mon_img/_egg_*.png"):
                    find_egg = mt.fort_image_matching(
                        file, self.tempPath + "/" + str(hash) + "_raid" +
                        str(i) + ".jpg", True, 0.9)
                    if foundegg is None or find_egg > foundegg[0]:
                        foundegg = find_egg, file

                if not foundegg is None and foundegg[0] > 0.9 and len(
                        raidtext) > 0:
                    eggfound = 1
                log.debug('Eggfound: ' + str(eggfound))
                log.debug('Scanning Time')
                if "R" not in timer:
                    now = datetime.datetime.now()
                    date1 = str(now.year) + "-0" + str(now.month) + "-" + str(
                        now.day)

                    raidstart = getHatchTime(self,
                                             timer) - (self.timezone * 60 * 60)
                    raidend = getHatchTime(self, timer) + int(
                        45 * 60) - (self.timezone * 60 * 60)

                    log.debug('Start: ' + str(raidstart) + ' End: ' +
                              str(raidend))
                else:
                    raidstart = "-"

                log.debug('Scanning Gym')

                gymHash = self.imageHashExists(
                    self.tempPath + "/" + str(hash) + "_raid" + str(i) +
                    ".jpg", True, 'gym')
                log.debug('Gymhash: ' + str(gymHash))
                if not gymHash:
                    for file in glob.glob("gym_img/*.jpg"):
                        find_gym = mt.fort_image_matching(
                            self.tempPath + "/" + str(hash) + "_raid" +
                            str(i) + ".jpg", file, True, 0.6)
                        if foundgym is None or find_gym > foundgym[0]:
                            foundgym = find_gym, file

                        if not foundgym is None and foundgym[0] > 0.6 and len(
                                raidtext) > 0:
                            gymfound = 1
                            gymSplit = foundgym[1].split('_')
                            gymID = gymSplit[2]

                    if gymfound == 1:
                        self.imageHash(
                            'temp/' + str(hash) + '_raid' + str(i) + '.jpg',
                            gymID, True, 'gym')

                else:
                    gymfound = 1
                    gymID = gymHash

                if eggfound == 0:
                    log.error('Scanning Mon')
                    monHash = self.imageHashExists(
                        self.tempPath + "/" + str(hash) + "_raidboss" +
                        str(i) + ".jpg", False, 'mon-' + str(lvl))
                    log.debug('Monhash: ' + str(monHash))
                    if not monHash:
                        for file in glob.glob("mon_img/_mon_*_" + str(lvl) +
                                              ".png"):
                            find_mon = mt.fort_image_matching(
                                file, self.tempPath + "/" + str(hash) +
                                "_raidboss" + str(i) + ".jpg", False, 0.7)
                            if foundmon is None or find_mon > foundmon[0]:
                                log.error("Found mon")
                                foundmon = find_mon, file

                            if not foundmon is None and foundmon[
                                    0] > 0.7 and len(raidtext) > 0:
                                log.error("Found mon")
                                monfound = 1
                                monSplit = foundmon[1].split('_')
                                monID = monSplit[3]

                        if monfound == 1:
                            self.imageHash(
                                'temp/' + str(hash) + '_raidboss' + str(i) +
                                '.jpg', monID, False, 'mon-' + str(lvl))
                    else:
                        monfound = 1
                        monID = monHash

                if gymfound == 1 and (monfound == 1 or eggfound == 1):

                    if monfound == 1:
                        logtext = 'Mon - ID: ' + str(monID)
                        log.error("Found mon %s Lv %s. GymID: %s" %
                                  (monID, lvl, gymID))
                        self.submitRaid(str(gymID), monID, lvl, '-', '-',
                                        'MON')

                    if eggfound == 1:
                        eggSplit = foundegg[1].split('_')
                        eggID = eggSplit[3]
                        logtext = 'Egg - ID: ' + str(eggID)
                        log.debug(
                            "Found egg Lv %s starting at %s and ending at %s. GymID: %s"
                            % (lvl, raidstart, raidend, gymID))
                        self.submitRaid(str(gymID), '0', lvl, raidstart,
                                        raidend, 'EGG')

                    log.info('Raid ' + str(i) + ' | Gym-ID: ' + str(gymID) +
                             ' | ' + logtext + ' | Level: ' + lvl)

                if gymfound == 1 and (monfound == 0 and eggfound == 0):
                    logtext = ' Mon or Egg: unknown '
                    log.info('Raid ' + str(i) + ' | Gym-ID: ' + str(gymID) +
                             ' | ' + logtext + ' | Level: ' + lvl)

                if gymfound == 0 and (monfound == 1 or eggfound == 1):
                    gymID = 'unknow'
                    lvlSplit = foundlvl[1].split('_')
                    lvl = lvlSplit[3]

                    if monfound == 1:
                        logtext = 'Mon - ID: ' + str(monID)

                    if eggfound == 1:
                        eggSplit = foundegg[1].split('_')
                        eggID = eggSplit[3]
                        logtext = 'Egg - ID: ' + str(eggID)

                    log.info('Raid ' + str(i) + ' | Gym-ID: ' + str(gymID) +
                             ' | ' + logtext + ' | Level: ' + lvl)

                if gymfound == 0 and (monfound == 0 and eggfound == 0):

                    gymID = 'uknown'
                    logtext = ' unknown Mon or Egg '

                    log.info('Raid ' + str(i) + ' | Gym-ID: ' + str(gymID) +
                             ' | ' + logtext + ' | Level: ' + lvl)

                foundmon = None
                foundgym = None
                foundegg = None
                foundlvl = None
            else:
                if i == 1:
                    log.error('No active Raids')
                    break
                else:
                    log.error('No more active Raids')
                    break

            if gymfound == 0 and len(raidtext) > 0:
                unknowngymfound = 0
                for file in glob.glob(self.unknownPath + "/gym_*.jpg"):
                    foundunknowngym = mt.fort_image_matching(
                        self.tempPath + "/" + str(hash) + "_raidpic" + str(i) +
                        ".jpg", file, True, 0.8)
                    if foundgym is None or foundunknowngym > foundgym[0]:
                        foundgym = foundunknowngym, file

                    if not foundgym is None and foundgym[0] > 0.7:
                        unknowngymfound = 1
                        foundgym = None

                if unknowngymfound == 0:
                    name22 = time.time()
                    cv2.imwrite(
                        self.unknownPath + "/gym_" + str(name22) + ".jpg",
                        raidpic)

            if monfound == 0 and len(raidtext) > 0 and eggfound == 0:
                unknownmonfound = 0
                for file in glob.glob(self.unknownPath + "/mon_*.jpg"):

                    foundunknownmon = mt.fort_image_matching(
                        self.tempPath + "/" + str(hash) + "_raidboss" +
                        str(i) + ".jpg", file, False, 0.8)
                    if foundmon is None or foundunknownmon > foundmon[0]:
                        foundmon = foundunknownmon, file

                    if not foundmon is None and foundmon[0] > 0.7:
                        unknownmonfound = 1
                        foundmon = None

                if unknownmonfound == 0:
                    name22 = time.time()
                    cv2.imwrite(
                        self.unknownPath + "/mon_" + str(name22) + ".jpg",
                        monAsset)

            gymfound = None
            foundmon = None
            foundgym = None
            raidtext = None
            foundegg = None
            eggfound = None
            lvlfound = None
            i = i + 1

        for file in glob.glob(self.tempPath + "/" + str(hash) + "_*raid*.jpg"):
            os.remove(file)
        os.remove(self.tempPath + "/" + str(hash) +
                  "_cropped_emptyraid_bw.png")
        os.remove(self.tempPath + "/" + str(hash) + "_emptyraid.png")
Example #8
0
    def detectGym(self, raidpic, hash, raidNo, captureLat, captureLng, radius, monId = None):
        foundgym = None
        gymId = None
        x1=0.30
        x2=0.62
        y1=0.62
        y2=1.23

        foundMonCrops = False

        log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: Scanning Gym')


        #if gymHash is none, we haven't seen the gym yet, otherwise, gymHash == gymId we are looking for
        if monId:
            log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: Got Mon-ID for Gym-Detection %s' % monId)
            with open('monsspec.json') as f:
                data = json.load(f)

            if str(monId) in data:
                foundMonCrops = True
                crop = data[str(monId)]["Crop"]
                log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: Found other Crops for Mon %s' % monId)
                log.debug(str(crop))
                x1 = crop['X1']
                x2 = crop['X2']
                y1 = crop['Y1']
                y2 = crop['Y2']

        log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: Cropsizes: x1:%s, x2:%s, y1:%s, y2:%s' % (str(x1), str(x2), str(y1), str(y2)))
            

        gymHashvalue = self.getImageHash(raidpic, True, raidNo, 'gym', x1, x2, y1, y2, radius)
        gymHash = self.imageHashExists(raidpic, True, 'gym', raidNo, x1, x2, y1, y2, radius)
            
        if gymHash is None:
            
            if self.checkDummy(raidpic, x1, x2, y1, y2, hash, raidNo, radius):
                log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: Found dummy gym pic')
                self.unknownfound(raidpic, 'unkgym', False, raidNo, hash, False, gymHashvalue, captureLat, captureLng)
                return 'dummy'
                
            log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: No Gym-Hash: found - searching')
            log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: Searching closest gyms')
            closestGymIds = self.dbWrapper.getNearGyms(captureLat, captureLng, hash, raidNo, str(args.gym_scan_distance))

            log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: Detecting Gym')
            for closegym in closestGymIds:

                for file in glob.glob("gym_img/_" + str(closegym[0]) + "_.jpg"):
                    find_gym = mt.fort_image_matching(raidpic, file, True, float(args.gym_detection_value), raidNo, hash, checkX=True, radius=radius, x1=x1, x2=x2, y1=y1, y2=y2)
                    log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: Compare Gym-ID - ' + str(closegym[0]) + ' - Match: ' + str(find_gym))
                    if foundgym is None or find_gym > foundgym[0]:
                        foundgym = find_gym, file

                    if foundgym and foundgym[0]>=float(args.gym_detection_value):
                        #okay, we very likely found our gym
                        gymSplit = foundgym[1].split('_')
                        gymId = gymSplit[2]

        else:
            self.imageHash(raidpic, gymHash, True, 'gym', raidNo, x1, x2, y1, y2, radius)
            log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: Detected Gym-ID: ' + str(gymHash))
            return gymHash

        if gymId:
            log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectGym: Detected Gym - Gym-ID: '+ str(gymId))
            gymHash = self.imageHash(raidpic, gymId, True, 'gym', raidNo, x1=x1, x2=x2, y1=y1, y2=y2, radius=radius)
            self.unknownfound(raidpic, 'gym', False, raidNo, hash, False, gymHashvalue, '0', '0')
            return gymId
        else:
            #we could not find the gym...
            self.unknownfound(raidpic, 'unkgym', False, raidNo, hash, False, gymHashvalue, captureLat, captureLng)
            return None
Example #9
0
    def run_fortmatching(self, session, fort_fullpath_filename):
        p_url = Path(self.url_image_path)
        fort_filename = os.path.basename(fort_fullpath_filename)
        LOG.info('find fort for {}'.format(fort_filename))
        max_fort_id = 0
        max_value = 0.0
        max_url_fullpath_filename = ''

        parts = str(fort_filename.replace('.jpg', '').replace('.png', '')).split('_')

        if len(parts) >= 3:
            device = parts[len(parts) - 2]
            time = int(parts[len(parts) - 1])

            teleport_delay = 1
            index = 0
            for device_conf in self.config.DEVICE_LIST:
                if device_conf == device:
                    teleport_delay = self.config.TELEPORT_DELAYS[index]
                    break
                index += 1

            time_a = math.floor(time - (teleport_delay / 2))
            time_b = math.ceil(time + (teleport_delay / 2))

            device_location_a = db.get_device_location_history(session, time_a, device)
            device_location_b = db.get_device_location_history(session, time_b, device)
            device_location_c = db.get_device_location_history(session, time, device)

            limit_forts = []

            if device_location_a is not None:
                ids_a = db.get_fort_ids_within_range(session, None, 800, device_location_a.lat, device_location_a.lon)
                for fort_id in ids_a:
                    if fort_id not in limit_forts:
                        limit_forts.append(fort_id)

            if device_location_b is not None:
                ids_b = db.get_fort_ids_within_range(session, None, 800, device_location_b.lat, device_location_b.lon)
                for fort_id in ids_b:
                    if fort_id not in limit_forts:
                        limit_forts.append(fort_id)

            if device_location_c is not None:
                ids_c = db.get_fort_ids_within_range(session, None, 800, device_location_c.lat, device_location_c.lon)
                if ids_a is not ids_c and ids_b is not ids_c:
                    for fort_id in ids_c:
                        if fort_id not in limit_forts:
                            limit_forts.append(fort_id)

            LOG.debug('Matching with gyms: {}'.format(limit_forts))
        else:
            LOG.debug('Matching without location')
            limit_forts = None

        for url_fullpath_filename in p_url.glob('*'):

            url_filename = os.path.basename(url_fullpath_filename)
            url_filename, url_filename_ext = os.path.splitext(url_filename)

            if url_filename_ext != '.png' and url_filename_ext != '.jpg':
                continue

            if limit_forts is not None and len(limit_forts) != 0:
                if int(url_filename) not in limit_forts:
                    continue

            if url_filename_ext == '.jpg' or url_filename_ext == '.png':
                try:
                    result = mt.fort_image_matching(str(url_fullpath_filename), str(fort_fullpath_filename))
                except KeyboardInterrupt:
                    os.killpg(0, signal.SIGINT)
                    sys.exit(1)
                except:
                    LOG.error('Matching error with {}'.format(str(url_fullpath_filename)))
                else:
                    url_filename = os.path.basename(url_fullpath_filename)
                    fort_id, ext = os.path.splitext(url_filename)
        #            print('fort_id:',fort_id,'result:',result,'max_value:',max_value, 'max_fort_id:', max_fort_id)
                    if result >= max_value:
                        max_value = result
                        max_fort_id = fort_id
                        max_url_fullpath_filename = url_fullpath_filename
    #            await asyncio.sleep(0.01)

        LOG.info('fort_filename:{} max_fort_id: {} max_value: {}'.format(fort_filename,max_fort_id, max_value))
        img = cv2.imread(str(fort_fullpath_filename), 3)
        gym_image_id = self.raidnearby.get_gym_image_id(img)
        gym_image_fort_id = db.get_gym_image_fort_id(session, gym_image_id)
        if float(max_value) >= 0.7:
            LOG.info(str(fort_fullpath_filename))
            if gym_image_fort_id is not None and int(max_fort_id) == int(gym_image_fort_id):
                LOG.info('This gym image is already trained')
                fort_result_file = os.getcwd() + '/success_img/Fort_' + str(max_fort_id) + '_GymImages_' + str(gym_image_id) + '_' + '{:.3f}'.format(max_value) + '.png'
                url_result_file = os.getcwd() + '/success_img/Fort_'+str(max_fort_id) + '_url' + str(url_filename_ext)
                shutil.move(fort_fullpath_filename, fort_result_file)
                shutil.copy(max_url_fullpath_filename, url_result_file)
            else:
                unknown_fort_id = db.get_unknown_fort_id(session)
                LOG.info('gym_images id:{} fort_id:{} unknow_fort_id:{}'.format(gym_image_id,gym_image_fort_id,unknown_fort_id))
                if gym_image_fort_id == unknown_fort_id:
                    try:
                        db.update_gym_image(session,gym_image_id,max_fort_id)
                    except KeyboardInterrupt:
                        os.killpg(0, signal.SIGINT)
                        sys.exit(1)
                    except:
                        LOG.error('Error to update gym_images for gym_images.id:{} gym_images.fort_id:{}'.format(gym_image_id,max_fort_id))
                        fort_result_file = os.getcwd() + '/success_img/Fort_' + str(max_fort_id) + '_GymImages_' + str(gym_image_id) + '_' + '{:.3f}'.format(max_value) + '.png'
                        url_result_file = os.getcwd() + '/not_find_img/Fort_'+str(max_fort_id) + '_url' + str(url_filename_ext)
                        shutil.move(fort_fullpath_filename, fort_result_file)
                        shutil.copy(max_url_fullpath_filename, url_result_file)
                        LOG.error('Successfully found fort fort_id:{}, but failed to updata gym_images database. Check not_find_img with the fort_id'.format(max_fort_id))
                    else:
                        fort_result_file = os.getcwd() + '/success_img/Fort_' + str(max_fort_id) + '_GymImages_' + str(gym_image_id) + '_' + '{:.3f}'.format(max_value) + '.png'
                        url_result_file = os.getcwd() + '/success_img/Fort_'+str(max_fort_id) + '_url' + str(url_filename_ext)
                        process_img_path = os.getcwd() + '/process_img/Fort_' + str(max_fort_id) + '.png'
                        shutil.copy(fort_fullpath_filename, process_img_path)
                        shutil.move(fort_fullpath_filename, fort_result_file)
                        shutil.copy(max_url_fullpath_filename, url_result_file)
                        LOG.info('Successfully found fort id: {}'.format(max_fort_id))
                else:
                    LOG.info('The gym image is assigned as fort id:{}'.format(gym_image_fort_id))
                    LOG.info('Check not_find_img directory.')       
                    LOG.info('If the Fort_{}.png and Fort_{}_url.jpg in not_find_img are correct'.format(str(max_fort_id),str(max_fort_id)))
                    LOG.info('Run "python3.6 manualsubmit.py force"'.format(str(max_fort_id),str(max_fort_id)))
                    fort_result_file = os.getcwd() + '/success_img/Fort_' + str(max_fort_id) + '_GymImages_' + str(gym_image_id) + '_' + '{:.3f}'.format(max_value) + '.png'
                    url_result_file = os.getcwd() + '/not_find_img/Fort_'+str(max_fort_id) + '_url' + str(url_filename_ext)
                    shutil.move(fort_fullpath_filename, fort_result_file)
                    shutil.copy(max_url_fullpath_filename, url_result_file)
        elif float(max_value) >= 0.60:
            fort_result_file = os.getcwd() + '/not_find_img/LowConfidence_Fort_' + str(max_fort_id) + '_GymImages_' + str(gym_image_id) + '_' + '{:.3f}'.format(max_value) + '.png'
            url_result_file = os.getcwd() + '/not_find_img/LowConfidence_Fort_'+str(max_fort_id) + '_url' + str(url_filename_ext)
            shutil.move(fort_fullpath_filename, fort_result_file)
            shutil.copy(max_url_fullpath_filename, url_result_file)
            LOG.info('Found fort id: {} but need to verify'.format(max_fort_id))
            LOG.info('If the Fort_{}.png and Fort_{}_url.jpg in not_find_img are correct'.format(str(max_fort_id),str(max_fort_id)))
            LOG.info('Run "python3.6 manualsubmit.py"'.format(str(max_fort_id),str(max_fort_id)))
        else:
            split = str(fort_filename).split('_')
            if len(split) == 4:
                fort_filename_real = split[0] + '_' + split[1] + '.png'
            else:
                fort_filename_real = fort_filename
            fort_result_file = os.getcwd() + '/not_find_img/' + str(fort_filename_real)
            url_result_file = os.getcwd() + '/not_find_img/'+str(max_fort_id) + str(url_filename_ext)
            shutil.move(fort_fullpath_filename, fort_result_file)
            shutil.copy(max_url_fullpath_filename, url_result_file)
            LOG.info('Can not find fort: {}, check the image in not_find_img'.format(max_fort_id))