1

Using opencv-contrib-python, while reading this line:

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

I'm getting an error:

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

My source code:

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])
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Livin
  • 166
  • 3
  • 8

1 Answers1

0

Instead of using the line:

ret, im1 = cam.read() 

try using this line:

 im1 = cv2.VideoCapture(0)

It will solve the error as here, you only need to capture the image and it could be done using the above line.

Anshul Vyas
  • 633
  • 9
  • 19
DujSn28
  • 13
  • 3