-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (109 loc) · 4.08 KB
/
Copy pathmain.py
File metadata and controls
134 lines (109 loc) · 4.08 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import sys
import os
import signal
# Add global packages
sys.path.append('/usr/lib/python3/dist-packages')
from picamera2 import Picamera2, Preview
# Add locally defined classes
from classes.TFLiteImageClassifier import TFLiteImageClassifier
from classes.EmailSender import EmailSender
from classes.LEDController import LEDController
# Dynamically add the submodule path to sys.path
sys.path.append(os.path.join(os.path.dirname(__file__), 'MFRC522-python'))
import MFRC522
import time
from dotenv import load_dotenv
# Initialise global flag
continue_reading = True
model_path = 'model2.tflite'
captured_image = 'image.jpg'
class_names = [
'BackColour',
'BackDL',
'BackOther',
'BackUC',
'Blank',
'FrontColour',
'FrontDL',
'FrontOther',
'FrontUC'
]
load_dotenv()
EMAIL = os.getenv('EMAIL')
EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD')
PATH_TO_CAPTURED_IMAGE = os.getenv('PATH_TO_CAPTURED_IMAGE')
# Function to convert UID to string
def uid_to_string(uid):
mystring = ""
for i in uid:
mystring = format(i, '02X') + mystring
return mystring
# Signal handler for exiting the loop
def end_read(signal, frame):
global continue_reading
print("Ctrl+C captured, ending read.")
continue_reading = False
def setup_camera_preview():
cam = Picamera2()
campreconfig = cam.create_preview_configuration(main={"size": (1000,1000)})
cam.configure(campreconfig)
cam.start_preview(Preview.QTGL)
cam.start()
return cam
def capture_and_save_image(cam):
cam.capture_file(f"{PATH_TO_CAPTURED_IMAGE}/{captured_image}")
def delete_image():
os.remove(f"{PATH_TO_CAPTURED_IMAGE}/{captured_image}")
def main():
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# Create an object of the class LEDController
led_controller = LEDController(23, 16)
# Setup the camera preview
cam = setup_camera_preview()
# Initialise the TensorFlow Lite Model
classifier = TFLiteImageClassifier(model_path, img_height=500, img_width=500)
email_sender = EmailSender(EMAIL, EMAIL_PASSWORD, EMAIL)
# Welcome message
print("Ready to scan for cards.\nPress Ctrl-C to stop.")
# This loop keeps checking for chips.
# If one is near it will get the UID and pass it to the TF Lite model
while continue_reading:
# Scan for cards
(status, TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
# Get the UID of the card
(status, uid) = MIFAREReader.MFRC522_SelectTagSN()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
print("-----------------------------------------------------")
print("Card read UID: %s" % uid_to_string(uid))
time.sleep(1)
start = time.time()
# Capture and save image
capture_and_save_image(cam)
if os.path.exists(f"{PATH_TO_CAPTURED_IMAGE}/{captured_image}"):
# Predict the class of the captured image
predicted_class, confidence = classifier.predict(captured_image)
print(f"Predicted class: {class_names[predicted_class]}")
print(f"Confidence score: {confidence:.2f}%")
if (class_names[predicted_class] == 'FrontUC' or class_names[predicted_class] == 'BackUC') and confidence > 75:
end = time.time()
led_controller.turn_on_success_led()
print('Success!')
else:
end = time.time()
led_controller.turn_on_failure_led()
print('Failure!')
email_sender.send_email(captured_image, uid_to_string(uid))
print (f"Time taken: {end - start:.2f} seconds")
# Delete captured image once prediction is made
delete_image()
print("-----------------------------------------------------")
else:
print("Failed to read UID")
if __name__ == "__main__":
main()