分类: Python/Ruby
2021-07-30 17:24:34
import numpy as np
import cv2
import time
start_brightness = 0.1
count_keyu = 0
count_keyd = 0
count_keyz = 0
count_keyj = 0
delay_time = 0
#——————————————————————————————播放视频——————————————————————
#————————添加自己的视频播放路径———————————
video_path="D:/python_test/opao_timeX0.1.mp4"
# 创建一个视频读写类
video_capture=cv2.VideoCapture(video_path)
#读取视频的fps, 大小 每秒传输帧数
fps=video_capture.get(cv2.CAP_PROP_FPS)
size=(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH),video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
#print("fps: {}\nsize: {}".format(fps,size))
#读取视频时长(帧总数)
total = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
#print("[INFO] {} total frames in video".format(total))
#设定从视频的第几帧开始读取
#From : https://blog.csdn.net/luqinwei/article/details/87973472
frameToStart = 00
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frameToStart);
#显示视频
current_frame=frameToStart
while True:
success, frame = video_capture.read()
time.sleep(0.0267)
# cv2.waitKey(40)
if success == False:
break
#设置延时,降低播放速度
# time.sleep(0.0188)
#自定义图像大小
h, w = frame.shape[:2] # 三通道
size = (int(w * 1.0), int(h * 1.0))
frame = cv2.resize(frame, size)
#显示当前视频已播放时间和总时间
#计算当前时间
now_seconds=int(current_frame /fps%60)
now_minutes=int(current_frame/fps/60)
total_second=int(total /fps%60)
total_minutes=int(total/fps/60)
# { <参数序号> : <填充> <对齐)> <宽度> <,> <.精度> <类型>}.
Time_now_vs_total="Time:{:>3}:{:>02}|{:>3}:{:0>2}".format(now_minutes,now_seconds,total_minutes,total_second)
print(Time_now_vs_total)
#--------键盘控制视频---------------
#读取键盘值
key = cv2.waitKey(1) & 0xff
#按键z增长延时,j减少延时。“zeng”、“jian”,实现在增长到值开始建
if key == ord("z"):
count_keyz += 1
delay_time = 0 + 0.02 * count_keyz
if(delay_time>1.0):
delay_time = 1.0
if key == ord("j"):
count_keyj += 1
delay_time = delay_time - 0.02 * count_keyj
if(delay_time<0):
delay_time = 0
time.sleep(delay_time)
# print(delay_time)
time.sleep(delay_time)
#设置空格按下时暂停
if key == ord(" "):
cv2.waitKey(1)
#设置Q按下时退出
if key == ord("q"):
break
#按下U,上升计数器加1,表示亮度增加,即UP
if key == ord("u"):
count_keyu +=1
# print(count_keyu)
brightness = start_brightness + count_keyu * 0.3
# print(brightness)
if(brightness>2):
brightness = 1.9
count_keyu = 0
#确保brightness的结果是保留一位小数,计算时经常出现0.00000000000000009或0.00000000000000001的误差
brightness_str =format(brightness, '.1f')
new_video_path = video_path[:25]+brightness_str+'.mp4'
video_capture = cv2.VideoCapture(new_video_path)
frameToStart = now_seconds * fps
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frameToStart);
print(new_video_path)
#按下D,下降计数器加1,表示亮度下调,即down
if key == ord("d"):
count_keyd += 1
brightness = start_brightness + count_keyu * 0.3 - count_keyd * 0.3 #表示亮度的变量
if(brightness
brightness = 0.1
count_keyd = 0
#确保brightness的结果是保留一位小数,计算时经常出现0.00000000000000009或0.00000000000000001的误差
brightness_str = format(brightness, '.1f')
new_video_path = video_path[:25]+brightness_str+'.mp4'
video_capture=cv2.VideoCapture(new_video_path)
frameToStart = now_seconds * fps
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frameToStart);
# print(new_video_path)
# putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None):
cv2.putText(frame,Time_now_vs_total,(300,50),cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,255),3)
cv2.imshow("frame",frame)
#人工对视频帧数进行计数
current_frame += 1
video_capture.release()