-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecognize.py
35 lines (23 loc) · 1003 Bytes
/
recognize.py
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
"""Image Recognition"""
#pylint:disable=no-member
import numpy as np
import cv2 as cv
haar_cascade = cv.CascadeClassifier('haar_face.xml')
objects = ['Building', 'Food', 'Other', 'People']
# features = np.load('features.npy', allow_pickle=True)
# labels = np.load('labels.npy')
object_recognizer = cv.face.LBPHFaceRecognizer_create()
object_recognizer.read('object_trained.yml')
img = cv.imread('Images/val/test.jpeg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Objects', gray)
# Detect the face in the image
faces_rect = haar_cascade.detectMultiScale(gray, 1.1, 4)
for (x,y,w,h) in faces_rect:
faces_roi = gray[y:y+h,x:x+w]
label, confidence = object_recognizer.predict(faces_roi)
print(f'Label = {objects[label]} with a confidence of {confidence}')
cv.putText(img, str(objects[label]), (20,20), cv.FONT_HERSHEY_COMPLEX, 1.0, (0,255,0), thickness=2)
cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)
cv.imshow('Detected Objects', img)
cv.waitKey(0)