def test_image(addr):
target = ['angry','disgust','fear','happy','sad','surprise','neutral']
font = cv2.FONT_HERSHEY_SIMPLEX
im = cv2.imread(addr)
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1)
for (x, y, w, h) in faces:
cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 2,5)
face_crop = im[y:y+h,x:x+w]
face_crop = cv2.resize(face_crop,(48,48))
face_crop = cv2.cvtColor(face_crop, cv2.COLOR_BGR2GRAY)
face_crop = face_crop.astype('float32')/255
face_crop = np.asarray(face_crop)
face_crop = face_crop.reshape(1, 1,face_crop.shape[0],face_crop.shape[1])
result = target[np.argmax(model.predict(face_crop))]
cv2.putText(im,result,(x,y), font, 1, (200,0,0), 3, cv2.LINE_AA)
cv2.imshow('result', im)
cv2.imwrite('result.jpg',im)
cv2.waitKey(0)
python类imread()的实例源码
def __init__(self, filename, folder=None, classifier=None):
"""
:param filename: image with sudoku
:param folder: folder where to save debug images
:param classifier: digit classifier
"""
self.filename = os.path.basename(filename)
image = cv2.imread(filename)
self.image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
self.folder = folder or FOLDER
os.mkdir(os.path.join(self.folder, 'debug/'))
self.classifier = classifier or DigitClassifier()
# Default initial values
self.perspective = False
self.debug = True
self.counter = 0
self.step = -1
def encode(img_path, wm_path, res_path, alpha):
img = cv2.imread(img_path)
img_f = np.fft.fft2(img)
height, width, channel = np.shape(img)
watermark = cv2.imread(wm_path)
wm_height, wm_width = watermark.shape[0], watermark.shape[1]
x, y = range(height / 2), range(width)
random.seed(height + width)
random.shuffle(x)
random.shuffle(y)
tmp = np.zeros(img.shape)
for i in range(height / 2):
for j in range(width):
if x[i] < wm_height and y[j] < wm_width:
tmp[i][j] = watermark[x[i]][y[j]]
tmp[height - 1 - i][width - 1 - j] = tmp[i][j]
res_f = img_f + alpha * tmp
res = np.fft.ifft2(res_f)
res = np.real(res)
cv2.imwrite(res_path, res, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
logoPredictor.py 文件源码
项目:vehicle_brand_classification_CNN
作者: nanoc812
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def loadImgs(imgsfolder, rows, cols):
myfiles = glob.glob(imgsfolder+'*.jpg', 0)
nPics = len(myfiles)
X = np.zeros((nPics, rows, cols), dtype = 'uint8')
i = 0; imgNames = []
for filepath in myfiles:
sd = filepath.rfind('/'); ed = filepath.find('.'); filename = filepath[int(sd+1):int(ed)]
imgNames.append(filename)
temp = cv2.imread(filepath, 0)
if temp == None:
continue
elif temp.size < 1000:
continue
elif temp.shape == [rows, cols, 1]:
X[i,:,:] = temp
else:
X[i,:,:] = cv2.resize(temp,(cols, rows), interpolation = cv2.INTER_CUBIC)
i += 1
return X, imgNames
def downscale(old_file_name):
img = cv2.imread(os.path.join(old_file_name))
new_file_name = (old_file_name
.replace('training', 'training_' + str(min_size))
.replace('validation', 'validation_' + str(min_size))
.replace('testing', 'testing_' + str(min_size))
)
height, width, _ = img.shape
if width > height:
new_width = int(1.0 * width / height * min_size)
new_height = min_size
else:
new_height = int(1.0 * height / width * min_size)
new_width = min_size
img_new = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
cv2.imwrite(new_file_name, img_new)
def pull_item(self, index):
img_id = self.ids[index]
target = ET.parse(self._annopath % img_id).getroot()
img = cv2.imread(self._imgpath % img_id)
height, width, channels = img.shape
if self.target_transform is not None:
target = self.target_transform(target, width, height)
if self.transform is not None:
target = np.array(target)
img, boxes, labels = self.transform(img, target[:, :4], target[:, 4])
# to rgb
img = img[:, :, (2, 1, 0)]
# img = img.transpose(2, 0, 1)
target = np.hstack((boxes, np.expand_dims(labels, axis=1)))
return torch.from_numpy(img).permute(2, 0, 1), target, height, width
# return torch.from_numpy(img), target, height, width
def execute_Threshold(proxy,obj):
try: img=obj.sourceObject.Proxy.img.copy()
except: img=cv2.imread(__dir__+'/icons/freek.png')
# img = cv2.imread('dave.jpg',0) ??
img = cv2.medianBlur(img,5)
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
if obj.globalThresholding:
ret,th1 = cv2.threshold(img,obj.param1,obj.param2,cv2.THRESH_BINARY)
obj.Proxy.img = cv2.cvtColor(th1, cv2.COLOR_GRAY2RGB)
if obj.adaptiveMeanTresholding:
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
cv2.THRESH_BINARY,11,2)
obj.Proxy.img = cv2.cvtColor(th2, cv2.COLOR_GRAY2RGB)
if obj.adaptiveGaussianThresholding:
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,17,2)
obj.Proxy.img = cv2.cvtColor(th3, cv2.COLOR_GRAY2RGB)
def loadLogoSet(path, rows,cols,test_data_rate=0.15):
random.seed(612)
_, imgID = readItems('data.txt')
y, _ = modelDict(path)
nPics = len(y)
faceassset = np.zeros((nPics,rows,cols), dtype = np.uint8) ### gray images
noImg = []
for i in range(nPics):
temp = cv2.imread(path +'logo/'+imgID[i]+'.jpg', 0)
if temp == None:
noImg.append(i)
elif temp.size < 1000:
noImg.append(i)
else:
temp = cv2.resize(temp,(cols, rows), interpolation = cv2.INTER_CUBIC)
faceassset[i,:,:] = temp
y = np.delete(y, noImg,0); faceassset = np.delete(faceassset, noImg, 0)
nPics = len(y)
index = random.sample(np.arange(nPics), int(nPics*test_data_rate))
x_test = faceassset[index,:,:]; x_train = np.delete(faceassset, index, 0)
y_test = y[index]; y_train = np.delete(y, index, 0)
return (x_train, y_train), (x_test, y_test)
def test_color():
image = cv2.imread('data/Lenna.png')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
noise = (np.random.rand(image.shape[0], image.shape[1], 3) - 0.5) * 50
image_noise = image + noise
radius = [1, 2, 4]
eps = [0.005]
combs = list(itertools.product(radius, eps))
vis.plot_single(to_32F(image), title='origin')
vis.plot_single(to_32F(image_noise), title='noise')
for r, e in combs:
GF = GuidedFilter(image, radius=r, eps=e)
vis.plot_single(to_32F(GF.filter(image_noise)), title='r=%d, eps=%.3f' % (r, e))
def _get_image_blob(roidb, scale_inds):
"""Builds an input blob from the images in the roidb at the specified
scales.
"""
num_images = len(roidb)
processed_ims = []
im_scales = []
for i in range(num_images):
im = cv2.imread(roidb[i]['image'])
if roidb[i]['flipped']:
im = im[:, ::-1, :]
target_size = cfg.TRAIN.SCALES[scale_inds[i]]
im, im_scale = prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size,
cfg.TRAIN.MAX_SIZE)
im_scales.append(im_scale)
processed_ims.append(im)
# Create a blob to hold the input images
blob = im_list_to_blob(processed_ims)
return blob, im_scales
def get_batch():
ran = random.randint(600, data_size)
#print(ran)
image = []
label = []
label_0 = []
n_pic = ran
# print(n_pic)
for i in range(batch_size * n_steps):
frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic+i), 0)
frame_0 = cv2.resize(frame_0, (LONGITUDE, LONGITUDE))
frame_0 = np.array(frame_0).reshape(-1)
image.append(frame_0)
#print(np.shape(image))
for i in range(batch_size):
frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic + batch_size * (i+1) ), 0)
frame_1 = cv2.resize(frame_1, (LONGITUDE, LONGITUDE))
frame_1 = np.array(frame_1).reshape(-1)
label.append(frame_1)
for i in range(batch_size):
frame_2 = cv2.imread('./cropedoriginalUS2/%d.jpg' % (n_pic + batch_size * (i+1) ), 0)
frame_2 = cv2.resize(frame_2, (LONGITUDE, LONGITUDE))
frame_2 = np.array(frame_2).reshape(-1)
label_0.append(frame_2)
return image , label , label_0
def get_batch(batch_size=20,data_size=6498):
ran = np.random.choice(data_size, batch_size,replace=False)
image=[]
outline=[]
for i in range(batch_size):
n_pic=ran[i]
#print(n_pic)
frame_0 = cv2.imread('./cropPicY/%d.jpg' % n_pic,0)
frame_0 = cv2.resize(frame_0, (24, 24))
frame_0 = np.array(frame_0).reshape(-1)
# print('np',frame_0)
# frame_0 = gray2binary(frame_0)
#print (frame_0)
frame_1 = cv2.imread('./cropPicX/%d.jpg' % n_pic, 0)
frame_1 = cv2.resize(frame_1, (24, 24))
frame_1 = np.array(frame_1).reshape(-1)
frame_1 = gray2binary(frame_1)
image.append(frame_0)
outline.append(frame_1)
#print(image)
return np.array(image),np.array(outline)
def get_train_batch(noise=0):
ran = random.randint(600, data_size)
#print(ran)
image = []
label = []
label_0 = []
n_pic = ran
# print(n_pic)
for i in range(batch_size ):
frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic+i), 0)
frame_0 = add_noise(frame_0, n = noise)
frame_0 = cv2.resize(frame_0, (LONGITUDE, LONGITUDE))
frame_0 = np.array(frame_0).reshape(-1)
image.append(frame_0)
#print(np.shape(image))
for i in range(batch_size):
frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic + batch_size * (i+1) ), 0)
frame_1 = cv2.resize(frame_1, (LONGITUDE, LONGITUDE))
frame_1 = np.array(frame_1).reshape(-1)
label.append(frame_1)
return image , label
def get_batch(batch_size=20,data_size=6498):
ran = np.random.choice(data_size, batch_size,replace=False)
image=[]
for i in range(batch_size):
n_pic=ran[i]
#print(n_pic)
frame_0 = cv2.imread('./cropPicX/%d.jpg' % n_pic,0)
frame_0 = cv2.resize(frame_0, (24, 24))
frame_0 = np.array(frame_0).reshape(-1)
image.append(frame_0)
#print(image)
return np.array(image)
# Visualize decoder setting
# Parameters
1.1.1autoencoder_self.py 文件源码
项目:US-image-prediction
作者: ChengruiWu008
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def get_batch(batch_size=20,data_size=6498):
ran = np.random.choice(data_size, batch_size,replace=False)
image=[]
outline=[]
for i in range(batch_size):
n_pic=ran[i]
#print(n_pic)
frame_0 = cv2.imread('./easyPixelImage2/%d.jpg' % n_pic,0)
frame_0 = cv2.resize(frame_0, (24, 24))
frame_0 = np.array(frame_0).reshape(-1)
# print('np',frame_0)
# frame_0 = gray2binary(frame_0)
#print (frame_0)
frame_1 = cv2.imread('./easyPixelImage2/%d.jpg' % n_pic, 0)
frame_1 = cv2.resize(frame_1, (24, 24))
frame_1 = np.array(frame_1).reshape(-1)
frame_1 = gray2binary(frame_1)
image.append(frame_0)
outline.append(frame_1)
#print(image)
return np.array(image),np.array(outline)
def get_train_batch(noise=500):
ran = np.random.randint(600,5800,size=10,dtype='int')
#print(ran)
image = []
label = []
label_0 = []
n_pic = ran
# print(n_pic)
for i in range(10):
frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
frame_0 = add_noise(frame_0, n = noise)
frame_0 = cv2.resize(frame_0, (24, 24))
frame_0 = np.array(frame_0).reshape(-1)
frame_0 = frame_0 / 255.0
image.append(frame_0)
#print(np.shape(image))
for i in range(10):
frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
frame_1 = cv2.resize(frame_1, (24, 24))
frame_1 = np.array(frame_1).reshape(-1)
frame_1 = gray2binary(frame_1)
label.append(frame_1)
return np.array(image,dtype='float') , np.array(label,dtype='float')
def get_test_batch(noise=500):
ran = np.random.randint(5800,6000,size=10,dtype='int')
#print(ran)
image = []
label = []
label_0 = []
n_pic = ran
# print(n_pic)
for i in range(10):
frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
frame_0 = add_noise(frame_0, n = noise)
frame_0 = cv2.resize(frame_0, (24, 24))
frame_0 = np.array(frame_0).reshape(-1)
frame_0 = frame_0 / 255.0
image.append(frame_0)
#print(np.shape(image))
for i in range(10):
frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
frame_1 = cv2.resize(frame_1, (24, 24))
frame_1 = np.array(frame_1).reshape(-1)
frame_1 = gray2binary(frame_1)
label.append(frame_1)
return np.array(image,dtype='float') , np.array(label,dtype='float')
def get_data(datadir):
#datadir = args.data
# assume each image is 512x256 split to left and right
imgs = glob.glob(os.path.join(datadir, '*.jpg'))
data_X = np.zeros((len(imgs),3,img_cols,img_rows))
data_Y = np.zeros((len(imgs),3,img_cols,img_rows))
i = 0
for file in imgs:
img = cv2.imread(file,cv2.IMREAD_COLOR)
img = cv2.resize(img, (img_cols*2, img_rows))
#print('{} {},{}'.format(i,np.shape(img)[0],np.shape(img)[1]))
img = np.swapaxes(img,0,2)
X, Y = split_input(img)
data_X[i,:,:,:] = X
data_Y[i,:,:,:] = Y
i = i+1
return data_X, data_Y
def gen_image_frames(img_path, fps, duration):
"""Generate frames from a image so that can play like a video.
Parameters
----------
img_path : string
absolute path to the image.
fps : int
frame rates per second
duration : float
duration of the sequence in second
"""
num_frames = int(fps*duration)
img = cv2.imread(img_path)
frames = []
for i in xrange(num_frames):
frames.append(img)
return frames, len(frames)
def _get_image_blob(roidb, scale_inds):
"""Builds an input blob from the images in the roidb at the specified
scales.
"""
num_images = len(roidb)
processed_ims = []
im_scales = []
im_shapes = np.zeros((0, 2), dtype=np.float32)
for i in xrange(num_images):
im = cv2.imread(roidb[i]['image'])
if roidb[i]['flipped']:
im = im[:, ::-1, :]
target_size = cfg.TRAIN.SCALES[scale_inds[i]]
im, im_scale, im_shape = prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size)
im_scales.append(im_scale)
processed_ims.append(im)
im_shapes = np.vstack((im_shapes, im_shape))
# Create a blob to hold the input images
blob = im_list_to_blob(processed_ims)
return blob, im_scales, im_shapes
def get_image_features(self, img_file, stride=5, padding=True):
"""
Take an image file as input, and output an array of image features whose matrix size is
based on the image size. When no padding, and the image size is smaller than the required
feature space size (in x or y direction), the image is not checked, and this method will
return a tuple of two empty lists; When padding is True, and the image size is more than
4 pixels smaller than the require feature space size (in x or y direction), the image is
not checked either. This method can be used by both the trainer and predictor.
Args:
img_file: The file name of the image.
stride: Optional. The stride of the sliding.
padding: Optional. Whether to pad the image to fit the feature space size or to
discard the extra pixels if padding is False.
Returns:
coordinates: A list of coordinates, each of which contains y and x that are the top
left corner offsets of the sliding window.
features: A matrix (python list), in which each row contains the features of the
sampling sliding window, while the number of rows depends on the image size of
the input.
"""
img = cv2.imread(img_file)
img_arr = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return self.get_image_array_features(img_arr, stride, padding)
def s1_predict(config_file, model_dir, model_file, predict_file_list, out_dir):
"""
This function serves as a test/validation tool during the model development. It is not used as
a final product in part of the pipeline.
"""
with open(config_file) as config_buffer:
config = json.loads(config_buffer.read())
with tf.Graph().as_default() as graph:
converted_model = ConvertedModel(config, graph, 's1_keras', model_dir, model_file)
with tf.Session(graph=graph) as sess:
for img_file in predict_file_list:
image = cv2.imread(img_file)
boxes = converted_model.predict(sess, image)
image = draw_boxes(image, boxes)
_, filename = os.path.split(img_file)
cv2.imwrite(os.path.join(out_dir, filename), image)
def get_batch_idx(self, idx):
hh = self.inp_height
ww = self.inp_width
x = np.zeros([len(idx), hh, ww, 3], dtype='float32')
orig_height = []
orig_width = []
ids = []
for kk, ii in enumerate(idx):
fname = self.ids[ii]
ids.append('{:06}'.format(ii))
x_ = cv2.imread(fname).astype('float32') / 255
x[kk] = cv2.resize(
x_, (self.inp_width, self.inp_height),
interpolation=cv2.INTER_CUBIC)
orig_height.append(x_.shape[0])
orig_width.append(x_.shape[1])
pass
return {
'x': x,
'orig_height': np.array(orig_height),
'orig_width': np.array(orig_width),
'id': ids
}
def process_frame(frame_number, frame, keypoint_data, detector, matcher):
log = logging.getLogger("process_frame")
# Create a copy of source frame to draw into
processed = frame.copy()
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
kp, des = detector.detectAndCompute(frame, None)
# Match descriptors
matches = matcher.match(keypoint_data.descriptors, des)
# Sort them in order of distance
matches = sorted(matches, key = lambda x:x.distance)
processed = drawMatches(cv2.imread('car.png',0), keypoint_data.keypoints, gray_frame, kp, matches[:])
return processed
# ============================================================================
def check_image(name):
expected_data = json.loads(open('./img/' + name + '.json').read())
if not expected_data['enabled']:
return
expected_targets = expected_data['targets']
img = cv2.imread('./img/' + name + '.jpg', cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
args = config.copy()
args['img'] = hsv
args['output_images'] = {}
actual_targets = find(**args)
# make sure same number of targets are detected
assert len(expected_targets) == len(actual_targets)
# targets is a list of 2-tuples with expected and actual results
targets = zip(expected_targets, actual_targets)
# compare all the different features of targets to make sure they match
for pair in targets:
expected, actual = pair
# make sure that the targets are close to where they are supposed to be
assert is_close(expected['pos']['x'], actual['pos']['x'], 0.02)
assert is_close(expected['pos']['y'], actual['pos']['y'], 0.02)
# make sure that the targets are close to the size they are supposed to be
assert is_close(expected['size']['width'], actual['size']['width'], 0.02)
assert is_close(expected['size']['height'], actual['size']['height'], 0.02)
def create_composite_image_coin_id(coin_id, crop_dir, data_dir):
images = []
images_gif = []
for id in range(0,56):
image_id = coin_id * 100 + id
crop = ci.get_rotated_crop(crop_dir, image_id, 56, 0)
images.append(crop)
filename = ci.get_filename_from(image_id,crop_dir)
images_gif.append(imageio.imread(filename))
composite_image = ci.get_composite_image(images, 8, 8)
cv2.imwrite(data_dir + str(coin_id) + '.png', composite_image)
imageio.mimsave(data_dir + str(coin_id) + '.gif', images_gif)
return
def get_image_array(roidb, scales, scale_indexes, need_mean=True):
"""
build image array from specific roidb
:param roidb: images to be processed
:param scales: scale list
:param scale_indexes: indexes
:return: array [b, c, h, w], list of scales
"""
num_images = len(roidb)
processed_ims = []
im_scales = []
for i in range(num_images):
im = cv2.imread(roidb[i]['image'])
if roidb[i]['flipped']:
im = im[:, ::-1, :]
target_size = scales[scale_indexes[i]]
im, im_scale = image_processing.resize(im, target_size, config.MAX_SIZE)
im_tensor = image_processing.transform(im, config.PIXEL_MEANS, need_mean=need_mean)
processed_ims.append(im_tensor)
im_scales.append(im_scale)
array = image_processing.tensor_vstack(processed_ims)
return array, im_scales
def _get_image_blob(roidb, scale_inds):
"""Builds an input blob from the images in the roidb at the specified
scales.
"""
num_images = len(roidb)
processed_ims = []
im_scales = []
for i in xrange(num_images):
im = cv2.imread(roidb[i]['image'])
if roidb[i]['flipped']:
im = im[:, ::-1, :]
target_size = cfg.TRAIN.SCALES[scale_inds[i]]
im, im_scale = prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size,
cfg.TRAIN.MAX_SIZE)
im_scales.append(im_scale)
processed_ims.append(im)
# Create a blob to hold the input images
blob = im_list_to_blob(processed_ims)
return blob, im_scales
def imdb_proposals(net, imdb):
"""Generate RPN proposals on all images in an imdb."""
_t = Timer()
imdb_boxes = [[] for _ in xrange(imdb.num_images)]
for i in xrange(imdb.num_images):
im = cv2.imread(imdb.image_path_at(i))
_t.tic()
imdb_boxes[i], scores = im_proposals(net, im)
_t.toc()
print 'im_proposals: {:d}/{:d} {:.3f}s' \
.format(i + 1, imdb.num_images, _t.average_time)
if 0:
dets = np.hstack((imdb_boxes[i], scores))
# from IPython import embed; embed()
_vis_proposals(im, dets[:3, :], thresh=0.9)
plt.show()
return imdb_boxes
def _get_feature_scale(self, num_images=100):
TARGET_NORM = 20.0 # Magic value from traditional R-CNN
_t = Timer()
roidb = self.imdb.roidb
total_norm = 0.0
count = 0.0
inds = npr.choice(xrange(self.imdb.num_images), size=num_images,
replace=False)
for i_, i in enumerate(inds):
im = cv2.imread(self.imdb.image_path_at(i))
if roidb[i]['flipped']:
im = im[:, ::-1, :]
_t.tic()
scores, boxes = im_detect(self.net, im, roidb[i]['boxes'])
_t.toc()
feat = self.net.blobs[self.layer].data
total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum()
count += feat.shape[0]
print('{}/{}: avg feature norm: {:.3f}'.format(i_ + 1, num_images,
total_norm / count))
return TARGET_NORM * 1.0 / (total_norm / count)