JavaScript
x
31
31
1
def face_reco(self):
2
def draw_boundary(img, classifier, scaleFactor, minNeighbors, color, text, classif):
3
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
4
features = classifier.detectMultiScale(
5
gray_image, scaleFactor, minNeighbors)
6
coord = []
7
8
for (x, y, w, h) in features:
9
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 3)
10
id, predict = classif.predict(gray_image[y:y+h, x:x+w])
11
confidence = int((100*(1-predict/300)))
12
13
conn = mysql.connector.connect(
14
host="localhost", username="root", password="jas@06111507", database="userdetails")
15
my_cursor = conn.cursor()
16
17
my_cursor.execute(
18
"Select employeename from employee where employeeid="+str(id))
19
n = my_cursor.fetchone()
20
n = "+".join(n)
21
if confidence > 77:
22
cv2.putText(
23
img, f"NAME:{n}", (x, y, -30), cv2.FONT_HERSHEY_COMPLEX, 0.8, (255, 255, 255), 3)
24
else:
25
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 3)
26
cv2.putText(img, "Face cannot be detected", (x, y, -5),
27
cv2.FONT_HERSHEY_COMPLEX, 0.8, (255, 255, 255), 3)
28
coord = [x, y, w, h]
29
30
31
Advertisement
Answer
In fact, this is a problem caused by the format of the image itself. There is an incompatibility issue with opencv. I don’t know if it’s a bug. This problem can be solved in two ways.
JavaScript
1
2
1
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
2
or
JavaScript
1
2
1
img = img.copy()
2