def refresh_images(self): from core.models.image import Image # collect local images images = Image.objects.all() images_dict = {} for image in images: images_dict[image.name] = image # collect glance images glance_images = self.client.glance.get_images() glance_images_dict = {} for glance_image in glance_images: glance_images_dict[glance_image['name']] = glance_image # add new images new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys()) for name in new_image_names: image = Image(image_id=glance_images_dict[name]['id'], name=glance_images_dict[name]['name'], disk_format=glance_images_dict[name]['disk_format'], container_format=glance_images_dict[name]['container_format']) image.save() # remove old images old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) Image.objects.filter(name__in=old_image_names).delete()
def refresh_images(self): from core.models.image import Image # collect local images images = Image.objects.all() images_dict = {} for image in images: images_dict[image.name] = image # collect glance images glance_images = self.client.glance.get_images() glance_images_dict = {} for glance_image in glance_images: glance_images_dict[glance_image['name']] = glance_image # add new images new_image_names = set(glance_images_dict.keys()).difference( images_dict.keys()) for name in new_image_names: image = Image( image_id=glance_images_dict[name]['id'], name=glance_images_dict[name]['name'], disk_format=glance_images_dict[name]['disk_format'], container_format=glance_images_dict[name]['container_format']) image.save() # remove old images old_image_names = set(images_dict.keys()).difference( glance_images_dict.keys()) Image.objects.filter(name__in=old_image_names).delete()
def fetch_pending(self, deleted): # Images come from the back end # You can't delete them if (deleted): return [] # get list of images on disk images_path = Config().observer_images_directory available_images = {} if os.path.exists(images_path): for f in os.listdir(images_path): filename = os.path.join(images_path, f) if os.path.isfile(filename): available_images[f] = filename images = Image.objects.all() image_names = [image.name for image in images] for image_name in available_images: #remove file extension clean_name = ".".join(image_name.split('.')[:-1]) if clean_name not in image_names: image = Image(name=clean_name, disk_format='raw', container_format='bare', path = available_images[image_name]) image.save() return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
def fetch_pending(self, deleted): # Images come from the back end # You can't delete them if (deleted): logger.info("SyncImages: returning because deleted=True") return [] # get list of images on disk images_path = Config().observer_images_directory logger.info("SyncImages: deleted=False, images_path=%s" % images_path) available_images = {} if os.path.exists(images_path): for f in os.listdir(images_path): filename = os.path.join(images_path, f) if os.path.isfile(filename) and filename.endswith(".img"): available_images[f] = filename logger.info("SyncImages: available_images = %s" % str(available_images)) images = Image.objects.all() image_names = [image.name for image in images] for image_name in available_images: #remove file extension clean_name = ".".join(image_name.split('.')[:-1]) if clean_name not in image_names: logger.info("SyncImages: adding %s" % clean_name) image = Image(name=clean_name, disk_format='raw', container_format='bare', path=available_images[image_name]) image.save() return Image.objects.filter( Q(enacted__lt=F('updated')) | Q(enacted=None))
def fetch_pending(self, deleted): # Images come from the back end # You can't delete them if (deleted): logger.info("SyncImages: returning because deleted=True") return [] # get list of images on disk images_path = Config().observer_images_directory logger.info("SyncImages: deleted=False, images_path=%s" % images_path) available_images = {} if os.path.exists(images_path): for f in os.listdir(images_path): filename = os.path.join(images_path, f) if os.path.isfile(filename): available_images[f] = filename logger.info("SyncImages: available_images = %s" % str(available_images)) images = Image.objects.all() image_names = [image.name for image in images] for image_name in available_images: #remove file extension clean_name = ".".join(image_name.split('.')[:-1]) if clean_name not in image_names: logger.info("SyncImages: adding %s" % clean_name) image = Image(name=clean_name, disk_format='raw', container_format='bare', path = available_images[image_name]) image.save() return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
def fetch_pending(self, deletion): if (deletion): return [] images = Image.objects.all() image_names = [image.name for image in images] new_images = [] try: aws_images = json.loads(open(XOS_DIR + '/aws-images').read()) except: aws_images = aws_run('ec2 describe-images --owner 099720109477') open(XOS_DIR + '/aws-images', 'w').write(json.dumps(aws_images)) aws_images = aws_images['Images'] aws_images = filter(lambda x: x['ImageType'] == 'machine', aws_images)[:50] names = set([]) for aws_image in aws_images: desc_ok = True try: desc = aws_image['Description'] except: try: desc = aws_image['ImageLocation'] except: desc_ok = False if (desc_ok): try: desc_ok = desc and desc not in names and desc not in image_names and '14.04' in desc except KeyError: desc_ok = False if desc_ok and aws_image['ImageType'] == 'machine': image = Image(disk_format=aws_image['ImageLocation'], name=desc, container_format=aws_image['Hypervisor'], path=aws_image['ImageId']) new_images.append(image) names.add(image.name) #print new_images return new_images
def attention(self, image: Image) -> Image: img_path = image.path.as_posix() width, height = self.config.width, self.config.height img = k_image.load_img(img_path, target_size=(width, height)) x = k_image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) vgg16 = self.model.get_layer("vgg16") last_conv_layer = vgg16.get_layer("block5_conv3") o = vgg16.get_output_at(0)[:, 8] grads = k_backend.gradients(o, last_conv_layer.output)[0] pooled_grads = k_backend.mean(grads, axis=(0, 1, 2)) iterate = k_backend.function([vgg16.get_input_at(0)], [pooled_grads, last_conv_layer.output[0]]) pooled_grads_value, conv_layer_output_value = iterate([x]) for i in range(512): conv_layer_output_value[:, :, i] *= pooled_grads_value[i] heatmap = np.mean(conv_layer_output_value, axis=-1) heatmap = np.maximum(heatmap, 0) heatmap /= np.max(heatmap) cv2_image = cv2.imread(img_path) heatmap = cv2.resize(heatmap, (cv2_image.shape[1], cv2_image.shape[0])) heatmap = np.uint8(255 * heatmap) heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) superimposed_img = heatmap * 0.4 + cv2_image # cv2.imwrite("pneumo.jpg", superimposed_img) cache_dir = image.path.parent.joinpath("cache") cache_dir.mkdir(parents=True, exist_ok=True) new_image_path = cache_dir.joinpath(image.path.name) cv2.imwrite(new_image_path.as_posix(), superimposed_img) return Image(new_image_path)
def _load_paths(self, paths: List[Path]) -> None: for each in paths: image = Image(each) self.images_vm.add(image)