Fork me on GitHub

rtsp opencv抽取图片小工具

import time
import cv2


class CaptView(object):
    def __init__(self, url, name, delay):
        self.url = url
        self.name = name
        self.capture = cv2.VideoCapture(url)
        self.fps = max(int(self.capture.get(cv2.CAP_PROP_FPS) % 100), 0) or 25
        self.delay = delay
        self.frame_count = 0
        self.start_time = time.time()
    def reconnect(self):
        self.capture = cv2.VideoCapture(self.url)

    def start_show(self):
        while self.capture.isOpened():
            self.frame_count += 1
            self.capture.grab()
            status, frame = self.capture.retrieve()
            current_time = time.time()
            current_delay = self.frame_count > (current_time - self.start_time - self.delay) * self.fps
            print("起始时间戳:", self.start_time, " 当前时间:", current_time, " 延迟要求:", self.delay, " 当前fps:", self.fps)
            if status and current_delay:
                self.cv_show(self.name, frame, 1)
            else:
                self.frame_count = 0
                self.capture.release()
                self.reconnect()
                print("【INFO】重新链接....")
            time.sleep(1 / self.fps)

    def cv_show(self, name, image, interval, half=0.25):
        img = cv2.resize(image, (0, 0), fx=half, fy=half)
        cv2.imshow(name, img)
        cv2.waitKey(interval)
        cv2.destroyWindow(name)

if __name__ == '__main__':
    capture = cv2.VideoCapture("rtsp://admin:***@192.***.110.64:554",)
    status, frame = capture.read()
    res = cv2.imencode('.jpg', frame)[1].tofile(r"D:\demo\1.jpg")

posted @ 2021-11-25 15:43  是阿凯啊  阅读(118)  评论(0编辑  收藏  举报