video.py 1.1 KB
Newer Older
J
JiaQi Xu 已提交
1 2 3 4 5 6 7 8
#-------------------------------------#
#       调用摄像头检测
#-------------------------------------#
from keras.layers import Input
from yolo import YOLO
from PIL import Image
import numpy as np
import cv2
J
JiaQi Xu 已提交
9
import time
J
JiaQi Xu 已提交
10 11
yolo = YOLO()
# 调用摄像头
J
JiaQi Xu 已提交
12
capture=cv2.VideoCapture(0) # capture=cv2.VideoCapture("1.mp4")
J
JiaQi Xu 已提交
13

J
JiaQi Xu 已提交
14
fps = 0.0
J
JiaQi Xu 已提交
15
while(True):
J
JiaQi Xu 已提交
16
    t1 = time.time()
J
JiaQi Xu 已提交
17 18 19 20 21 22 23 24 25 26 27 28
    # 读取某一帧
    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)
J
JiaQi Xu 已提交
29 30 31 32 33
    
    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)
    
J
JiaQi Xu 已提交
34
    cv2.imshow("video",frame)
B
Bubbliiiing 已提交
35
    c= cv2.waitKey(1) & 0xff 
J
JiaQi Xu 已提交
36 37 38 39
    if c==27:
        capture.release()
        break

J
JiaQi Xu 已提交
40
yolo.close_session()