问题描述
我一直在进行一个项目,目的是从实时流式视频中获取前额区域,而不仅仅是像本示例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,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,255,0),-1)
的界标设法获得了额头区域
但是我需要的是矩形边界框,该矩形边界框位于检测前额区域时地标所在的位置。这可能得到吗?如果没有,我应该怎么做才能获得额头区域。预先感谢。
解决方法
您已经通过以下方式找到所需的坐标:
for face in faces:
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255),3)
但是我需要的是矩形边界框,该矩形框位于地标检测前额区域的位置。
然后更改y坐标:
cv2.rectangle(frame,y1-100),y2-100),3)
更新
要坚持住前额点,我们需要获取最小landmark
和最大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,255,0),-1)
点存储到数组中。
- 将
x1 = min(x_pts)
x2 = max(x_pts)
y1 = min(y_pts)
y2 = max(y_pts)
cv2.rectangle(frame,3)
第2步:在检测到的点周围绘制矩形
-
- 获取最低和最高积分
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,-1)
x1 = min(x_pts)
x2 = max(x_pts)
y1 = min(y_pts)
y2 = max(y_pts)
cv2.rectangle(frame,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
结果:
当我缩放到网络摄像头时:
当我很远的时候:
代码:
<ng-template [ngVar]="variable">
your code
</ng-template>