I made a deep learning program that uses the webcam to recognize a persons emotion, race and gender. The text shows a persons characteristics is inside of each other. How can I move them underneath each other?
The code
while cap.isOpened():
ret, frame = cap.read()
result = DeepFace.analyze(frame, actions=['emotion', "race", "gender"], enforce_detection=False)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,1.1,4)
for(x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (50, 50, 50), 2)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame,
result['dominant_emotion'],
(50, 50),
font, 1,
(220, 220, 220),
2,
cv2.LINE_4)
cv2.putText(frame,
result['gender'],
(40, 50),
font, 1,
(220, 220, 220),
2,
cv2.LINE_4)
cv2.putText(frame,
result['dominant_race'],
(30, 50),
font, 1,
(220, 220, 220),
2,
cv2.LINE_4)
cv2.imshow('Facial rec.', frame)
if cv2.waitKey(2) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows
Change y
- second value in (50,50), (40,50), (30,50)
- ie. (50,50), (50,80), (50,110)
Minimal working code
import cv2
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
result = {'dominant_emotion': 'hello', "gender": 'world', "dominant_race": 'of python'}
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame,
result['dominant_emotion'],
(50, 50),
font, 1,
(220, 220, 220),
2,
cv2.LINE_4)
cv2.putText(frame,
result['gender'],
(50, 80),
font, 1,
(220, 220, 220),
2,
cv2.LINE_4)
cv2.putText(frame,
result['dominant_race'],
(50, 110),
font, 1,
(220, 220, 220),
2,
cv2.LINE_4)
cv2.imshow('Facial rec.', frame)
if cv2.waitKey(2) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Result:
EDIT:
cv2
has also getTextSize()
to calculate text height and use it to set position for next line.
(width, height), baseline = cv2.getTextSize(text, font, font_scale, font_thickness)
import cv2
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
result = {'dominant_emotion': 'hello', "gender": 'world', "dominant_race": 'of python'}
font = cv2.FONT_HERSHEY_DUPLEX
font_scale = 1
font_thickness = 2
x = 50
y = 50
for text in result.values():
cv2.putText(frame,
text,
(x, y),
font, font_scale,
(220, 220, 220),
font_thickness,
cv2.LINE_4)
(width, height), baseline = cv2.getTextSize(text, font, font_scale, font_thickness)
y += (height + 10) # +10 margin
cv2.imshow('Facial rec.', frame)
if cv2.waitKey(2) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()