功能:opencv读视频,显示进度条,推动进度条快进、后退,按q退出。代码如下:
import os import cv2 def nothing(emp): pass def jindu(name,video): cv2.namedWindow(name,0) cv2.resizeWindow(name, 800, 600) cap = cv2.VideoCapture(video) frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) loop_flag = 0 pos = 0 cv2.createTrackbar('time', name, 0, frames, nothing) while 1: if loop_flag == pos: loop_flag = loop_flag + 1 cv2.setTrackbarPos('time', name, loop_flag) print('loop_flag1', loop_flag) else: pos = cv2.getTrackbarPos('time', name) loop_flag = pos cap.set(cv2.CAP_PROP_POS_FRAMES, pos) print('loop_flag2', loop_flag) ret, img = cap.read() cv2.imshow(name, img) if cv2.waitKey(1) & 0xFF == ord('q'): #按q退出 cv2.waitKey(0) if __name__ == '__main__': video = r"D:Team-CVvideo_wang est/1.MOV" name = video.split('.')[-1] jindu(name, video)
功能:opencv循环读图片,显示进度条,推动进度条快进、后退,按q退出。代码如下:
import os import cv2 def nothing(emp): pass def jindu_imgpath(name,frames): cv2.namedWindow(name,0) cv2.resizeWindow(name, 800, 600) loop_flag = 0 pos = 0 cv2.createTrackbar('time', name, 0, frames, nothing) path_list = os.listdir(img_path) for m, img in enumerate(path_list): img_file = img_path + '/' + img img_new = cv2.imread(img_file) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.waitKey(0) if loop_flag == pos: loop_flag = loop_flag + 1 cv2.setTrackbarPos('time', name, loop_flag) else: pos = cv2.getTrackbarPos('time', name) loop_flag = pos img = path_list[loop_flag] img_file = img_path + '/' + img img_new = cv2.imread(img_file) cv2.imshow(name, img_new) if __name__ == '__main__': img_path = r"D:Team-CVvideo_wang estJPEGImages" frames = len(os.listdir(img_path)) jindu_imgpath('img', frames)