-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanva.py
52 lines (44 loc) · 1.62 KB
/
canva.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from collections import deque
import cv2
from PIL import Image, ImageTk
import time
import Tkinter as tk
def quit_(root):
root.destroy()
def update_image(image_label, cam):
(readsuccessful, f) = cam.read()
gray_im = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
a = Image.fromarray(gray_im)
b = ImageTk.PhotoImage(image=a)
image_label.configure(image=b)
image_label._image_cache = b # avoid garbage collection
root.update()
def update_fps(fps_label):
frame_times = fps_label._frame_times
frame_times.rotate()
frame_times[0] = time.time()
sum_of_deltas = frame_times[0] - frame_times[-1]
count_of_deltas = len(frame_times) - 1
try:
fps = int(float(count_of_deltas) / sum_of_deltas)
except ZeroDivisionError:
fps = 0
fps_label.configure(text='FPS: {}'.format(fps))
def update_all(root, image_label, cam, fps_label):
update_image(image_label, cam)
update_fps(fps_label)
root.after(20, func=lambda: update_all(root, image_label, cam, fps_label))
if __name__ == '__main__':
root = tk.Tk()
image_label = tk.Label(master=root)# label for the video frame
image_label.pack()
cam = cv2.VideoCapture(1)
fps_label = tk.Label(master=root)# label for fps
fps_label._frame_times = deque([0]*5) # arbitrary 5 frame average FPS
fps_label.pack()
# quit button
quit_button = tk.Button(master=root, text='Quit',command=lambda: quit_(root))
quit_button.pack()
# setup the update callback
root.after(0, func=lambda: update_all(root, image_label, cam, fps_label))
root.mainloop()