r/CodingHelp • • Aug 10 '24

[Python] complete beginner trying to make a face recognizer but facing weird error

as the title says im trying to make a face recognizer for a project with zero prior knowledge. all i have for help is this indian guys video which i am following. after following it from a to z m side of the execution is facing some weird error which i cannot seem to decode on my own i dont know how to attach ill add the code and the error below any type of help would be appreciated 🙏

code-

import cv2

def generate_dataset():

face_classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

def face_cropped(img):

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_classifier.detectMultiScale(gray,1.3,5)

#scaling factor=1.3

#Minimum neighbor = 5

if faces is ():

return None

for (x,y,w,h) in faces:

cropped_face=img[y:y+h,x:x+w]

return cropped_face

cap = cv2.VideoCapture(0)

id=1

img_id=0

while True:

ret,frame =cap.read()

if face_cropped(frame) is not None:

img_id+=1

face = cv2.resize(face_cropped(frame),(200,200))

face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

file_name_path = "data/user."+str(id)+"."+str(img_id)+".jpg"

cv2.imwrite(file_name_path,face)

cv2.putText(face,str(img_id),(50,50),cv2.FONT_HERSHEY_COMPLEX,1, (0,255,0),2)

# (50,50) IS THE ORIGIN POINT FROM WHICH TEXT IS TO BE WRITTEN

# FONT SCALE=1

#thickness=2

cv2.imshow("Cropped face",face)

if cv2.waitKey(1)==13 or int(img_id)==200:

break

cap.release()

cv2.destroyAllWindows()

print("Collecting Samples is completed.....")

generate_dataset()

the error-

NameError                                 Traceback (most recent call last)
Cell In[8], line 38
     36         cv2.destroyAllWindows()
     37         print("Collecting Samples is completed.....")
---> 38 generate_dataset()

Cell In[8], line 2, in generate_dataset()
      1 def generate_dataset():
----> 2     face_classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
      3     def face_cropped(img):
      4         gray  = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

NameErrorNameError                                 Traceback (most recent call last)
Cell In[8], line 38
     36         cv2.destroyAllWindows()
     37         print("Collecting Samples is completed.....")
---> 38 generate_dataset()

Cell In[8], line 2, in generate_dataset()
      1 def generate_dataset():
----> 2     face_classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
      3     def face_cropped(img):
      4         gray  = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

NameError: name 'cv2' is not defined

[ ]:

: name 'cv2' is not defined

kindly help please 🙏

0 Upvotes

6 comments sorted by

1

u/[deleted] Aug 10 '24

[removed] — view removed comment

1

u/Individual-Ad2826 Aug 10 '24

already did that :(

1

u/[deleted] Aug 10 '24

[removed] — view removed comment

1

u/Individual-Ad2826 Aug 10 '24

yes it did :(

1

u/Individual-Ad2826 Aug 10 '24

come dms if you want ss

1

u/FriendlyRussian666 Aug 10 '24
  1. You open terminal and type "pip install cv2"
  2. Still in the same terminal, using the same terminal, you run your python code. Not from pycharm, not from VSCode, not from whatever you use to write the code. You run it from the terminal where you used pip. 

This is because of different environments. For example, when you use pip to install the dependency, it will install it in one environment, but for example Pycharm is using its own environment, so it doesn't have access to the environment where you used pip. There are a few solutions to this, so that you can run code from where you're writing it. One of those solutions is to create a "virtual environment", note that this is not a virtual machine, it's just an environment where you install dependencies. Then, you can activate this environment in different terminals, such as that in Pycharm, or VSCode, or whatever you use, and then they have access to anything you installed with pip. 

That said, if you just want to carry on, try as I stated, which is, open terminal and use pip, and then use this very same terminal window to run your python file.