python opencv 读取图片和视频

x33g5p2x  于2021-11-19 转载在 Python  
字(0.6k)|赞(0)|评价(0)|浏览(331)

可以同时读取图片或者视频: 

from natsort import natsorted
import cv2
import glob
import numpy as np
if __name__ == '__main__':

    path ="aaaa.mp4"

    if "mp4" in path:
        vid_path =path
        cap =cv2.VideoCapture(path)
    else:
        vid_path =r'/data/imgs/'

        files =glob.glob(vid_path +"*.jpg")
        files =natsorted(files)
    track_total=0
    while True:
        track_total+=1
        if "weilan2" in vid_path:
            ret, img_raw = cap.read()
            img_raw = img_raw[300:-300, :]

        elif "yuexian" in vid_path:
            ret, img_raw = cap.read()  # img_raw=img_raw[:800,:]
        else:
            image_name = files[track_total]
            img_raw = cv2.imread(image_name)
        if img_raw is None:
            break

相关文章