python-----opencv读视频循环读图片显示进度条

Posted 小呆丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-----opencv读视频循环读图片显示进度条相关的知识,希望对你有一定的参考价值。

功能: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)

 

以上是关于python-----opencv读视频循环读图片显示进度条的主要内容,如果未能解决你的问题,请参考以下文章

文件处理

Python-复习-文件操作-21

python+opencv选出视频中一帧再利用鼠标回调实现图像上画矩形框

#导入MD文档图片#超好玩的Python3工具--利用OpenCV视频转字符动画

python学习day9

Python OpenCV 从视频保存图片