facerecognizer java_JAVACV人脸识别代码下载 酷~~

package test;

import org.eclipse.swt.graphics.Point;

import org.eclipse.swt.widgets.FileDialog;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.SWT;

import org.eclipse.swt.graphics.Rectangle;

import org.eclipse.swt.graphics.Font;

import org.eclipse.swt.widgets.Button;

import com.googlecode.javacv.FrameGrabber.Exception;

public class LoginShell {

public static Shell sShell = null;

private Button button_FaceLogin = null;

private Button button_Register = null;

private Label label = null;

public static Button checkBox_video=null;

private Button button_RecongizeFromImage = null;

public static boolean video_flag=false;

int count=1;

public Shell getShell()

{

return sShell;

}

public LoginShell(){

createSShell();

}

/**

* @param args

*/

@SuppressWarnings("static-access")

public static void main(String[] args) {

Display display = Display.getDefault();

LoginShell thisClass = new LoginShell();

thisClass.createSShell();

thisClass.sShell.open();

while (!thisClass.sShell.isDisposed()) {

if (!display.readAndDispatch())

display.sleep();

}

display.dispose();

}

private void createSShell() {

sShell = new Shell(SWT.APPLICATION_MODAL | SWT.SHELL_TRIM | SWT.BORDER);

sShell.setText("BP_FaceRecoginzer");

sShell.setSize(new Point(476, 333));

sShell.setLayout(null);

button_FaceLogin = new Button(sShell, SWT.NONE);

button_FaceLogin.setBounds(new Rectangle(35, 70, 170, 112));

button_FaceLogin.setFont(new Font(Display.getDefault(), "宋体", 14, SWT.NORMAL));

button_FaceLogin.setText("人脸识别");

button_Register = new Button(sShell, SWT.NONE);

button_Register.setBounds(new Rectangle(249, 70, 186, 111));

button_Register.setFont(new Font(Display.getDefault(), "宋体", 14, SWT.NORMAL));

button_Register.setText("人脸注册");

label = new Label(getShell(), SWT.NONE);

label.setBounds(new Rectangle(172, 280, 150, 30));

label.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));

label.setText("第一次使用先注册训练样本");

button_RecongizeFromImage = new Button(getShell(), SWT.NONE);

button_RecongizeFromImage.setBounds(new Rectangle(42, 190, 381, 72));

button_RecongizeFromImage.setFont(new Font(Display.getDefault(), "微软雅黑", 14, SWT.NORMAL));

button_RecongizeFromImage.setText("识别图片");

checkBox_video = new Button(getShell(), SWT.CHECK);

checkBox_video.setBounds(new Rectangle(80, 0,20, 20));

checkBox_video.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {

public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {

count++;

if(count%2==0)

video_flag=true;

else

video_flag=false;

}

});

label = new Label(getShell(), SWT.NONE);

label.setBounds(new Rectangle(100, 0, 350, 60));

label.setFont(new Font(Display.getDefault(), "微软雅黑", 10, SWT.NORMAL));

label.setText("点击. 用视频文件进行识别或注册.\n注意:只支持avi,wmv.mp4格式的视频\n且路径名和文件名不能包含中文!");

label.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));

button_RecongizeFromImage

.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {

public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {

FaceRecognizer fr=new FaceRecognizer();

FileDialog fd=new FileDialog(LoginShell.sShell,SWT.OPEN);

fd.setFilterExtensions(new String[]{"*.jpg","*.bmp","*.png","*.*"});

fd.setFilterNames(new String[]{".jpeg",".bmp",".png"});

String filename=fd.open();

fr.recongizeFormImage(filename);

}

});

button_FaceLogin

.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {

public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {

FaceRecognizer fr=new FaceRecognizer();

boolean flag=true;

while(flag)

{

try

{

flag=fr.recognizeFromCam();

}

catch (Exception e1)

{

e1.printStackTrace();

}

}

}

});

button_Register

.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {

public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {

RegisterShell displayshell=new RegisterShell();

Shell newShell= displayshell.getShell();

Shell oldShell=LoginShell.sShell;

LoginShell.sShell=newShell;

LoginShell.sShell.open();

oldShell.dispose();

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中使用JavaCV进行人脸识别,你可以使用OpenCV的人脸识别模块。下面是一个简单的示例代码,演示了如何使用JavaCV进行人脸识别: ```java import org.bytedeco.opencv.opencv_core.*; import org.bytedeco.opencv.opencv_face.*; import org.bytedeco.opencv.opencv_objdetect.*; import static org.bytedeco.opencv.global.opencv_core.*; import static org.bytedeco.opencv.global.opencv_imgcodecs.*; import static org.bytedeco.opencv.global.opencv_imgproc.*; import static org.bytedeco.opencv.global.opencv_objdetect.*; public class FaceRecognitionExample { public static void main(String[] args) { // 加载人脸识别模型 String cascadeFile = "path/to/haarcascade_frontalface_default.xml"; CascadeClassifier cascadeClassifier = new CascadeClassifier(cascadeFile); // 加载人脸识别模型 String modelFile = "path/to/trained/model.yml"; LBPHFaceRecognizer faceRecognizer = LBPHFaceRecognizer.create(); faceRecognizer.read(modelFile); // 读取测试图像 Mat testImage = imread("path/to/test/image.jpg"); // 转换为灰度图像 Mat grayImage = new Mat(); cvtColor(testImage, grayImage, COLOR_BGR2GRAY); // 检测人脸 RectVector faces = new RectVector(); cascadeClassifier.detectMultiScale(grayImage, faces); // 对每一个检测到的人脸进行识别 for (int i = 0; i < faces.size(); i++) { Rect face = faces.get(i); // 提取人脸区域 Mat faceROI = new Mat(grayImage, face); // 标准化人脸图像 Mat normalizedFace = new Mat(); resize(faceROI, normalizedFace, new Size(100, 100)); // 进行人脸识别 IntPointer label = new IntPointer(1); DoublePointer confidence = new DoublePointer(1); faceRecognizer.predict(normalizedFace, label, confidence); // 获取识别结果 int predictedLabel = label.get(0); double predictedConfidence = confidence.get(0); // 在图像上绘制识别结果 String labelStr = "Unknown"; if (predictedConfidence < 70) { labelStr = "Person " + predictedLabel; } putText(testImage, labelStr, new Point(face.x(), face.y() - 10), FONT_HERSHEY_SIMPLEX, 0.9, Scalar.GREEN); rectangle(testImage, face, Scalar.RED); } // 显示结果图像 imshow("Face Recognition", testImage); waitKey(0); } } ``` 在这个示例中,我们首先加载了一个预训练的人脸检测器,即`haarcascade_frontalface_default.xml`。然后,我们加载了一个预训练的人脸识别模型,即`trained/model.yml`。接下来,我们读取一个测试图像,并将其转换为灰度图像。然后,我们使用人脸检测器检测图像中的人脸,并对每个检测到的人脸进行识别。最后,我们在图像上绘制识别结果,并显示结果图像。 请确保将代码中的文件路径替换为实际的文件路径。此外,你需要在项目中添加JavaCV和OpenCV的依赖项。 这只是一个简单的示例,你可以根据实际需求进行修改和扩展。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值