Распознавание образов
МИНИСТЕРСТВО ПРОСВЕЩЕНИЯ РОССИЙСКОЙ ФЕДЕРАЦИИ
ФЕДЕРАЛЬНОЕ ГОСУДАРСТВЕННОЕ БЮДЖЕТНОЕ ОБРАЗОВАТЕЛЬНОЕ УЧРЕЖДЕНИЕ ВЫСШЕГО ОБРАЗОВАНИЯ
«НОВОСИБИРСКИЙ ГОСУДАРСТВЕННЫЙ ПЕДАГОГИЧЕСКИЙ УНИВЕРСИТЕТ»
ИНСТИТУТ ФИЗИКО-МАТЕМАТИЧЕСКОГО, ИНФОРМАЦИОННОГО И ТЕХНОЛОГИЧЕСКОГО ОБРАЗОВАНИЯ
КАФЕДРА ИНФОРМАЦИОННЫХ СИСТЕМ И ЦИФРОВОГО ОБРАЗОВАНИЯ
ПРАКТИЧЕСКАЯ РАБОТА
по дисциплине «Технологии искусственного интеллекта»
Распознавание образов
Выполнил студент группы 3. 008. 2. 21 ИСиТвО
Денега Владислав
Проверил Розов К. В.
Новосибирск 2022
1. 1.
Код:
import cv2
face_cascade = cv2. CascadeClassifier(". \haarcascades\haarcascade_frontalface_default. xml" ) eye_cascade = cv2. CascadeClassifier(". \haarcascades\haarcascade_eye. xml" ) smile_cascade = cv2. CascadeClassifier(". \haarcascades\haarcascade_smile. xml" ) #change to gray img = cv2. imread(". \somefaces\oneface4.jpg" ) gray = cv2. cvtColor(img, cv2. COLOR_BGR2GRAY) #detect objects(head). for better spotting increase or decrease second and third argument faces = face_cascade. detectMultiScale(gray, 3. 8, 3) #highlight objects for x, y, w, h in faces: cv2. rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) # looking for eyes roi_color = img[y: y + h, x: x + w] roi_gray = gray[y: y + h, x: x + w] # detect objects(eyes). for better spotting increase or decrease second and third argument eyes = eye_cascade. detectMultiScale(roi_gray, 3. 9, 8) for ex, ey, ew, eh in eyes: cv2. rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) smile = smile_cascade. detectMultiScale(roi_gray, 3. 9, 8) for sx, sy, sw, sh in smile: cv2. rectangle(roi_color, (sx, sy), (sx + sw, sy + sh), (0, 0, 255), 2) # display window with img cv2. imshow('Face detected', img) #close window after click cv2. waitKey(0) cv2. destroyAllWindows()
1. 2.
Код:
import cv2
face_cascade = cv2. CascadeClassifier(". \haarcascades\haarcascade_frontalface_default. xml" ) eye_cascade = cv2. CascadeClassifier(". \haarcascades\haarcascade_eye. xml" ) smile_cascade = cv2. CascadeClassifier(". \haarcascades\haarcascade_smile. xml" ) cam = cv2. VideoCapture(". \Video\Film. mp4" )
while True: _, frame = cam. read() if frame is None: break
if cv2. waitKey(1) == 27: break gray = cv2. cvtColor(frame, cv2. COLOR_BGR2GRAY) faces = face_cascade. detectMultiScale(gray, 3, 5) for x, y, w, h in faces: cv2. rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # looking for eyes roi_color = frame[y: y + h, x: x + w] roi_gray = gray[y: y + h, x: x + w] # detect objects(eyes). for better spotting increase or decrease second and third argument eyes = eye_cascade. detectMultiScale(roi_gray, 2. 2, 4) for ex, ey, ew, eh in eyes: cv2. rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) cv2. imshow('Face detected', frame) cv2. destroyAllWindows() cam. release()
2.
Код:
import cv2
face_cascade = cv2. CascadeClassifier(". \haarcascades\haarcascade_frontalface_default. xml" ) eye_cascade = cv2. CascadeClassifier(". \haarcascades\haarcascade_eye. xml" ) smile_cascade = cv2. CascadeClassifier(". \haarcascades\haarcascade_smile. xml" ) cam = cv2. VideoCapture(". \Video\Film. mp4" )
while True: _, frame = cam. read() if frame is None: break
if cv2. waitKey(1) == 27: break gray = cv2. cvtColor(frame, cv2. COLOR_BGR2GRAY) faces = face_cascade. detectMultiScale(gray, 3, 5) for x, y, w, h in faces: cv2. rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # looking for eyes roi_color = frame[y: y + h, x: x + w] roi_gray = gray[y: y + h, x: x + w] # detect objects(eyes). for better spotting increase or decrease second and third argument eyes = eye_cascade. detectMultiScale(roi_gray, 2. 2, 4) for ex, ey, ew, eh in eyes: cv2. rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) smile = smile_cascade. detectMultiScale(roi_gray, 2. 8, 8) for sx, sy, sw, sh in smile: cv2. rectangle(roi_color, (sx, sy), (sx + sw, sy + sh), (0, 0, 255), 2) cv2. imshow('Face detected', frame) cv2. destroyAllWindows() cam. release()
3. 1.
Код:
import cv2
number_cascade = cv2. CascadeClassifier(". \haarcascades\haarcascade_russian_plate_number. xml" )
img = cv2. imread(" Cars/photo3.jpg" ) gray = cv2. cvtColor(img, cv2. COLOR_BGR2GRAY)
numbers = number_cascade. detectMultiScale(gray, 3. 5, 3) for x, y, w, h in numbers: cv2. rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2. imshow('Face detected', img) cv2. waitKey(0) cv2. destroyAllWindows() 4. 1.
4. 2.
Код:
import cv2
catface_cascade = cv2. CascadeClassifier('haarcascades/haarcascade_frontalcatface_extended. xml')
img = cv2. imread('faces/cat_5.jpg') gray = cv2. cvtColor(img, cv2. COLOR_BGR2GRAY)
catface =catface_cascade. detectMultiScale(gray, scaleFactor=1. 04, minNeighbors=7)
for x, y, w, h in catface: cv2. rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)
cv2. imshow('Face detect', img)
cv2. waitKey(0) cv2. destroyAllWindows()
4. 3.
4. 4
Код:
import cv2
face_cascade = cv2. CascadeClassifier('haarcascades/haarcascade_frontalcatface. xml')
img = cv2. imread(" Cats/cat1.jpg" ) gray = cv2. cvtColor(img, cv2. COLOR_BGR2GRAY)
faces = face_cascade. detectMultiScale(gray, 1. 05, 3) #highlight objects for i, (x, y, w, h ) in enumerate(faces): radius = 100 center = (x+radius, y+radius) cv2. putText(img, 'Cat {}'. format(i+1), (x, y-10), cv2. FONT_HERSHEY_SIMPLEX, 0. 7, (0, 0, 255), 2) cv2. circle(img, center, radius, (255, 0, 0), 2) cv2. imshow('Face detected', img) #close window after click cv2. waitKey(0) cv2. destroyAllWindows()
|