from PIL import Image
import face_recognition
import cv2
import matplotlib.pyplot as plt
filename="3.jpg"
# 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file(filename)
# 使用CNN模型
#face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")
face_locations =face_recognition.face_locations(image, number_of_times_to_upsample=1, model="hog")
print("I found {} face(s) in this photograph.".format(len(face_locations)))
for face_location in face_locations:
top, right, bottom, left = face_location
#print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# 指定人脸的位置信息,然后显示人脸图片
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
img =cv2.imread(filename)
for face_location in face_locations:
top, right, bottom, left = face_location
cv2.rectangle(img,(left,top),(right,bottom),(0,255,0), 3)
#cv2.imshow('Detected',img)
#plt.imshow(img)
#plt.show()
fn1="h4.jpg"
fn2="h2.jpg"
known_image = face_recognition.load_image_file(fn1)
unknown_image = face_recognition.load_image_file(fn2)
biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
print('匹配结果',results)
print('-------------------------end----------------------------')
package com.web.us.controller;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class DemoController{
/**
getPythonDemo2 中参数pyPath, args1, args2 可通过前端(AJAX)传过来
注意:(1)参数pyPath ,python脚本路径最好使用绝对路径,如果python(.py)脚本中也有文件路径读取,同样推荐使用绝对路径
(2)该案例中,python脚本接受的参数 args1, args2是String类型,务必确认python脚本中使用的参数类型是否是String类型,根据需要进行相应的类型转换
*/
/**
* 调用python
* 参数pyPath, args1, args2 可通过前端(AJAX)传过来
*/
@RequestMapping("/fuck.action")
public void getPythonDemo2(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception{
String args1=request.getParameter("args1");
String args2=request.getParameter("args2");
String pyPath=request.getParameter("pyPath");
DemoController demo = new DemoController (); //实例化类
System.out.println("pyPath= "+pyPath);
System.out.println("args1= "+args1);
System.out.println("args2= "+args2);
String res =demo.getPythonDemo(pyPath, args1, args2); //调用python的方法
response.setCharacterEncoding("gbk");
Writer out = response.getWriter();
out.write(res);
out.flush();
}
/**
* 调用python脚本 该方法支持python中的第三方库
* @param pyPath python脚本路径
* @param args1 参数1
* @param args2 参数2
*/
public String getPythonDemo(String pyPath, String args1, String args2){
Process proc;
String line = null;
List<String> lines = new ArrayList<String>();
try {
String[] args = new String[] { "python"
// ,"/usr/local/tomcat/webapps/demo_console/WEB-INF/model/demo.py" linux绝对路径
,"D:\\Desktop\\2.py"
//,pyPath
,String.valueOf(args1)
,String.valueOf(args2)
};
proc = Runtime.getRuntime().exec(args); //该方法参数必须是String类型的
//用输入输出流来截取结果
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "gbk")); //gbk 避免汉字乱码
while ((line = in.readLine()) != null) {
System.out.println(line);
lines.add(line); //把Python的print值保存了下来
}
//System.out.println(line);
in.close();
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Java调Python脚本结束");
String lineData = lines.toString();
return lineData;
}
}
from PIL import Image
import face_recognition
import cv2
import matplotlib.pyplot as plt
fn1="D:\Desktop\h4.jpg"
fn2="D:\Desktop\h2.jpg"
print('-------------------------start----------------------------')
known_image = face_recognition.load_image_file(fn1)
unknown_image = face_recognition.load_image_file(fn2)
biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
print('匹配结果',results)
print('-------------------------end----------------------------')