-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen_cv_video_read.py
53 lines (43 loc) · 1.29 KB
/
open_cv_video_read.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import cv2
import numpy as np
#Read a Video Stream and Display It
#Camera Object
cam = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
face_data = []
cnt = 0
user_name = input("enter your name")
while True:
ret,frame = cam.read()
if ret==False:
print("Something Went Wrong!")
continue
key_pressed = cv2.waitKey(1)&0xFF #Bitmasking to get last 8 bits
if key_pressed==ord('q'): #ord-->ASCII Value(8 bit)
break
faces = face_cascade.detectMultiScale(frame,1.3,5)
#print(faces)
if(len(faces)==0):
cv2.imshow("Video",frame)
continue
for face in faces:
x,y,w,h = face
face_section = frame[y-10:y+h+10,x-10:x+w+10];
face_section = cv2.resize(face_section,(100,100))
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)
if cnt%10==0:
print("Taking picture ",int(cnt/10))
face_data.append(face_section)
cnt +=1
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow("Video",frame)
cv2.imshow("Video Gray",face_section)
#Save the face data in a numpy file
print("Total Faces" ,len(face_data))
face_data = np.array(face_data)
face_data = face_data.reshape((face_data.shape[0],-1))
np.save("FaceData/"+user_name+".npy",face_data)
print("Saved at FaceData/"+user_name+".npy")
print(face_data.shape)
cam.release()
cv2.destroyAllWindows()