Created
May 9, 2018 05:54
-
-
Save neale/e658a99ec0273f3061cc09adf7877b55 to your computer and use it in GitHub Desktop.
Bbox Cropping Tool - Cycle through images in directory, crop and save ROIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2, os, sys, getopt | |
from glob import glob | |
import natsort | |
# Global Variables | |
helpMessage = 'Usage:\n\tcrop.py <command> [argument]\n\nCommands:\n\t-h --help\t\tDisplay this help message\n\t-i --image [path]\tInput image\n\t-f --folder [path]\tImage Folder\n\t-r --regex [regex]\tNaming of output files [WIP]\n\t-s --save [path]\tPath to Save' | |
# Arguments | |
pathIMG = '' | |
pathDIR = '' | |
pathSAV = '' | |
regex = '' | |
# Graphical | |
drawing = False # true if mouse is pressed | |
cropped = False | |
ix,iy = -1,-1 | |
# MISC | |
img_index = 0 | |
# Mouse callback function | |
def draw(event,x,y,flags,param): | |
global ix, iy, drawing, img, DEFAULT, cropped | |
cropped = False | |
if event == cv2.EVENT_LBUTTONDOWN: | |
drawing = True | |
ix,iy = x,y | |
elif event == cv2.EVENT_MOUSEMOVE: | |
if drawing == True: | |
img = DEFAULT.copy() | |
cv2.imshow(pathIMG,img) | |
if (abs(ix - x) < abs(iy -y)): | |
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),1) | |
else: | |
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),1) | |
elif event == cv2.EVENT_LBUTTONUP: | |
drawing = False | |
crop(ix,iy,x,y) | |
# Get Arguments | |
def getArgvs(argv): | |
try: | |
opts, args = getopt.getopt(argv,"hi:f:r:s:",["help","image=","folder=","regex=","save="]) | |
except getopt.GetoptError: | |
print(helpMessage) | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt in ('-h', "--help"): | |
print(helpMessage) | |
sys.exit() | |
elif opt in ("-i", "--image"): | |
global pathIMG | |
pathIMG = arg | |
#print("img: " + arg) | |
elif opt in ("-f", "--folder"): | |
global pathDIR | |
pathDIR = arg | |
#print("dir: " + arg) | |
elif opt in ("-r","--regex"): | |
global regex | |
regex = arg | |
#print("regex: " + arg) | |
elif opt in ("-s","--save"): | |
global pathSAV | |
pathSAV = arg | |
# Crop Image | |
def crop(ix,iy,x,y): | |
global img, DEFAULT, cropped | |
img = DEFAULT.copy() | |
cv2.imshow(pathIMG,img) | |
if (abs(ix - x) < abs(iy -y)): | |
img = img[iy:y, ix:x] | |
else: | |
img = img[iy:y, ix:x] | |
# Save image | |
def save(crop_img, ind): | |
# Set name | |
name = pathSAV + "img" + str(img_index) + str(ind) + ".png" | |
# Resize | |
dst = cv2.resize(crop_img, (64,64)) | |
# Save | |
cv2.imwrite(name,dst) | |
print("Saved image sucsessfully") | |
# Main Loop | |
def loop(): | |
global img | |
done = False | |
print ("creating new window") | |
cv2.namedWindow(pathIMG) | |
cv2.setMouseCallback(pathIMG,draw) | |
while (1): | |
try: | |
cv2.imshow(pathIMG,img) | |
except: | |
# tried to crop a single point -- happens \shrug | |
break | |
k = cv2.waitKey(1) & 0xFF | |
if (k == 27): | |
print("Cancelled Crop") | |
break | |
elif (k == ord('s')): | |
print("Image Saved") | |
ind = np.random.randint(1000000) | |
save(img, ind) | |
break | |
elif(k == ord('n')): | |
print ("next image") | |
done = True | |
break | |
print("Done!") | |
cv2.destroyAllWindows() | |
return done | |
# Iterate through images in path | |
def getIMG(path): | |
global img, DEFAULT | |
done = False | |
paths = natsort.natsorted(glob(path + '*.png'))[5627:] | |
print (len(paths)) | |
for filename in paths: | |
done = False | |
print ("processing {}".format(filename)) | |
while done is False: | |
# Get Image Path | |
pathIMG = filename | |
# Read Image | |
img = cv2.imread(pathIMG) | |
img = cv2.flip(img, 0) | |
DEFAULT = img.copy() | |
# Draw image | |
done = loop() | |
print ("All fininshed, Exiting") | |
return 0 | |
# Main Function | |
def main(): | |
getArgvs(sys.argv[1:]) | |
global img, DEFAULT | |
if (pathDIR != ''): | |
# Print Path | |
print("dir: " + pathDIR) | |
# Cycle through files | |
print (pathDIR) | |
getIMG(pathDIR) | |
elif (pathIMG != ''): | |
# Print Path | |
print("img: " + pathIMG) | |
# Load Image | |
img = cv2.imread(pathIMG,-1) | |
DEFAULT = img.copy() | |
# Draw Image | |
loop() | |
# Run Main | |
if __name__ == "__main__": | |
main() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment