-
-
Save sidgan/4e1e54c93aec505070d6220a7881e2d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import string, random | |
vc = cv2.VideoCapture(0) | |
if vc.isOpened(): # try to get the first frame | |
rval, frame = vc.read() | |
else: | |
rval = False | |
def write_bb_on_image(image): | |
face_cascade_path = '' | |
face_cascade = cv2.CascadeClassifier(face_cascade_path) | |
scale_factor = 1.1 | |
min_neighbors = 3 | |
min_size = (30, 30) | |
outfname = "" | |
flags = cv2.CASCADE_SCALE_IMAGE | |
faces = face_cascade.detectMultiScale(image, scaleFactor = scale_factor, minNeighbors = min_neighbors, \ | |
minSize = min_size, flags = flags) | |
#( x, y, w, h ) in faces | |
return faces | |
def grab_face(x,y,w,h,frame): | |
#a is array of size 96x96x3 | |
#a = 0 | |
a = frame[y: y + h, x: x + w] | |
#reshape to 96x96x3 | |
resized_image = cv2.resize(a, (96, 96)) | |
return a | |
def recognize_person(grabbed_faces): | |
print len(grabbed_faces) | |
#generate random names | |
names = ''.join(random.sample(string.ascii_lowercase, len(bbs))) | |
return names | |
#RECOGNIZE ONLY ONE PERSON | |
while rval: | |
rgbImg = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
bbs = write_bb_on_image(rgbImg) | |
#limit number of faces to only 4 | |
bbs = bbs[:4] | |
#send x,y,w,h and 96x96x3 to recognize | |
grabbed_faces = [0]*len(bbs) | |
for index in range(len(bbs)): | |
(x, y, w, h ) = bbs[index] | |
grabbed_faces[index] = grab_face(x,y,w,h,frame) | |
returned_box = recognize_person(grabbed_faces) | |
for index in range(len(returned_box)): | |
(x, y, w, h ) = bbs[index] | |
name_of_person = list(returned_box)[index] | |
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2) | |
cv2.putText(frame,name_of_person, (int(x),int(y)), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 6) | |
cv2.imshow('see this', frame) | |
rval, frame = vc.read() | |
key = cv2.waitKey(20) | |
if key == 27: # exit on ESC | |
break | |
cv2.destroyWindow("OpenCV") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml