예제 #1
0
    def get_pokemon_image_id(self, img):
        ret,bin_img = cv2.threshold(cv2.cvtColor(img,cv2.COLOR_BGR2GRAY),240,255,cv2.THRESH_BINARY_INV)
        bin_color = cv2.cvtColor(bin_img,cv2.COLOR_GRAY2BGR)
        
        height, width, channels = img.shape
        
        org_x1 = [288, 300]
        org_y1 = [125, 195]
        org_x2 = [264, 280]
        org_y2 = [234, 250]    
        org_x3 = [244, 260]
        org_y3 = [254, 270]    
        org_x4 = [224, 240]
        org_y4 = [270, 286]    
        org_x5 = [310, 318]
        org_y5 = [220, 350]    
        org_x6 = [280, 308]
        org_y6 = [270, 350]    
        org_x7 = [244, 278]
        org_y7 = [300, 350]    

        scale = width/320

        x1 = [int(org_x1[0]*scale), int(org_x1[1]*scale)]
        y1 = [int(org_y1[0]*scale), int(org_y1[1]*scale)]
        x2 = [int(org_x2[0]*scale), int(org_x2[1]*scale)]
        y2 = [int(org_y2[0]*scale), int(org_y2[1]*scale)]    
        x3 = [int(org_x3[0]*scale), int(org_x3[1]*scale)]
        y3 = [int(org_y3[0]*scale), int(org_y3[1]*scale)]    
        x4 = [int(org_x4[0]*scale), int(org_x4[1]*scale)]
        y4 = [int(org_y4[0]*scale), int(org_y4[1]*scale)]    
        x5 = [int(org_x5[0]*scale), int(org_x5[1]*scale)]
        y5 = [int(org_y5[0]*scale), int(org_y5[1]*scale)]    
        x6 = [int(org_x6[0]*scale), int(org_x6[1]*scale)]
        y6 = [int(org_y6[0]*scale), int(org_y6[1]*scale)]    
        x7 = [int(org_x7[0]*scale), int(org_x7[1]*scale)]
        y7 = [int(org_y7[0]*scale), int(org_y7[1]*scale)]    
        
        crop1 = bin_img[y1[0]:y1[1], x1[0]:x1[1]]
        crop2 = bin_img[y2[0]:y2[1], x2[0]:x2[1]]
        crop3 = bin_img[y3[0]:y3[1], x3[0]:x3[1]]
        crop4 = bin_img[y4[0]:y4[1], x4[0]:x4[1]]
        crop5 = bin_img[y5[0]:y5[1], x5[0]:x5[1]]
        crop6 = bin_img[y6[0]:y6[1], x6[0]:x6[1]]
        crop7 = bin_img[y7[0]:y7[1], x7[0]:x7[1]]

        mean1 = int(crop1.mean())
        mean2 = int(crop2.mean())
        mean3 = int(crop3.mean())
        mean4 = int(crop4.mean())
        mean5 = int(crop5.mean())
        mean6 = int(crop6.mean())
        mean7 = int(crop7.mean())

        LOG.debug('pokemon image param: {} {} {} {} {} {} {}'.format(mean1,mean2,mean3,mean4,mean5,mean6,mean7))
        session = database.Session()
        pokemon_image_id = database.get_pokemon_image_id(session,mean1,mean2,mean3,mean4,mean5,mean6,mean7)
        session.close()
        return pokemon_image_id
예제 #2
0
    def detectMon(self, img):
        ret,bin_img = cv2.threshold(cv2.cvtColor(img,cv2.COLOR_BGR2GRAY),240,255,cv2.THRESH_BINARY_INV)
        bin_color = cv2.cvtColor(bin_img,cv2.COLOR_GRAY2BGR)

        height, width, channels = img.shape
        
        org_x1 = [288, 300]
        org_y1 = [125, 195]
        org_x2 = [264, 280]
        org_y2 = [234, 250]    
        org_x3 = [244, 260]
        org_y3 = [254, 270]    
        org_x4 = [224, 240]
        org_y4 = [270, 286]    
        org_x5 = [310, 318]
        org_y5 = [220, 350]    
        org_x6 = [280, 308]
        org_y6 = [270, 350]    
        org_x7 = [244, 278]
        org_y7 = [300, 350]    

        scale = width/320
        LOG.debug('raid image scale :{}'.format(scale))

        x1 = [int(org_x1[0]*scale), int(org_x1[1]*scale)]
        y1 = [int(org_y1[0]*scale), int(org_y1[1]*scale)]
        x2 = [int(org_x2[0]*scale), int(org_x2[1]*scale)]
        y2 = [int(org_y2[0]*scale), int(org_y2[1]*scale)]    
        x3 = [int(org_x3[0]*scale), int(org_x3[1]*scale)]
        y3 = [int(org_y3[0]*scale), int(org_y3[1]*scale)]    
        x4 = [int(org_x4[0]*scale), int(org_x4[1]*scale)]
        y4 = [int(org_y4[0]*scale), int(org_y4[1]*scale)]    
        x5 = [int(org_x5[0]*scale), int(org_x5[1]*scale)]
        y5 = [int(org_y5[0]*scale), int(org_y5[1]*scale)]    
        x6 = [int(org_x6[0]*scale), int(org_x6[1]*scale)]
        y6 = [int(org_y6[0]*scale), int(org_y6[1]*scale)]    
        x7 = [int(org_x7[0]*scale), int(org_x7[1]*scale)]
        y7 = [int(org_y7[0]*scale), int(org_y7[1]*scale)]    
        
        crop1 = bin_img[y1[0]:y1[1], x1[0]:x1[1]]
        crop2 = bin_img[y2[0]:y2[1], x2[0]:x2[1]]
        crop3 = bin_img[y3[0]:y3[1], x3[0]:x3[1]]
        crop4 = bin_img[y4[0]:y4[1], x4[0]:x4[1]]
        crop5 = bin_img[y5[0]:y5[1], x5[0]:x5[1]]
        crop6 = bin_img[y6[0]:y6[1], x6[0]:x6[1]]
        crop7 = bin_img[y7[0]:y7[1], x7[0]:x7[1]]

        mean1 = int(crop1.mean())
        mean2 = int(crop2.mean())
        mean3 = int(crop3.mean())
        mean4 = int(crop4.mean())
        mean5 = int(crop5.mean())
        mean6 = int(crop6.mean())
        mean7 = int(crop7.mean())
        
        min_error = 10000000
        mon_id = 0
        mon_form = None
        mon_image_id = 0

        session = database.Session()
        mon_db = database.get_pokemon_images(session)
        LOG.info('PokemonImages in DB : {}'.format(len(mon_db)))
        unknown_mon_num = 0
        for mon in mon_db:
            if int(mon.pokemon_id) == 0:
                unknown_mon_num = unknown_mon_num + 1

        if unknown_mon_num != 0:
            LOG.info('{} Unknown pokemon in DB'.format(unknown_mon_num))

        # get error from all gyms
        for mon in mon_db:
            dif1 = pow(mean1 - mon.param_1,2)
            dif2 = pow(mean2 - mon.param_2,2)
            dif3 = pow(mean3 - mon.param_3,2)
            dif4 = pow(mean4 - mon.param_4,2)
            dif5 = pow(mean5 - mon.param_5,2)
            dif6 = pow(mean6 - mon.param_6,2)
            dif7 = pow(mean7 - mon.param_7,2)
            error = math.sqrt(dif1+dif2+dif3+dif4+dif5+dif6+dif7)
            # find minimum error
            if error < min_error:
                min_error = error
                mon_id = int(mon.pokemon_id)
                mon_image_id = int(mon.id)
                mon_form = mon.form

        if min_error > 5:
            mon_id = -1
            database.add_pokemon_image(session,0,mean1,mean2,mean3,mean4,mean5,mean6,mean7)
            mon_image_id = database.get_pokemon_image_id(session,mean1,mean2,mean3,mean4,mean5,mean6,mean7)

        session.commit()
        session.close()

        return mon_image_id, mon_id, mon_form, min_error