OpenCv:改变 putText() 的位置

OpenCv: change position of putText()

我制作了一个深度学习程序,使用网络摄像头识别一个人的情绪、种族和性别。文本显示一个人的特征是彼此内在的。如何将它们移动到彼此下方?

代码

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
    

更改 y - (50,50), (40,50), (30,50) 中的第二个值 - 即。 (50,50), (50,80), (50,110)


最少的工作代码

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()

结果:


编辑:

cv2 也有 getTextSize() 计算文本高度并用它来设置下一行的位置。

(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()