def face_identify(tmp): global current_uid global current_ufname global current_face_emotion global CONSUMER_KEY global CONSUMER_SECRET global ACCESS_TOKEN global REFRESH_TOKEN detected_personid = '' cascPath = 'C:\\Users\\ADMIN\\SM\\haarcascade_frontalface_default.xml' faceCascade = cv2.CascadeClassifier(cascPath) ramp_frames = 10 print ("Face identification started ..........") cam = cv2.VideoCapture(1) try: while True: for i in range(ramp_frames): s, im = cam.read() ret,image = cam.read() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #cv2.imshow('Recognizing you...', gray) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE ) # Draw a rectangle around the faces max_area = 0 mx = 0 my = 0 mh = 0 mw = 0 for (x, y, w, h) in faces: #cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) if w*h > max_area: mx = x my = y mh = h mw = w max_area=w*h if max_area>=15000: image_crop = image[my:my+mh,mx:mx+mw] file_name = id_generator()+'.jpg' file = os.path.join(tmp_path,file_name) cloudinary_url=cloudinary_tmp + '/' + file_name cv2.imwrite(file, image_crop) imup.upload_image(file,file_name) faceid=msface.face_detect(cloudinary_url) faceEmotion=msface.emotion_detect(cloudinary_url) if faceEmotion=='happiness': faceEmotion='happy' elif faceEmotion=='sadness': faceEmotion='sad' elif faceEmotion=='disgust': faceEmotion='disgusting' print ("faceId = " + str(faceid)) print("Emotion detected = " + str(faceEmotion)) detected_personid = msface.face_identify(faceid) print ("detected_personid = " + str(detected_personid)) else: continue if detected_personid: comm = "SELECT * FROM user WHERE personid = '%s'" % detected_personid res = cursor.execute(comm) res = cursor.fetchone() if res: CONSUMER_KEY=res[8] CONSUMER_SECRET=res[9] ACCESS_TOKEN=res[10] REFRESH_TOKEN=res[11] current_uid = res[7] current_ufname = res[1] fname = res[1] current_face_emotion=faceEmotion print ("Welcome "+fname+"!"+"You are feeling "+current_face_emotion+" today!") return else: print ("person id not found in database") else: print ("Unknown person found") except Exception as e: print ("Errors occured !") print (e) cam.release() cv2.destroyAllWindows()
def update_dynamic_frame(self): global current_userid global current_userfname detected_personid = '' welcome_names='' ramp_frames = 10 print "Face identification started .........." cv2.destroyAllWindows() try: for i in xrange(ramp_frames): s, im = self.capture.read() ret,frame = self.capture.read() #self.message_label.setText('Image Captured') self.capture_cnt+=1 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(80, 80), flags=cv2.cv.CV_HAAR_SCALE_IMAGE ) print "Total Faces in Image = %d " % len(faces) #self.message_label.setText("Total Faces in Image = %d " % len(faces)) if len(faces) > 0: detected_persons = [] persons = [] persons_cnt=0 detected_persons_cnt=0 for (x, y, w, h) in faces: if w*h>500: persons_cnt+=1 image_crop = frame[y:y+h,x:x+w] #self.message_label.setText("Processing.. %d " % persons_cnt) file_name = id_generator()+'.jpg' file = os.path.join(tmp_path,file_name) cloudinary_url=cloudinary_tmp + '/' + file_name cv2.imwrite(file, image_crop) imup.upload_image(file,file_name) faceid=msface.face_detect(cloudinary_url) print "Result for person %d " % persons_cnt print "Image File = " + str(file) print "faceId = " + str(faceid) detected_personid = msface.face_identify(faceid) if detected_personid: print "detected_personid = " + str(detected_personid) comm = "SELECT * FROM %s WHERE personid = '%s'" % (TABLE_NAME,detected_personid) res = cursor.execute(comm) res = cursor.fetchone() if res: userid = res[0] uname = res[1] fname = res[2] lname = res[3] print "Welcome %s !" % (fname+' '+lname) detected_persons_cnt+=1 detected_persons.append(fname) persons.append(fname) now = datetime.datetime.now() comm = "SELECT * FROM users_present WHERE userid = %d and date = '%s' " % (int(userid), now.strftime("%Y-%m-%d")) #print comm res2=cursor.execute(comm) res2=cursor.fetchone() if res2==None: format_str = "INSERT INTO users_present (id, userid) VALUES (NULL,%d)" %(int(userid)) #print format_str conn.execute(format_str) conn.commit() print "Attendance marked for user %s " % uname else print "Attendance already marked for user %s " % uname else: time_str=strftime("%Y-%m-%d_%H:%M:%S", gmtime()) print "Unknown person found" cv2.imwrite(os.path.join(unknown_user_path,'cam1_'+time_str+'.jpg'),image_crop) persons.append('Unknown') if detected_persons_cnt > 1: for i in range(detected_persons_cnt-1): welcome_names = welcome_names + detected_persons[i] + ', ' welcome_names=welcome_names[:-2] welcome_names=welcome_names + ' & ' + detected_persons[detected_persons_cnt-1] elif detected_persons_cnt>0: welcome_names = detected_persons[0] self.label2.setText('Hello '+ welcome_names) else: self.label2.setText('') print "No person in image" k=0 for (x, y, w, h) in faces: if persons[k]!='Unknown': cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(frame, persons[k],(x, y-10), cv2.FONT_HERSHEY_COMPLEX_SMALL , 1,(0,255,0),1) else: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) cv2.putText(frame, persons[k],(x, y-10), cv2.FONT_HERSHEY_COMPLEX_SMALL , 1,(0,0,255),1) k=k+1 #image=cv2.flip(frame, 1) image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) face_image = QImage(image, image.shape[1], image.shape[0], image.strides[0], QImage.Format_RGB888) self.face_image.setPixmap(QPixmap.fromImage(face_image)) except Exception as e: print "Errors occured !" print e
def face_verify(self, personid): print "Face identification started .........." self.capture = cv2.VideoCapture(camera_port) self.capturing = True faceid = '' detected_personid = '' try: r, frame = self.capture.read() frame = cv2.flip(frame, 1) #cv2.imshow('Video', frame) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100), flags=cv2.cv.CV_HAAR_SCALE_IMAGE) if len(faces) > 0: max_area = 0 mx = 0 my = 0 mh = 0 mw = 0 for (x, y, w, h) in faces: if w * h > max_area: mx = x my = y mh = h mw = w max_area = w * h print max_area image_crop = frame[my:my + mh, mx:mx + mw] file_name = id_generator() + '.jpg' file = os.path.join(tmp_path, file_name) cloudinary_url = cloudinary_tmp + '/' + file_name cv2.imwrite(file, image_crop) imup.upload_image(file, file_name) faceid = msface.face_detect(cloudinary_url) print "faceId = " + str(faceid) detected_personid = msface.face_identify(faceid) print "detected_personid = " + str(detected_personid) print "personid = " + str(personid) if detected_personid: if detected_personid == personid: print "Face verified sucessfully" self.capture.release() return True else: print "Other person found" self.capture.release() return False else: print "Unknown person found" self.capture.release() return False else: print "No person found" self.capture.release() return False except Exception as e: print "Errors occured !" print e self.capture.release() return False
def face_identify(tmp): global current_userid global current_userfname detected_personid = '' camera_port = 1 cascPath = 'haarcascade_frontalface_default.xml' faceCascade = cv2.CascadeClassifier(cascPath) ramp_frames = 10 print "Face identification started .........." cam = cv2.VideoCapture(camera_port) try: while True: for i in xrange(ramp_frames): s, im = cam.read() ret, image = cam.read() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100), flags=cv2.cv.CV_HAAR_SCALE_IMAGE) # Draw a rectangle around the faces max_area = 0 mx = 0 my = 0 mh = 0 mw = 0 for (x, y, w, h) in faces: #cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) if w * h > max_area: mx = x my = y mh = h mw = w max_area = w * h #cv2.imshow('Video', image) if max_area >= 15000: image_crop = image[my:my + mh, mx:mx + mw] file_name = id_generator() + '.jpg' file = os.path.join(tmp_path, file_name) cloudinary_url = cloudinary_tmp + '/' + file_name cv2.imwrite(file, image_crop) imup.upload_image(file, file_name) faceid = msface.face_detect(cloudinary_url) print "faceId = " + str(faceid) detected_personid = msface.face_identify(faceid) print "detected_personid = " + str(detected_personid) else: continue if detected_personid: comm = "SELECT * FROM users WHERE personid = '%s'" % detected_personid res = cursor.execute(comm) res = cursor.fetchone() if res: current_userid = res[0] current_userfname = res[2] fname = res[2] print "Welcome %s !" % fname return else: print "person id not found in database" else: print "Unknown person found" except Exception as e: print "Errors occured !" print e cam.release() cv2.destroyAllWindows()
def face_upload(imgPath, fileName): try: print 'Uploading...' imup.upload_image(imgPath, fileName) except Exception as e: print("Error: %s" % e.message)