python - AttributeError: 'int' 对象没有属性 'read'
问题描述
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])
解决方案
而不是使用该行:
ret, im1 = cam.read()
尝试使用这一行:
im1 = cv2.VideoCapture(0)
它将解决这里的错误,您只需要捕获图像,就可以使用上面的行来完成。
推荐阅读
- java - InjectMocks 类的方法返回 Null
- python - Python socket .connect()“提供了一个无效的参数”(OSError:[WinError 10022])
- reactjs - 如何在两个组件的原生反应中导入和使用函数?
- peoplesoft - 将 winmessage 用于方法外的变量时出错
- asp.net-core - 是否可以使用 Sustainsys.Saml2 在 Startup.cs 之外更改 SAML IdentityProvider 选项?
- java - 使用 GWT.Create 和 UI-Binder 创建实例的流程
- r - 案例编号是否等于 Lime 中的数据行编号?
- web3 - web3.eth.accounts.create 和 web3.eth.personal.newAccount 都不起作用
- javascript - 使用 AngularJS 进行验证
- arrays - 在 VBA 中声明数组变量