def resize(im, target_size, max_size):
"""
only resize input image to target size and return scale
:param im: BGR image input by opencv
:param target_size: one dimensional size (the short side)
:param max_size: one dimensional max size (the long side)
:return:
"""
im_shape = im.shape
im_size_min = np.min(im_shape[0:2])
im_size_max = np.max(im_shape[0:2])
im_scale = float(target_size) / float(im_size_min)
if np.round(im_scale * im_size_max) > max_size:
im_scale = float(max_size) / float(im_size_max)
im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR)
return im, im_scale
python类resize()的实例源码
predict_video_res10.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def predict(image,the_net):
inputs = []
try:
tmp_input = 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
the_net.blobs['data'].reshape(1, *tmp_input.shape)
the_net.reshape()
the_net.blobs['data'].data[...] = tmp_input
the_net.forward()
scores = the_net.blobs['prob'].data[0]
return copy.deepcopy(scores)
extract_res10.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 33
收藏 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[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
test_vgg.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def predict(image,the_net):
inputs = []
try:
tmp_input = 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
the_net.blobs['data'].reshape(1, *tmp_input.shape)
the_net.reshape()
the_net.blobs['data'].data[...] = tmp_input
the_net.forward()
scores = the_net.blobs['prob'].data[0]
return copy.deepcopy(scores)
test_res10.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def predict(image,the_net):
inputs = []
try:
tmp_input = 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
the_net.blobs['data'].reshape(1, *tmp_input.shape)
the_net.reshape()
the_net.blobs['data'].data[...] = tmp_input
the_net.forward()
scores = the_net.blobs['prob'].data[0]
return copy.deepcopy(scores)
extract_emotion.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 22
收藏 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
predict_video.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def predict(image,the_net):
inputs = []
try:
tmp_input = 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
the_net.blobs['data'].reshape(1, *tmp_input.shape)
the_net.reshape()
the_net.blobs['data'].data[...] = tmp_input
the_net.forward()
scores = the_net.blobs['prob'].data[0]
return copy.deepcopy(scores)
test_afew_face_vgg.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def predict(image,the_net):
inputs = []
try:
tmp_input = 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
the_net.blobs['data'].reshape(1, *tmp_input.shape)
the_net.reshape()
the_net.blobs['data'].data[...] = tmp_input
the_net.forward()
scores = the_net.blobs['prob'].data[0]
return copy.deepcopy(scores)
extract_emotion_bak.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 25
收藏 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 __init__(self,
folder:str,
resize:(int, int),
batch_size:int,
timesteps:int,
windowsteps:int,
shift:int,
train:bool):
self.folder = folder
self.resize = resize
self.batch_size = batch_size
self.timesteps = timesteps
self.train = train
self.images = sorted(os.listdir(folder + 'images/'))
self.labels = open(folder + 'labels.txt').readlines()
self.data = self._sliding_window(self.images, shift, windowsteps)
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 format_img(img, C):
img_min_side = float(C.im_size)
(height,width,_) = img.shape
if width <= height:
f = img_min_side/width
new_height = int(f * height)
new_width = int(img_min_side)
else:
f = img_min_side/height
new_width = int(f * width)
new_height = int(img_min_side)
fx = width/float(new_width)
fy = height/float(new_height)
img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
img = img[:, :, (2, 1, 0)]
img = img.astype(np.float32)
img[:, :, 0] -= C.img_channel_mean[0]
img[:, :, 1] -= C.img_channel_mean[1]
img[:, :, 2] -= C.img_channel_mean[2]
img /= C.img_scaling_factor
img = np.transpose(img, (2, 0, 1))
img = np.expand_dims(img, axis=0)
return img, fx, fy
def prep_im_for_blob(im, pixel_means, target_size, max_size):
"""Mean subtract and scale an image for use in a blob."""
im = im.astype(np.float32, copy=False)
im -= pixel_means
im = im / 127.5
im_shape = im.shape
im_size_min = np.min(im_shape[0:2])
im_size_max = np.max(im_shape[0:2])
im_scale = float(target_size) / float(im_size_min)
# Prevent the biggest axis from being more than MAX_SIZE
if np.round(im_scale * im_size_max) > max_size:
im_scale = float(max_size) / float(im_size_max)
im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale,
interpolation=cv2.INTER_LINEAR)
return im, im_scale
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 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 _gene_signature(self,wm,size,key):
'''????????????????????????'''
wm = cv2.resize(wm,(size,size))
wU,_,wV = np.linalg.svd(np.mat(wm))
sumU = np.sum(np.array(wU),axis=0)
sumV = np.sum(np.array(wV),axis=0)
sumU_mid = np.median(sumU)
sumV_mid = np.median(sumV)
sumU=np.array([1 if sumU[i] >sumU_mid else 0 for i in range(len(sumU)) ])
sumV=np.array([1 if sumV[i] >sumV_mid else 0 for i in range(len(sumV)) ])
uv_xor=np.logical_xor(sumU,sumV)
np.random.seed(key)
seq=np.random.randint(2,size=len(uv_xor))
signature = np.logical_xor(uv_xor, seq)
sqrts = int(np.sqrt(size))
return np.array(signature,dtype=np.int8).reshape((sqrts,sqrts))
def _gene_signature(self,wm,key):
'''????????????????????????'''
wm = cv2.resize(wm,(256,256))
wU,_,wV = np.linalg.svd(np.mat(wm))
sumU = np.sum(np.array(wU),axis=0)
sumV = np.sum(np.array(wV),axis=0)
sumU_mid = np.median(sumU)
sumV_mid = np.median(sumV)
sumU=np.array([1 if sumU[i] >sumU_mid else 0 for i in range(len(sumU)) ])
sumV=np.array([1 if sumV[i] >sumV_mid else 0 for i in range(len(sumV)) ])
uv_xor=np.logical_xor(sumU,sumV)
np.random.seed(key)
seq=np.random.randint(2,size=len(uv_xor))
signature = np.logical_xor(uv_xor, seq)
return np.array(signature,dtype=np.int8)
def create_heatmaps(img, pred):
"""
Uses objectness probability to draw a heatmap on the image and returns it
"""
# find anchors with highest prediction
best_pred = np.max(pred[..., 0], axis=-1)
# convert probabilities to colormap scale
best_pred = np.uint8(best_pred * 255)
# apply color map
# cv2 colormaps create BGR, not RGB
cmap = cv2.cvtColor(cv2.applyColorMap(best_pred, cv2.COLORMAP_JET), cv2.COLOR_BGR2RGB)
# resize the color map to fit image
cmap = cv2.resize(cmap, img.shape[1::-1], interpolation=cv2.INTER_NEAREST)
# overlay cmap with image
return cv2.addWeighted(cmap, 1, img, 0.5, 0)
def swap(self,head_name,face_path):
'''
??? ????
head_name?
?????????
face_path:
?????????
'''
im_head,landmarks_head,im_face,landmarks_face=self.resize(*self.heads[head_name],*self.read_and_mark(face_path))
M = self.transformation_from_points(landmarks_head[self.ALIGN_POINTS],
landmarks_face[self.ALIGN_POINTS])
face_mask = self.get_face_mask(im_face, landmarks_face)
warped_mask = self.warp_im(face_mask, M, im_head.shape)
combined_mask = np.max([self.get_face_mask(im_head, landmarks_head), warped_mask],
axis=0)
warped_face = self.warp_im(im_face, M, im_head.shape)
warped_corrected_im2 = self.correct_colours(im_head, warped_face, landmarks_head)
out=im_head * (1.0 - combined_mask) + warped_corrected_im2 * combined_mask
return out
def detect(img):
img_h, img_w, _ = img.shape
inputs = cv2.resize(img, (settings.image_size, settings.image_size))
inputs = cv2.cvtColor(inputs, cv2.COLOR_BGR2RGB).astype(np.float32)
inputs = (inputs / 255.0) * 2.0 - 1.0
inputs = np.reshape(inputs, (1, settings.image_size, settings.image_size, 3))
result = detect_from_cvmat(inputs)[0]
print result
for i in range(len(result)):
result[i][1] *= (1.0 * img_w / settings.image_size)
result[i][2] *= (1.0 * img_h / settings.image_size)
result[i][3] *= (1.0 * img_w / settings.image_size)
result[i][4] *= (1.0 * img_h / settings.image_size)
return result