Dasturiy injiniringi va raqamli iqtisodiyoti


Download 65.06 Kb.
bet5/5
Sana11.11.2023
Hajmi65.06 Kb.
#1767144
1   2   3   4   5
Bog'liq
Ibrohimjon

Kiritilgan rasm:



9-amaliy mashg’ulot
Mavzu: Yuzning old qismini tanib olish
Amaliy qism:
Code:
import itertools
import os
import pickle
from pathlib import Path

import cv2


import dlib
import face_recognition
import numpy as np

folder_path = '../Images/users'


face_detector = dlib.get_frontal_face_detector()
known_faces_folder = "../face_encodings"
known_face_encodings = []
known_face_names = []
def save_face_encoding(name, face_encoding):
file_path = os.path.join(known_faces_folder, f'{name}.pkl')
os.makedirs(known_faces_folder, exist_ok=True)
with open(file_path, 'wb') as file:
pickle.dump(face_encoding, file)

def generate_encodings():


dir=folder_path
if dir[-1]!='/':
dir += '/'
train_dir = os.listdir(dir)
for person in train_dir:
pix = os.listdir(dir + person)
for person_img in pix:
file_path = os.path.join(known_faces_folder, f'{person}{person_img}.pkl')

if os.path.exists(file_path):


with open(file_path, 'rb') as file:
face_enc = pickle.load(file)
known_face_encodings.append(face_enc)
known_face_names.append(person)
else:
face = face_recognition.load_image_file(
dir + person + "/" + person_img)
face_bounding_boxes = face_recognition.face_locations(face)
if len(face_bounding_boxes) > 0:
face_enc = face_recognition.face_encodings(face)[0]
save_face_encoding(f'{person}{person_img}',face_enc)
known_face_encodings.append(face_enc)
known_face_names.append(person)

generate_encodings()

video_capture = cv2.VideoCapture("videos/dinner.mp4") # Use 0 for the default camera, or provide the camera's index
cnt = 0
while True:
if cnt !=3:
cnt+=1
continue
else:
cnt=0
ret, frame = video_capture.read()
if not ret:
break
# Find all face locations in the current frame using dlib's face detector
face_locations = face_detector(frame)

# Convert the dlib rectangles to tuples (top, right, bottom, left)


face_locations = [(rect.top(), rect.right(), rect.bottom(), rect.left()) for rect in face_locations]

# Loop through the face locations found in the current frame


# Loop through the face locations found in the current frame
for top, right, bottom, left in face_locations:
face_image = frame[top:bottom, left:right]

# Convert face_image to the correct data type (np.uint8)


face_image = np.array(face_image, dtype=np.uint8)

face_encodings = face_recognition.face_encodings(face_image)

if len(face_encodings) > 0:
# Compare the current face encoding with known face encodings
matches = face_recognition.compare_faces(known_face_encodings, face_encodings[0])
name = "Unknown"

# If a match is found, use the name of the known person


if True in matches:
match_index = matches.index(True)
name = known_face_names[match_index]
# Draw a rectangle and label around the recognized face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

# Display the resulting frame


cv2.imshow('Video', frame)

# Break the loop when 'q' is pressed


if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the video capture and close OpenCV windows


video_capture.release()
cv2.destroyAllWindows()
Natija:

Download 65.06 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling