0

This is my code:

import cv2
cam=cv2.VideoCapture(0,cv2.CAP_DSHOW)
cam.set(3,640)
cam.set(4,480)
detector=cv2.CascadeClassifier('C:\\Users\\Hp\\Downloads\\8f51e58ac0813cb695f3733926c77f52- 
07eed8d5486b1abff88d7e34891f1326a9b6a6f5\\8f51e58ac0813cb695f3733926c77f52- 
07eed8d5486b1abff88d7e34891f1326a9b6a6f5\\haarcascade_frontalface_default.xml')
face_id=input("Enter a numeric face id: ")
print("taking sample, look at camera......")
count=0
while True:
      ret, img=cam.read('C:\\Users\\Hp\\Desktop\\leo software 
      company\\gamedata\\.vscode\\Maya.Ai')
      converted_image=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      faces=detector.detectMultiScale(converted_image, 1.3, 5)
      for (x,y,w,h) in faces:
          cv2.rectangle(img, (x,y),(x+w,y+h),(255,0,0),2)
          count += 1
          cv2.imwrite("samples/face."+str(face_id)+'.'+str(count)+".jpg", 
          converted_image[y:y+h,x:x+w])
          cv2.imshow('image',img)
          k=cv2.waitKey(100) & 0xff
          if k == 27:
          break
         elif count>=10:
         break
         print("id made succesfully")
         cam.release()
         cv2.destroyAllWindows

I am getting this error:

Traceback (most recent call last):
  File "c:\Users\Hp\Desktop\leo software company\game data\.vscode\Maya.Ai\id.py", line 10, in <module>
    ret, img=cam.read('C:\\Users\\Hp\\Desktop\\leo software company\\game data\\.vscode\\Maya.Ai')
cv2.error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'read'
> Overload resolution failed:
>  - image is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'image'
CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
SCI FI
  • 1
  • 1

1 Answers1

0

It might help to look at the tutorial from the documentation. You can initialize your VideoCapture to read from a connected camera (as you have done) or from a saved file, as it appears that you intend. After creating the VideoCapture object, its read method doesn't require any arguments, it just grabs the next frame.

So you probably want something like:

cam = cv2.VideoCapture(
      r'C:\Users\Hp\Desktop\leo software company\gamedata\.vscode\Maya.Ai',
      cv2.CAP_DSHOW
)

And then:

ret, img = cam.read()

Also: The way you show your strings split across lines would throw a SyntaxError, so your actual code must not look like that. And you can use a raw string literal to avoid needing to double all your backslashes for Windows file paths.

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25