def gen_train(path,label): res = "" detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(sys.argv[2]) for f in glob.glob(os.path.join(path, "*.jpg")): print("Processing file: {}".format(f)) img = io.imread(f) # Ask the detector to find the bounding boxes of each face. The 1 in the # second argument indicates that we should upsample the image 1 time. This # will make everything bigger and allow us to detect more faces. dets = detector(img, 1) print("Number of faces detected: {}".format(len(dets))) for k, d in enumerate(dets): # print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( # k, d.left(), d.top(), d.right(), d.bottom())) # Get the landmarks/parts for the face in box d. shape = predictor(img, d) # print("Part 0: {}, Part 1: {} ...".format(shape.part(0), # shape.part(1))) val = feature_calc(shape) res += str(label) + ' ' for x in range(1,len(val)+1): res += str(x) + ':' + str(val[x-1]) if x != len(val): res += ' ' else: res += '\n' with open(str(label)+'.csv','w') as fout: fout.write(res)
def calcPoints(path) : predictor_path = '/Users/vovanmozg/Downloads/bigdata/shape_predictor_68_face_landmarks.dat' detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(predictor_path) print("Processing file: {}".format(path)) img = io.imread(path) # Ask the detector to find the bounding boxes of each face. The 1 in the # second argument indicates that we should upsample the image 1 time. This # will make everything bigger and allow us to detect more faces. dets = detector(img, 1) if len(dets) != 1 : return False; print("Number of faces detected: {}".format(len(dets))) points = []; for k, d in enumerate(dets): #print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( # k, d.left(), d.top(), d.right(), d.bottom())) # Get the landmarks/parts for the face in box d. shape = predictor(img, d) #print(numpy.matrix([[p.x, p.y] for p in shape.parts()])) for p in shape.parts(): points.append((p.x, p.y)) return points
def load_model(self): self.predictor = dlib.shape_predictor(self.predictor_path) self.detector = dlib.get_frontal_face_detector() self.mean3DShape, self.blendshapes, self.mesh, self.idxs3D, self.idxs2D = utils.load3DFaceModel(self.FaceModel_file) self.projectionModel = models.OrthographicProjectionBlendshapes(self.blendshapes.shape[0]) self.renderer = FaceRendering.FaceRenderer(width=self.width, height=self.height) self.projectionModel = models.OrthographicProjectionBlendshapes(self.blendshapes.shape[0])
def process_frames_face(self, frames): detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(self.face_predictor_path) mouth_frames = self.get_frames_mouth(detector, predictor, frames) self.face = np.array(frames) self.mouth = np.array(mouth_frames) self.set_data(mouth_frames)
def __init__(self, facePredictor, padding=0.2): """ """ assert facePredictor is not None self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(facePredictor) self.padding = padding # add padding new_template =[] for item in TEMPLATE: new_item = ((item[0]+self.padding)/(2*self.padding+1),(item[1]+self.padding)/(2*self.padding+1)) new_template.append(new_item) self.new_template = np.float32(new_template) # this shape is reference from dlib implement. self.mean_shape_x = [0.000213256, 0.0752622, 0.18113, 0.29077, 0.393397, 0.586856, 0.689483, 0.799124, 0.904991, 0.98004, 0.490127, 0.490127, 0.490127, 0.490127, 0.36688, 0.426036, 0.490127, 0.554217, 0.613373, 0.121737, 0.187122, 0.265825, 0.334606, 0.260918, 0.182743, 0.645647, 0.714428, 0.793132, 0.858516, 0.79751, 0.719335, 0.254149, 0.340985, 0.428858, 0.490127, 0.551395, 0.639268, 0.726104, 0.642159, 0.556721, 0.490127, 0.423532, 0.338094, 0.290379, 0.428096, 0.490127, 0.552157, 0.689874, 0.553364, 0.490127, 0.42689]#17-67 self.mean_shape_y = [0.106454, 0.038915, 0.0187482, 0.0344891, 0.0773906, 0.0773906, 0.0344891, 0.0187482, 0.038915, 0.106454, 0.203352, 0.307009, 0.409805, 0.515625, 0.587326, 0.609345, 0.628106, 0.609345, 0.587326, 0.216423, 0.178758, 0.179852, 0.231733, 0.245099, 0.244077, 0.231733, 0.179852, 0.178758, 0.216423, 0.244077, 0.245099, 0.780233, 0.745405, 0.727388, 0.742578, 0.727388, 0.745405, 0.780233, 0.864805, 0.902192, 0.909281, 0.902192, 0.864805, 0.784792, 0.778746, 0.785343, 0.778746, 0.784792, 0.824182, 0.831803, 0.824182] #17-67
def __init__(self): self.cap = cv2.VideoCapture(0) self.fourcc = cv2.cv.CV_FOURCC(*'XVID') self.out = cv2.VideoWriter('data/' + subject + '/' + video +'/output.avi', -1, 9, (width, height)) self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # Imaging device - must be a connected camera (not an ip camera or mjpeg # stream) self.camera = Camera(camera=0) # first camera by default self.w, self.h = 0, 0 self.pressed = 0 # Containerized analysis of recieved image frames (an openMDAO assembly) # is defined next. # This assembly is designed to handle all image & signal analysis, # such as face detection, forehead isolation, time series collection, # heart-beat detection, etc. # Basically, everything that isn't communication # to the camera device or part of the GUI self.processor = findFaceGetPulse(bpm_limits=[50, 160], data_spike_limit=2500., face_detector_smoothness=10.) # Init parameters for the cardiac data plot self.bpm_plot = False self.plot_title = "Cardiac info - raw signal, filtered signal, and PSD" # Maps keystrokes to specified methods # (A GUI window must have focus for these to work) self.key_controls = {"s": self.toggle_search, "d": self.toggle_display_plot, "f": self.write_csv}
def __init__(self, landmarks=None, openface=None, size=96, torch="th"): """Face detection Parameters ---------- landmarks : str Path to dlib's 68 facial landmarks predictor model. size : int Size of the normalized face thumbnail. openface : str Path to openface FaceNet model. """ super(Face, self).__init__() # face detection self._face_detector = dlib.get_frontal_face_detector() # landmark detection if landmarks is not None: self._landmarks_detector = dlib.shape_predictor(landmarks) # normalization self.size = size self._landmarks = MINMAX_TEMPLATE * self.size if openface is not None: self._net = TorchWrap(torch=torch, model=openface, size=self.size, cuda=False)
def feature_extraction(img): dist = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] dist = np.asarray(dist) #Loading the facial land mark pointer predictor predictor_path = '/home/qburst/Desktop/Emotion Detection Final/ED_dist/Necessaries/shape_predictor_68_face_landmarks.dat' #Creating objects for frontal face detector and landmark point predictor detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(predictor_path) #'dets' has all the faces detected represented as rectangles, their co-ordinates are obtainable by using left(), top(), #right(), bottom(). 'dets' is iterable dets = detector(img, 1) for rect in dets: #'shape' has the 68 facial land mark points. Each of which can be accessed using shape.part().co-ordinate #eg: shape.part(1).x, shape.part(1).y for (x, y) of point 1. 'shape' is not iterable. shape = predictor(img, rect) points = _shape_to_np(shape) dist_as_np = np.asarray(distance(points)) dist = np.vstack((dist, dist_as_np)) dist = np.delete(dist, 0, 0) return len(dets), dist, dets
def __init__(self, predictor, detector=None): if detector is None: self.detector = dlib.get_frontal_face_detector() else: self.detector = detector self.predictor = predictor self.reset()
def __init__(self): self._detector = dlib.get_frontal_face_detector() pose_predictor_path = '../models/dlib/shape_predictor_68_face_landmarks.dat' self._predictor = dlib.shape_predictor(pose_predictor_path) if not self.SERVER_NO_GUI_MODE: self._win = dlib.image_window() self._lock = RLock()
def main(): detector = dlib.get_frontal_face_detector() try: src=sys.argv[1] except: src=0 cap=videoCap(src) while True: ret,frame=cap.read() t=clock() rects=detector(frame,1) vis=frame.copy() draw_dlib_rects(vis,rects,(0,255,0)) dt=clock()-t draw_str(vis,(20,20),'time: %.1f ms' % (dt*1000)) cv2.imshow('facedetect',vis) if 0xFF & cv2.waitKey(5) == 27: break cv2.destroyAllWindows()
def __init__(self): self.PREDICTOR_PATH = "shape_predictor_68_face_landmarks.dat" MOUTH_POINTS = list(range(48, 61)) self.OVERLAY_POINTS = [MOUTH_POINTS] self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(self.PREDICTOR_PATH)
def camera_fd(): import cv2 cam = cv2.VideoCapture(-1) win = dlib.image_window() detector = detector = dlib.get_frontal_face_detector() def detect(): s, img = cam.read() if not s: return img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) dets = detector(img2) num = len(dets) if num > 0: print("found!") win.clear_overlay() win.set_image(img2) win.add_overlay(dets) try: while True: detect() except KeyboardInterrupt as e: print("exiting") cam.release() cv2.destroyAllWindows()
def get_landmarks(line_arr): predictor_path = 'shape_predictor_68_face_landmarks.dat' # http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(predictor_path) lmarks = [] bboxes = [] for i,line in enumerate(line_arr): print('%d/%d'%(i,len(line_arr))) img = io.imread(line) dets = detector(img, 0) if len(dets) == 0: rect = dlib.rectangle(0,0,img.shape[0], img.shape[1]) else: rect = dets[0] shape = predictor(img, rect) xy = _shape_to_np(shape) lmarks.append(xy) bboxes.append(rect) lmarks = np.vstack(lmarks) bboxes = np.asarray(bboxes) return lmarks,bboxes
def initialize(detector, scale_to=2048): global dlib_detectors global keras_model global is_initialized if not is_initialized: dlib_cnn_face_detector_path = os.path.join(os.path.dirname(__file__), "mmod_human_face_detector.dat") if not os.path.exists(dlib_cnn_face_detector_path): raise Exception ("Error: Unable to find %s, reinstall the lib !" % (dlib_cnn_face_detector_path) ) if detector == 'cnn' or detector == "all": dlib_cnn_face_detector = dlib.cnn_face_detection_model_v1(dlib_cnn_face_detector_path) #DLIB and TF competiting for VRAM, so dlib must do first allocation to prevent OOM error dlib_cnn_face_detector ( np.zeros ( (scale_to, scale_to, 3), dtype=np.uint8), 0 ) dlib_detectors.append(dlib_cnn_face_detector) if detector == "hog" or detector == "all": dlib_face_detector = dlib.get_frontal_face_detector() dlib_face_detector ( np.zeros ( (scale_to, scale_to, 3), dtype=np.uint8), 0 ) dlib_detectors.append(dlib_face_detector) keras_model_path = os.path.join( os.path.dirname(__file__) , "2DFAN-4.h5" ) if not os.path.exists(keras_model_path): print ("Error: Unable to find %s, reinstall the lib !" % (keras_model_path) ) else: print ("Info: initializing keras model...") keras_model = keras.models.load_model (keras_model_path, custom_objects={'TorchBatchNorm2D': TorchBatchNorm2D} ) is_initialized = True
def findFace(img): detector = dlib.get_frontal_face_detector() orgImg = img gray = orgGray = cv2.cvtColor(orgImg, cv2.COLOR_BGR2GRAY) angles = (0, 5, -5, 10, -10, 15, -15, 30, -30, 45, -45, 70, -70, 90, -90) for i in range(0, len(angles)): dets = detector(gray, 1) if len(dets)==1: break nextIndex = i+1 if nextIndex<len(angles): #cv2.imshow("Faces found", gray) #cv2.waitKey(0) gray = rotateImage(orgGray, angles[nextIndex]) if len(dets)==1: img = rotateImage(orgImg, angles[i]) for k, d in enumerate(dets): #cv2.rectangle(img, (d.left(), d.top()), (d.right(), d.bottom()), (0, 255, 0), 2) img = img[max(d.top(),0):d.bottom(),max(d.left(),0):d.right()] img = cv2.resize(img, (128,128)) return img #cv2.imwrite(outputDir + imgName, img) #cv2.imshow("Faces found", img) #cv2.waitKey(0) return None
def get_landmarks(argv): line = argv[1] left = argv[2] top = argv[3] right = argv[2]+argv[4] bottom = argv[3]+argv[5] rect = dlib.rectangle(int(left),int(top),int(right), int(bottom)) predictor_path = 'shape_predictor_68_face_landmarks.dat' # http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(predictor_path) lmarks = [] bboxes = [] #for i,line in enumerate(line_arr): # print('%d/%d'%(i,len(line_arr))) img = io.imread(line) dets = detector(img, 0) if len(dets) == 0: rect = dlib.rectangle(0,0,img.shape[0], img.shape[1]) else: rect = dets[0] shape = predictor(img, rect) xy = _shape_to_np(shape) np.savetxt('landmarks.txt', xy) lmarks.append(xy) bboxes.append(rect) lmarks = np.vstack(lmarks) bboxes = np.asarray(bboxes) print lmarks return lmarks
def loadData(imagepath): img = io.imread(imagepath) detector = dlib.get_frontal_face_detector() grayimg = cv2.cvtColor( img, cv2.COLOR_RGB2GRAY ) start_time = time.time() dets = detector(grayimg,1) print("--- %s seconds sdssd---" % (time.time() - start_time)) start_time = time.time() imgWithRect = cv2.imread(imagepath) ListOfFaces = [] CoordinatesList = [] for rectangle in dets: left = rectangle.left() top = rectangle.top() right = rectangle.right() bottom = rectangle.bottom() if top<0: offset = 0-top top = 0 bottom = bottom+offset if left<0: offset = 0 -left left =0 right = right+offset cv2.rectangle(imgWithRect,(left,top),(right,bottom),(255,255,255),2) crop = grayimg[top:bottom, left:right] crop2 = cv2.resize(crop, (48, 48)) data = imagePreprocessing(crop2) ListOfFaces.append(data) CoordinatesList.append((left,top,right,bottom)) cv2.imwrite(imagepath,imgWithRect) print("--- %s seconds ---" % (time.time() - start_time)) return ListOfFaces, CoordinatesList
def get_training_data(image_path, labels_path): landmarks_predictor = face_landmarks.faceLandmarks("shape_predictor_68_face_landmarks.dat") detector = dlib.get_frontal_face_detector() features = [] labels = [] for dirpath, dirnames, filenames in os.walk(image_path): files = [name for name in glob.glob(dirpath+"/*.png")] if(len(files) != 0): #if images exist file_path = max(files) pure_name = os.path.splitext(os.path.basename(file_path))[0] emotion = glob.glob(labels_path+"/*/*/"+pure_name+"_emotion.txt") if(len(emotion) > 0): #data about emotion exist in_file = open(emotion[0],"r") label = int(float(in_file.read())) in_file.close() labels.append(label) img = cv2.imread(file_path,cv2.IMREAD_GRAYSCALE) dets = detector(img, 1) #in base case will be iterate only one time (every image in CK+ dataset have one face) for k, rect in enumerate(dets): landmarks = landmarks_predictor.get_landmarks(img, rect) features.append(landmarks) return features, labels
def __init__(self): self.bridge = CvBridge() self.imgDim = 96 self.align = openface.AlignDlib(DLIB_FACEPREDICTOR) self.face_pose_predictor = dlib.shape_predictor(DLIB_FACEPREDICTOR) self.net = openface.TorchNeuralNet(NETWORK_MODEL, self.imgDim) self.landmarkIndices = openface.AlignDlib.OUTER_EYES_AND_NOSE self.face_detector = dlib.get_frontal_face_detector() self.count = 0 self.face_count = 0 # Cumulative total faces in training. self.max_face_count = 10 self.train = False self.enable = True self.data_root = os.path.join(CWD, 'faces') self.train_dir = os.path.join(self.data_root, 'training-images') self.aligned_dir = os.path.join(self.data_root, 'aligned-images') self.classifier_dir = CLASSIFIER_DIR self.clf, self.le = None, None self.load_classifier(os.path.join(self.classifier_dir, 'classifier.pkl')) self.node_name = rospy.get_name() self.multi_faces = False self.threshold = 0.5 self.detected_faces = deque(maxlen=10) self.training_job = None self.stop_training = threading.Event() self.faces = [] self.event_pub = rospy.Publisher( 'face_training_event', String, latch=True, queue_size=1) self.faces_pub = rospy.Publisher( '~faces', Faces, latch=True, queue_size=1) self.imgpub = rospy.Publisher( '~image', Image, latch=True, queue_size=1) self._lock = threading.RLock() self.colors = [ (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255) ]
def predictVideo(uvcID): """ uvcID: video camera ID """ detector = dlib.get_frontal_face_detector() posePredictor = facePose.FacePosePredictor() cap = cv2.VideoCapture(uvcID) cv2.namedWindow("headPose and landmark q:quit", cv2.WINDOW_NORMAL) while True: ok, colorImage = cap.read() if not ok: continue numUpSampling = 0 dets, _, _ = detector.run(colorImage, numUpSampling) bboxs = facePose.dets2xxyys(dets) predictpoints, landmarks, predictpose = posePredictor.predict(colorImage, bboxs) show_image(colorImage, landmarks, bboxs, predictpose) k = cv2.waitKey(10) & 0xff if k == ord('q') or k == 27: break
def detect_gaze_direction(video_capture,predictor): cam = cv2.VideoCapture(video_capture) cam.set(3,640) cam.set(4,480) video_capture = cam detector = dlib.get_frontal_face_detector() while True: # Capture frame-by-frame ret, frame = video_capture.read() if ret: frame_color = frame frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) dets = detector(frame, 1) for k, d in enumerate(dets): # Get the landmarks/parts for the face in box d. shape = predictor(frame, d) # print(type(shape.part(1).x)) cv2.circle(frame_color,(shape.part(36).x,shape.part(36).y),2,(0,0,255)) cv2.circle(frame_color,(shape.part(39).x,shape.part(39).y),2,(0,0,255)) cv2.circle(frame_color,(shape.part(42).x,shape.part(42).y),2,(0,0,255)) cv2.circle(frame_color,(shape.part(45).x,shape.part(45).y),2,(0,0,255)) x1 = shape.part(36).x y1 = shape.part(37).y-2 x2 = shape.part(39).x y2 = shape.part(40).y+2 split = frame[y1:y2,x1:x2] split = process_eye(split) split = filter_eye(split) centre = cross_spread(split) frame[y1:y2,x1:x2]=split y1 = y1+2 y2 = y2-2 centre[1]=centre[1]-2 # cv2.rectangle(frame_color,(x1,y1), (x2,y2), (0, 0, 255), 1) # cv2.circle(frame_color,(x1+centre[0],y1+centre[1]),2,(0,0,255)) cv2.line(frame_color,(x1+centre[0],y1+centre[1]), (int((3*x1+4*centre[0]-x2)/2),int((3*y1+4*centre[1]-y2)/2)),(255,0,0)) x1 = shape.part(42).x y1 = shape.part(43).y-2 x2 = shape.part(45).x y2 = shape.part(46).y+2 split = frame[y1:y2,x1:x2] split = process_eye(split) split = filter_eye(split) centre = cross_spread(split) frame[y1:y2,x1:x2]=split y1 = y1+2 y2 = y2-2 centre[1]=centre[1]-2 # cv2.rectangle(frame_color,(x1,y1), (x2,y2), (0, 0, 255), 1) # cv2.circle(frame_color,(x1+centre[0],y1+centre[1]),2,(0,0,255)) cv2.line(frame_color,(x1+centre[0],y1+centre[1]), (int((3*x1+4*centre[0]-x2)/2),int((3*y1+4*centre[1]-y2)/2)),(255,0,0)) # Display the resulting frame cv2.imshow('Video', frame_color) if cv2.waitKey(1) & 0xFF == ord('q'): break # Release video capture video_capture.release() cv2.destroyAllWindows()
def __init__(self, image, with_saliency=True, with_face_detection=True, with_text_detection=True): self.image = image self.with_saliency = with_saliency self.with_face_detection = with_face_detection self.with_text_detection = with_text_detection if with_face_detection: self.haar_profile = options.haar_profile self.profile_face_cascade = cv2.CascadeClassifier() self.profile_face_cascade.load(self.haar_profile) if SmartCrop._dlib_face_detector is None: SmartCrop._dlib_face_detector = \ dlib.get_frontal_face_detector() self.dlib_face_detector = SmartCrop._dlib_face_detector self.haar_params = {'minNeighbors': 8, 'minSize': (100, 100), 'scaleFactor': 1.1} self._faces = None if with_text_detection: self.text_classifier1 = options.text_classifier1 self.text_classifier2 = options.text_classifier2 self._text_boxes = None self._bottom_percent = 0.7 if with_saliency: self._saliency_map = None self._saliency_threshold = options.saliency_threshold
def detectFace(img): offset = 20 detector = dlib.get_frontal_face_detector() dets = detector(img,1) face_image = [] for d in dets: face_image.append(img[d.top()-offset:d.bottom()+(offset*1.5), d.left()-offset:d.right()+(offset*1.5)]) return face_image, dets
def getVisualFetB(): ''' Second attempt at extracting features Simply extract the face from each frame, followed by extracting details of the landmarks The generated file for each video contains a numpy array of the vectors (Of facial landmarks) ''' # fileName = '../training/training_gt.csv' # trueMap = getTruthVal(fileName) print 'Started extracting features B' videoPath = '../training/download_train-val/trainFiles/' vidNames = os.listdir(videoPath) vidNames = [x for x in vidNames if x.endswith(".mp4")] # videoPath = '../training/download_train-val/validationFiles/' # vidNames = os.listdir(videoPath) # vidNames = [x for x in vidNames if x.endswith(".mp4")] # vidNames.extend(vidNamesTrain) # Initialize detectors, load it for face detection predictorPath = 'coreData/shape_predictor_68_face_landmarks.dat' faceDetector = dlib.get_frontal_face_detector() shapePredictor = dlib.shape_predictor(predictorPath) saveFetPath = 'tmpData/visualFetB/' saveVidPath = 'tmpData/vidData/' if not os.path.exists(saveFetPath): os.makedirs(saveFetPath) if not os.path.exists(saveVidPath): os.makedirs(saveVidPath) vidNames = vidNames for i in range(len(vidNames)): fileName = vidNames[i] if (os.path.isfile(saveFetPath+fileName.strip('.mp4')+'.npy')): continue frameList = GetFrames(videoPath+fileName, redFact = 0.5, skipLength = 5) savePath = saveVidPath + fileName.strip('.mp4') # np.save(savePath, frameList) # Do not save, too large! faceList = DetectFaceLandmarksInList(frameList, faceDetector, shapePredictor) savePath = saveFetPath + fileName.strip('.mp4') np.save(savePath, faceList) print ('\r'), ((i*(1.0))/len(vidNames)), 'part completed. Currently at file:', fileName, sys.stdout.flush() print '\n'
def __init__(self, visulize = True): self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(predictor_path) self.fronter = getDefaultFrontalizer() if visulize: self.win = dlib.image_window() self.win2 = dlib.image_window() else: self.win = self.win2 = None
def __init__(self, predictor_path): """ :rtype : LandmarkExtractor """ self.predictor_path = predictor_path self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(self.predictor_path) self.featureMatrix = FeatureMatrix()
def __init__(self, facePredictor): """ Instantiate an 'AlignDlib' object. :param facePredictor: The path to dlib's :type facePredictor: str """ self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(facePredictor)
def detect(self, src): if np.ndim(src) == 3: src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) src = cv2.equalizeHist(src) detector = dlib.get_frontal_face_detector() rects = detector(src, 1) if len(rects) == 0: return [] return rects
def __init__(self): """Summary.""" model_filename = 'shape_predictor_68_face_landmarks.dat' if not os.path.exists(model_filename): os.system( 'wget http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2') os.system('bunzip2 shape_predictor_68_face_landmarks.dat.bz2') self.model = dlib.shape_predictor(model_filename) self.detector = dlib.get_frontal_face_detector()
def live_cam_detect(ss_path, sep_path, cc_path, usr_list): """ 程序核心函数,主要利用opencv使用摄像头,然后调用上述函数进行完整的程序过程 :param usr_list: 从参数入口传入初始化获取的员工列表 :param ss_path: screenshots路径 :param sep_path: separate路径 :param cc_path: 缓存路径 """ path_screenshots = ss_path # 人脸检测模块 detector = dlib.get_frontal_face_detector() # dlib同样支持轮廓预测 # predictor = dlib.shape_predictor(pre_path) # 开启笔记本默认摄像头 cap = cv2.VideoCapture(0) # 设置分辨率及格式 cap.set(3, 960) if cap.isOpened(): # 检测摄像头是否开启 print("Camera is on.") ss_cnt = 0 # 仅用于screenshot计数,图片起名字用。测试用 while cap.isOpened(): # 程序主循环,保持摄像头开启状态 flag, img_rd = cap.read() # 两个参数, # 一个flag是接收读取图片状态,即True or False第二个是frame表示截取到的一帧的图片 k = cv2.waitKey(1) # 等待信号指令 img_gray = cv2.cvtColor(img_rd, cv2.COLOR_RGB2GRAY) # 灰度测试并保存,用于人脸的探测 # 灰度有利于提高检测速度 faces = detector(img_gray, 0) # 利用灰度检查人脸 font = cv2.FONT_HERSHEY_SIMPLEX # 设置指令显示的字体 if k & 0xFF == ord('q'): # 输入q时退出循环 break else: # 否则,开始干正事 # 开始画方框用于标识人脸 if len(faces) != 0: for face in faces: # faces并非数字类型,其包含了众多实用的函数通过这些函数计算出矩形框的长宽 cv2.rectangle(img_rd, tuple([face.left(), face.top()]), tuple([face.right(), face.bottom()]), (0, 255, 255), 2) # 写入文字,显示总共显示的人脸数 cv2.putText(img_rd, "Faces in all: " + str(len(faces)), (20, 350), font, 0.8, (255, 215, 0), 2, cv2.LINE_AA) else: # 如果人脸数为零则显示没有脸 cv2.putText(img_rd, "no face", (20, 350), font, 0.8, (255, 215, 0), 2, cv2.LINE_AA) # 注明指令和要求并设置字体格式 字体:HERSHEY_SIMPLEX 颜色:GOLD 粗细:2 img_rd = cv2.putText(img_rd, "Press 'S': Screen shot", (20, 400), font, 0.8, (255, 215, 0), 2, cv2.LINE_AA) img_rd = cv2.putText(img_rd, "Press 'C': Confirm image", (20, 450), font, 0.8, (255, 215, 0), 2, cv2.LINE_AA) img_rd = cv2.putText(img_rd, "Press 'Q': Quit", (20, 500), font, 0.8, (255, 215, 0), 2, cv2.LINE_AA) img_rd = cv2.putText(img_rd, "Press 'O': Output", (20, 550), font, 0.8, (255, 215, 0), 2, cv2.LINE_AA) img_rd = cv2.putText( img_rd, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), (900, 50), font, 0.8, (255, 255, 0), 2, cv2.LINE_AA) # s键保存截图,仅作为初期测试用功能 if k & 0xFF == ord('s'): ss_cnt += 1 print(path_screenshots + "screenshot" + "_" + str(ss_cnt) + "_" + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + ".jpg") cv2.imwrite( path_screenshots + "screenshot" + "_" + str(ss_cnt) + "_" + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + ".jpg", img_rd) # c->confirm 确认录入信息,程序开始进行比对 if k & 0xFF == ord('c'): print('\n', '*' * 50, '\n') print("Face info confirmed, detecting ...") cv2.imwrite(cc_path + "confirm_cache.jpg", img_rd) # 将截图写入缓存路径 faces = face_separate(cc_path, sep_path, detector) # 从缓存路径读取原始图片并分割 print(faces, " faces detected. Recognizing...") print("Please Wait ...") stuff_list = [] # 员工列表 guest_list = [] # 客人列表 reco_cnt = 0 # 识别出的人脸数 unreco_cnt = 0 # 未识别出的人脸数 for face_num in range(0, faces): read_path = sep_path + "img_face_" + str( face_num + 1) + ".jpg" # 自动依次读取分割路径下的图片 result = face_recognize(online_client, read_path) # 开始上传检测 if result['error_msg'] == 'SUCCESS': # 如果检测成功 if result['result']['user_list'][0][ 'score'] >= 80: # 相似度阀值定为80 stuff_list.append( result['result']['user_list'][0]['user_id']) reco_cnt += 1 rb = xlrd.open_workbook('./log/employee.xls') wb = copy(rb) ws = wb.get_sheet(0) sheet1 = rb.sheet_by_index(0) if sheet1.cell_value( usr_list.index(stuff_list[reco_cnt - 1]) + 1, 1) == 'Registered': wx.MessageBox("* * " + stuff_list[reco_cnt - 1] + ' has already checked in. * *', caption='Ops!') # print("* * ", stuff_list[reco_cnt - 1], ' has already checked in. * *') else: wx.MessageBox('* *Welcome ' + stuff_list[reco_cnt - 1] + '! Check in successful.* *', caption='Congratulate!') # print('* *Welcome ', stuff_list[reco_cnt - 1], '! Check in successful.* *') # 打开已经初始化的表格准备修改状态 ws.write( usr_list.index(stuff_list[reco_cnt - 1]) + 1, 1, 'Registered') ws.write( usr_list.index(stuff_list[reco_cnt - 1]) + 1, 2, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) wb.save('./log/employee.xls') else: # 未达到80相似度,稍后询问是否手动录入 unreco_cnt += 1 guest_list.append(read_path) print(reco_cnt, ' of ', faces, 'faces recognized. Process complete.') # 如果存在未识别的人脸 if unreco_cnt != 0: type_flag = 1 while type_flag: # type flag仅作为防止输入错误指令用 regist_flag = input( "Unemployee faces detected. Register? (Y/N)\n") if regist_flag == 'Y' or regist_flag == 'y': for i in range(0, unreco_cnt): show_img(guest_list[i]) # 显示未识别成员工的图片,请求用户指认该人脸姓名 name = input( "Who is this(name)? Please watch the image on your right side.\n" ) face_register(online_client, guest_list[i], 'employee', name) # 录入姓名 rb = xlrd.open_workbook('./log/employee.xls') wb = copy(rb) ws = wb.get_sheet(0) # 在上传的同时更新本地出勤表 ws.write(len(usr_list) + 2, 0, name) ws.write(len(usr_list) + 2, 1, 'Registered') ws.write( len(usr_list) + 2, 2, time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())) wb.save('./log/employee.xls') type_flag = 0 elif regist_flag == 'N' or regist_flag == 'n': for j in range(0, unreco_cnt): face_register( online_client, guest_list[j], 'guest', time.strftime("%Y%m%d%H%M%S", time.localtime())) type_flag = 0 # 如果录入成功则改变flag终止循环 else: print('Invalid Command.') print('\n', '*' * 50, '\n') if k & 0xFF == ord('o'): # 请求输入导出出勤表的路径 output_path = input( "Please input output path (Backspace for default path):") # 设置空格为默认路径 if output_path == ' ': shutil.copy('./log/employee.xls', './') print('File has been saved to ' + os.path.dirname(os.path.abspath('./employee.xls'))) elif os.path.exists(output_path): shutil.copy('./log/employee.xls', output_path) print('File has been saved to ' + output_path) else: # 无效路径 print('Invalid path.') # 设置摄像头窗口的相关参数 cv2.namedWindow("camera", 1) cv2.imshow("camera", img_rd) cap.release() # 释放摄像头 if not cap.isOpened(): # 检测摄像头状态 print("Camera is off.") cv2.destroyAllWindows() # 关闭所有窗口
def calculate_distance(reference_clip, compare_clip): # construct the `argument parse and parse the arguments # ap = argparse.ArgumentParser() # ap.add_argument("-p", "--shape-predictor", required=True, default = "shape_predictor_68_face_landmarks.dat", # help="path to facial landmark predictor") # ap.add_argument("-r", "--picamera", type=int, default=-1, # help="whether or not the Raspberry Pi camera should be used") # args = vars(ap.parse_args()) # initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor print("[INFO] loading facial landmark predictor...") # 얼굴 자체를 인식하는 기능 detector = dlib.get_frontal_face_detector() # face 안에 얼굴 인식하는 기능 predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # predictor = dlib.shape_predictor(args["shape_predictor"]) # vs = VideoStream(usePiCamera=args["picamera"] > 0).start() # 비디오 출력 : https://076923.github.io/posts/Python-opencv-4/ # https://docs.opencv.org/master/dd/d43/tutorial_py_video_display.html # capture = cv2.VideoCapture("cut_2.mp4") clips = [reference_clip, compare_clip] time.sleep(2.0) clips_frame_info = [] for clip in clips: i = 0 every_frame_info = [] # loop over the frames from the video stream while True: # grab the frame from the threaded video stream, resize it to # have a maximum width of 400 pixels, and convert it to # ret, frame = capture.read() # frame = numpy array임 frame = clip.get_frame(i * 1.0 / clip.fps) i += skip_frame_rate # 1초에 60 fps가 있으므로 몇개는 skip해도 될거 같음! if (i * 1.0 / clip.fps) > clip.duration: break # if not ret: # print("Error") # break # width 높이면 더 판별 잘되지만, computational power 높음 # The benefit of increasing the resolution of the input image prior to face detection is that it may allow us to detect more faces in the imag frame = imutils.resize(frame, width=800) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame # 얼굴 자체의 위치를 찾음 rects = detector(gray, 0) if len(rects) > 0: # 얼굴 개수만큼 loop max_width = 0 max_rect = None # 얼굴 박스 제일 큰거 하나로 for rect in rects: if int(rects[0].width()) > max_width: max_rect = rect # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy array shape = predictor(gray, max_rect) shape = face_utils.shape_to_np(shape) every_frame_info.append(shape) else: every_frame_info.append([]) clips_frame_info.append(np.array(every_frame_info)) cv2.destroyAllWindows() min_size = min(len(clips_frame_info[0]), len(clips_frame_info[1])) min_diff = float("inf") min_idx = 0 for i in range(min_size): if len(clips_frame_info[0][i]) > 0 and len( clips_frame_info[1][i]) > 0: # 얼굴 둘다 있으면 # 양쪽 눈 left_eye = ((clips_frame_info[0][i][36][0] - clips_frame_info[1][i][36][0])**2 + (clips_frame_info[0][i][36][1] - clips_frame_info[1][i][36][1])**2)**0.5 right_eye = ((clips_frame_info[0][i][45][0] - clips_frame_info[1][i][45][0])**2 + (clips_frame_info[0][i][45][1] - clips_frame_info[1][i][45][1])**2)**0.5 total_diff = left_eye + right_eye if min_diff > total_diff: min_diff = total_diff min_idx = i return min_diff, (min_idx * skip_frame_rate) / clip.fps # 거리와 해당 초 위치를 계산해준다!
def getSkeleton(frame, isHarr=True): ### Inferencing Pose ### nPoints = 18 POSE_PAIRS = [ [1,0],[1,2],[1,5],[2,3],[3,4],[5,6],\ [6,7],[1,8],[8,9],[9,10], [14,11], [11,12], [12,13] ] BODY_PARTS = { "Nose": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4, "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9, "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "REye": 14, "LEye": 15, "REar": 16, "LEar": 17, "Background": 18 } idx_2_BODY_PARTS = {int(num): part for part, num in BODY_PARTS.items()} frameCopy = np.copy(frame) frameWidth = frame.shape[1] frameHeight = frame.shape[0] threshold = 0.1 # inHeight / inWidth inWidth = 368 inHeight = 368 inpBlob = cv2.dnn.blobFromImage(frame, 1.0/255, (inWidth, inHeight),\ (0,0,0), swapRB=False, crop=False) dotsize = int((frameWidth + frameHeight) / (inWidth)) textsize = int((frameWidth + frameHeight) / (inWidth * 4)) linesize = dotsize // 2 network.setInput(inpBlob) output = network.forward() H = output.shape[2] W = output.shape[3] points = [] points_with_num = [] facelist = [] ### Inferencing face & emotion ### if isHarr: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) facelist = face_cascade.detectMultiScale(gray, 1.3, 5) facelist = list(np.array(facelist).flatten())[:4] else: import dlib hogFaceDetector = dlib.get_frontal_face_detector() faceRects = hogFaceDetector(frame, 0) facelist = [faceRects[0].left(),faceRects[0].top(),\ faceRects[0].right()-faceRects[0].left(), faceRects[0].bottom()-faceRects[0].top()] emotion = 'None' # 초기값 None 이 아니라 string 이어야 합니다. if len(facelist) != 0: (fx, fy, fw, fh) = list(map(int, facelist)) # print(type(fx),type(fy),type(fw),type(fh)) face = cv2.cvtColor(frame[fy:fy + fh, fx:fx + fw], cv2.COLOR_BGR2GRAY) face = cv2.resize(face, dsize=(48, 48), interpolation=cv2.INTER_LINEAR) input_face = face.reshape(-1, 48, 48, 1) model = load_model() label_dict = { 'Angry': 0, 'Disgust': 1, 'Fear': 2, 'Happy': 3, 'Neutral': 4, 'Sad': 5, 'Surprise': 6 } label_dict = {num: emotion for emotion, num in label_dict.items()} emotion = label_dict[np.argmax(model.predict(input_face))] ## memory flush ## ### Draw points, lines, rectangle ### ### Pose estimation ### for i in range(nPoints): # confidence map of corresponding body's part probMap = output[0, i, :, :] minVal, prob, minLoc, point = cv2.minMaxLoc(probMap) x = (frameWidth * point[0]) / W y = (frameHeight * point[1]) / H if prob > threshold: cv2.circle(frameCopy, (int(x), int(y)), dotsize, (0,255,255),\ thickness=dotsize, lineType=cv2.FILLED) cv2.putText(frameCopy, "{}".format(i), (int(x), int(y)),\ cv2.FONT_HERSHEY_SIMPLEX, textsize, (0,0,255), textsize, \ lineType=cv2.LINE_AA) cv2.circle(frame, (int(x), int(y)), dotsize, (0, 0, 255),\ thickness=dotsize, lineType=cv2.FILLED) # Add the point to the list if the probability is greater than the threshold points.append((int(x), int(y))) points_with_num.append( (int(x), int(y), int(i), idx_2_BODY_PARTS[int(i)])) else: points.append(None) # Draw Skeleton for pair in POSE_PAIRS: partA = pair[0] partB = pair[1] if points[partA] and points[partB]: cv2.line(frame, points[partA], points[partB], (0, 255, 255), linesize) ### Draw Face box and emotion ### if len(facelist) != 0: cv2.rectangle(frame, (int(fx), int(fy)), (int(fx + fw), int(fy + fh)), (0, 255, 0), linesize) cv2.rectangle(frameCopy, (int(fx), int(fy)), (int(fx + fw), int(fy + fh)), (0, 255, 0), linesize) img_skeleton = frame img_with_dot = frameCopy return img_skeleton, img_with_dot, points_with_num, emotion
def __init__(self, shape_predictor_path): self.detector = dlib.get_frontal_face_detector() self.shape_predictor = dlib.shape_predictor(shape_predictor_path)
def main(): global COUNTER global TOTAL shape_predictor_path = '../shape_predictor_68_face_landmarks.dat' video_path = '../blink_test.mp4' detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(shape_predictor_path) print('Load detector successfully') (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] vs = FileVideoStream(video_path).start() file_stream = True while True: if file_stream and not vs.more(): break frame = vs.read() frame = imutils.resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame rects = detector(gray, 0) for rect in rects: # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) # extract the left and right eye coordinates, then use the # coordinates to compute the eye aspect ratio for both eyes leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) # average the eye aspect ratio together for both eyes ear = (leftEAR + rightEAR) / 2.0 leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) if ear < EYE_AR_THRESH: COUNTER += 1 else: if COUNTER >= EYE_AR_CONSEC_FRAMES: TOTAL += 1 COUNTER = 0 cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break cv2.destroyAllWindows() vs.stop()
class OrientationClassifier(object): # Initializing the face detector detector = dlib.get_frontal_face_detector() def __init__(self, args): """ construct the argument parser and parse the arguments args: parsed arguments """ try: # reading the first image self.image = cv2.imread(args["image"]) # creating another copy of first image for comparison. After the facial detector has been applied, if the image exist in correct orientation, # it will have a bounding box and hence would be different. Else, it would be the same image self.image2 = cv2.imread(args["image"]) # using predictor for identification of facial landmarks in the face self.predictor = dlib.shape_predictor(args["shape_predictor"]) self.imageResize() except: raise Exception def imageResize(self): """ Construct for resizing the image """ self.image = imutils.resize(self.image, width=500) self.image2 = imutils.resize(self.image2, width=500) def getDifference(self): """ Construct for detection of facial landmark and thus identifying if the imaghe is in correct orientation not """ try: # using the dlib detector. This is a general detector used for detection of regid objects. rects = self.detector(self.image, 1) for (i, rect) in enumerate(rects): # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array shape = self.predictor(self.image, rect) shape = face_utils.shape_to_np(shape) # convert dlib's rectangle to a OpenCV-style bounding box # [i.e., (x, y, w, h)], then draw the face bounding box (x, y, w, h) = face_utils.rect_to_bb(rect) cv2.rectangle(self.image, (x, y), (x + w, y + h), (0, 255, 0), 2) # show the face number cv2.putText(self.image, "Face #{}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # loop over the (x, y)-coordinates for the facial landmarks # and draw them on the image for (x, y) in shape: cv2.circle(self.image, (x, y), 1, (0, 0, 255), -1) # Getting difference between altered images and initial image to see if there is a bounding box present. #If bounding box is present, the image is in correct oreintation difference = cv2.subtract(self.image2, self.image) b, g, r = cv2.split(difference) if cv2.countNonZero(b) == 0 and cv2.countNonZero( g) == 0 and cv2.countNonZero(r) == 0: return 1 else: return 0 except: # This is because even a non image file can be read, however, it would raise an error when applying a detector print('Please check the image entered and provide a valid image') return -1 def fixOrientation(self): """ construct for fixing orientation and identifying the class """ count = 1 classV = 0 # we loop through 0 to 360 until the facial landmarks are detecting in the image for angle in np.arange(0, 360, 90): count += 1 # rotating both images because we want to have both images similar and the only difference to be the presence # or absense of facial landmarks self.image = imutils.rotate_bound(self.image, angle) self.image2 = imutils.rotate_bound(self.image2, angle) self.imageResize() val = self.getDifference() if val == 0: if count == 2: classV = 4 elif count == 3: classV = 3 elif count == 4: classV = 2 return self.image2, classV
def findFace(l, s, f, v, a): #Create HOG face detector from dlib class face_detector = dlib.get_frontal_face_detector() landmark_predictor_model = "shape_predictor_68_face_landmarks.dat" face_pose_predictor = dlib.shape_predictor(landmark_predictor_model) video_cap = cv2.VideoCapture(0) #image from video (default webcam) #if no one is in the frame last = True (leftStart, leftEnd) = facial_locations["left_eye"] (rightStart, rightEnd) = facial_locations["right_eye"] EYE_THRESH = 0.3 EYE_FRAMES = 3 COUNTER = 0 TOTAL = 0 while True: #capture frame by frame #returns return code(tells us if we have run out of frames) #frame = one frame ret, frame = video_cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) detected_faces = face_detector(gray, 1) if (v): print("Found {} faces in this frame.".format(len(detected_faces))) if (len(detected_faces) == 0): last = True # Loop through all faces found for i, face_rect in enumerate(detected_faces): if (v): print( "- Face #{} found at Left: {} Top: {} Right: {} Bottom: {}" .format(i, face_rect.left(), face_rect.top(), face_rect.right(), face_rect.bottom())) # Draw a box around each face we found cv2.rectangle(frame, (face_rect.left(), face_rect.top()), (face_rect.right(), face_rect.bottom()), (0, 255, 0), 2) crop = frame[face_rect.top():face_rect.bottom(), face_rect.left():face_rect.right()] #Generate and store landmarks face_landmarks = face_pose_predictor(frame, face_rect) points = store_landmarks(face_landmarks) if (a): #Collect facial feature coordinates leftEye = points[leftStart:leftEnd] rightEye = points[rightStart:rightEnd] lear = eye_aspect_ratio(leftEye) rear = eye_aspect_ratio(rightEye) aear = (lear + rear) / 2.0 leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) if aear < EYE_THRESH: COUNTER += 1 else: if COUNTER >= EYE_FRAMES: TOTAL += 1 COUNTER = 0 cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) #If user wants to save faces, save into file #TODO: given filename save if (s and last): last = False cv2.imwrite("face.jpg", crop) #If user wants to show landmarks on screen if (l): frame = show_landmarks(frame, face_landmarks, points, v) #Display info cv2.putText(frame, 'SEARCHING FOR FACES...', topLeft, font, fontScale, fontColor, lineType) cv2.imshow('Video', frame) # Close when 'q' key is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break video_cap.release() cv2.destroyAllWindows()
if w1 > w2: width = w2 else: width = w1 frameV1 = cv2.resize(frameV1, (width, height)) frameV2 = cv2.resize(frameV2, (width, height)) fps1 = video1.get(cv2.CAP_PROP_FPS) fps2 = video1.get(cv2.CAP_PROP_FPS) fourcc = cv2.VideoWriter_fourcc(*'XVID') vOut1 = cv2.VideoWriter('output1.mp4', cv2.VideoWriter_fourcc(*'MP4V'), fps1, (width, height)) vOut2 = cv2.VideoWriter('output2.mp4', cv2.VideoWriter_fourcc(*'MP4V'), fps2, (width, height)) #feature detection and extraction DETECTOR = dlib.get_frontal_face_detector() PREDICTOR = dlib.shape_predictor(PREDICTOR_PATH) detectedPts1 = DETECTOR(frameV1, 1) detectedPts2 = DETECTOR(frameV2, 2) pointsF1 = [] pointsF2 = [] frameNum = 0 for k, d in enumerate(detectedPts2): shape = PREDICTOR(frameV2, d) for i in range(68): pointsF2.append((shape.part(i).x, shape.part(i).y)) for k, d in enumerate(detectedPts1): shape = PREDICTOR(frameV1, d) for i in range(68): pointsF1.append((shape.part(i).x, shape.part(i).y)) old2FeaturePoints = np.array(pointsF2)
# if len(sys.argv) != 2: # print( # "Call this program like this:\n" # " ./face_jitter.py shape_predictor_5_face_landmarks.dat\n" # "You can download a trained facial shape predictor from:\n" # " http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2\n") # exit() predictor_path = "E:/bigdata/ai/dlib/data/shape_predictor_5_face_landmarks.dat" #sys.argv[1] face_file_path = "./faces/Tom_Cruise_avp_2014_4.jpg" # Load all the models we need: a detector to find the faces, a shape predictor # to find face landmarks so we can precisely localize the face detector = dlib.get_frontal_face_detector() sp = dlib.shape_predictor(predictor_path) # Load the image using dlib img = dlib.load_rgb_image(face_file_path) # Ask the detector to find the bounding boxes of each face. dets = detector(img) num_faces = len(dets) # Find the 5 face landmarks we need to do the alignment. faces = dlib.full_object_detections() for detection in dets: faces.append(sp(img, detection))
class FrameHandler(object): """Handle all necessary transformations of a frame to predict the facial expressions. To simplify the process and to achieve consistent results, this class only focusses on one single face. If there are multiple faces in the frame, only the first detected face will be returned. Detecting multiple faces would require we track where in subsequent frames a persons face is, and make sure the data is aligned with the faces. Our concept for this assignment doesn't involve multiple faces, so this functionality is forsaken in favour of a single face. Class attributes: predictor: The dlib facial landmarks shape predictor detector: The dlib face detector clahe: The cv2 CLAHE algorithm Attributes: detection: The detected face from the original frame resized_frame: A new frame, resized from the original frame based on the detected face. resized_detection: The detections of the face from the resized frame. """ clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor( 'models/shape_predictor_68_face_landmarks.dat') def __init__(self, frame): self.frame = frame gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY) self.clahe_image = self.clahe.apply(gray) def _resize_face(self, rect): """Resize the given face to 350x350 (same as dataset) Args: rect: The rectangle of the face. This is the area of the frame that will be cut from the frame. Returns: frame | None: A resized version of the `self.clahe_image`, or None is resizing failed. """ x = rect.left() y = rect.top() w = rect.right() - x h = rect.bottom() - y cut = self.clahe_image[y:y + h, x:x + w] try: return cv2.resize(cut, (350, 350)) except: return None def get_vectorized_landmarks(self, resized=True): """Get the vectorized landmarks of the frame. Args: resized (bool): If true, gets the landmarks from the resized frames, otherwise, use the original frame. """ if resized: frame = self.resized_frame rect = self.resized_detection else: frame = self.clahe_image rect = self.detection if rect is None: return None shape = self.predictor(frame, rect) xlist = [] ylist = [] for i in range(68): xlist.append(float(shape.part(i).x)) ylist.append(float(shape.part(i).y)) xmean = np.mean(xlist) ymean = np.mean(ylist) xcentral = [(x - xmean) for x in xlist] ycentral = [(y - ymean) for y in ylist] landmarks_vectorised = [] for x, y, w, z in zip(xcentral, ycentral, xlist, ylist): landmarks_vectorised.append(w) landmarks_vectorised.append(z) meannp = np.asarray((ymean, xmean)) coornp = np.asarray((z, w)) dist = np.linalg.norm(coornp - meannp) landmarks_vectorised.append(dist) landmarks_vectorised.append( (math.atan2(y, x) * 360) / (2 * math.pi)) return landmarks_vectorised def draw_landmarks(self, thickness=1): # Can't draw that which doesn't exist. if self.detections is None: return shape = self.predictor(self.clahe_image, self.detection) for i in range(68): cv2.circle(self.frame, (shape.part(i).x, shape.part(i).y), 1, (0, 0, 255), thickness=thickness) @property def detection(self): if not hasattr(self, '_detections'): self._detections = self.detector(self.clahe_image, 1) return self._detections[0] if len(self._detections) > 0 else None @property def resized_frame(self): if not hasattr(self, '_resized'): if self.detection: self._resized = self._resize_face(self.detection) else: self._resized = None return self._resized @property def resized_detection(self): if not hasattr(self, '_re_detections'): if self.resized_frame is not None: self._re_detections = self.detector(self.resized_frame, 1) else: self._re_detections = [] return self._re_detections[0] if len(self._re_detections) > 0 else None
def FrameExtract(path1, path2): print(f"Working with video file {path1[path1.find('/')+1:]} ") video_obj = cv2.VideoCapture(path1) #Video object detector = dlib.get_frontal_face_detector() #Face detector predictor = dlib.shape_predictor( path2 ) #Landmark identifier. Set the filename to whatever you named the downloaded file count = 0 ret = 1 p = [] while ret: #Runs ONCE for each FRAME ret, frame = video_obj.read() if (ret == 0): continue gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #Convert image to grayscale clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) clahe_image = clahe.apply(gray) detections = detector(clahe_image, 1) #Detect the faces in the image for k, d in enumerate(detections): #print("enter loop") #For each detected face shape = predictor(clahe_image, d) #shape1 = face_utils.shape_to_np(shape) This works by utilizing imutis library #print(shape1) #print(shape.part(i).y)#Get coordinates #print(shape) vec = np.empty([68, 2], dtype=int) for b in range(68): vec[b][0] = shape.part(b).x vec[b][1] = shape.part(b).y #print(vec.shape) v = vec.tolist() #print(type(v)) #print(type(p)) p.append(v) # #print(shape1-vec) #cv2.imshow("image", frame) #Display the frame #print("enter after loop") if not os.path.exists(path1[:path1.find(".")]): os.makedirs(path1[:path1.find(".")]) #print(count) #cv2.imwrite(path1[:path1.find(".")]+"/frame%d.jpg" % count, imag) count += 1 p1 = np.array(p) print(p1.shape) panel = pd.Panel( p1, items=['Frame {}'.format(i) for i in range(0, p1.shape[0])], major_axis=['Landmark {}'.format(i) for i in range(0, 68)], minor_axis=['x', 'y']) pnl = panel.to_frame() print(p1) print(pnl) pnl.to_csv(path1[:path1.find(".")] + '/landmark_points_dataframe.csv') np.save(path1[:path1.find(".")] + '/landmark_points_array.out', p1) #np.savetxt(path1[:path1.find(".")]+"/reshaped.txt", p1.reshape((3,-1)), fmt="%s", header=str(p1.shape)) return p1
def run_detector(): # define two constants, one for the eye aspect ratio to indicate # blink and then a second constant for the number of consecutive # frames the eye must be below the threshold for to set off the # alarm EYE_AR_THRESH = 0.3 EYE_AR_CONSEC_FRAMES = 48 # initialize the frame counter as well as a boolean used to # indicate if the alarm is going off COUNTER = 0 ALARM_ON = False # initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # grab the indexes of the facial landmarks for the left and # grab the indexes of the facial landmarks for the left and # right eye, respectively (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] # start the video stream thread print("[INFO] starting video stream thread...") # vs = VideoStream(src=args["webcam"]).start() # # time.sleep(1.0) cam = cv2.VideoCapture(1 + cv2.CAP_DSHOW) # Current time in millis start_time = int(round(time.time() * 1000)) #time 30 seconds later in millis stop_time = int(round(time.time() * 1000)) + 5000 # loop over frames from the video stream while True: # grab the frame from the threaded video file stream, resize # it, and convert it to grayscale # channels) ret, frame = cam.read() frame = imutils.resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame rects = detector(gray, 0) # loop over the face detections for rect in rects: # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) # extract the left and right eye coordinates, then use the # coordinates to compute the eye aspect ratio for both eyes leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) # average the eye aspect ratio together for both eyes ear = (leftEAR + rightEAR) / 2.0 # compute the convex hull for the left and right eye, then # visualize each of the eyes leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) # check to see if the eye aspect ratio is below the blink # threshold, and if so, increment the blink frame counter if ear < EYE_AR_THRESH: COUNTER += 1 # if the eyes were closed for a sufficient number of # then sound the alarm if COUNTER >= EYE_AR_CONSEC_FRAMES: # if the alarm is not on, turn it on if not ALARM_ON: ALARM_ON = True # check to see if an alarm file was supplied, # and if so, start a thread to have the alarm # sound played in the background t = Thread(target=sound_alarm, args=("alarm.wav", )) t.deamon = True t.start() # draw an alarm on the frame cv2.putText(frame, "DROWSINESS ALERT!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # otherwise, the eye aspect ratio is not below the blink # threshold, so reset the counter and alarm else: COUNTER = 0 ALARM_ON = False # draw the computed eye aspect ratio on the frame to help # with debugging and setting the correct eye aspect ratio # thresholds and frame counters cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # show the frame cv2.imshow("Frame", frame) cv2.waitKey(1) & 0xFF # Time to run detector for # Currently set to 1 seconds for testing start_time = int(round(time.time() * 1000)) if start_time > stop_time: break # do a bit of cleanup cv2.destroyAllWindows() cam.release()
def detect_faces(image): face_detector = dlib.get_frontal_face_detector() detected_faces = face_detector(image, 1) face_frames = [(x.left(), x.top(), x.right(), x.bottom()) for x in detected_faces] return face_frames
def main() : args = vars(ap.parse_args()) EYE_AR_THRESH = args['threshold'] EYE_AR_CONSEC_FRAMES = args['frames'] COUNTER = 0 TOTAL = 0 print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(args["shape_predictor"]) (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] print("[INFO] starting video stream thread...") print("[INFO] print q to quit...") if args['video'] == "camera": vs = VideoStream(src=1).start() fileStream = False else: vs = FileVideoStream(args["video"]).start() fileStream = True time.sleep(1.0) while True: if fileStream and not vs.more(): break frame = vs.read() frame = imutils.resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = detector(gray, 0) for rect in rects: shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) ear = (leftEAR + rightEAR) / 2.0 leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) if ear < EYE_AR_THRESH: COUNTER += 1 else: if COUNTER >= EYE_AR_CONSEC_FRAMES: TOTAL += 1 COUNTER = 0 cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break cv2.destroyAllWindows() vs.stop()
import cv2 # imports OpenCV library and associated functions and methods import numpy as np # imports numpy as "np" library and associated functions and methods import dlib # imports dlib library and associated functions path = "enter the path to the .dat file" #creates a variable with the address of the dlib library predictor = dlib.shape_predictor(path) # creates a variable that stores the method of face decection from dlib library detector = dlib.get_frontal_face_detector() # creates a variable that stores another method that dlib provides for detecting faces def get_landmarks(im): # creating a function that gets the landmarks on the face to make the code easier (detector returns cordinates of major facial features) rects = detector(im,1) # creates a variable that returns the number of faces in the screen if len(rects) > 1: # To check if there are more than 1 faces, if yes, return an empty matrix print("Toomanyfaces") return np.matrix([0]) if len(rects) == 0: # To check if there are 0 faces, if yes, return an empty matrix print("Toofewfaces") return np.matrix([0]) return np.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()]) #if there is exactly 1 face, the predictor method is used to append cordinates of facial features in a numpy matrix def upper_lip(landmarks): # To find out the mean cordinates of the upper lip position top_lip = [] # Creates a new list for i in range(50,53): # Creating a range from "50" to "53" because they represent the upper lip in dlib face predictor top_lip.append(landmarks[i]) # Appending the same in the top lip list created above for j in range(61,64): # Creating a range from "61" to "64" because they represent the upper lip in dlib face predictor top_lip.append(landmarks[j])
def blinkCounter(video): directions = [] TOTAL = 0 COUNTER = 0 Total_Frames = 0 time_for_blink = [] consecutive_frames = [] print("starting blink detection on: ", video) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(data_set) (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] vs = FileVideoStream(video).start() fileStream = True time.sleep(1.0) while in_loop: if fileStream and not vs.more(): return TOTAL, Total_Frames, time_for_blink, directions frame = vs.read() Total_Frames += 1 frame = imutils.resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = detector(gray, 0) for rect in rects: shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) ear = (leftEAR + rightEAR) / 2.0 if ear < EYE_AR_THRESH: COUNTER += 1 consecutive_frames.append(Total_Frames) else: if COUNTER >= EYE_AR_CONSEC_FRAMES: TOTAL += 1 if len(consecutive_frames) != 0: timeStamp = consecutive_frames[0] / 30 time_for_blink.append(int(timeStamp)) consecutive_frames = [] COUNTER = 0 cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): return TOTAL, Total_Frames, time_for_blink, directions if key == ord("7"): print("Upper Left") save_position("UL", Total_Frames, directions) if key == ord("8"): print("Up") save_position("U", Total_Frames, directions) if key == ord("9"): print("Upper Right") save_position("UR", Total_Frames, directions) if key == ord("4"): print("left") save_position("L", Total_Frames, directions) if key == ord("5"): print("middle") save_position("M", Total_Frames, directions) if key == ord("6"): print("right") save_position("R", Total_Frames, directions) if key == ord("1"): print("down left") save_position("DL", Total_Frames, directions) if key == ord("2"): print("down") save_position("D", Total_Frames, directions) if key == ord("3"): print("down right") save_position("DR", Total_Frames, directions) cv2.destroyAllWindows() vs.stop()
def __init__(self, dlib_model): self.face_regressor = dlib.shape_predictor(dlib_model) self.face_detector = dlib.get_frontal_face_detector()
def get_cropped_image(self): face = self.find_face(self.image) #[1][0] print(face[0]) if face[0] is False: from imutils import face_utils import dlib face_detector = dlib.get_frontal_face_detector() rects = face_detector(self.image) left, right, top, bottom = rects[0][0], rects[0][2], rects[0][ 1], rects[0][3] else: face = face[1][0] left, top, right, bottom = face[0], face[ 1], face[0] + face[2], face[1] + face[3] image_size = self.image.shape hori_adjustment_scale = image_size[1] / 10 vert_adjustment_scale = image_size[0] / 10 # print('hori_adjustment_scale- {}'.format(hori_adjustment_scale)) # print('vert_adjustment_scale- {}'.format(vert_adjustment_scale)) # # print('top- {}'.format(top)) # print('bottom- {}'.format(bottom)) # print('left- {}'.format(left)) # print('right- {}'.format(right)) # if (left - hori_adjustment_scale) >= 0: left -= hori_adjustment_scale left = int(left) else: left = 0 if (right + hori_adjustment_scale) <= image_size[1]: right += hori_adjustment_scale right = int(right) else: right = image_size[1] if (top - vert_adjustment_scale) >= 0: print('ye') top -= vert_adjustment_scale top = int(top) else: top = 0 if (bottom + vert_adjustment_scale) <= image_size[0]: bottom += vert_adjustment_scale bottom = int(bottom) else: bottom = image_size[0] # print('top- {}'.format(top)) # print('bottom- {}'.format(bottom)) # print('left- {}'.format(left)) # print('right- {}'.format(right)) self.image = self.image[top:bottom, left:right] self.image = cv2.resize(self.image, (640, 490)) return self.image
def fatigue_detection(self,Adafruit_CharLCD): #眼睛长宽比的阈值,如果超过这个值就代表眼睛长/宽大于采集到的平均值,默认已经"闭眼" eyesRatioLimit=0 #数据采集的次数 collectCountInterval=40 #数据采集的计数,采集collectCountInterval次然后取平均值 collectCount=0 #用于数据采集的求和 collectSum=0 #是否开始检测 startCheck=False #统计"闭眼"的次数 eyesCloseCount=0 #初始化dlib detector=dlib.get_frontal_face_detector() predictor=dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") #获取面部各器官的索引 #左右眼 (left_Start,left_End)=face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (right_Start,right_End)=face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] # #嘴 # (leftMouth,rightMouth)=face_utils.FACIAL_LANDMARKS_IDXS['mouth'] # #下巴 # (leftJaw,rightJaw)=face_utils.FACIAL_LANDMARKS_IDXS['jaw'] # #鼻子 # (leftNose,rightNose)=face_utils.FACIAL_LANDMARKS_IDXS['nose'] # #左右眉毛 # (left_leftEyebrow,left_rightEyebrow)=face_utils.FACIAL_LANDMARKS_IDXS['left_eyebrow'] # (right_leftEyebrow,right_rightEyebrow)=face_utils.FACIAL_LANDMARKS_IDXS['right_eyebrow'] #开启视频线程,延迟 print("[INFO] starting video stream...") cap = cv2.VideoCapture(0) #调节摄像头帧率 cap.set(cv2.CAP_PROP_FPS,2) #调整摄像头分辨率 # ret = cap.set(cv2.CAP_PROP_FRAME_WIDTH,240) # ret = cap.set(cv2.CAP_PROP_FRAME_HEIGHT,130) #初始化 print("[INFO] initializing...") time.sleep(2.0) #循环检测 while True: #对每一帧进行处理,设置宽度并转化为灰度图 ret,frame = cap.read() #print('collect data') frame = imutils.resize(frame, width=320) #转换为GRAY颜色空间 img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #检测灰度图中的脸 faces = detector(img, 0) for k in faces: #确定面部区域的面部特征点,将特征点坐标转换为numpy数组 shape = predictor(img, k) shape = face_utils.shape_to_np(shape) #左右眼 leftEye = shape[left_Start:left_End] rightEye = shape[right_Start:right_End] leftEyesVal = self.eyesRatio(leftEye) rightEyesVal = self.eyesRatio(rightEye) #凸壳 leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) #绘制轮廓 cv2.drawContours(img, [leftEyeHull], -1, (0, 0, 0), 1) cv2.drawContours(img, [rightEyeHull], -1, (0, 0, 0), 1) #取两只眼长宽比的的平均值作为每一帧的计算结果 eyeRatioVal = (leftEyesVal + rightEyesVal) / 2.0 # #嘴 # mouth=shape[leftMouth:rightMouth] # mouthHull=cv2.convexHull(mouth) # cv2.drawContours(frame, [mouthHull], -1, (0, 255, 0), 1) # #鼻子 # nose=shape[leftNose:rightNose] # noseHull=cv2.convexHull(nose) # cv2.drawContours(frame, [noseHull], -1, (0, 255, 0), 1) # #下巴 # jaw=shape[leftJaw:rightJaw] # jawHull=cv2.convexHull(jaw) # cv2.drawContours(frame, [jawHull], -1, (0, 255, 0), 1) # #左眉毛 # leftEyebrow=shape[left_leftEyebrow:left_rightEyebrow] # leftEyebrowHull=cv2.convexHull(leftEyebrow) # cv2.drawContours(frame, [leftEyebrowHull], -1, (0, 255, 0), 1) # #右眉毛 # rightEyebrow=shape[right_leftEyebrow:right_rightEyebrow] # rightEyebrowHull=cv2.convexHull(rightEyebrow) # cv2.drawContours(frame, [rightEyebrowHull], -1, (0, 255, 0), 1) if collectCount < collectCountInterval: collectCount+=1 collectSum+=eyeRatioVal # cv2.putText(img, "DATA COLLECTING", (10, 10),cv2.FONT_HERSHEY_SIMPLEX, 0.2, (0, 0, 0), 1) print("[INFO] DATA COLLECTING... ") Adafruit_CharLCD.clear() Adafruit_CharLCD.message("DATA COLLECTING") startCheck=False else: if not startCheck: eyesRatioLimit=collectSum/(1.0*collectCountInterval) # Adafruit_CharLCD.clear() Adafruit_CharLCD.clear() Adafruit_CharLCD.message("fatigue detection") print('眼睛长宽比均值',eyesRatioLimit) # eyesRatioLimit = round(eyesRatioLimit,2) # AverageEyesRatio = str(eyesRatioLimit) startCheck=True # Adafruit_CharLCD.clear() # Adafruit_CharLCD.message("fatigue detection") if startCheck: #如果眼睛长宽比大于之前检测到的阈值,则计数,闭眼次数超过50次则认为已经"睡着" if eyeRatioVal > 1.11*eyesRatioLimit: eyesCloseCount += 1 if eyesCloseCount >= 22: # cv2.putText(frame, "SLEEP!!!", (580, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2) # cv2.putText(img, "SLEEP!!!", (60, 40),cv2.FONT_HERSHEY_SIMPLEX, 0.2, (0, 0, 0), 1) # if eyesCloseCount%2 == 0 : #Adafruit_CharLCD.clear() #Adafruit_CharLCD.message(str(eyesCloseCount)+'/n sleep!') #time.sleep(0.1) #Adafruit_CharLCD.message("sleep!") self.buzzer.blink(0.3) #time.sleep(0.5) elif eyesCloseCount >= 4: # cv2.putText(img, "EXHAUSTED!", (60, 40),cv2.FONT_HERSHEY_SIMPLEX, 0.2, (0, 0, 0), 1) # if eyesCloseCount%2 == 0 : #Adafruit_CharLCD.clear() #Adafruit_CharLCD.message('close'+str(eyesCloseCount)+'/n exhausted!') #time.sleep(0.1) # Adafruit_CharLCD.message('close'+"exhausted!") self.buzzer.blink(0.1) #time.sleep(0.5) elif eyesCloseCount >= 0: # cv2.putText(img, "WIDE AWAKE", (60, 40),cv2.FONT_HERSHEY_SIMPLEX, 0.2, (0, 0, 0), 1) # #Adafruit_CharLCD.clear() #Adafruit_CharLCD.message('close'+str(eyesCloseCount)+'/n Wake!') #time.sleep(0.1) print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) #time.sleep(0.5) print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) # Adafruit_CharLCD.message("Wake!") #print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) #print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) self.buzzer.off() # alarm_beep(eyesCloseCount) else: eyesCloseCount = 0 print('眼睛实时长宽比:{:.2f} '.format(eyeRatioVal/eyesRatioLimit)) # eyeRatioVal = round(eyeRatioVal,2) # Adafruit_CharLCD.clear() #Adafruit_CharLCD.message('av:'+AverageEyesRatio+'\n now:'+str(eyeRatioVal)) #眼睛长宽比 # cv2.putText(img, "EYES_RATIO: {:.2f}".format(eyeRatioVal), (20, 20),cv2.FONT_HERSHEY_SIMPLEX, 0.2, (0, 0, 0), 1) #闭眼次数 # cv2.putText(img,"EYES_COLSE: {}".format(eyesCloseCount),(40,30),cv2.FONT_HERSHEY_SIMPLEX,0.2,(0,0,0),1) #通过检测嘴的长宽比检测有没有打哈欠,后来觉得没什么用 # cv2.putText(frame,"MOUTH_RATIO: {:.2f}".format(mouthRatio(mouth)),(500, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) #cv2.imshow("Frame", img) #key = cv2.waitKey(1) & 0xFF #停止 #if key == ord("S"): break if self.button_reset.is_pressed : # if self.button_reset() : print('program is over') mixer.init() mixer.music.load('xiaoge.mp3') mixer.music.play() break cap.release()
def create_resource(ti=15, v="Hybernation", inac=5, asleep=2, fold="/home/satya/Pictures"): # Initialize Pygame and load music ft = 0 pygame.mixer.init() pygame.mixer.music.load(r'assets\audio\faded.ogg') # Minimum threshold of eye aspect ratio below which alarm is triggerd EYE_ASPECT_RATIO_THRESHOLD = 0.3 # Minimum consecutive frames for which eye ratio is below threshold for alarm to be triggered EYE_ASPECT_RATIO_CONSEC_FRAMES = 50 # COunts no. of consecutuve frames below threshold value COUNTER = 0 b = 1 flag1 = True global inace # Load face cascade which will be used to draw a rectangle around detected faces. face_cascade = cv2.CascadeClassifier( r"assets\haarcascades\haarcascade_frontalface_default.xml") # This function calculates and return eye aspect ratio def eye_aspect_ratio(eye): A = distance.euclidean(eye[1], eye[5]) B = distance.euclidean(eye[2], eye[4]) C = distance.euclidean(eye[0], eye[3]) # EAR = eye_aspect_ratio ear = (A + B) / (2 * C) return ear # Load face detector and predictor, uses dlib shape predictor file detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor( r'assets\shape_predictor_68_face_landmarks.dat') # Extract indexes of facial landmarks for the left and right eye (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS['left_eye'] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS['right_eye'] # Start webcam video capture /Give some time for camera to initialize(not required) /time.sleep(2) a = 1 while (True): video_capture = cv2.VideoCapture(0) nowt = datetime.datetime.now() nt1 = nowt.minute newt = nowt + datetime.timedelta(minutes=ti) nt2 = newt.minute print("***********") print(nowt, newt) print("***********") while (True): # Read each frame and flip it, and convert to grayscale ret, frame = video_capture.read() frame = cv2.flip(frame, 1) if ret: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect facial points through detector function faces = detector(gray, 0) # Detect faces through haarcascade_frontalface_default.xml face_rectangle = face_cascade.detectMultiScale(gray, 1.3, 5) # Draw rectangle around each face detected for (x, y, w, h) in face_rectangle: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # Detect facial points # print(type(faces)) if len(faces) == 0: COUNTER = 0 inacs = datetime.datetime.now() # print(inacs) if flag1: inace = inacs + datetime.timedelta(minutes=inac) inace = inace.minute flag1 = False if (inacs.minute > inace and b == 1 and flag1 == False): pygame.mixer.music.play(-1) b = 0 print(inacs, inace) if (b == 0 and flag1 == False and inacs.minute > (inace + 1)): flag1 = True b = 1 pygame.mixer.music.stop() pic = pyautogui.screenshot() sour = fold + "screenshot.png" pic.save(sour) ft = 1 for face in faces: if (b != 1): flag1 = True b = 1 pygame.mixer.music.stop() shape = predictor(gray, face) shape = face_utils.shape_to_np(shape) # Get array of coordinates of leftEye and rightEye leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] # Calculate aspect ratio of both eyes leftEyeAspectRatio = eye_aspect_ratio(leftEye) rightEyeAspectRatio = eye_aspect_ratio(rightEye) eyeAspectRatio = (leftEyeAspectRatio + rightEyeAspectRatio) / 2 # Use hull to remove convex contour discrepencies and draw eye shape around eyes leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) # Detect if eye aspect ratio is less than threshold # print(eyeAspectRatio) if (eyeAspectRatio < EYE_ASPECT_RATIO_THRESHOLD): COUNTER += 1 # If no. of frames is greater than threshold frames, print(COUNTER, EYE_ASPECT_RATIO_CONSEC_FRAMES) if COUNTER >= EYE_ASPECT_RATIO_CONSEC_FRAMES and COUNTER >= asleep and a == 1: pygame.mixer.music.play(-1) a = 0 cv2.putText(frame, "You are Drowsy", (150, 200), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 2) if COUNTER >= EYE_ASPECT_RATIO_CONSEC_FRAMES and COUNTER >= ( asleep + 20): pic = pyautogui.screenshot() sour = fold + "screenshot.png" pic.save(sour) pygame.mixer.music.stop() ft = 1 break else: pygame.mixer.music.stop() a = 1 COUNTER = 0 if ft == 1: break cv2.imshow('Video', frame) kp = cv2.waitKey(1) nt1 = datetime.datetime.now().minute if (nt2 < nt1): break elif (kp & 0xFF == ord('q')): return if ft == 1: video_capture.release() cv2.destroyAllWindows() pygame.mixer.music.stop() break else: video_capture.release() cv2.destroyAllWindows() pygame.mixer.music.stop() time.sleep(60 * ti) if ft == 1: time.sleep(10) if v == "Hybernation": changeStatus.hybernation() else: changeStatus.shut_down()
def main(): args = get_args() model_name = args.model_name weight_file = args.weight_file margin = args.margin image_dir = args.image_dir if not weight_file: weight_file = get_file( "age_only_resnet50_weights.061-3.300-4.410.hdf5", pretrained_model, cache_subdir="pretrained_models", file_hash=modhash, cache_dir=Path(__file__).resolve().parent) # for face detection detector = dlib.get_frontal_face_detector() # load model and weights model = get_model(model_name=model_name) model.load_weights(weight_file) img_size = model.input.shape.as_list()[1] image_generator = yield_images_from_dir( image_dir) if image_dir else yield_images() for img in image_generator: input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_h, img_w, _ = np.shape(input_img) # detect faces using dlib detector detected = detector(input_img, 1) faces = np.empty((len(detected), img_size, img_size, 3)) if len(detected) > 0: for i, d in enumerate(detected): x1, y1, x2, y2, w, h = d.left(), d.top( ), d.right() + 1, d.bottom() + 1, d.width(), d.height() xw1 = max(int(x1 - margin * w), 0) yw1 = max(int(y1 - margin * h), 0) xw2 = min(int(x2 + margin * w), img_w - 1) yw2 = min(int(y2 + margin * h), img_h - 1) cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2) faces[i, :, :, :] = cv2.resize( img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size)) # predict ages and genders of the detected faces results = model.predict(faces) ages = np.arange(0, 101).reshape(101, 1) predicted_ages = results.dot(ages).flatten() # draw results for i, d in enumerate(detected): label = str(int(predicted_ages[i])) draw_label(img, (d.left(), d.top()), label) cv2.imshow("result", img) key = cv2.waitKey(-1) if image_dir else cv2.waitKey(30) if key == 27: # ESC break
def main(): video_file = args.video_path video_name = video_file.split("/")[-1] cap = cv2.VideoCapture(video_file) frame_count = 0 shape_predictor = dlib.shape_predictor(args.shape_predictor) face_detector = dlib.get_frontal_face_detector() transform = transforms.Compose( [transforms.ToPILImage(), transforms.ToTensor()]) model = extendNet(num_classes=8) model = nn.DataParallel(model) model = load_model(model_path=args.model_path, model=model) model.eval() prev_frame = None prev_emotion = -1 continuous_emotion = 0 saved_faces = [] saved_frame_num = [] while cap.isOpened(): ret, frame = cap.read() if ret is not True: break if frame is None: continue frame_count = frame_count + 1 if frame_count % frame_interval != 0: continue curr_frame = Frame(frame, face_detector, shape_predictor, frame_count) if prev_frame is not None and curr_frame.face_rects is not None: for curr_face_rect in curr_frame.face_rects: if check_for_lip_movement(curr_frame, prev_frame, curr_face_rect): face_img = curr_frame.get_face_from_rect(curr_face_rect) image_input_for_model = transform( resize_face_image(face_img)) emotion_label = check_emotion_in_face( image_input_for_model, model=model) if emotion_label > 0: if emotion_label == prev_emotion: # extend the emotion scene for the face continuous_emotion = continuous_emotion + 1 saved_faces.append(face_img) saved_frame_num.append(frame_count) if continuous_emotion >= streak_for_emotions: folder_name = create_rename_folder( saved_frame_num, emotion_label, video_name) write_all_faces(saved_faces, saved_frame_num, continuous_emotion, emotion_label, folder_name, video_name) else: # reinitialize the emotion and saved faces continuous_emotion = 1 saved_faces = [face_img] saved_frame_num = [frame_count] prev_emotion = emotion_label print(frame_count) prev_frame = curr_frame cap.release() cv2.destroyAllWindows()
def main(args): sleep(random.random()) output_dir = os.path.expanduser(args.output_dir) if not os.path.exists(output_dir): os.makedirs(output_dir) # Store some git revision info in a text file in the log directory src_path, _ = os.path.split(os.path.realpath(__file__)) facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv)) dataset = facenet.get_dataset(args.input_dir) print('Loading predictor model') # Create a HOG face detector using the built-in dlib class face_detector = dlib.get_frontal_face_detector() face_pose_predictor = dlib.shape_predictor(args.model) face_aligner = openface.AlignDlib(args.model) # Add a random key to the filename to allow alignment using multiple processes random_key = np.random.randint(0, high=99999) nrof_images_total = 0 nrof_successfully_aligned = 0 nrof_unchanged_images = 0 print('Total number of classes:', len(dataset)) print('Number of classes to align:', args.num_classes) for cls in dataset[:args.num_classes]: print('Processing class:', cls.name) output_class_dir = os.path.join(output_dir, cls.name) if not os.path.exists(output_class_dir): os.makedirs(output_class_dir) for image_path in cls.image_paths: nrof_images_total += 1 filename = os.path.splitext(os.path.split(image_path)[1])[0] output_filename = os.path.join(output_class_dir, filename + '.png') if not os.path.exists(output_filename): try: img = misc.imread(image_path) except (IOError, ValueError, IndexError) as e: errorMessage = '{}: {}'.format(image_path, e) print(errorMessage) else: if img.ndim < 2: print('Unable to align "%s"' % image_path) continue if img.ndim == 2: img = facenet.to_rgb(img) img = img[:, :, 0:3] # Run the HOG face detector on the image data detected_face = face_detector(img, 1) if len(detected_face) > 0: # As face(s) detected is/ are stored in a list, we need to pull it out. face_rect = detected_face[0] # Get the the face's pose pose_landmarks = face_pose_predictor(img, face_rect) # Use openface to calculate and perform the face alignment alignedFace = face_aligner.align( 534, img, face_rect, landmarkIndices=openface.AlignDlib. OUTER_EYES_AND_NOSE) misc.imsave(output_filename, alignedFace) nrof_successfully_aligned += 1 else: filename_base, file_extension = os.path.splitext( output_filename) # Add 'fail' to indicates that dlib cannot detect face in this image. output_filename = "{}{}".format( filename_base + '_failed', file_extension) nrof_unchanged_images += 1 misc.imsave(output_filename, img) print('Total number of images:', nrof_images_total) print('Number of successfully aligned images:', nrof_successfully_aligned) print( 'Number of resized images (MTCNN cannot detect face in these images):', nrof_unchanged_images)
def get_mouse_dlib(video_path, save_path, frame_detection=float("inf"), save_pic_or_not=None): """ :param video_path: 视频路径 :param save_path: 存储路径 :param frame_detection: 处理多少帧,默认所有 :param save_pic_or_not: 是否保存标记出68点图片 :return: None """ video_name = os.path.basename(video_path)[:-4] save_path = os.path.join(save_path, video_name) if not os.path.exists(save_path): os.makedirs(save_path) save_picture = save_path + "/picture" save_mouse_picutre = save_path + "/mouse_picture" if not os.path.exists(save_picture): os.makedirs(save_picture) if not os.path.exists(save_mouse_picutre): os.makedirs(save_mouse_picutre) save_txt = save_path + "/txt" if not os.path.exists(save_txt): os.makedirs(save_txt) print("make_path") x_txt = open(save_txt + "/x.txt", "a+") #记录刻画嘴部的20个点的横坐标,以及脸宽 y_txt = open(save_txt + "/y.txt", "+a") #记录刻画嘴部的20个点的纵坐标,以及脸宽 cap = cv2.VideoCapture(video_path) frame_index = 0 zero = 0 data_ = [] all_data = {} if cap.isOpened(): success = True else: success = False print("读取失败!") return None while (success and frame_detection > 0): frame_detection = frame_detection - 1 success, frame = cap.read() if not success: break frame = np.rot90(frame, -1) frame = np.rot90(frame, -1) """ """ detector = dlib.get_frontal_face_detector() # dlib的68点模型,使用作者训练好的特征预测器 predictor = dlib.shape_predictor( r"C:\Users\admin\Desktop\net_for_mouse\project_for_mouse\shape_predictor_68_face_landmarks.dat" ) # 特征提取器的实例化 dets = detector(frame, 1) print("人脸数:", len(dets)) if len(dets) == 0: print("未检测到人脸") cv2.imwrite(save_picture + "/{}wrong.jpg".format(zero), frame) zero += 1 continue elif (len(dets) == 1): frame_index += 1 for k, d in enumerate(dets): print("第", frame_index, "个人脸") width = d.right() - d.left() heigth = d.bottom() - d.top() shape = predictor(frame, d) print('人脸面积为:', (width * heigth)) if save_pic_or_not: # frame = frame[..., ::-1] img = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) img1 = img[ int(max(shape.part(49).y, shape.part(50).y) - 5 ):int(min(shape.part(57).y, shape.part(58).y) + 5), int(shape.part(48).x - 5):int(shape.part(54).x + 5)] cv2.imwrite( save_mouse_picutre + "/{}.jpg".format(frame_index), img1) for i in range(68): frame = cv2.circle(frame, (shape.part(i).x, shape.part(i).y), 2, (0, 255, 0), 1, 3) cv2.imwrite(save_picture + "/{}.jpg".format(frame_index), frame) pose = [] score = [1] * 20 skeleton = {} for j in range(48, 68): x_txt.write(str(shape.part(j).x) + " ") pose.append(shape.part(j).x) pose.append(shape.part(j).y) x_txt.write(str(width)) x_txt.write("\n") for j in range(48, 68): y_txt.write(str(shape.part(j).y) + " ") y_txt.write(str(heigth)) y_txt.write("\n") skeleton["pose"] = pose skeleton["score"] = score skeleton_ = [] skeleton_.append(skeleton) data = {} data["frame_index"] = frame_index data["skeleton"] = skeleton_ data_.append(data) all_data["data"] = data_ all_data["label"] = str(video_path.split("/")[-2]) all_data["label_index"] = int(video_path.split("/")[-2]) with open( save_path + "/" + str(video_path.split("/")[-2]) + "-" + str(video_path.split("/")[-1][:-4]) + ".json", "w") as f: json.dump(all_data, f)
class FaceAction: tot = 0 detect = get_frontal_face_detector() predict = shape_predictor("shape_predictor_68_face_landmarks.dat") (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"] (mStart, mEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["mouth"] K = [ 6.5308391993466671e+002, 0.0, 3.1950000000000000e+002, 0.0, 6.5308391993466671e+002, 2.3950000000000000e+002, 0.0, 0.0, 1.0 ] D = [ 7.0834633684407095e-002, 6.9140193737175351e-002, 0.0, 0.0, -1.3073460323689292e+000 ] cam_matrix = np.array(K).reshape(3, 3).astype(np.float32) dist_coeffs = np.array(D).reshape(5, 1).astype(np.float32) object_pts = np.float32([[6.825897, 6.760612, 4.402142], [1.330353, 7.122144, 6.903745], [-1.330353, 7.122144, 6.903745], [-6.825897, 6.760612, 4.402142], [5.311432, 5.485328, 3.987654], [1.789930, 5.393625, 4.413414], [-1.789930, 5.393625, 4.413414], [-5.311432, 5.485328, 3.987654], [2.005628, 1.409845, 6.165652], [-2.005628, 1.409845, 6.165652], [2.774015, -2.080775, 5.048531], [-2.774015, -2.080775, 5.048531], [0.000000, -3.116408, 6.097667], [0.000000, -7.415691, 4.070434]]) reprojectsrc = np.float32([[10.0, 10.0, 10.0], [10.0, 10.0, -10.0], [10.0, -10.0, -10.0], [10.0, -10.0, 10.0], [-10.0, 10.0, 10.0], [-10.0, 10.0, -10.0], [-10.0, -10.0, -10.0], [-10.0, -10.0, 10.0]]) def eye_aspect_ratio(self, eye): A = distance.euclidean(eye[1], eye[5]) B = distance.euclidean(eye[2], eye[4]) C = distance.euclidean(eye[0], eye[3]) ear = (A + B) / (2.0 * C) return ear def mouth_aspect_ratio(self, mouth): A = distance.euclidean(mouth[13], mouth[19]) B = distance.euclidean(mouth[14], mouth[18]) C = distance.euclidean(mouth[15], mouth[17]) D = distance.euclidean(mouth[12], mouth[16]) mar = (A + B + C) / (2.0 * D) return mar def drowsy(self, frame): frame = resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) subjects = self.detect(gray, 0) self.tot = len(subjects) # print(len(subjects)) # print(self.tot) if (len(subjects) == 0): return 1 for subject in subjects: shape = self.predict(gray, subject) shape = face_utils.shape_to_np(shape) leftEye = shape[self.lStart:self.lEnd] rightEye = shape[self.rStart:self.rEnd] leftEAR = self.eye_aspect_ratio(leftEye) rightEAR = self.eye_aspect_ratio(rightEye) ear = (leftEAR + rightEAR) / 2.0 return ear def yawn(self, frame): frame = resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) subjects = self.detect(gray, 0) if (len(subjects) == 0): return 0 for subject in subjects: shape = self.predict(gray, subject) shape = face_utils.shape_to_np(shape) mouth = shape[self.mStart:self.mEnd] mar = self.mouth_aspect_ratio(mouth) return mar def get_head_pose(self, shape, object_pts, cam_matrix, dist_coeffs, reprojectsrc): image_pts = np.float32([ shape[17], shape[21], shape[22], shape[26], shape[36], shape[39], shape[42], shape[45], shape[31], shape[35], shape[48], shape[54], shape[57], shape[8] ]) _, rotation_vec, translation_vec = cv2.solvePnP( object_pts, image_pts, cam_matrix, dist_coeffs) reprojectdst, _ = cv2.projectPoints(reprojectsrc, rotation_vec, translation_vec, cam_matrix, dist_coeffs) reprojectdst = tuple(map(tuple, reprojectdst.reshape(8, 2))) # calc euler angle rotation_mat, _ = cv2.Rodrigues(rotation_vec) pose_mat = cv2.hconcat((rotation_mat, translation_vec)) _, _, _, _, _, _, euler_angle = cv2.decomposeProjectionMatrix(pose_mat) return reprojectdst, euler_angle def head_pose(self, frame): face_rects = self.detect(frame, 0) if (len(face_rects) > 0): shape = self.predict(frame, face_rects[0]) shape = face_utils.shape_to_np(shape) _, euler_angle = self.get_head_pose(shape, self.object_pts, self.cam_matrix, self.dist_coeffs, self.reprojectsrc) if (-10 <= euler_angle[2, 0] and euler_angle[2, 0] <= 10): return 0 else: return 1 else: return 1 def run_frame(self, frame): return (self.drowsy(frame), self.yawn(frame), self.head_pose(frame), self.tot)
def detect_blink(): # construct the argument parse and parse the arguments # ap = argparse.ArgumentParser() # ap.add_argument("-p", "--shape-predictor", required=True, # help="path to facial landmark predictor") # ap.add_argument("-v", "--video", help="path to input video file") # args = vars(ap.parse_args()) # define two constants, one for the eye aspect ratio to indicate # blink and then a second constant for the number of consecutive # frames the eye must be below the threshold EYE_AR_THRESH = 0.23 EYE_AR_CONSEC_FRAMES = 3 # initialize the frame counters and the total number of blinks COUNTER = 0 TOTAL = 0 # initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor( "C:\\Users\\t-amsi\\Eye-blink-detection\\WFHealth\\Modules\\shape_predictor_68_face_landmarks.dat" ) # grab the indexes of the facial landmarks for the left and # right eye, respectively (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] # start the video stream thread print("[INFO] starting video stream thread...") # vs = FileVideoStream(args["video"]).start() # fileStream = True # vs = VideoStream(src=0).start() vs = cv2.VideoCapture('../blink.mp4') # vs = VideoStream(usePiCamera=True).start() fileStream = False time.sleep(1.0) # loop over frames from the video stream while vs.isOpened(): # if this is a file video stream, then we need to check if # there any more frames left in the buffer to process # if fileStream and not vs.more(): # break # grab the frame from the threaded video file stream, resize # it, and convert it to grayscale # channels) # frame = vs.read() ret, frame = vs.read() if ret == False: break # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # cv2.imshow('frame',gray) # if cv2.waitKey(1) & 0xFF == ord('q'): # breakcap.release() # cv2.destroyAllWindows() frame = imutils.resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame rects = detector(gray, 0) # loop over the face detections for rect in rects: # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) # extract the left and right eye coordinates, then use the # coordinates to compute the eye aspect ratio for both eyes leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) # average the eye aspect ratio together for both eyes ear = (leftEAR + rightEAR) / 2.0 # compute the convex hull for the left and right eye, then # visualize each of the eyes leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) # check to see if the eye aspect ratio is below the blink # threshold, and if so, increment the blink frame counter if ear < EYE_AR_THRESH: COUNTER += 1 # otherwise, the eye aspect ratio is not below the blink # threshold else: # if the eyes were closed for a sufficient number of # then increment the total number of blinks if COUNTER >= EYE_AR_CONSEC_FRAMES: TOTAL += 1 # reset the eye frame counter COUNTER = 0 # draw the total number of blinks on the frame along with # the computed eye aspect ratio for the frame # cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30), # cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30), # cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # # show the frame # cv2.imshow("Frame", frame) # key = cv2.waitKey(1) & 0xFF # # if the `q` key was pressed, break from the loop # if key == ord("q"): # break # do a bit of cleanup cv2.destroyAllWindows() vs.release() # print(COUNTER) return COUNTER
def startRec(): depth = 16 k = 8 margin = 0.4 image_dir = nameEdit.text() weight_file = get_file("weights.28-3.73.hdf5", pretrained_model, cache_subdir="pretrained_models", file_hash=modhash, cache_dir=str(Path(__file__).resolve().parent)) # for face detection detector = dlib.get_frontal_face_detector() # load model and weights img_size = 64 model = WideResNet(img_size, depth=depth, k=k)() model.load_weights(weight_file) image_generator = yield_images_from_dir( image_dir) if image_dir else yield_images() count_im_in_dir = count_im_dir(image_dir) count = 0 for img in image_generator: #print(count) input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_h, img_w, _ = np.shape(input_img) # detect faces using dlib detector detected = detector(input_img, 1) faces = np.empty((len(detected), img_size, img_size, 3)) if len(detected) > 0: for i, d in enumerate(detected): x1, y1, x2, y2, w, h = d.left(), d.top( ), d.right() + 1, d.bottom() + 1, d.width(), d.height() xw1 = max(int(x1 - margin * w), 0) yw1 = max(int(y1 - margin * h), 0) xw2 = min(int(x2 + margin * w), img_w - 1) yw2 = min(int(y2 + margin * h), img_h - 1) cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2) # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2) faces[i, :, :, :] = cv2.resize( img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size)) # predict ages and genders of the detected faces results = model.predict(faces) predicted_genders = results[0] ages = np.arange(0, 101).reshape(101, 1) predicted_ages = results[1].dot(ages).flatten() # draw results for i, d in enumerate(detected): label = "{}, {}".format( int(predicted_ages[i]), "M" if predicted_genders[i][0] < 0.5 else "F") draw_label(img, (d.left(), d.top()), label) #cv2.imshow("result", img) cv2.imwrite(name1Edit.text() + "/res" + str(count) + ".jpg", img) count += 1 progBarUpdate(100 * (count / count_im_in_dir)) key = cv2.waitKey(-1) if image_dir else cv2.waitKey(30) if key == 27: # ESC break cv2.destroyAllWindows()
def run(self, bpm): # Capture video and set up dlib. cap = cv2.VideoCapture(0) hog_face_detector = dlib.get_frontal_face_detector() dlib_facelandmark = dlib.shape_predictor( "data_files/shape_predictor_68_face_landmarks.dat") self.keep_recording = True while self.keep_recording: # Determine blinks per min cur_time = time.time() #blink_queue = [x for x in self.blink_queue if cur_time - x < 60] #self.bpm = len(self.blink_queue) self.bpm = round(self.total_bpm * 60 / (cur_time - self.start_time)) bpm = self.bpm drowsy_window = [ x for x in self.drowsy_window if cur_time - x < 10 ] if len(drowsy_window) >= 5: self.condition = 'bad' elif self.bpm < 15: self.condition = 'bad' else: self.condition = 'good' #if cur_time - start_time >= 60: # blinks_pm = cur_bpm # cur_bpm = 0 # start_time = cur_time _, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = hog_face_detector(gray) for face in faces: face_landmarks = dlib_facelandmark(gray, face) leftEye = [] rightEye = [] for n in range(36, 42): x = face_landmarks.part(n).x y = face_landmarks.part(n).y leftEye.append((x, y)) next_point = n + 1 if n == 41: next_point = 36 x2 = face_landmarks.part(next_point).x y2 = face_landmarks.part(next_point).y cv2.line(frame, (x, y), (x2, y2), (0, 255, 0), 1) for n in range(42, 48): x = face_landmarks.part(n).x y = face_landmarks.part(n).y rightEye.append((x, y)) next_point = n + 1 if n == 47: next_point = 42 x2 = face_landmarks.part(next_point).x y2 = face_landmarks.part(next_point).y cv2.line(frame, (x, y), (x2, y2), (0, 255, 0), 1) #print(leftEye) #print(rightEye) left_ear = calculate_EAR(leftEye) right_ear = calculate_EAR(rightEye) # EAR = EYE ASPECT RATIO EAR = (left_ear + right_ear) / 2 EAR = round(EAR, 2) # Default threshold for drowsiness if EAR < 0.26: cv2.putText(frame, "DROWSY", (20, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 4) #cv2.putText(frame,"Are you Sleepy?",(20,400), cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,255),4) #print("Drowsy") if self.drowsy_window and (round(self.drowsy_window[-1]) != round(cur_time)): self.drowsy_window.append(cur_time) elif not self.drowsy_window: self.drowsy_window.append(cur_time) # Threshold for blinking if EAR < 0.2 and not self.eye_closed: self.total_bpm += 1 self.blink_queue.append(cur_time) self.eye_closed = True elif EAR >= 0.2: self.eye_closed = False #print(EAR) # Display GUI frame cv2.putText(frame, f'BPM: {self.bpm}', (360, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4) cv2.putText(frame, f'Total BPM: {self.total_bpm}', (360, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4) cv2.putText(frame, f'Condition: {self.condition}', (180, 350), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4) cv2.imshow("Are you Sleepy", frame) key = cv2.waitKey(1) if key == 27: break cap.release() cv2.destroyAllWindows()
def get_detector(): """ :return: A face detector. """ return dlib.get_frontal_face_detector()
def main(): detector=dlib.get_frontal_face_detector() eyes_calibrated=False total_frames_count=0 eye_contact_frames=0 predictor=dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") lefteye_vertical_line=0 lefteye_horizontal_line=0 righteye_vertical_line=0 righteye_horizontal_line=0 cap=cv2.VideoCapture('video2.mp4') while True: if total_frames_count%15==0: ret, frame = cap.read() if ret and frame is not None: frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) #frame=increase_contrast(frame,cv2) --Don't Apply Contrast on the image because the contours won't be detected frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) faces=detector(frame) shape=predictor(frame,faces[0]) #Left Eye lefteye=extract_left_eye(shape,frame) _,lefteye=cv2.threshold(lefteye,0,255,cv2.THRESH_BINARY_INV) lefteye_contour=return_biggest_contour_in_image(lefteye) if lefteye_contour is None: continue #Right Eye righteye=extract_right_eye(shape,frame) _,righteye=cv2.threshold(righteye,0,255,cv2.THRESH_BINARY_INV) righteye_contour=return_biggest_contour_in_image(righteye) if righteye_contour is None: continue #calibrate eyes for finding the center poin left_v,left_h=detect_contour_location(lefteye_contour,cv2) right_v,right_h=detect_contour_location(righteye_contour,cv2) if eyes_calibrated is False: print("Setting the eye midpoints") lefteye_vertical_line,lefteye_horizontal_line=left_v,left_h righteye_vertical_line,righteye_horizontal_line=right_v,right_h print("Calibrated: left (%f, %f) Right(%f, %f) " % (left_v,left_h,right_v,right_h)) eyes_calibrated=True else: #determine lefteyeContact is_left_eye_contact_maintained=detect_eye_contact(left_v,left_h,lefteye_vertical_line,lefteye_horizontal_line) is_right_eye_contact_maintained=detect_eye_contact(right_v,right_h,righteye_vertical_line,righteye_horizontal_line) if is_left_eye_contact_maintained and is_right_eye_contact_maintained: eye_contact_frames=eye_contact_frames+15 else: #path='C:\Hardik Personal Data\C Drive Data\EyeContactDetection\eyeContactGone\image{}.png'.format(count) #cv2.imwrite(path,frame) print("Eye Contact is gone: left (%f, %f) Right(%f, %f) " % (left_v,left_h,right_v,right_h)) cv2.imshow('Video',frame) cv2.imshow('Left Eye',lefteye) cv2.imshow('Right Eye',righteye) if cv2.waitKey(1) & 0xFF == ord('q'): cap.release() cv2.destroyAllWindows() break else: break total_frames_count=total_frames_count+1 print("Final Result:") print("Percentage of eyecontact maintained is:") print((eye_contact_frames/total_frames_count)*100) cap.release() cv2.destroyAllWindows()