Reputation: 31
I have a .txt file that contains path/to/image.jpg,xmin,ymin,xmax,ymax
for each row and a img
folder that contains the jpg images. What is the best way using python to extract the "objects" inside the coordinates of each file and look if the bounding boxes are set correctly?
Upvotes: 2
Views: 2883
Reputation: 629
You can use the opencv-python
library:
import cv2
# Reading the file
with open("filename.txt") as iostream:
content = iostream.read()
# Visualizing the data
color = (0, 0, 255) # RED
for line in content.split("\n"):
image_path, xmin, ymin, xmax, ymax = line.split(",")
image = cv2.imread(image_path)
pt1 = (int(xmin), int(ymin))
pt2 = (int(xmax), int(ymax))
cv2.rectangle(image, pt1, pt2, color)
cv2.imshow("Visualization bounding box", image)
cv2.waitKey()
This code will show each image, with a red rectangle to display the bounding box. If you press any key, it will switch to the next one.
If you want to save the cropped images, you can use something like this:
outfile = image_path[:-4] + "_bbox.jpg"
outimage = image[int(ymin):int(ymax), int(xmin):int(xmax)]
cv2.imwrite(outfile, outimage)
Upvotes: 3
Reputation: 356
Your text file is a CSV (Comma Separated Values) and can be loaded as such.
You could simply use PIL to crop each image in your folder to the bounding box:
import csv
from PIL import Image
f = open('test.csv')
csv_f = csv.reader(f)
#iterate through rows of your CSV
for row in csv_f:
#open image using PIL
im = Image.open(row[0])
#crop box format: xmin, ymin, xmax, ymax
crop_box = (row[1], row[2], row[3], row[4])
#convert values to int
crop_box = map(int, crop_box)
#crop using PIL
im = im.crop((crop_box))
#save the image to predetermined savepath
im.save(save_name)
Upvotes: 2