1
- from flask import Flask , render_template , Response , request
1
+ from flask import Flask , render_template , Response , request , jsonify
2
2
import cv2
3
- import easyocr
4
3
import time
5
4
import threading
6
- from your_motor_library import MotorController
7
- from your_audio_detection_library import AudioDetector
5
+ import serial
6
+
8
7
9
8
app = Flask (__name__ )
10
9
11
10
# Kameralar
12
- outside_garage_cam = cv2 .VideoCapture (0 ) # 10 metre uzakta olan kamera
13
- inside_garage_cam = cv2 .VideoCapture (1 ) # Garajın içindeki kamera
14
- entrance_cam = cv2 .VideoCapture (2 ) # Ev girişindeki kamera
15
-
16
- # Plaka tanıma ve motor kontrolü
17
- target_plate = "34AEA154"
18
- motor = MotorController ()
19
- audio_detector = AudioDetector () # Kapı zil sesi algılama
20
-
21
- def check_license_plate (frame , target_plate ):
22
- reader = easyocr .Reader (['en' ])
23
- results = reader .readtext (frame )
24
- for (bbox , text , prob ) in results :
25
- if text == target_plate :
26
- return True
27
- return False
11
+ outside_garage_cam = cv2 .VideoCapture (0 )
12
+ inside_garage_cam = cv2 .VideoCapture (1 )
13
+ entrance_cam = cv2 .VideoCapture (2 )
14
+
15
+ # Seri port bağlantısı (Arduino)
16
+ ser = serial .Serial ('/dev/ttyUSB0' , 115200 ) # Arduino's serial port
17
+
18
+ result_message = ""
28
19
29
- def gen_frames (cam ): # video akışı için bir jeneratör fonksiyon
20
+ # Kullanıcı şifreleri
21
+ user_passwords = {
22
+ "user1" : "1234" ,
23
+ "user2" : "5678"
24
+ # Add more users
25
+ }
26
+
27
+ def gen_frames (cam ): # A generator function for streaming video
30
28
while True :
31
29
success , frame = cam .read ()
32
30
if not success :
@@ -40,35 +38,33 @@ def gen_frames(cam): # video akışı için bir jeneratör fonksiyon
40
38
def garage_control ():
41
39
while True :
42
40
ret , frame = outside_garage_cam .read ()
43
- if ret and check_license_plate (frame , target_plate ):
44
- motor .open_garage ()
45
- time .sleep (2 ) # Garajın açılma süresi
46
- inside_detection ()
47
- time .sleep (1 )
48
-
49
- def inside_detection ():
50
- while True :
51
- ret , frame = inside_garage_cam .read ()
52
41
if ret :
53
- # Aracın içeri girip girmediğini kontrol et
54
- # Burada basit bir hareket algılama yapılabilir
55
- # Daha gelişmiş algoritmalar kullanabilirsiniz
56
- gray = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
57
- motion_detected = cv2 .absdiff (gray , cv2 .GaussianBlur (gray , (21 , 21 ), 0 )).sum () > 100000 # Basit bir hareket algılama
58
- if motion_detected :
59
- time .sleep (10 ) # Aracın içeri girmesi için bekleme süresi
60
- motor .close_garage ()
61
- break
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 ' )
45
+ time .sleep (1 )
62
46
63
47
def audio_detection ():
64
48
while True :
65
- if audio_detector . detect_doorbell ():
66
- print ("Zil Sesi Algılandı !" )
67
- # Ev giriş kamera akışını başlat
68
- # Bu durumda kullanıcının bildirim alıp kamerayı görüntülemesi sağlanabilir
69
- # Bu kısımda Flask route'ları kullanarak istemci tarafına bilgi gönderebilirsiniz .
49
+ if detect_doorbell ():
50
+ print ("ringtone detected !" )
51
+ # 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 .
70
54
time .sleep (1 )
71
55
56
+ def detect_doorbell ():
57
+ # Here will be the ringtone detection algorithm
58
+ return False
59
+
60
+ def read_from_arduino ():
61
+ global result_message
62
+ while True :
63
+ if ser .in_waiting > 0 :
64
+ line = ser .readline ().decode ('utf-8' ).strip ()
65
+ if line .startswith ("RESULT:" ):
66
+ result_message = line .split ("RESULT:" )[1 ]
67
+
72
68
@app .route ('/video_feed/<int:cam_id>' )
73
69
def video_feed (cam_id ):
74
70
if cam_id == 0 :
@@ -82,10 +78,26 @@ def video_feed(cam_id):
82
78
83
79
@app .route ('/open_door' , methods = ['POST' ])
84
80
def open_door ():
85
- # Solenoid kontrol kodu buraya gelecek
86
- motor .open_door () # Örneğin: motor.open_door()
81
+ ser .write (b'OPEN_DOOR\n ' ) # Command to send signal to Arduino
87
82
return "Door Opened" , 200
88
83
84
+ @app .route ('/validate_password' , methods = ['POST' ])
85
+ def validate_password ():
86
+ data = request .json
87
+ user = data .get ('user' )
88
+ password = data .get ('password' )
89
+
90
+ if user in user_passwords and user_passwords [user ] == password :
91
+ ser .write (b'VALID_PASSWORD\n ' )
92
+ return "Password Validated" , 200
93
+ else :
94
+ return "Invalid Password" , 401
95
+
96
+ @app .route ('/result' )
97
+ def result ():
98
+ global result_message
99
+ return jsonify (result = result_message )
100
+
89
101
@app .route ('/' )
90
102
def index ():
91
103
return render_template ('index.html' )
@@ -94,4 +106,5 @@ def index():
94
106
# Arka planda çalışan görevler
95
107
threading .Thread (target = garage_control ).start ()
96
108
threading .Thread (target = audio_detection ).start ()
109
+ threading .Thread (target = read_from_arduino ).start ()
97
110
app .run (host = '0.0.0.0' , port = 5000 , debug = True )
0 commit comments