def extract_image_features(img_path):
model = models.VGG_16('weights/vgg16_weights.h5')
img = image.load_img(img_path,target_size=(224,224))
x = image.img_to_array(img)
x = np.expand_dims(x,axis=0)
x = preprocess_input(x)
last_layer_output = K.function([model.layers[0].input,K.learning_phase()],
[model.layers[-1].input])
features = last_layer_output([x,0])[0]
return features
python类preprocess_input()的实例源码
def preprocess_image(image_path):
img = load_img(image_path, target_size=(img_nrows, img_ncols))
img = img_to_array(img)
img = np.expand_dims(img, axis=0)
img = vgg16.preprocess_input(img)
return img
# util function to convert a tensor into a valid image
def testonefile(testmodel, img_path, confid_thresh=0.3, fordebug=False ):
(s,w,h,c) = testmodel.layers[0].input_shape
fimg, sx, sy, dx, dy, flip,ssx,ssy = genregiontruth_bnum.crop_image(img_path.strip(), w, h, randomize=False)
xx = fimg.copy()
img = fimg.astype(float)
if fordebug: # read label
fn=img_path.replace("/JPEGImages/","/labels/")
fn=fn.replace(".jpg",".txt") #VOC
boxlist = genregiontruth_bnum.readlabel(fn.strip(), sx, sy, dx, dy, flip, ssx, ssy)
for box in boxlist:
draw.rectangle([(box.x-box.w/2)*w,(box.y-box.h/2)*h,(box.x+box.w/2)*w,(box.y+box.h/2)*h])
#
ttimg, x0_list, y0_list, x1_list, y1_list, classprob_list, class_id_list, confid_value_list = predict(preprocess_input(np.asarray([xx])), testmodel, confid_thresh,w,h,c)
iimg = Image.fromarray(img.astype(np.uint8))
draw = ImageDraw.Draw(iimg, 'RGBA')
sortedindexlist = np.argsort(confid_value_list)
colors=[]
for i in range(3):
for j in range(7):
if i==0:
rcolor = (j+1)*32
gcolor = 0
bcolor = 0
elif i==1:
rcolor = 0
gcolor = (j+1)*32
bcolor = 0
else:
rcolor = 0
gcolor = 0
bcolor = (j+1)*32
colors.append( (rcolor, gcolor, bcolor, 127) )
#print colors
for i in range(len(confid_value_list)):
index = sortedindexlist[len(confid_value_list)-i-1]
for k in range(5): # thick line
draw.rectangle([x0_list[index]+k,y0_list[index]+k,x1_list[index]-k,y1_list[index]-k], outline=colors[class_id_list[index]])
labelim = Image.open(cfgconst.label_names[class_id_list[index]])
draw.bitmap((x0_list[index],y0_list[index]),labelim)
x = (x0_list[index]+x1_list[index])/2.
y = (y0_list[index]+y1_list[index])/2.
x0 = int(x/w*cfgconst.side)*w/cfgconst.side
y0 = int(y/h*cfgconst.side)*h/cfgconst.side
x1 = x0 + float(w)/cfgconst.side
y1 = y0 + float(h)/cfgconst.side
draw.rectangle([x0,y0,x1,y1], fill=colors[class_id_list[index]] )
print cfgconst.label_names[class_id_list[index]].split('/')[1].split('.')[0]+': '+str(confid_value_list[index])
del draw
iimg.save('predicted.png')
def testvideo(testmodel, videofile, confid_thresh=0.2):
print 'testdemo '+videofile
#testmodel = load_model(model_weights_path, custom_objects={'yololoss': ddd.yololoss})
(s,w,h,c) = testmodel.layers[0].input_shape
cap = cv2.VideoCapture(videofile)
while (cap.isOpened()):
ret, frame = cap.read()
if not ret:
break
#print frame
nim = scipy.misc.imresize(frame, (w, h, c))
img = nim
xx = image.img_to_array(cv2.cvtColor(nim, cv2.COLOR_RGB2BGR))
ttimg, x0_list, y0_list, x1_list, y1_list, classprob_list, class_id_list, confid_value_list = predict(preprocess_input(np.asarray([xx])), testmodel, confid_thresh,w,h,c)
# found confid box
for x0,y0,x1,y1,classprob,class_id,confid_value in zip(x0_list, y0_list, x1_list, y1_list, classprob_list, class_id_list, confid_value_list):
#
# draw bounding box
cv2.rectangle(img, (x0, y0), (x1, y1), (255,255,255), 2)
# draw classimg
classimg = cv2.imread(cfgconst.label_names[class_id])
if y0-classimg.shape[0] <= 0:
yst =0
yend =classimg.shape[0]
elif y0 >= img.shape[0]:
yst = img.shape[0]-classimg.shape[0]-1
yend = img.shape[0]-1
else:
yst = y0 - classimg.shape[0]
yend = y0
if x0+classimg.shape[1] >= img.shape[1]:
xst = img.shape[1]-classimg.shape[1]-1
xend = img.shape[1]-1
elif x0 <=0:
xst = 0
xend = classimg.shape[1]
else:
xst = x0
xend = x0+classimg.shape[1]
#
img[yst:yend, xst:xend] = classimg
# draw text
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, str(classprob), (x0,y0+classimg.shape[0]-1), font, 0.5,(255,255,255),2,cv2.LINE_AA)
cv2.putText(img, str(confid_value), (x0,y1), font, 0.5,(128,255,255),1,cv2.LINE_AA)
#
cv2.imshow('frame',img)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()