我制作了一个使用网络摄像头识别人的情绪、种族和性别的深度学习程序。文本显示了一个人的特征是相互嵌套的。我该如何将它们移动到彼此下面呢?
代码
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'): breakcap.release()cv2.destroyAllWindows
回答:
更改y
– 在(50,50), (40,50), (30,50)
中的第二个值 – 例如(50,50), (50,80), (50,110)
最小工作代码
import cv2cap = 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'): breakcap.release()cv2.destroyAllWindows()
结果:
编辑:
cv2
还有getTextSize()
函数来计算文本高度,并用它来设置下一行的位置。
(width, height), baseline = cv2.getTextSize(text, font, font_scale, font_thickness)
import cv2cap = 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的边距 cv2.imshow('Facial rec.', frame) if cv2.waitKey(2) & 0xFF == ord('q'): breakcap.release()cv2.destroyAllWindows()