【问题标题】:OpenCv: change position of putText()OpenCv:改变 putText() 的位置
【发布时间】:2021-07-11 20:19:32
【问题描述】:

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

代码

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
    

【问题讨论】:

  • 你必须手动更改(50, 50)(40, 50)(30, 50)中的y(第二个值)

标签: python opencv artificial-intelligence


【解决方案1】:

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

【讨论】:

  • 谢谢,忘了x是第一个值,y是第二个值。解释为什么我以前数学课不及格
  • 我想知道 CV 是否具有测量字体高度以自动更改 y 的功能 - 我找到了 getTextSize - 我添加了 getTextSize 的示例
  • 顺便说一句:图形模块和 GUI 使用 x,y,但数学模块(如 numpypandas)和 GEO 位置使用 y,x - 有时会出现问题 :)
猜你喜欢
  • 2015-02-23
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
相关资源
最近更新 更多