Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ap.add_argument("-p", "--plot", type=str, default="./output/plot.png", help="path to output acc/loss curve.")
args = vars(ap.parse_args())
# Initialize the number of epochs to train for, initialize learning rate, batch size, and image dimensions.
EPOCHS = 75
INIT_LR = 1e-3 # The default value for the 'Adam' optimizer.
BATCH_SIZE = 32
IMAGE_DIMS = (96, 96, 3)
# Initialize the data and labels.
data = []
labels = []
# Grob the image paths and randomly shuffle them.
print("[INFO] loading image...")
imagePaths = sorted(list(paths.list_images(args["dataset"])))
random.seed(42)
random.shuffle(imagePaths)
# Loop over the input image.
for imagePath in imagePaths:
# Load image, preprocess it, and store it in the data list.
image = cv2.imread(imagePath)
image = cv2.resize(image, (IMAGE_DIMS[1], IMAGE_DIMS[0]))
image = img_to_array(image)
data.append(image)
# Extract set of class labels (each element is a 2-element list, e.g., ['red', 'dress']) from the image path and update the label list.
label = imagePath.split(os.path.sep)[-2].split("_")
labels.append(label)
# Scaling the raw pixel intensities to the range [0, 1].
# 命令行参数
ap = argparse.ArgumentParser()
ap.add_argument('-d',"--dataset",required = True,
help = 'path to input dataset')
ap.add_argument("-0",'--output',required = True,
help = 'path ot output hdf5 file')
ap.add_argument('-b','--batch_size',type = int,default = 16,
help='batch size of images to ba passed through network')
ap.add_argument('-s','--buffer_size',type=int,default=1000,
help = 'size of feature extraction buffer')
args = vars(ap.parse_args())
# batch
bs = args['batch_size']
print("[INFO] loading images...")
imagePaths = list(paths.list_images(args['dataset']))
# 混洗数据
random.shuffle(imagePaths)
# 标签获取
labels = [p.split(os.path.sep)[-1].split(".")[0] for p in imagePaths]
# 编码编码化
le = LabelEncoder()
labels = le.fit_transform(labels)
print("[INFO] loading network...")
# imagenet上训练的权重
model = ResNet50(weights = 'imagenet',include_top=False)
#ResNet50的最后一个average pooling层的维度是2048
dataset = HDF.HDF5DatasetWriter((len(imagePaths),2048),
args['output'],dataKey='feature',buffSize=args['buffer_size'])
dataset.storeClassLabels(le.classes_)
# 初始化进度条
def load_data(path,norm_size,class_num):
data = []#数据x
label = []#标签y
image_paths = sorted(list(paths.list_images(path)))#imutils模块中paths可以读取所有文件路径
random.seed(0)#保证每次数据顺序一致
random.shuffle(image_paths)#将所有的文件路径打乱
for each_path in image_paths:
image = cv2.imread(each_path)#读取文件
image = cv2.resize(image,(norm_size,norm_size))#统一图片尺寸
image = img_to_array(image)
data.append(image)
maker = int(each_path.split(os.path.sep)[-2])#切分文件目录,类别为文件夹整数变化,从0-61.如train文件下00014,label=14
label.append(maker)
data = np.array(data,dtype="float")/255.0#归一化
label = np.array(label)
label = to_categorical(label,num_classes=class_num)#one-hot
return data,label
def find_background_image(class_name, path):
"""Given a directory and a class name, search for a viable background image"""
# This method based on: http://www.pyimagesearch.com/2015/11/09/pedestrian-detection-opencv/
# initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
# loop over the image paths for each class
class_path = os.path.join(path, class_name)
for imagePath in paths.list_images(class_path):
# load the image and resize it to [faster, more accurate]
image = cv2.imread(imagePath)
image = imutils.resize(image, width=min(400, image.shape[1]))
orig = image.copy()
# detect people in the image
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
padding=(8, 8), scale=1.05)
# draw bounding boxes
for (x, y, w, h) in rects:
cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
# If there are no people (no boxes), return the path of this image
if len(rects) == 0:
return imagePath
import numpy as np
import argparse
import imutils
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True, help="path to images directory")
args = vars(ap.parse_args())
# initialize a rectangular and square structuring kernel
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 21))
# loop over the input image paths
for imagePath in paths.list_images(args["images"]):
# load the image, resize it, and convert it to grayscale
print imagePath
image = cv2.imread(imagePath)
image = imutils.resize(image, height=600)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# smooth the image using a 3x3 Gaussian, then apply the blackhat
# morphological operator to find dark regions on a light background
gray = cv2.GaussianBlur(gray, (3, 3), 0)
blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, rectKernel)
# compute the Scharr gradient of the blackhat image and scale the
# result into the range [0, 255]
gradX = cv2.Sobel(blackhat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
gradX = np.absolute(gradX)
(minVal, maxVal) = (np.min(gradX), np.max(gradX))
import argparse
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True, help="path to the dataset of shirt images")
ap.add_argument("-q", "--query", required=True, help="path to the query image")
args = vars(ap.parse_args())
# initialize the local binary patterns descriptor and initialize the index dictionary
# where the image filename is the key and the features are the value
desc = LocalBinaryPatterns(24, 8)
index = {}
# loop over the shirt images
for imagePath in paths.list_images(args["dataset"]):
# load the image, convert it to grayscale, and describe it
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
hist = desc.describe(gray)
# update the index dictionary
filename = imagePath[imagePath.rfind("/") + 1:]
index[filename] = hist
# load the query image and extract Local Binary Patterns from it
query = cv2.imread(args["query"])
queryFeatures = desc.describe(cv2.cvtColor(query, cv2.COLOR_BGR2GRAY))
# show the query image and initialize the results dictionary
cv2.imshow("Query", query)
results = {}
def animate(src, gif_name, reshape=None, fps=25):
if not isinstance(src, list):
if os.path.isdir(src):
src = list(paths.list_images(src))
for idx, image in enumerate(src):
src[idx] = cv2.imread(image)
if reshape:
for idx, image in enumerate(src):
src[idx] = cv2.resize(image, reshape)
for idx, image in enumerate(src):
src[idx] = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
src = np.array(src)
imageio.mimsave(gif_name, src, fps=fps)
# initialize characters string
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
# initialize the data and labels for the alphabet and digits
alphabetData = []
digitsData = []
alphabetLabels = []
digitsLabels = []
# initialize the descriptor
print("[INFO] describing font examples...")
blockSizes = ((5, 5), (5, 10), (10, 5), (10, 10))
desc = BlockBinaryPixelSum(targetSize=(30, 15), blockSizes=blockSizes)
# loop over the font paths
for fontPath in paths.list_images(args["fonts"]):
# load the font image, convert it to grayscale and threshold it
font = cv2.imread(fontPath)
font = cv2.cvtColor(font, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(font, 128, 255, cv2.THRESH_BINARY_INV)[1]
# detect contours in the thresholded image and sort them from left to right
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key=lambda c:(cv2.boundingRect(c)[0] + cv2.boundingRect(c)[1]))
# loop over the contours
for (i, c) in enumerate(cnts):
# grab the bounding box for the contour, extract the ROI, and extract features
(x, y, w, h) = cv2.boundingRect(c)
roi = thresh[y:y + h, x:x + w]
features = desc.describe(roi)
def create_gif(inputPath, outputPath, delay, finalDelay, loop):
# grab all image paths in the input directory
imagePaths = sorted(list(paths.list_images(inputPath)))
# remove the last image path in the list
lastPath = imagePaths[-1]
imagePaths = imagePaths[:-1]
# construct the image magick 'convert' command that will be used
# generate our output GIF, giving a larger delay to the final
# frame (if so desired)
cmd = "convert -delay {} {} -delay {} {} -loop {} {}".format(
delay, " ".join(imagePaths), finalDelay, lastPath, loop,
outputPath)
os.system(cmd)