【问题标题】:how to upload video to AWS while capturing videos using openCV python如何在使用 openCV python 捕获视频的同时将视频上传到 AWS
【发布时间】:2021-12-17 03:07:29
【问题描述】:

我有一个 Tkinter 多帧/页面应用程序在其中一个框架中,我有与相机相关的工作,即当我进入页面时,相机会自动启动并开始使用 openCV 捕获 20 秒的视频录制,这意味着当视频长到 20 秒时在本地保存视频,然后开始录制 20 秒的新视频现在我希望这些视频在录制视频并保存在本地时上传到 aws从本地计算机中删除,然后在录制新的 20 秒视频时重新开始此过程。

我的代码


class FrontCameraPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller


        self.cameraFrame = tk.Frame(self, bg=gray)
        self.cameraFrame.grid(row=1, column=0, sticky="nsew")
        self.buttonFrame = tk.Frame(self, bg="white")
        self.buttonFrame.grid(row=1, column=1, sticky="nsew", padx=(10, 0))

        self.stop= tk.Button(self.buttonFrame, text="END TRIP", font=small_Font, bg=dark_blue, fg="White")
        self.stop.grid(row=2, column=0, ipadx=10, pady=(0, 5))
        self.stop['command'] = self.stop_capture

        self.cancelButton = tk.Button(self.buttonFrame, text="Cancel", font=small_Font, bg=dark_blue, fg="white")
        self.cancelButton.grid(row=3, column=0, ipadx=10)
        self.cancelButton['command'] = lambda: controller.show_frame(go_to_some_page)

        # setup callbacks for switching in and out events
        self.bind('<<SwitchIn>>', self.start_capture)
        self.bind('<<SwitchOut>>', self.stop_capture)

        self.lmain = tk.Label(self.cameraFrame)
        self.lmain.pack()
        self.capture = None

    def start_capture(self, event=None):
        if self.capture is None:
            #-----------------------------------
            width, height = 200, 200
            self.cap = cv2.VideoCapture(0)
            self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
            self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
            # used instance variables for width and height
            self.width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
            self.height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
            # initialize file rotation and frame counters
            self.timer = 1    # file rotation counter
            self.frames = 0   # frame counter
            self.fourcc = cv2.VideoWriter_fourcc(*'XVID')
            self.out = cv2.VideoWriter(
                f'{self.timer}.avi', self.fourcc, 25, (self.width, self.height))
            #-----------------------------------
            self.show_frame()
            print('capture started')

    def stop_capture(self, event=None):
        if self.capture:
            self.after_cancel(self.capture)
            #-----------------------------------
            self.cap.release()
            self.out.release()
            #------------------------------
            self.capture = None
            print('capture stopped')


    def show_frame(self):
        ret, frame = self.cap.read()
        if ret:
            self.out.write(frame)
            self.frames += 1   
            if self.frames >= 500:    
                self.out.release()    
                self.timer += 1       
                self.out.open(f'{self.timer}.avi', self.fourcc, 25, (self.width, self.height))
                self.frames = 0       
            frame = cv2.flip(frame, 1)
            cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
            img = Image.fromarray(cv2image)
            self.imgtk = ImageTk.PhotoImage(image=img)
            self.lmain.configure(image=self.imgtk)
        self.capture = self.after(10, self.show_frame)

【问题讨论】:

  • thread 函数开始。
  • 您的问题的 opencv 和 tkinter 部分无关紧要。您在问如何上传文件。专注于此。最多你需要线程,这样 GUI 就不会被上传阻塞。
  • 我怎样才能在这个里面做线程。

标签: python amazon-web-services tkinter


【解决方案1】:

我知道的方法是创建一个 S3 存储桶。在 S3 存储桶中加载视频时,aws 会为视频生成一个 http URL,OpenCV vidocapture 可以读取该 URL。

cv2.VideoCapture(“/tmp/vid_test.mp4”), 

现在应该是

cv2.VideoCapture(“http://some_name.s3.amazonaws.com/original/vid_test.mp4”)

如果您注意到 URL 是 http 而不是 https,如果是 https,OpenCV 将不会运行,这就是我们将其指定为 http 的原因

【讨论】:

  • 关于如何上传文件的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多