目标检测keras-ssd之视频检测
在作者原代码基础上,添加了对视频及摄像头的检测,帧数在10左右,在自己的电脑上的话,比yolov3快一些,比tiny慢一些,效果还是不错的。
from keras.layers import Input
from ssd import SSD
import numpy as np
from PIL import Image
import cv2
from timeit import default_timer as timer
ssd = SSD()
while True:
img = input('Input image filename:')
try:
#image = Image.open(img)
vid = cv2.VideoCapture(img)
except:
print('Open Error! Try again!')
continue
else:
video_fps= vid.get(cv2.CAP_PROP_FPS)
accum_time = 0
curr_fps = 0
fps = "FPS: ??"
prev_time = timer()
while True:
#读取每帧
return_value,frame = vid.read()
#frame = cv2.resize(frame,(416,416),interpolation=cv2.INTER_CUBIC)
image = Image.fromarray(frame)
r_image = ssd.detect_image(image)
result = np.asarray(r_image)
curr_time = timer()
exec_time = curr_time - prev_time
prev_time = curr_time
accum_time = accum_time + exec_time
curr_fps = curr_fps + 1
if accum_time > 1:
accum_time = accum_time - 1
fps = "FPS: " + str(curr_fps)
curr_fps = 0
cv2.putText(result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.50, color=(255, 0, 0), thickness=2)
cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
cv2.imshow("result", result)
cv2.waitKey(1)
ssd.close_session()
评论(0)
您还未登录,请登录后发表或查看评论