-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI2.py
More file actions
80 lines (66 loc) · 2.55 KB
/
Copy pathUI2.py
File metadata and controls
80 lines (66 loc) · 2.55 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
77
78
79
80
import cv2
import numpy as np
import tkinter as tk
from tkinter import Label
from PIL import Image, ImageTk
from tensorflow.keras.models import load_model
model= load_model('bestmodel.h5')
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
def predict(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV)
resized = cv2.resize(thresh, (28, 28))
normalized = resized / 255.0
reshaped = np.reshape(normalized, (1, 28, 28, 1))
prediction = model.predict(reshaped)
max_prob = np.max(prediction)
if max_prob > 0.5: # Set your threshold here
digit = np.argmax(prediction)
# Find contours to locate the digit
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if contours:
# Get the largest contour
largest_contour = max(contours, key=cv2.contourArea)
# Get the bounding box coordinates
x, y, w, h = cv2.boundingRect(largest_contour)
else:
x, y, w, h = None, None, None, None
else:
digit = None
x, y, w, h = None, None, None, None
return digit, x, y, w, h
def update_frame():
ret, frame = cap.read()
if ret:
digit, x, y, w, h = predict(frame)
if digit is not None:
print(f"Predicted Digit: {digit}") # Debugging
cv2.putText(frame, f'The magical fairy predicts: {digit}', (20, 60),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)
if x is not None and y is not None:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) # Draw rectangle
else:
print("No number found") # Debugging
cv2.putText(frame, 'No number found', (10, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
print("Frame updated with text overlay") # Debugging
else:
print("Failed to read frame") # Debugging
lmain.after(100, update_frame)
root = tk.Tk()
root.title("Adrians magic number fairy")
root.geometry("700x500")
cap = cv2.VideoCapture(0)
lmain = Label(root)
lmain.pack()
digit_label = Label(root, text="The magical Fairy predicts this digit:")
digit_label.pack()
update_frame()
root.mainloop()
cap.release()
cv2.destroyAllWindows()