-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtkinter.py
More file actions
76 lines (60 loc) · 1.96 KB
/
tkinter.py
File metadata and controls
76 lines (60 loc) · 1.96 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import tkinter as tk
from tkinter import ttk
import cv2
import numpy as np
import tensorflow as tf
from PIL import Image, ImageTk
# Load the model
model = tf.keras.models.load_model('finalmodel')
# Initialize variables
is_recording = False
cap = None
def preprocess(image):
resized = cv2.resize(image, (28, 28), interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
normalized = gray / 255.0
reshaped = normalized.reshape(-1, 28, 28, 1)
return reshaped
def predict(input_data):
predictions = model.predict(input_data)
return predictions
def start_recording():
global is_recording, cap
if not is_recording:
cap = cv2.VideoCapture(0) # Start camera capture
is_recording = True
capture_video()
def stop_recording():
global is_recording, cap
if is_recording:
cap.release() # Release camera
is_recording = False
def capture_video():
global cap
if cap.isOpened():
ret, frame = cap.read()
if ret:
preprocessed = preprocess(frame)
predictions = predict(preprocessed)
print(predictions)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame)
imgtk = ImageTk.PhotoImage(image=img)
video_canvas.imgtk = imgtk
video_canvas.configure(image=imgtk)
if is_recording:
video_canvas.after(10, capture_video)
root = tk.Tk()
root.title("Camera Interface")
video_canvas = tk.Label(root)
video_canvas.pack(padx=10, pady=10)
control_frame = ttk.Frame(root)
control_frame.pack(padx=10, pady=10)
start_button = ttk.Button(control_frame, text="Start Recording", command=start_recording)
start_button.grid(row=0, column=0, padx=5, pady=5)
stop_button = ttk.Button(control_frame, text="Stop Recording", command=stop_recording)
stop_button.grid(row=0, column=1, padx=5, pady=5)
root.mainloop()
if cap is not None:
cap.release()
cv2.destroyAllWindows()