1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import cv2
import matplotlib.pyplot as plt
import numpy as np

template = cv2.imread('face_template.png',cv2.IMREAD_COLOR)
template = cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)
print(template.shape)
vc = cv2.VideoCapture('test.mp4')
#检测打开是否正确
if vc.isOpened():
open,frame = vc.read()
else:
open = False


while open:
ret,frame = vc.read()
if frame is None:
break
if ret ==True:
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray = cv2.pyrDown(gray)
# gray = cv2.pyrDown(gray)
print(gray.shape)
w,h = gray.shape[:2]
res = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
# 取匹配程度大于80%的坐标
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]): # *号表示可选参数
bottom_right = (pt[0] + w, pt[1] + h)
cv2.rectangle(gray, pt, bottom_right, (255, 0, 0), 1)

cv2.imshow('result',gray)

if cv2.waitKey(1) &0xFF == 27:
break