Timsollarni tanib olish tizimlari fanidan 7-amaliy mashg’ulot Mavzu: Rangli tasvirni qayta ishlash asoslari Ishdan maqsad



Yüklə 62,18 Kb.
səhifə2/3
tarix13.12.2023
ölçüsü62,18 Kb.
#174487
1   2   3
2-dedline (1)

Natija:

8-amaliy mashg’ulot
Mavzu: Ko‘zni qorachig‘i orqali tanib olish
Amaliy qism:
Code:
import cv2
import numpy as np

# Load the image


image = cv2.imread('eye_image.jpg')

# Convert the image to grayscale


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Gaussian blur to reduce noise


gray = cv2.GaussianBlur(gray, (11, 11), 0)

# Use Hough Circle Transform to detect circular shapes (pupils)


circles = cv2.HoughCircles(
gray,
cv2.HOUGH_GRADIENT,
dp=1,
minDist=20,
param1=50,
param2=30,
minRadius=5,
maxRadius=30
)

if circles is not None:


circles = np.uint16(np.around(circles))
for circle in circles[0, :]:
center = (circle[0], circle[1])
radius = circle[2]
cv2.circle(image, center, radius, (0, 0, 255), 2)

# Display the image with pupils marked


cv2.imshow("Pupil Detection", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Natija:



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()


Yüklə 62,18 Kb.

Dostları ilə paylaş:
1   2   3




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin