def get_gym_image_id(self, raid_img): height, width, channels = raid_img.shape org_top_y = 40 org_top_h = 30 org_top_x = 135 org_top_w = 50 org_left_y = 125 org_left_h = 70 org_left_x = 45 org_left_w = 25 scale = width / 320 top_y = int(org_top_y * scale) top_h = int(org_top_h * scale) top_x = int(org_top_x * scale) top_w = int(org_top_w * scale) left_y = int(org_left_y * scale) left_h = int(org_left_h * scale) left_x = int(org_left_x * scale) left_w = int(org_left_w * scale) cropTop = raid_img[top_y:top_y + top_h, top_x:top_x + top_w] cropLeft = raid_img[left_y:left_y + left_h, left_x:left_x + left_w] cropTopMask = self.getMonMask(cropTop) cropLeftMask = self.getMonMask(cropLeft) cropTopMean = cv2.mean(cropTop, cropTopMask) cropLeftMean = cv2.mean(cropLeft, cropLeftMask) top_mean0 = int( cropTopMean[0]) # int(cropTop[:,:,0].mean(mask = cropTopMask)) top_mean1 = int( cropTopMean[1]) # int(cropTop[:,:,1].mean(mask = cropTopMask)) top_mean2 = int( cropTopMean[2]) # int(cropTop[:,:,2].mean(mask = cropTopMask)) left_mean0 = int( cropLeftMean[0]) # int(cropLeft[:,:,0].mean(mask = cropLeftMask)) left_mean1 = int( cropLeftMean[1]) # int(cropLeft[:,:,1].mean(mask = cropLeftMask)) left_mean2 = int( cropLeftMean[2]) # int(cropLeft[:,:,2].mean(mask = cropLeftMask)) LOG.debug('gym image param: {} {} {} {} {} {}'.format( top_mean0, top_mean1, top_mean2, left_mean0, left_mean1, left_mean2)) session = database.Session() gym_image_id = database.get_gym_image_id(session, top_mean0, top_mean1, top_mean2, left_mean0, left_mean1, left_mean2) session.close() return gym_image_id
def detectGym(self, raid_img, level): height, width, channels = raid_img.shape org_top_y = 40 org_top_h = 30 org_top_x = 135 org_top_w = 50 org_left_y = 125 org_left_h = 70 org_left_x = 45 org_left_w = 25 scale = width/320 LOG.debug('raid image scale: {}'.format(scale)) top_y = int(org_top_y*scale) top_h = int(org_top_h*scale) top_x = int(org_top_x*scale) top_w = int(org_top_w*scale) left_y = int(org_left_y*scale) left_h = int(org_left_h*scale) left_x = int(org_left_x*scale) left_w = int(org_left_w*scale) cropTop = raid_img[top_y:top_y+top_h, top_x:top_x+top_w] cropLeft = raid_img[left_y:left_y+left_h, left_x:left_x+left_w] cropTopMask = self.getMonMask(cropTop) cropLeftMask = self.getMonMask(cropLeft) cropTopMean = cv2.mean(cropTop, cropTopMask) cropLeftMean = cv2.mean(cropLeft, cropLeftMask) top_mean0 = int(cropTopMean[0]) # int(cropTop[:,:,0].mean(mask = cropTopMask)) top_mean1 = int(cropTopMean[1]) # int(cropTop[:,:,1].mean(mask = cropTopMask)) top_mean2 = int(cropTopMean[2]) # int(cropTop[:,:,2].mean(mask = cropTopMask)) left_mean0 = int(cropLeftMean[0]) # int(cropLeft[:,:,0].mean(mask = cropLeftMask)) left_mean1 = int(cropLeftMean[1]) # int(cropLeft[:,:,1].mean(mask = cropLeftMask)) left_mean2 = int(cropLeftMean[2]) # int(cropLeft[:,:,2].mean(mask = cropLeftMask)) min_error = 10000000 gym_id = 0 gym_image_id = 0 session = database.Session() gym_db = database.get_gym_images(session) LOG.info('Gyms in gym_images: {}'.format(len(gym_db))) unknown_gym_num = 0 for gym in gym_db: if int(gym.fort_id) == int(self.unknown_fort_id): unknown_gym_num = unknown_gym_num + 1 if unknown_gym_num != 0: LOG.info('{} Unknown gym in DB'.format(unknown_gym_num)) LOG.debug('top_mean0:{} top_mean1:{} top_mean2:{} left_mean0:{} left_mean1:{} left_mean2:{} '.format(top_mean0,top_mean1,top_mean2,left_mean0,left_mean1,left_mean2)) for gym in gym_db: dif1 = pow(top_mean0 - gym.param_1,2) dif2 = pow(top_mean1 - gym.param_2,2) dif3 = pow(top_mean2 - gym.param_3,2) dif4 = pow(left_mean0 - gym.param_4,2) dif5 = pow(left_mean1 - gym.param_5,2) dif6 = pow(left_mean2 - gym.param_6,2) error = math.sqrt(dif1+dif2+dif3+dif4+dif5+dif6) # print(gym.fort_id,error,gym.param_1,gym.param_2,gym.param_3,gym.param_4,gym.param_5,gym.param_6) # find minimum error if error < min_error: min_error = error gym_id = gym.fort_id gym_image_id = gym.id error_limit = 7 if level == 4: error_limit = 10 if min_error > error_limit: LOG.info('gym_id:{} min_error:{}'.format(gym_id, min_error)) LOG.info('GymImage added to database') gym_id = -1 database.add_gym_image(session,self.unknown_fort_id,top_mean0,top_mean1,top_mean2,left_mean0,left_mean1,left_mean2) gym_image_id = database.get_gym_image_id(session,top_mean0,top_mean1,top_mean2,left_mean0,left_mean1,left_mean2) session.commit() session.close() return gym_image_id, gym_id, min_error