Skip to content

Commit f8337c9

Browse files
Add files via upload
1 parent 7dc4ac1 commit f8337c9

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

app.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from flask import Flask, render_template, Response, request
2+
import cv2
3+
import easyocr
4+
import time
5+
import threading
6+
from your_motor_library import MotorController
7+
from your_audio_detection_library import AudioDetector
8+
9+
app = Flask(__name__)
10+
11+
# 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
28+
29+
def gen_frames(cam): # video akışı için bir jeneratör fonksiyon
30+
while True:
31+
success, frame = cam.read()
32+
if not success:
33+
break
34+
else:
35+
ret, buffer = cv2.imencode('.jpg', frame)
36+
frame = buffer.tobytes()
37+
yield (b'--frame\r\n'
38+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
39+
40+
def garage_control():
41+
while True:
42+
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+
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
62+
63+
def audio_detection():
64+
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.
70+
time.sleep(1)
71+
72+
@app.route('/video_feed/<int:cam_id>')
73+
def video_feed(cam_id):
74+
if cam_id == 0:
75+
return Response(gen_frames(outside_garage_cam), mimetype='multipart/x-mixed-replace; boundary=frame')
76+
elif cam_id == 1:
77+
return Response(gen_frames(inside_garage_cam), mimetype='multipart/x-mixed-replace; boundary=frame')
78+
elif cam_id == 2:
79+
return Response(gen_frames(entrance_cam), mimetype='multipart/x-mixed-replace; boundary=frame')
80+
else:
81+
return "Invalid Camera ID", 404
82+
83+
@app.route('/open_door', methods=['POST'])
84+
def open_door():
85+
# Solenoid kontrol kodu buraya gelecek
86+
motor.open_door() # Örneğin: motor.open_door()
87+
return "Door Opened", 200
88+
89+
@app.route('/')
90+
def index():
91+
return render_template('index.html')
92+
93+
if __name__ == '__main__':
94+
# Arka planda çalışan görevler
95+
threading.Thread(target=garage_control).start()
96+
threading.Thread(target=audio_detection).start()
97+
app.run(host='0.0.0.0', port=5000, debug=True)

templates/index.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>H3RU Home Control Automation</title>
7+
<style>
8+
body {
9+
text-align: center;
10+
}
11+
.container {
12+
display: flex;
13+
justify-content: space-around;
14+
align-items: center;
15+
flex-wrap: wrap;
16+
}
17+
.camera-feed {
18+
flex: 1;
19+
margin: 10px;
20+
}
21+
.centered-button {
22+
text-align: center;
23+
width: 100%;
24+
margin-top: 20px;
25+
}
26+
button {
27+
font-size: 20px;
28+
padding: 10px 20px;
29+
}
30+
</style>
31+
</head>
32+
<body>
33+
<h1>Home Control Automation</h1>
34+
<div class="container">
35+
<div class="camera-feed">
36+
<h2>Outside Garage Camera</h2>
37+
<img src="{{ url_for('video_feed', cam_id=0) }}">
38+
</div>
39+
<div class="camera-feed">
40+
<h2>Inside Garage Camera</h2>
41+
<img src="{{ url_for('video_feed', cam_id=1) }}">
42+
</div>
43+
<div class="camera-feed">
44+
<h2>Entrance Camera</h2>
45+
<img src="{{ url_for('video_feed', cam_id=2) }}">
46+
</div>
47+
</div>
48+
<div class="centered-button">
49+
<button onclick="openDoor()">Open Door</button>
50+
</div>
51+
52+
<script>
53+
function openDoor() {
54+
fetch('/open_door', { method: 'POST' })
55+
.then(response => {
56+
if(response.ok) {
57+
alert("Door opened");
58+
}
59+
});
60+
}
61+
</script>
62+
</body>
63+
</html>

0 commit comments

Comments
 (0)