[英]Is there a way to get the area of the forehead (bounding box) by using opencv/dlib and for a live stream video
我一直在從事一個項目,從實時流媒體視頻中獲取前額區域,而不僅僅是使用圖像和裁剪前額,就像從這個例子中一樣如何使用 opencv 和 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)
但我需要的是矩形邊界框到地標檢測前額區域的位置。
然后改變y坐標:
cv2.rectangle(frame, (x1, y1-100), (x2, y2-100), (0, 0, 255), 3)
更新
為了貼在額頭點,我們需要得到最小和最大landmark
坐標,然后我們需要繪制矩形。
Step1:獲取坐標:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.