To create a more advanced driver drowsiness detection system using a Raspberry Pi 5 with an IP camera
To create a more advanced driver drowsiness detection system using a Raspberry Pi 5 with an IP camera
4. Use of Machine Learning: Implement a more robust model for detecting drowsiness.
5. User Authentication: Secure the web app with basic user authentication.
Components Required:
Raspberry Pi 5
bash
Create a SQLite database to log drowsiness events. You can create a file named database.py:
python
1import sqlite3
3def create_db():
4 conn = sqlite3.connect('drowsiness_log.db')
5 c = conn.cursor()
6 c.execute('''
10 status TEXT
11 )
12 ''')
13 conn.commit()
14 conn.close()
15
17 create_db()
bash
1python3 database.py
python
1import cv2
2import dlib
3import numpy as np
4from flask import Flask, Response, render_template, redirect, url_for, request, session
5import sqlite3
6import time
7import os
10
11app = Flask(__name__)
12app.secret_key = 'your_secret_key' # Change this to a random secret key
13
15detector = dlib.get_frontal_face_detector()
16predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
17
19EYE_AR_THRESH = 0.25
20EYE_AR_CONSEC_FRAMES = 48
21COUNTER = 0
22ALERTED = False
23
24def eye_aspect_ratio(eye):
25 A = np.linalg.norm(eye[1] - eye[5])
26 B = np.linalg.norm(eye[2] - eye[4])
27 C = np.linalg.norm(eye[0] - eye[3])
28 ear = (A + B) / (2.0 * C)
29 return ear
30
31def log_event(status):
32 conn = sqlite3.connect('drowsiness_log.db')
33 c = conn.cursor()
35 conn.commit()
36 conn.close()
37
38def detect_drowsiness(frame):
41 faces = detector(gray)
42
43 for face in faces:
46 (landmarks.part(37).x, landmarks.part(37).y),
47 (landmarks.part(38).x, landmarks.part(38).y),
48 (landmarks.part(39).x, landmarks.part(39).y),
49 (landmarks.part(40).x, landmarks.part(40).y),
50 (landmarks.part(41).x, landmarks.part(41).y)])
51
53 (landmarks.part(43).x, landmarks.part(43).y),
54 (landmarks.part(44).x, landmarks.part(44).y),
55 (landmarks.part(45).x, landmarks.part(45).y),
56 (landmarks.part(46).x, landmarks.part(46).y),
57 (landmarks.part(47).x, landmarks.part(47).y)])
58
59 left_ear = eye_aspect_ratio(left_eye)
60 right_ear = eye_aspect_ratio(right_eye)
62
64 COUNTER += 1
66 if not ALERTED:
67 ALERTED = True
68 log_event("DROWSY")
69 Thread(target=playsound, args=("alarm.mp3",)).start()
71 else:
72 COUNTER = 0
73 ALERTED = False
74
75 return frame
76
77def generate_frames():
78 camera_url = "http://<IP_CAMERA_URL>/video"
79 camera = cv2.VideoCapture(camera_url)
80
81 while True:
83 if not success:
84 break
85 frame = detect_drowsiness(frame)
87 frame = buffer.tobytes()
88 yield (b'--frame\r\n'
90
[email protected]('/video_feed')
92def video_feed():
94
96def login():
97 if request.method == 'POST':
98 username = request.form['username']
99 password = request.form['password']
100 if username == 'admin' and password == 'password': # Change these to your desired
credentials
103 else:
104 return render_template('login.html', error='Invalid credentials')
106
[email protected]('/logout')
108def logout():
111
[email protected]('/')
113def index():
116 else:
118
login.html:
html
1<!DOCTYPE html>
2<html>
3<head>
4 <title>Login</title>
5</head>
6<body>
7 <h1>Login</h1>
9 <label for="username">Username:</label><br>
10 <input type="text" id="username" name="username"><br>
11 <label for="password">Password:</label><br>
14 </form>
15 {% if error %}
17 {% endif %}
18</body>
19</html>
index.html:
html
1<!DOCTYPE html>
2<html>
3<head>
5</head>
6<body>
9</body>
10</html>
Run the Python script using python3 your_script.py. Open a web browser and navigate
to http://<Raspberry_Pi_IP>:5000 to access the web dashboard. Log in with your credentials, and
you'll see the video feed with drowsiness detection.
Remember to replace <IP_CAMERA_URL> with your IP camera's URL and <Raspberry_Pi_IP> with
your Raspberry Pi's IP address. Also, update the username and password in the login function to
your desired credentials.
forums.raspberrypi
pimylifeup
forums.raspberrypi
To find the IP address of a Raspberry Pi, you can use several methods depending on whether you
have physical access to the device or if you need to find it remotely. Here are the most common
methods:
If you have physical access to your Raspberry Pi and can use a monitor and keyboard, follow these
steps:
1. Open the Terminal: You can do this by clicking on the terminal icon or pressing Ctrl + Alt + T.
2. Run the Command: Type the following command and press Enter:
bash
1hostname -I
This command will display the IP address(es) assigned to your Raspberry Pi.
If you don't have direct access to the Raspberry Pi, you can find its IP address through your router:
1. Access Your Router: Open a web browser and enter your router's IP address
(commonly http://192.168.1.1 or http://192.168.0.1).
3. Find Connected Devices: Look for a section that lists connected devices. This may be labeled
as "Device List," "Connected Devices," or something similar.
4. Identify the Raspberry Pi: Look for a device named "raspberrypi" or similar in the list. The
corresponding IP address will be displayed next to it.
If you have access to another device on the same network, you can try to ping the Raspberry Pi:
bash
1ping raspberrypi.local
If the above methods do not work, you can use the nmap tool to scan your network:
1. Install nmap: If you don't have it installed, you can install it using:
Linux:
bash
2. Find Your Subnet: Determine your local IP address and subnet. For example, if your IP
is 192.168.1.45, your subnet is 192.168.1.0/24.
bash
This command will scan the subnet and list all devices connected, including your
Raspberry Pi.
Conclusion
By using any of these methods, you should be able to find the IP address of your Raspberry Pi.
Choose the method that best suits your situation based on your access to the device and network.