|
| 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) |
0 commit comments