Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
gray1 = cv2.cvtColor(img_ref, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale frame
rects1 = detector(gray1, 0)
if (len(rects1) < 2): #at least 2 faces in image need to be found
return None
img1Warped = np.copy(img_ref);
shape1 = predictor(gray1, rects1[0])
points1 = face_utils.shape_to_np(shape1) #type is a array of arrays (list of lists)
#need to convert to a list of tuples
points1 = list(map(tuple, points1))
shape2 = predictor(gray1, rects1[1])
points2 = face_utils.shape_to_np(shape2)
points2 = list(map(tuple, points2))
# Find convex hull
hull1 = []
hull2 = []
hullIndex = cv2.convexHull(np.array(points2), returnPoints = False)
for i in xrange(0, len(hullIndex)):
hull1.append(points1[ int(hullIndex[i]) ])
hull2.append(points2[ int(hullIndex[i]) ])
# Find delanauy traingulation for convex hull points
sizeImg2 = img_ref.shape
rect = (0, 0, sizeImg2[1], sizeImg2[0])
# it, and convert it to grayscale
# channels)
frame = vs.read()
frame = imutils.resize(frame, width=640)
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 mouth coordinates, then use the
# coordinates to compute the mouth aspect ratio
mouth = shape[mStart:mEnd]
mouthMAR = mouth_aspect_ratio(mouth)
mar = mouthMAR
# compute the convex hull for the mouth, then
# visualize the mouth
mouthHull = cv2.convexHull(mouth)
cv2.drawContours(frame, [mouthHull], -1, (0, 255, 0), 1)
cv2.putText(frame, "MAR: {:.2f}".format(mar), (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# Draw text if mouth is open
if mar > MOUTH_AR_THRESH:
# image = imutils.resize(image, width=250)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale image
print('Detecting faces...')
rects = detector(gray, 1)
# loop over the face detections
for (i, rect) in enumerate(rects):
print('Recovering face parts for person #' + str(i))
# 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)
# 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(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# show the face number
cv2.putText(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(image, (x, y), 1, (0, 0, 255), -1)
# loop over the (x, y)-coordinates for the facial landmarks
def calculate(self,frame):
frame = imutils.resize(frame, width=640)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
subjects = self.detect(gray, 0)
for subject in subjects:
shape = self.predict(gray, subject)
shape = face_utils.shape_to_np(shape) # converting to NumPy Array
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
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 < self.thresh: # closed eyes
self.flag += 1
self.pts.appendleft(self.flag)
self.openEye = 0
else:
self.openEye += 1
self.flag = 0
# it, and convert it to grayscale
# channels)
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)
# 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)
def get_face_shape(image, rect):
predictor = dlib.shape_predictor(SHAPE_PREDICTOR_LOCATION)
shape = predictor(image, rect)
shape = face_utils.shape_to_np(shape)
return shape