简体   繁体   中英

webcam recording with python tkinter

I want to record video (not audio) from webcam. I have put two buttons for start recording & stop recording. As program starts it pick image from camera & shows on screen.works perfect. My problem is when i click start recording & after some times stop recording, only avi file created ,with 0K or 6K size found. No further recording found.

 import tkinter import cv2 import PIL.Image, PIL.ImageTk stopb = None class App(): def __init__(self, window, window_title): self.window = window self.window.title = window_title self.ok = False self.video = cv2.VideoCapture(0) self.width = self.video.get(cv2.CAP_PROP_FRAME_WIDTH) self.height = self.video.get(cv2.CAP_PROP_FRAME_HEIGHT) #create videowriter self.fourcc = cv2.VideoWriter_fourcc(*'XVID') self.out = cv2.VideoWriter('output.avi',self.fourcc,10,(640,480)) # Create a canvas that can fit the above video source size self.canvas = tkinter.Canvas(window, width=self.width, height=self.height) self.canvas.pack() self.opencamera = tkinter.Button(window, text="open camera", command=self.open_camera) self.opencamera.pack() self.closecamera = tkinter.Button(window, text="close camera", command=self.close_camera) self.closecamera.pack() self.delay = 10 self.update() # After it is called once, the update method will be automatically called every delay milliseconds self.window.mainloop() def update(self): ret, frame = self.video.read() if self.ok == 'T': self.out.write(frame) if ret: self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame)) self.canvas.create_image(0, 0, image=self.photo, anchor=tkinter.NW) self.window.after(self.delay, self.update) def open_camera(self): self.ok = True print("camera opened") print(self.ok) def close_camera(self): print("camera closed") self.ok = False self.video.release() self.out.release() def __del__(self): if self.video.isOpened(): self.video.release() self.out.release() App(tkinter.Tk(), "mywindow")

Your problem is that you're never writing anything to your output, as if self.ok == 'T' will never evaluate to true. You should change it to just if self.ok , the same thing you did with ret .

def update(self):
        ret, frame = self.video.read()
        if self.ok:
            self.out.write(frame)
        if ret:
            self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame))
            self.canvas.create_image(0, 0, image=self.photo, anchor=tkinter.NW)

        self.window.after(self.delay, self.update)

One more issue I found was- after clicking the close camera button followed by the open camera button, the new recording won't start. Meaning you will just be able to record the first video captured before clicking the close camera button.

This is because the close camera function releases the video object and output object thus video.read() of the update function won't work.

A quick fix is to create a method of set_camera() which creates the video object. Call this function every time in open camera.

https://replit.com/@AmitYadav4/video-record-in-tkinker-canvas

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