-1

opencv-contrib-python在阅读此行时使用,:

cam = cv2.FONT_HERSHEY_SIMPLEX ret,img= cam.read()

我收到一个错误:

ret,trackImg= cam.read() AttributeError: 'int' object has no attribute 'read'**

我的源代码:

def TrackImages():
  recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("TrainingImageLabel\Trainner.yml")
harcascadePath = "haarcascaade_frontface_default.xml"
faceCascade = cv2.CascadeClassifier(harcascadePath)
df = pd.read_csv("StudentDetails\studentDetails.csv")
cam = cv2.FONT_HERSHEY_SIMPLEX
col_names = ['Id', 'Name', 'Date', 'Time']
attendance = pd.DataFrame(columns = col_names)
while True:
  ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)

for (x, y, w, h) in faces:
  cv2.rectangle("test", (x, y), (x + w, y + h), (255, 0, 0), 2)
Id, conf = recognizer.predict(gray[y: y + h, x: x + w])
if (conf < 50):
  ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%m,%s')
aa = df.loc[df['Id'] == Id]['Name'].values
tt = str(Id) + "-" + aa
attendance.loc[len(attendance)] = [Id, aa, date, timestamp]
else :
  Id = 'Unknown'
tt = str(Id)

if (conf > 75):
  noOfFile = len(os.listdir("ImageUnknown")) + 1
cv2.imwrite("ImageUnknown\Image" + str(noOfFile) + ".jpg", img[y: y + h, x: x + w])
4

1 回答 1

0

而不是使用该行:

ret, im1 = cam.read() 

尝试使用这一行:

 im1 = cv2.VideoCapture(0)

它将解决这里的错误,您只需要捕获图像,就可以使用上面的行来完成。

于 2020-05-21T16:01:28.893 回答