#-------------------------------------#
# 调用摄像头检测/读取视频检测
#-------------------------------------#
from keras.layers import Input
from yolo import YOLO
from PIL import Image
import numpy as np
import cv2
yolo = YOLO()
# 调用摄像头
# capture=cv2.VideoCapture(1) # capture=cv2.VideoCapture("1.mp4")
# 读取视频
capture = cv2.VideoCapture("./img/baiyan.mp4")
print(capture)
# 设置帧率
fps = 5
# 获取窗口大小
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# 调用VideoWrite()函数
videoWrite = cv2.VideoWriter('MySaveVideo.avi', cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)
while(True):
# 读取某一帧
ref, frame = capture.read()
# 格式转变,BGRtoRGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 转变成Image
frame = Image.fromarray(np.uint8(frame))
# 进行检测
frame = yolo.detect_image_video(frame)
frame = np.array(frame[0])
# RGBtoBGR满足opencv显示格式
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
videoWrite.write(frame)
cv2.imshow("video", frame)
c = cv2.waitKey(5) & 0xff
if c == 5:
capture.release()
break
yolo.close_session()
目标检测视频,并保存识别结果
最新推荐文章于 2024-08-05 08:58:21 发布