opencv 如何从始终运行的视频中每隔n秒捕获一次图像?

wn9m85ua  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(131)

我想捕获图像并存储在我的本地系统后,每隔n秒,我不能设置帧(5),因为我想视频和检测运行完全。目前我的代码是捕获图像时,条件失败。

def create_alert(self):
        count = 0
        cap = cv2.VideoCapture(0)
        while cap.isOpened():
            r,f = cap.read()
            try:
                info = ppe.detection(f)
                x,y,w,h,label,conf = info[0]
                if label == "lineman_fail":
                    # engine.say("Warning")
                    # engine.runAndWait()

                    ppe.take_screenshot(f,count)
                    count+=1
                    print(count)
                    print("Something wrong")

                    # cv2.imwrite("img_"+str(count)+".jpg",f)

            except Exception as e:
                print("_______-",e)

            cv2.imshow("image",f)

            if cv2.waitKey(1) & 0xFF == ord("q") :
                break

        cap.release()
        cv2.destroyAllWindows()

    def take_screenshot(self,frame,count):
        prev = time.time()
        cv2.imwrite("screen_shot/img_"+str(count)+".jpg",frame)
s3fp2yjn

s3fp2yjn1#

为了每隔n秒捕获一次图像,可以尝试使用datetime库,并找出当前时间和经过时间之间的差值,然后使用cv2的imwrite

while True:
        current = time()

        yolo_v4.delta += current - previous
        previous = current
        frame = camera.get_frame()

if yolo_v4.delta > 10:
    
    ct=datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
    
    cv2.imwrite("screen_shot/img_"+str(ct)+".jpg",frame)

相关问题