windows xp上的Opencv VideoCapture崩溃

2024-03-19 08:42:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在通过oracle VirtualBox将windowsxp(sp3,32位)作为VM运行。在

我试图用opencv2播放一个视频文件,但是每当我调用cap=VideoCapture(url)时,它就会崩溃(windowsxp的send和dont send报告弹出窗口)。在

opencv2版本是3.1.0 python版本是3.4 windows xp sp3 32位。在

有人知道吗?在

代码写在下面。在

from PIL import Image, ImageTk
import tkinter as tk
import argparse
import datetime
import cv2
import os

class Application:
    def __init__(self, output_path = "./"):
        """ Initialize application which uses OpenCV + Tkinter. It displays
            a video stream in a Tkinter window and stores current snapshot on disk """
        self.vs = cv2.VideoCapture('images/5a121d1ede80980cf28c8eb50674f661.mp4') # capture video frames, 0 is your default video camera
        self.output_path = output_path  # store output path
        self.current_image = None  # current image from the camera

        self.root = tk.Tk()  # initialize root window
        self.root.title("PyImageSearch PhotoBooth")  # set window title

        # self.destructor function gets fired when the window is closed
        self.root.attributes("-fullscreen", True)

        # getting size to resize! 30 - space for button
        self.size = (self.root.winfo_screenwidth(), self.root.winfo_screenheight() - 30)

        self.panel = tk.Label(self.root)  # initialize image panel
        self.panel.pack(fill='both', expand=True)

        # create a button, that when pressed, will take the current frame and save it to file

        # start a self.video_loop that constantly pools the video sensor
        # for the most recently read frame
        # init frames
        self.frames = self.resize_video()
        self.video_loop()

    def resize_video(self):
        temp = list()
        try:
            temp_count_const = cv2.CAP_PROP_FRAME_COUNT
        except AttributeError:
            temp_count_const = cv2.cv.CV_CAP_PROP_FRAME_COUNT

        frames_count = self.vs.get(temp_count_const)

        while self.vs.isOpened():
            ok, frame = self.vs.read()  # read frame from video stream
            if ok:  # frame captured without any errors
                cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)  # convert colors from BGR to RGBA
                cv2image = cv2.resize(cv2image, self.size, interpolation=cv2.INTER_NEAREST)
                cv2image = Image.fromarray(cv2image)  # convert image for PIL
                temp.append(cv2image)
                # simple progress print w/o sys import
                print('%d/%d\t%d%%' % (len(temp), frames_count, ((len(temp)/frames_count)*100)))
            else:
                return temp

    def video_loop(self):
        """ Get frame from the video stream and show it in Tkinter """
        if len(self.frames) != 0:
            self.current_image = self.frames.pop(0)
            self.panel.imgtk = ImageTk.PhotoImage(self.current_image)
            self.panel.config(image=self.panel.imgtk)
            self.root.after(1, self.video_loop)  # call the same function after 30 milliseconds

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", default="./",
                help="path to output directory to store snapshots (default: current folder")
args = vars(ap.parse_args())

# start the app
print("[INFO] starting...")
pba = Application(args["output"])
print("no , I was first")
pba.root.mainloop()

下面是导致崩溃的代码,只需2行代码就可以了

^{2}$

Tags: theimageimportselfoutputframesvideocount