1.while flag:
2. ref, frame = capture.read()
3. gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
4. faceRects = classifier.detectMultiScale(gray)
5. # 读取到的人脸
6. if len(faceRects) > 0:
7. flag = False
8. for faceRect in faceRects:
9. x_roi, y_roi, w_roi, h_roi = faceRect
10. break
1. frame = cv.GaussianBlur(frame, (5, 5), 0)
1.hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
1.low = 15
2.high = 255
3.mask = cv.inRange(hsv_roi, (low, low, low), (high, high, high))
4.cv.imshow('b', mask)
5.#cv.waitKey(1000)
6.roi_hist = cv.calcHist([hsv_roi], [0], mask, [180], [0, 180])
7.cv.normalize(roi_hist, roi_hist, 0, 255, cv.NORM_MINMAX)
8.term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1)
1.hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
2.# 直方图反向投影
3.dst = cv.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
1.ret, track_window = cv.meanShift(dst, track_window, term_crit)
2.# 绘制窗口
3.x, y, w, h = track_window
4.cv.rectangle(frame, (x, y), (x + w , y + h+100), 255, 2)
1.faceRects = classifier.detectMultiScale(hsv)
2.f len(faceRects) > 0:
3. for faceRect in faceRects:
4. x, y, w, h = faceRect
5. if w > 100:
6. #标定出人脸方框
7. cv.rectangle(frame, (x, y), (x + w, y + h), color, 2)
import cv2 as cv
CADES_PATH = 'D:/computer_view_exercise1/venv\Lib/site-packages/cv2/data/haarcascade_frontalface_alt2.xml'
'''
face_test:将摄像头人脸检测,并进行标注
'''
def face_test():
color = (0, 255, 0)
capture = cv.VideoCapture(0, cv.CAP_DSHOW)
classifier = cv.CascadeClassifier(CADES_PATH)
flag = True
#存检测点的列表,大小为M
points = []
M = 15
# 读取ROI区域的坐标
while flag:
ref, frame = capture.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
faceRects = classifier.detectMultiScale(gray)
# 读取到的人脸
if len(faceRects) > 0:
flag = False
for faceRect in faceRects:
x_roi, y_roi, w_roi, h_roi = faceRect
break
roi = frame[y_roi:y_roi+h_roi, x_roi:x_roi+w_roi]
#cv.imshow('a', roi)
#cv.waitKey(1000)
hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
cv.imshow('m', hsv_roi)
track_window = (x_roi, y_roi, w_roi, h_roi)
#设置阈值
low = 16
high = 95
mask = cv.inRange(hsv_roi, (low, low, low), (high, high, high))
cv.imshow('b', mask)
#cv.waitKey(1000)
roi_hist = cv.calcHist([hsv_roi], [0], mask, [180], [0, 180])
cv.normalize(roi_hist, roi_hist, 0, 255, cv.NORM_MINMAX)
term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1)
# 目标跟踪
while True:
ret, frame = capture.read()
#frame = cv.GaussianBlur(frame, (5, 5), 0)
if ret is False:
break;
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
cv.imshow('hsv', hsv)
# 直方图反向投影
dst = cv.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
# 均值迁移,搜索更新roi区域
ret, track_window = cv.meanShift(dst, track_window, term_crit)
# 绘制窗口
x, y, w, h = track_window
cv.rectangle(frame, (x, y), (x + w , y + h+100), 255, 2)
x_center = int(x+w/2)
y_center = int(y+h/2+50)
if len(points) < M:
points.append((x_center, y_center))
else:
for i in range(M-1):
points[i] = points[i+1]
points[-1] = (x_center, y_center)
radius = 10
points_color = (0, 0, 255)
#绘制点轨迹
if len(points) == M:
for point in points:
cv.circle(frame, point, radius, points_color, -4)
#检测人脸
faceRects = classifier.detectMultiScale(hsv)
if len(faceRects) > 0:
for faceRect in faceRects:
x, y, w, h = faceRect
if w > 100:
#标定出人脸方框
cv.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv.imshow('picture', cv.flip(frame, 1))
cv.waitKey(60)
cv.destroyAllWindows()
if __name__ == '__main__':
face_test()
无