YOLOV4视频检测并保存

YOLOV4视频检测并保存

YOLOV4用于视频检测的代码:
读取视频并检测,
显示检测后的结果,
保存检测完后的视频。

from yolo import YOLO
from PIL import Image
import numpy as np
import cv2
import time
yolo = YOLO()
# 调用摄像头
capture=cv2.VideoCapture("mp4/01.mp4") # capture=cv2.VideoCapture("1.mp4") #检测的视频路径

width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))


fps = 10.0

# 创建视频
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('mp4/video_result_01.avi', fourcc=fourcc,fps=fps, frameSize=(width,height)) #保存检测视频的路径

while(True):
    t1 = time.time()
    # 读取某一帧
    ref,frame=capture.read()
    # 格式转变,BGRtoRGB
    frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
    # 转变成Image
    frame = Image.fromarray(np.uint8(frame))

    # 进行检测
    frame = np.array(yolo.detect_image(frame))
    

    # RGBtoBGR满足opencv显示格式
    frame = cv2.cvtColor(frame,cv2.COLOR_RGB2BGR)

    fps  = ( fps + (1./(time.time()-t1)) ) / 2
    print("fps= %.2f"%(fps))
    frame = cv2.putText(frame, "fps= %.2f"%(fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

    cv2.imshow("video",frame) #显示
    
    out.write(frame) #保存视频
    
 
    c= cv2.waitKey(30) & 0xff 
    if c==27:
        capture.release()
        out.release()
        break

你可能感兴趣的:(深度学习,python,计算机视觉)