1. 程式人生 > >python-----opencv讀視頻、循環讀圖片顯示進度條

python-----opencv讀視頻、循環讀圖片顯示進度條

edwin 圖片 not list ges lag msh imp class

功能: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-CV\video_wang\test/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-CV\video_wang\test\JPEGImages"
    frames = len(os.listdir(img_path))
    jindu_imgpath(img, frames)

python-----opencv讀視頻、循環讀圖片顯示進度條