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()
评论列表
文章目录