Skip to content

Commit 439fa04

Browse files
Merge pull request #1 from SentryCoderDev/feature/pytesseract_plate
added feature/pytesseract
2 parents 4dfb7c8 + 9af945d commit 439fa04

File tree

2 files changed

+86
-80
lines changed

2 files changed

+86
-80
lines changed

app.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
from flask import Flask, render_template, Response, request, jsonify
22
import cv2
3+
import pytesseract
34
import time
45
import threading
56
import serial
67

7-
88
app = Flask(__name__)
99

10-
# Kameralar
10+
# All Cameras
1111
outside_garage_cam = cv2.VideoCapture(0)
1212
inside_garage_cam = cv2.VideoCapture(1)
1313
entrance_cam = cv2.VideoCapture(2)
1414

15-
# Seri port bağlantısı (Arduino)
1615
ser = serial.Serial('/dev/ttyUSB0', 115200) # Arduino's serial port
17-
1816
result_message = ""
1917

20-
# Kullanıcı şifreleri
2118
user_passwords = {
2219
"user1": "1234",
2320
"user2": "5678"
2421
# Add more users
2522
}
2623

27-
def gen_frames(cam): # A generator function for streaming video
24+
# Pytesseract configuration
25+
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # Path to Tesseract OCR executable
26+
27+
def gen_frames(cam):
2828
while True:
2929
success, frame = cam.read()
3030
if not success:
@@ -39,22 +39,25 @@ def garage_control():
3939
while True:
4040
ret, frame = outside_garage_cam.read()
4141
if ret:
42-
# Motion detection and target plate recognition code will be here
43-
# Send RF signal if target plate detected
44-
ser.write(b'OPEN_GARAGE\n')
42+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
43+
# Perform OCR using Pytesseract
44+
text = pytesseract.image_to_string(gray)
45+
print("OCR Text:", text)
46+
# Check if the target plate number is recognized
47+
if "your_target_plate" in text:
48+
ser.write(b'OPEN_GARAGE\n')
49+
print("Opening garage for plate 'your_target_plate'")
4550
time.sleep(1)
4651

4752
def audio_detection():
4853
while True:
4954
if detect_doorbell():
5055
print("ringtone detected!")
5156
# Start camera streaming at home entrance
52-
# In this case, the user can be notified and view the camera
53-
# In this section, you can send information to the client side using Flask routes.
57+
# Notify and view the camera as needed
5458
time.sleep(1)
5559

5660
def detect_doorbell():
57-
# Here will be the ringtone detection algorithm
5861
return False
5962

6063
def read_from_arduino():
@@ -78,7 +81,7 @@ def video_feed(cam_id):
7881

7982
@app.route('/open_door', methods=['POST'])
8083
def open_door():
81-
ser.write(b'OPEN_DOOR\n') # Command to send signal to Arduino
84+
ser.write(b'OPEN_DOOR\n')
8285
return "Door Opened", 200
8386

8487
@app.route('/validate_password', methods=['POST'])
@@ -103,8 +106,8 @@ def index():
103106
return render_template('index.html')
104107

105108
if __name__ == '__main__':
106-
# Arka planda çalışan görevler
107109
threading.Thread(target=garage_control).start()
108110
threading.Thread(target=audio_detection).start()
109111
threading.Thread(target=read_from_arduino).start()
110112
app.run(host='0.0.0.0', port=5000, debug=True)
113+

arduino/arduino_code/arduino_code.ino

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
// RFID
77
#define SS_PIN 10
88
#define RST_PIN 9
9+
#define solenoidPin 11
910
MFRC522 rfid(SS_PIN, RST_PIN);
1011

1112
// Keypad
1213
const byte ROWS = 4; // Four rows
1314
const byte COLS = 3; // Three columns
1415
char keys[ROWS][COLS] = {
15-
{'1', '2', '3'},
16-
{'4', '5', '6'},
17-
{'7', '8', '9'},
18-
{'*', '0', '#'}
16+
{'1', '2', '3'},
17+
{'4', '5', '6'},
18+
{'7', '8', '9'},
19+
{'*', '0', '#'}
1920
};
2021
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
2122
byte colPins[COLS] = {5, 4, 3}; // Connect to the column pinouts of the keypad
@@ -29,96 +30,98 @@ String userNames[] = {"X", "Y"};
2930
String enteredPassword = "";
3031

3132
void setup() {
32-
Serial.begin(115200);
33-
SPI.begin();
34-
rfid.PCD_Init();
33+
Serial.begin(115200);
34+
SPI.begin();
35+
rfid.PCD_Init();
3536
}
3637

