01/10/2018, 15:37

Lưu ảnh từ Webcam bằng python bị trễ

Bởi vì dùng Tkinter tạo giao diện nên mình không thể dùng vòng lặp While như những hướng dẫn trên mạng được.
Mình dùng def để tạo các vòng lặp, khi chạy lần thứ nhất, webcam chụp nhưng ảnh save về là khung trắng. Lặp đến khi webcam chụp lần 2 thì ảnh được save thành ảnh chụp từ lần 1. Và nó cứ trễ như vậy luôn…
Bạn nào biết giúp mình với! Mình dùng webcam Logitech C920 chạy trên Raspberry Pi 3
Đây là code của mình:

root = Tk()
root.title("AAA")
label = Label(root)
label.pack()

def start():
    GPIO.output(LedPin1, GPIO.HIGH)
    GPIO.wait_for_edge(ButtonPin1, GPIO.RISING)
    GPIO.output(LedPin1, GPIO.LOW)
    root.after(2000, start1)
def start1():
    GPIO.output(LedPin2, GPIO.HIGH)
    GPIO.wait_for_edge(ButtonPin2, GPIO.RISING)
    root.after(0, start2)
def start2():
    cap = cv2.VideoCapture(0)
    ret, img = cap.read()
    out = cv2.imwrite('capture.jpg', img)
    root.after(10, start3)
def start3():
    img1 = cv2.imread('capture.jpg', 1)
    img2 = Image.fromarray(img1)
    imgtk = ImageTk.PhotoImage(image=img2)
    label.imgtk = imgtk
    label.configure(image=imgtk)
    root.after(10, start4)
def start4():
    GPIO.output(LedPin2, GPIO.LOW)  
    GPIO.output(LedPin3, GPIO.HIGH)
    GPIO.wait_for_edge(ButtonPin3, GPIO.RISING)
    GPIO.output(LedPin3, GPIO.LOW)
    root.after(2000, start5)
def start5():
    GPIO.output(LedPin4, GPIO.HIGH)
    GPIO.wait_for_edge(ButtonPin4, GPIO.RISING)
    GPIO.output(LedPin4, GPIO.LOW)  
    root.after(500, start)
root.after(500, start)
root.mainloop()
Aragami1408 viết 17:48 ngày 01/10/2018

Đừng dùng tkinter. Tkinter vừa xấu và tác dụng của nó không được hữu hiệu mấy nhất là làm kiểu của bạn. Mình khuyên bạn nên dùng python GTK+. Python GTK+ docs

Văn Dương viết 17:49 ngày 01/10/2018

Đấy là điều bình thường. Bởi vì khi lấy dữ liệu ảnh thì nó lấy từ buffer lên. Sau đó nó mới update dữ liệu mới vào buffer. Như vậy thì muốn lấy ảnh hiện tại thì phải chụp liên tiếp 2 lần.

Bài liên quan
0