检测视频或摄像头中的目标

注:调用 yolo v4 的模型实现的,下面代码仅是读取视频并将格式转换为可传入模型的格式。

#-------------------------------------#
#       调用摄像头/视频检测
#-------------------------------------#
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)
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(frame)
    # frame = np.array(frame[0])
    # 转换为np格式
    frame = np.array(frame)
    # RGBtoBGR满足opencv显示格式
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    cv2.imshow("video", frame)
    c= cv2.waitKey(30) & 0xff
    if c == 27:
        capture.release()
        break

yolo.close_session()

你可能感兴趣的:(检测视频或摄像头中的目标)