我一直在进行一个项目,以从直播视频中获取前额区域,而不仅仅是像本例How can i detect the forehead region using opencv and dlib?那样使用和成像并裁剪前额。
cap = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predict_path)
while True:
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray) #detects number of faces present
for face in faces:
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)
landmarks = predictor(gray, face)
for n in range(68, 81):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(frame, (x, y), 4, (0, 255, 0), -1)
我使用https://github.com/codeniko/shape_predictor_81_face_landmarks/blob/master/shape_predictor_81_face_landmarks.dat的地标设法获得了额头区域但是我需要的是矩形边界框,该矩形边界框位于检测前额区域时地标所在的位置。这可能得到吗?如果没有,我该怎么做才能获得额头区域。提前致谢。
最佳答案
您已经通过以下方式找到了所需的坐标:
for face in faces:
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)
But what I need is the rectangle bounding box onto where the landmark is at detecting the forehead region.
然后更改y坐标:
cv2.rectangle(frame, (x1, y1-100), (x2, y2-100), (0, 0, 255), 3)
更新要坚持到额头,我们需要获得最小和最大
landmark
坐标,然后需要绘制矩形。步骤1:获取坐标:
x_pts
和y_pts
landmark(n)
点存储到数组中。 for n in range(68, 81):
x = landmarks.part(n).x
y = landmarks.part(n).y
x_pts.append(x)
y_pts.append(y)
cv2.circle(frame, (x, y), 4, (0, 255, 0), -1)
步骤2:在检测到的点周围绘制矩形x1 = min(x_pts)
x2 = max(x_pts)
y1 = min(y_pts)
y2 = max(y_pts)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)
结果:当我放大网络摄像头时:
当我很远的时候:
码:
import cv2
import dlib
cap = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_81_face_landmarks.dat")
while True:
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray) # detects number of faces present
for face in faces:
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
landmarks = predictor(gray, face)
x_pts = []
y_pts = []
for n in range(68, 81):
x = landmarks.part(n).x
y = landmarks.part(n).y
x_pts.append(x)
y_pts.append(y)
cv2.circle(frame, (x, y), 4, (0, 255, 0), -1)
x1 = min(x_pts)
x2 = max(x_pts)
y1 = min(y_pts)
y2 = max(y_pts)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)
cv2.imshow("out", frame)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
关于python - 有没有一种方法可以通过使用opencv/dlib和直播视频来获取额头(边界框)的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63770831/