Je développe une CNN pour la reconnaissance d'images. J'ai un ensemble d'images différentes, où dans chaque image, j'ai un ensemble de zones de bornies différentes (AL au moins 3 boîtes de liaison pour chaque image).
Je voudrais extraire automatiquement la pièce dans les zones de liaison, puis les recadrer un ensemble d'image recadrée correspondant au contenu de chaque bornisation. J'ai créé à la fois un fichier p > ,filepath,x1,x2,y1,y2,class_name
0,71.jpeg,81,118,98,122,os
1,71.jpeg,120,156,83,110,od
2,71.jpeg,107,161,136,154,m
3 Réponses :
Eh bien, j'ai trouvé le moyen d'extraire des images recadrées avec ce code:
p>
#crop images import numpy as np # linear algebra import xml.etree.ElementTree as ET # for parsing XML import matplotlib.pyplot as plt # to show images from PIL import Image # to read images import os import glob root_images="/content/images" root_annots="/content/annotation" all_images=os.listdir("/content/images/") print(f"Total images : {len(all_images)}") breeds = glob.glob('/content/annotation/') annotation=[] for b in breeds: annotation+=glob.glob(b+"/*") print(f"Total annotation : {len(annotation)}") breed_map={} for annot in annotation: breed=annot.split("/")[-2] index=breed.split("-")[0] breed_map.setdefault(index,breed) print(f"Total Breeds : {len(breed_map)}") def bounding_box(image): #bpath=root_annots+str(breed_map[image.split("_")[0]])+"/"+str(image.split(".")[0]) #print (bpath) #print(root_annots) #print (str(breed_map[image.split("_")[0]])) #print (str(image.split(".")[0])) bpath=root_annots+"/"+str(image.split(".")[0]+".xml") tree = ET.parse(bpath) root = tree.getroot() objects = root.findall('object') for o in objects: bndbox = o.find('bndbox') # reading bound box xmin = int(bndbox.find('xmin').text) ymin = int(bndbox.find('ymin').text) xmax = int(bndbox.find('xmax').text) ymax = int(bndbox.find('ymax').text) return (xmin,ymin,xmax,ymax) plt.figure(figsize=(10,10)) bbox=[] for i,image in enumerate(all_images): bbox=bounding_box(image) print(bbox) im=Image.open(os.path.join(root_images,image)) im=im.crop(bbox) im.save('/content/results_imgs/{}.jpeg'.format(i,im))
Je suppose que vous voulez recadrer les images pour les boîtes de sélection. Vous pouvez simplement utiliser un tableau numpy:
Veuillez trouver un exemple de travail ici. P>
import matplotlib.pyplot as plt mydic = { "annotations": [ { "class": "rect", "height": 98, "width": 113, "x": 177, "y": 12 }, { "class": "rect", "height": 80, "width": 87, "x": 373, "y": 43 } ], "class": "image", "filename": "https://i.stack.imgur.com/9qe6z.png" } def crop(dic, i): image = plt.imread(dic["filename"]) x0 = dic["annotations"][i]["x"] y0 = dic["annotations"][i]["y"] width = dic["annotations"][i]["width"] height = dic["annotations"][i]["height"] return image[y0:y0+height , x0:x0+width, :] fig = plt.figure() ax = fig.add_subplot(121) ax.imshow(plt.imread(mydic["filename"])) ax1 = fig.add_subplot(222) ax1.imshow(crop(mydic, 0)) ax2 = fig.add_subplot(224) ax2.imshow(crop(mydic, 1)) plt.show()
J'ai trouvé un référentiel git pour créer des images recadrées à partir de toutes les zones de liaison des objets détectés des images de Pascal COV (images avec des zones de bornisation générées par LabelImg): https://github.com/giovannicimolin/pascalvoc-a-images P >
Le code fonctionne assez bien. J'espère que cela vous aidera à résoudre votre problème. P>
Avez-vous essayé OpenCV ou Scikit-Image pour recadrer vos images?