#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 16 18:17:41 2017
@author: vicky
"""
#!/usr/bin/env python
# coding=utf-8
import os
import numpy as np
from PIL import Image, ImageDraw
import cv2
import copy
def Euclidean(list1,list2):
List1 = np.array(list1)
List2 = np.array(list2)
return np.sqrt((List1-List2).dot(List1-List2))
Threshold = 100.0
cap = cv2.VideoCapture("/Users/vicky/Desktop/第五问视频/4p-c0.mp4")
cap1 = cv2.VideoCapture("/Users/vicky/Desktop/第五问视频/4p-c1.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# fourcc = cv2.VideoWriter_fourcc('I', '4', '2', '0')
# video = cv2.VideoWriter("output_4p_c0.avi", fourcc, 5, size)
fourcc=cv2.VideoWriter_fourcc('X','V','I','D')
video = cv2.VideoWriter('output_4p-c0.avi',fourcc,5,size)
#print(cap.isOpened())
classifier = cv2.CascadeClassifier("/Users/vicky/Documents/code/python/haarcascade_frontalface_alt.xml")
count = 0
Body_Num = []
Center = []
Count = 0
while count > -1:
ret, img = cap.read()
ret1,img1 = cap1.read()
faceRects = classifier.detectMultiScale(img, 1.2, 2, cv2.CASCADE_SCALE_IMAGE, (20, 20))
faceRects1 = classifier.detectMultiScale(img, 1.2, 2, cv2.CASCADE_SCALE_IMAGE, (20, 20))
#判断该人脸是否已存在,不存在则添加进入人脸库
if len(faceRects1) > 0:
Data_Face1 = list(faceRects1[0])
if len(Center) == 0:
Center.append(Data_Face1)
else:
#计算最小值,且判断是是否满足阈值
Min = 10000000000.0
Location = 0
for dataset in Center:
RES = Euclidean(dataset,Data_Face1)
if RES < Min:
Min = copy.deepcopy(RES)
Location += 1
if Min > Threshold:
print('第二个监控探测到的人脸不存在于数据库!')
Center.append(Data_Face1)
else:
print("第二个监控探测到的人脸存在于数据库!编号为:",Location)
if len(faceRects) > 0:
Data_Face = list(faceRects[0])
if len(Center) == 0:
Center.append(Data_Face)
else:
#计算最小值,且判断是是否满足阈值
Min = 10000000000.0
Location = 0
for dataset in Center:
RES = Euclidean(dataset,Data_Face)
if RES < Min:
Min = copy.deepcopy(RES)
Location += 1
if Min > Threshold:
print('第一个监控探测到的人脸不存在于数据库!')
Center.append(Data_Face)
else:
print("第一个监控探测到的人脸存在于数据库!编号为:",Location)
for faceRect in faceRects:
x, y, w, h = faceRect
cv2.rectangle(img, (int(x), int(y)), (int(x) + int(w), int(y) + int(h)), (0, 255, 0), 2, 0)
video.write(img)
cv2.imshow('video', img)
cv2.imshow('video1',img1)
key = cv2.waitKey(1)
if key == ord('q'):
break
video.release()
cap.release()
cv2.destroyAllWindows()