1、源码地址:https://github.com/qqwweee/keras-yolo3
2、环境要求:
Python 3.5.2
pillow
matplotlib
opencv_python==3.3.1.11
Keras 2.1.5
tensorflow 1.6.0
3、下载权重文件到根目录
wget https://pjreddie.com/media/files/yolov3.weights
4、模型转换
python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5
安装期间可能出现以下错误
解决办法:pip install --upgrade h5py
转换成功后:
5、测试单张图片
(1)从网上下载一张图片到根目录下
(2)修改yolo_video.py文件
parser.add_argument(
'--image', default=False, action="store_true",
help='Image detection mode, will ignore all positional arguments'
)
将default=False改为default=True
(3)运行yolo_video.py,,如果出现 No module named 'PIL',输入:pip install pillow,结果如下
6、测试视频文件
(1)下载视频小素材,点击下载小样,虽然有水印,但是免费,推荐网址:https://www.vjshi.com/
(2)下载后的视频如果觉得有点大的话,可以使用moviepy对视频进行裁剪
from moviepy.editor import *
def render(input_video, output_video="dog2.mp4"):
# 剪个5s的480x270px的视频
background_clip = VideoFileClip(input_video, target_resolution=(270, 480)).subclip(0, 5)
background_clip.write_videofile(output_video)
if __name__ == '__main__':
input_vid = 'target.mp4'
render(input_vid)
(3)将视频文件放在根目录下,然以下方式修改参数
parser.add_argument(
'--image', default=False, action="store_true",
help='Image detection mode, will ignore all positional arguments'
)
'''
Command line positional arguments -- for video detection mode
'''
parser.add_argument(
"--input", nargs='?', type=str,required=False,default='./dog2.mp4',
help = "Video input path"
)
(4)检测结果
note1:使用opencv分帧保存视频示例
import cv2
cap = cv2.VideoCapture('target.mp4')
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (424, 240))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
note2:视频转gif
import moviepy.editor as mpy
#视频文件的本地路径
content = mpy.VideoFileClip("123.avi")
# 剪辑0秒到2秒的片段。注意:不使用resize则不会修改清晰度
c1 = content.subclip((0,0),(0, 2)).resize((480,270))
# 将片段保存为gif图到python的默认路径,可保存到"C:\Users\Administrator\Desktop"
c1.write_gif("gav.gif")