module' cv2.cv2'没有属性' PutText'

时间:2018-02-16 15:05:07

标签: python-3.x

在Python 3.6.4上运行OpenCV 3.2 Windows 10以下代码抛出错误

AttributeError:' module'对象没有属性' PutText'

import cv2 
import numpy as np

face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
vid_cam = cv2.VideoCapture(0)
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("recognizer\\trainingData.yml")
id=0
font = cv2.FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1
while(True):
    ret, image_frame = vid_cam.read()
    gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY)
    faces = face_detector.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(image_frame, (x,y), (x+w,y+h), (0,0,255), 2)
        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        cv2.PutText(cv2.fromarray(im),str(Id), (x,y+h),font, 255)
        cv2.waitKey(100);
    cv2.imshow('Face', image_frame)
    if cv2.waitKey(1)==ord('q'):
        break
vid_cam.release()
cv2.destroyAllWindows()

2 个答案:

答案 0 :(得分:1)

如果您使用的是opencv == 4.1.0和python3.x,请使用以下命令将文字写在图像上。

cv2.putText(image,str(name), (x,y),cv2.FONT_HERSHEY_SIMPLEX, 1, 255)

答案 1 :(得分:0)

不应该有资本' P'。该函数应为cv2.putText()(请注意小写' p')。