(365)
(8)
(130)
(155)
(50)
(22)
分类: 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`)
now_minutes=int(current_frame/fps/60)
total_second=int(total /fps`)
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()