Syntax Error in code

import cv2
import numpy as np

Initiate camera
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_alt.xml’)
skip = 0
face_data = []
dataset_path =‘D:\Java_Projects\Machine Learning’
face_section =[]
filename = input("enter name ")
while True:
ret,frame = cap.read()

if ret == False:
continue
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(frame,1.3,5)
faces = sorted(faces,key=lambda f:f[2]*f[3])

for face in faces[-1:]:
x,y,w,h = face
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)

offset = 10
face_section = frame[y-offset:y+h+offset,x-offset:x+w+offset]
#face_section = cv2.resize(face_section,(100,100))

skip +=1
if skip%10 ==0 :
face_data.append(face_section)
print(len(face_data))

cv2.imshow(“frame”,frame)
cv2.imshow(“face_section”,face_section)

key_pressed = cv2.waitKey(1) &0xFF
if key_pressed == ord(‘q’):
break
face_data = np.asarray(face_data)
face_data = face_data.reshape((face_data.shape([0],-1))
print(face_data.shape())

np.save(dataset_path+filename+’.npy’,face_data)
print(“data saved”)
cap.release()
cap.destroyAllWindows()

This is my code.
I am getting an invalid syntax error on the print(face_data.shape()) line. If i comment that line the invalid syntax error goes to the np.save line.
I am unable to figure this out .
Please help.

Hi @rehan123mahajan_da9155725078172c

regrading your print statement error use np.shape function instead of shape()

regrading error in line np.save ; In dataset_path variable you should replace " \ " with " / "
np.save wasn’t receiving the correct file path

Hope this might helps :slight_smile:

Still doesnt’t work.
It still shows syntax error when print(face_data.np.shape()) is used.
Moreover i tried to comment out these 4 lines :

face_data = face_data.reshape((face_data.shape([0],-1))
print(face_data.shape())

np.save(dataset_path+filename+’.npy’,face_data)
print(“data saved”)

Now these errors pops up :
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify ‘dtype=object’ when creating the ndarray
return array(a, dtype, copy=False, order=order)

AttributeError: ‘cv2.VideoCapture’ object has no attribute ‘destroyAllWindows’

Hi @rehan123mahajan_da9155725078172c

It should rather be print(np.shape(face_data)) and regrading saving " .npy file" as such there isn’t any error, there just a warning which can be ignored

The error that you facing is because of this statement

It should be cv2.destroyAllWindows() instead of cap.destroyAllWindows()

Hope this might help :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.