def predict(the_net,image):
inputs = []
if not os.path.exists(image):
raise Exception("Image path not exist")
return
try:
tmp_input = cv2.imread(image)
tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
tmp_input = tmp_input[11:11+128,11:11+128]
tmp_input = np.subtract(tmp_input,mean)
tmp_input = tmp_input.transpose((2, 0, 1))
tmp_input = np.require(tmp_input, dtype=np.float32)
except Exception as e:
#raise Exception("Image damaged or illegal file format")
return None
the_net.blobs['data'].reshape(1, *tmp_input.shape)
the_net.reshape()
the_net.blobs['data'].data[...] = tmp_input
the_net.forward()
scores = copy.deepcopy(the_net.blobs['feature'].data)
return scores
python类imread()的实例源码
extract_res10.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
extract_emotion.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def predict(the_net,image):
inputs = []
if not os.path.exists(image):
raise Exception("Image path not exist")
return
try:
tmp_input = cv2.imread(image)
tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
tmp_input = tmp_input[13:13+224,13:13+224]
#tmp_input = np.subtract(tmp_input,mean)
tmp_input = tmp_input.transpose((2, 0, 1))
tmp_input = np.require(tmp_input, dtype=np.float32)
except Exception as e:
#raise Exception("Image damaged or illegal file format")
return None
the_net.blobs['data'].reshape(1, *tmp_input.shape)
the_net.reshape()
the_net.blobs['data'].data[...] = tmp_input
the_net.forward()
scores = copy.deepcopy(the_net.blobs['fc6'].data)
return scores
extract_emotion_bak.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def predict(the_net,image):
inputs = []
if not os.path.exists(image):
raise Exception("Image path not exist")
return
try:
tmp_input = cv2.imread(image)
tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
tmp_input = tmp_input[13:13+224,13:13+224]
tmp_input = np.subtract(tmp_input,mean)
tmp_input = tmp_input.transpose((2, 0, 1))
tmp_input = np.require(tmp_input, dtype=np.float32)
except Exception as e:
#raise Exception("Image damaged or illegal file format")
return None
the_net.blobs['data'].reshape(1, *tmp_input.shape)
the_net.reshape()
the_net.blobs['data'].data[...] = tmp_input
the_net.forward()
scores = copy.deepcopy(the_net.blobs['fc6'].data)
return scores
def get_batcher(self, shuffle=True, augment=True):
""" produces batch generator """
w, h = self.resize
if shuffle: np.random.shuffle(self.data)
data = iter(self.data)
while True:
x = np.zeros((self.batch_size, self.timesteps, h, w, 3))
y = np.zeros((self.batch_size, 1))
for b in range(self.batch_size):
images, label = next(data)
for t, img_name in enumerate(images):
image_path = self.folder + 'images/' + img_name
img = cv2.imread(image_path)
img = img[190:350, 100:520] # crop
if augment:
img = aug.augment_image(img) # augmentation
img = cv2.resize(img.copy(), (w, h))
x[b, t] = img
y[b] = label
x = np.transpose(x, [0, 4, 1, 2, 3])
yield x, y
def _get_image_blob(roidb, scale_inds, data_i):
"""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'][data_i])
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_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)
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)
def init_file_path(directory):
"""
Get the image file path array
:param directory: the directory that store images
:return: an array of image file path
"""
paths = []
if not debug:
print "Throwing all gray space images now... this may takes some time.."
for file_name in os.listdir(directory):
# Skip files that is not jpg
file_path = '%s/%s' % (directory, file_name)
if not file_name.endswith('.jpg') or imghdr.what(file_path) is not 'jpeg':
continue
if debug:
paths.append(file_path)
else:
# Throw gray space images, this takes long time if have many images
# TODO: maybe can change to a fast way
img = cv2.imread(file_path, cv2.IMREAD_UNCHANGED)
if len(img.shape) == 3 and img.shape[2] != 1:
paths.append(file_path)
return paths
def store_raw_images():
'''To download images from image-net
(Change the url for different needs of cascades)
'''
neg_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n07942152'
neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()
pic_num = 1
for i in neg_image_urls.split('\n'):
try:
print i
urllib.urlretrieve(i, "neg/" + str(pic_num) + '.jpg')
img = cv2.imread("neg/" + str(pic_num) +'.jpg',
cv2.IMREAD_GRAYSCALE)
resized_image = cv2.resize(img, (100, 100))
cv2.imwrite("neg/" + str(pic_num) + '.jpg', resized_image)
pic_num = pic_num + 1
except:
print "error"
def find_uglies():
'''image-net gives a default image when it's real one is not available,
this is to remove them
'''
for file_type in ['neg']:
for img in os.listdir(file_type):
for ugly in os.listdir('uglies'):
try:
current_image_path = str(file_type) + '/' + str(img)
ugly = cv2.imread('uglies/' + str(ugly))
question = cv2.imread(current_image_path)
if ugly.shape == question.shape and not (np.bitwise_xor(ugly, question).any()):
print "girl you ugly"
os.remove(current_image_path)
except:
print "error"
FeatureExtraction.py 文件源码
项目:SummerProject_MacularDegenerationDetection
作者: WDongYuan
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def GetFeature(image_path):
#MinBlackRate, left_most_pixel_gradiant, hill_number, average_hill_peak, average_hill_valley, BlackRate
boundary_path = image_path.split(".")[0]+"_upper_boundary.txt"
file = open(boundary_path)
tmp_str = file.readline().strip()
tmp_arr = tmp_str.split(" ")
boundary = []
for i in range(len(tmp_arr)):
if tmp_arr[i]!="":
boundary.append(int(tmp_arr[i]))
boundary = np.array(boundary)
file.close()
image = cv2.imread(image_path,cv2.IMREAD_GRAYSCALE)
image = CropLowerBoundary(image)
feature = MinGridBlackRate(image,boundary)+BlackRate(image,boundary)
flag,tmp_feature = CountHill(boundary,image)
if flag==False:
return [False,feature]
feature += tmp_feature
return [True,feature]
def read_images( filenames, domain=None, image_size=64):
images = []
for fn in filenames:
image = cv2.imread(fn)
if image is None:
continue
if domain == 'A':
kernel = np.ones((3,3), np.uint8)
image = image[:, :256, :]
image = 255. - image
image = cv2.dilate( image, kernel, iterations=1 )
image = 255. - image
elif domain == 'B':
image = image[:, 256:, :]
image = cv2.resize(image, (image_size,image_size))
image = image.astype(np.float32) / 255.
image = image.transpose(2,0,1)
images.append( image )
images = np.stack( images )
return images
def train_dir(nets, optim, optim2, dataloader, args):
global image_size, it, image_sizes
caffe.set_mode_gpu()
if args.debug:
image_sizes = [[416, 416]]
while True:
if it % 500 == 0:
image_size = image_sizes[random.randint(0, len(image_sizes) - 1)]
print(image_size)
#im = cv2.imread('/home/busta/data/90kDICT32px/background/n03085781_3427.jpg')
#try:
process_batch(nets, optim, optim2, image_size, args)
if it % valid_interval == 0:
validate(nets, dataloader, image_size = [416, 416], split_words=False)
#except:
# continue
def __init__(self, g_pool,selected_watermark_path = None,pos = (20,20)):
super().__init__(g_pool)
self.order = .9
self.menu = None
available_files = glob(os.path.join(self.g_pool.user_dir,'*png')) #we only look for png's
self.available_files = [f for f in available_files if cv2.imread(f,-1).shape[2]==4] #we only look for rgba images
logger.debug('Found {} watermark files: {}'.format(len(self.available_files), self.available_files))
self.watermark = None
self.watermark_path = None
self.alpha_mask = None
if selected_watermark_path in self.available_files:
self.load_watermark(selected_watermark_path)
elif self.available_files:
self.load_watermark(self.available_files[0])
else:
logger.warning("No .png files found. Make sure they are in RGBA format.")
self.pos = list(pos) #if we make the default arg a list the instance will edit the default vals and a new call of the class constructor creates an ainstace with modified default values.
self.move_watermark = False
self.drag_offset = None
def color_quant(input,K,output):
img = cv2.imread(input)
Z = img.reshape((-1,3))
# convert to np.float32
Z = np.float32(Z)
# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 15, 1.0)
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
cv2.imshow('res2',res2)
cv2.waitKey(0)
cv2.imwrite(output, res2)
cv2.destroyAllWindows()
utils.py 文件源码
项目:kaggle-dstl-satellite-imagery-feature-detection
作者: u1234x1234
项目源码
文件源码
阅读 17
收藏 0
点赞 0
评论 0
def get_rgb_data(img_id):
image_path = three_band_path + '/' + img_id + '.tif'
image = tiff.imread(image_path)
image = image.transpose((1, 2, 0))
image = image.astype(np.float32)
return image
def decode(ori_path, img_path, res_path, alpha):
ori = cv2.imread(ori_path)
img = cv2.imread(img_path)
ori_f = np.fft.fft2(ori)
img_f = np.fft.fft2(img)
height, width = ori.shape[0], ori.shape[1]
watermark = (ori_f - img_f) / alpha
watermark = np.real(watermark)
res = np.zeros(watermark.shape)
random.seed(height + width)
x = range(height / 2)
y = range(width)
random.shuffle(x)
random.shuffle(y)
for i in range(height / 2):
for j in range(width):
res[x[i]][y[j]] = watermark[i][j]
cv2.imwrite(res_path, res, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
def plot_face_bb(p, bb, scale=True, path=True, plot=True):
if path:
im = cv2.imread(p)
else:
im = cv2.cvtColor(p, cv2.COLOR_RGB2BGR)
if scale:
h, w, _ = im.shape
cv2.rectangle(im, (int(bb[0] * h), int(bb[1] * w)),
(int(bb[2] * h), int(bb[3] * w)),
(255, 255, 0), thickness=4)
# print bb * np.asarray([h, w, h, w])
else:
cv2.rectangle(im, (int(bb[0]), int(bb[1])), (int(bb[2]), int(bb[3])),
(255, 255, 0), thickness=4)
print "no"
if plot:
plt.figure()
plt.imshow(im[:, :, ::-1])
else:
return im[:, :, ::-1]