简体   繁体   中英

how to upload video to aws while capturing videos using openCV python

I have a Tkinter multiFrame/pages application In one of the frames i have camera related work ie when I go on the page the camera automatically start and start capturing 20 sec video recording with openCV that means when video gets 20 second long it saves video locally and then starts recording new video of 20 second now i want those videos to upload on aws when a video gets recorded and got saved locally how can i achieve that whenever a video gets record i want that video should upload to aws automatically and get deleted from local computer and than this process should again start when new 20 second video is recorded.

my code


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)

The way I know is to create an S3 bucket. In S3 bucket when video is loaded, aws generates an http URL for the video which can be read by OpenCV vidocapture.

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

and now should be

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

If you notice, the URL is http and not https, OpenCV will not run if is https, that's why we specify it as just http

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM