-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.py
46 lines (39 loc) · 1.32 KB
/
video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#-------------------------------------#
# 调用摄像头或者视频进行检测
# 调用摄像头直接运行即可
# 调用视频可以将cv2.VideoCapture()指定路径
#-------------------------------------#
import time
import cv2
import numpy as np
from PIL import Image
from yolo import YOLO
yolo = YOLO()
#-------------------------------------#
# 使用本地视频文件
capture=cv2.VideoCapture("2.mp4")
#-------------------------------------#
#调用摄像头
# capture=cv2.VideoCapture(0)
fps = 0.0
while(True):
t1 = time.time()
# 读取某一帧,ref是否采集到图片,frame获取的每一帧图片
ref,frame=capture.read()
# 格式转变,BGRtoRGB
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
# 转变成Image
frame = Image.fromarray(np.uint8(frame))
# 进行检测
frame,boxes,predicted_class=yolo.detect_image(frame)
frame = np.array(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)
c= cv2.waitKey(1) & 0xff
if c==27:
capture.release()
break