3738
void loop() {
38-
// process incoming commands from serial port
39-
if (Serial.available() > 0) {
40-
String command = Serial.readStringUntil('\n');
41-
if (command == "OPEN_DOOR") {
42-
openDoor();
39+
// process incoming commands from serial port
40+
if (Serial.available() > 0) {
41+
String command = Serial.readStringUntil('\n');
42+
if (command == "OPEN_DOOR") {
43+
openDoor();
44+
}
4345
}
44-
}
4546

46-
// RFID Read
47-
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
48-
String cardUID = "";
49-
for (byte i = 0; i < rfid.uid.size; i++) {
50-
cardUID += String(rfid.uid.uidByte[i], HEX);
47+
// RFID Read
48+
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
49+
String cardUID = "";
50+
for (byte i = 0; i < rfid.uid.size; i++) {
51+
cardUID += String(rfid.uid.uidByte[i], HEX);
52+
}
53+
if (isValidCard(cardUID)) {
54+
String user = getUserByCard(cardUID);
55+
openDoor();
56+
Serial.print("Hello ");
57+
Serial.println(user);
58+
sendResultToJetson("Hello " + user);
59+
}
60+
rfid.PICC_HaltA();
5161
}
52-
if (isValidCard(cardUID)) {
53-
String user = getUserByCard(cardUID);
54-
openDoor();
55-
Serial.print("Hello ");
56-
Serial.println(user);
57-
sendResultToJetson("Hello " + user);
58-
}
59-
rfid.PICC_HaltA();
60-
}
6162

62-
// Keypad Read
63-
char key = keypad.getKey();
64-
if (key) {
65-
if (key == '#') {
66-
if (isValidPassword(enteredPassword)) {
67-
String user = getUserByPassword(enteredPassword);
68-
openDoor();
69-
Serial.print("Hello ");
70-
Serial.println(user);
71-
sendResultToJetson("Hello " + user);
72-
}
73-
enteredPassword = "";
74-
} else {
75-
enteredPassword += key;
63+
// Keypad Read
64+
char key = keypad.getKey();
65+
if (key) {
66+
if (key == '#') {
67+
if (isValidPassword(enteredPassword)) {
68+
String user = getUserByPassword(enteredPassword);
69+
openDoor();
70+
Serial.print("Hello ");
71+
Serial.println(user);
72+
sendResultToJetson("Hello " + user);
73+
}
74+
enteredPassword = "";
75+
} else {
76+
enteredPassword += key;
77+
}
7678
}
77-
}
7879
}
7980

8081
bool isValidCard(String uid) {
81-
for (String validCard : validCards) {
82-
if (validCard == uid) {
83-
return true;
82+
for (String validCard : validCards) {
83+
if (validCard == uid) {
84+
return true;
85+
}
8486
}
85-
}
86-
return false;
87+
return false;
8788
}
8889

8990
bool isValidPassword(String password) {
90-
for (String userPassword : userPasswords) {
91-
if (userPassword == password) {
92-
return true;
91+
for (String userPassword : userPasswords) {
92+
if (userPassword == password) {
93+
return true;
94+
}
9395
}
94-
}
95-
return false;
96+
return false;
9697
}
9798

9899
String getUserByCard(String uid) {
99-
for (int i = 0; i < sizeof(validCards) / sizeof(validCards[0]); i++) {
100-
if (validCards[i] == uid) {
101-
return userNames[i];
100+
for (int i = 0; i < sizeof(validCards) / sizeof(validCards[0]); i++) {
101+
if (validCards[i] == uid) {
102+
return userNames[i];
103+
}
102104
}
103-
}
104-
return "Unknown";
105+
return "Unknown";
105106
}
106107

107108
String getUserByPassword(String password) {
108-
for (int i = 0; i < sizeof(userPasswords) / sizeof(userPasswords[0]); i++) {
109-
if (userPasswords[i] == password) {
110-
return userNames[i];
109+
for (int i = 0; i < sizeof(userPasswords) / sizeof(userPasswords[0]); i++) {
110+
if (userPasswords[i] == password) {
111+
return userNames[i];
112+
}
111113
}
112-
}
113-
return "Unknown";
114+
return "Unknown";
114115
}
115116

116117
void openDoor() {
117-
// Solenoidi açma kodu burada olacak
118-
// Örneğin: digitalWrite(solenoidPin, HIGH);
118+
119+
digitalWrite(solenoidPin, HIGH);
120+
sleep(1);
119121
}
120122

121123
void sendResultToJetson(String message) {
122-
Serial.print("RESULT:");
123-
Serial.println(message);
124+
Serial.print("RESULT:");
125+
Serial.println(message);
124126
}
127+

0 commit comments

Comments
 (0)