-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtemp-mini-project.py
More file actions
376 lines (312 loc) · 12.9 KB
/
temp-mini-project.py
File metadata and controls
376 lines (312 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import cv2
import numpy as np
from ultralytics import YOLO
import pyttsx3
import sqlite3
import os
model = YOLO("yolov8l.pt")
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("Face-Recognition/recognizer/trainingdata.yml")
face_cascade = cv2.CascadeClassifier('Face-Recognition/haarcascade_frontalface_default.xml')
engine = pyttsx3.init()
def get_profile(id):
conn = sqlite3.connect("Face-Recognition/sqlite.db")
cursor = conn.execute("SELECT * FROM STUDENTS WHERE id=?", (id,))
profile = None
for row in cursor:
profile = row
conn.close()
return profile
def person_exists(name):
conn = sqlite3.connect("Face-Recognition/sqlite.db")
cursor = conn.execute("SELECT * FROM STUDENTS WHERE NAME=?", (name.upper(),))
exists = cursor.fetchone() is not None
conn.close()
return exists
def draw_button(frame, text, position, size=(200, 50), padding=10, color="#C509EB"):
(x, y) = position
(w, h) = size
text_size, _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)
button_width = max(text_size[0] + 2 * padding, w)
button_height = max(text_size[1] + 2 * padding, h)
text_x = x + (button_width - text_size[0]) // 2
text_y = y + (button_height + text_size[1]) // 2
cv2.rectangle(frame, (x, y), (x + button_width, y + button_height), (175, 9, 235), -1)
cv2.rectangle(frame, (x, y), (x + button_width, y + button_height), (0, 0, 0), 2) # Black border
cv2.putText(frame, text, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
def check_button_click(position, size, mouse_pos):
(x, y) = position
(w, h) = size
(mx, my) = mouse_pos
return x <= mx <= x + w and y <= my <= y + h
def estimate_distance(box, real_height=1.7, focal_length=700):
x1, y1, x2, y2 = box
pixel_height = y2 - y1
distance = (real_height * focal_length) / pixel_height
return distance
def environment_read():
cam = cv2.VideoCapture(0)
button_clicked = False
def on_mouse(event, x, y, flags, param):
nonlocal button_clicked
if event == cv2.EVENT_LBUTTONDOWN:
if check_button_click((10, 10), (200, 50), (x, y)):
button_clicked = True
cv2.namedWindow("Environment Read", cv2.WINDOW_NORMAL)
cv2.setMouseCallback("Environment Read", on_mouse)
while True:
ret, frame = cam.read()
if not ret:
continue
results = model(frame)
detections = results[0].boxes
for box in detections:
conf = box.conf[0]
cls = int(box.cls[0])
xyxy = box.xyxy[0]
if conf > 0.5:
label = f'{model.names[cls]} {conf:.2f}'
x1, y1, x2, y2 = map(int, xyxy)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
if model.names[cls] == 'person':
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
pro,face,ex,ey,ew,eh=False,[],0,0,0,0
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
id, confidence = recognizer.predict(gray[y:y + h, x:x + w])
profile = get_profile(id)
if profile is not None:
pro,face=True,profile
ex,ey,ew,eh=x,y,w,h
break
x,y,w,h=ex,ey,ew,eh
if pro:
profile=face
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.putText(frame, str(profile[1]), (x, y + h + 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
engine.say(str(profile[1]))
engine.runAndWait()
else:
cv2.putText(frame, "Unknown", (x, y + h + 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
engine.say(f"Person")
engine.runAndWait()
else:
engine.say(model.names[cls])
engine.runAndWait()
draw_button(frame, "Back", (10, 10), (200, 50))
cv2.imshow("Environment Read", frame)
if cv2.waitKey(1) & 0xFF == 27 or button_clicked:
revokeMainfun()
break
def add_person():
face_detect = cv2.CascadeClassifier('Face-Recognition/haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
def insert_or_update(id, name, age):
conn = sqlite3.connect("Face-Recognition/sqlite.db")
cmd = "SELECT * FROM STUDENTS WHERE ID=" + str(id)
cursor = conn.execute(cmd)
is_record_exist = 0
for row in cursor:
is_record_exist = 1
if is_record_exist == 1:
conn.execute("UPDATE STUDENTS SET NAME=?, AGE=? WHERE ID=?", (name.upper(), age, id))
else:
conn.execute("INSERT INTO STUDENTS (ID, NAME, AGE) values (?, ?, ?)", (id, name.upper(), age))
speech = f"{name} was added successfully"
engine.say(speech)
engine.runAndWait()
conn.commit()
conn.close()
id = input("Enter User Id: ")
name = input("Enter User Name: ")
age = input("Enter User Age: ")
insert_or_update(id, name, age)
sample_num = 0
global button_clicked
button_clicked = False
def on_mouse(event, x, y, flags, param):
global button_clicked
if event == cv2.EVENT_LBUTTONDOWN:
if check_button_click((10, 10), (200, 50), (x, y)):
button_clicked = True
cv2.namedWindow("Add Person")
cv2.setMouseCallback("Add Person", on_mouse)
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detect.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
sample_num += 1
cv2.imwrite(f"Face-Recognition/dataset/user.{id}.{sample_num}.jpg", gray[y:y + h, x:x + w])
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.waitKey(100)
draw_button(img, "Back", (10, 10), (200, 50))
cv2.imshow("Add Person", img)
if cv2.waitKey(1) & 0xFF == 27 or button_clicked:
break
if sample_num > 20:
break
cam.release()
cv2.destroyAllWindows()
recognizer = cv2.face.LBPHFaceRecognizer_create()
path = "Face-Recognition/dataset"
def get_images_with_id(path):
image_paths = [os.path.join(path, f) for f in os.listdir(path)]
faces = []
ids = []
for image_path in image_paths:
face_img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
id = int(os.path.split(image_path)[-1].split(".")[1])
faces.append(face_img)
ids.append(id)
cv2.imshow("Training", face_img)
cv2.waitKey(10)
return np.array(ids), faces
ids, faces = get_images_with_id(path)
recognizer.train(faces, ids)
recognizer.save("Face-Recognition/recognizer/trainingdata.yml")
cv2.destroyAllWindows()
main()
def remove_face():
conn = sqlite3.connect("Face-Recognition/sqlite.db")
cursor = conn.cursor()
name = input("Enter the name of the person to remove: ")
cursor.execute("SELECT ID FROM STUDENTS WHERE NAME=?", (name.upper(),))
rows = cursor.fetchall()
if rows:
for row in rows:
id = row[0]
cursor.execute("DELETE FROM STUDENTS WHERE ID=?", (id,))
dataset_path = "Face-Recognition/dataset"
for file in os.listdir(dataset_path):
if file.startswith(f"user.{id}."):
os.remove(os.path.join(dataset_path, file))
conn.commit()
print(f"Removed {name} from the database.")
speech = f"{name} was removed successfully"
engine.say(speech)
engine.runAndWait()
else:
print(f"No person named {name} found in the database.")
speech = f"No person named {name} found in the database"
engine.say(speech)
engine.runAndWait()
conn.close()
cv2.destroyAllWindows()
main()
def revokeMainfun():
cv2.VideoCapture(0).release()
cv2.destroyAllWindows()
main()
return
def navigate_to_person(name):
if not person_exists(name):
print(f"No person found with the name {name}.")
engine.say(f"No person found with the name {name}.")
engine.runAndWait()
return
cam = cv2.VideoCapture(0)
while True:
ret, frame = cam.read()
if not ret:
continue
results = model(frame)
detections = results[0].boxes
target_coordinates = None
for box in detections:
conf = box.conf[0]
cls = int(box.cls[0])
xyxy = box.xyxy[0]
if conf > 0.5 and model.names[cls] == 'person':
x1, y1, x2, y2 = map(int, xyxy)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
id, confidence = recognizer.predict(gray[y:y + h, x:x + w])
profile = get_profile(id)
if profile is not None and profile[1] == name:
target_coordinates = (x, y, w, h)
distance = estimate_distance((x, y, x + w, y + h))
cv2.putText(frame, f"Distance: {distance:.2f}m", (x1, y1 - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 2)
break
if target_coordinates:
x, y, w, h = target_coordinates
frame_center_x = frame.shape[1] // 2
object_center_x = x + w // 2
direction = ""
if distance <= 11:
direction = f"You have succesfully reached {name}."
engine.say(direction)
engine.runAndWait()
# cam.release()
# cv2.destroyAllWindows()
revokeMainfun()
break
else:
if object_center_x < frame_center_x - 100:
direction = "left"
elif object_center_x > frame_center_x + 100:
direction = "right"
else:
direction = "forward"
direction = f"Move {direction}. Distance to {name} is {distance:.2f} meters."
engine.say(direction)
engine.runAndWait()
else:
engine.say(f"Searching for {name}.")
engine.runAndWait()
cv2.imshow("Navigate to Person", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cam.release()
cv2.destroyAllWindows()
def main():
global button_clicked
button_clicked = False
def on_mouse(event, x, y, flags, param):
global button_clicked
if event == cv2.EVENT_LBUTTONDOWN:
if check_button_click((10, 10), (200, 50), (x, y)):
button_clicked = "environment_read"
elif check_button_click((10, 70), (200, 50), (x, y)):
button_clicked = "add_person"
elif check_button_click((10, 130), (200, 50), (x, y)):
button_clicked = "remove_face"
elif check_button_click((10, 190), (200, 50), (x, y)):
button_clicked = "navigate_to_person"
cv2.namedWindow("Main Menu")
cv2.setMouseCallback("Main Menu", on_mouse)
while True:
frame = np.zeros((600, 800, 3), np.uint8)
draw_button(frame, "Read Environment", (10, 10), (200, 50))
draw_button(frame, "Add Person", (10, 70), (200, 50))
draw_button(frame, "Remove Person", (10, 130), (200, 50))
draw_button(frame, "Navigate to Person", (10, 190), (200, 50))
cv2.imshow("Main Menu", frame)
if cv2.waitKey(1) & 0xFF == 27 or button_clicked:
break
if button_clicked:
cv2.destroyWindow("Main Menu")
if button_clicked == "environment_read":
environment_read()
elif button_clicked == "add_person":
add_person()
elif button_clicked == "remove_face":
remove_face()
elif button_clicked == "navigate_to_person":
target_name = input("Enter the name of the person to navigate to: ")
target_name=target_name.upper()
if person_exists(target_name):
navigate_to_person(target_name)
else:
print(f"No person named {target_name} found in the database.")
speech = f"No person named {target_name} found in the database"
engine.say(speech)
engine.runAndWait()
main()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
# Version (V - 2.1)