用caffe做个人脸识别

上篇我们把lenet改成caffe,试了试可以!

好,我们再改改,做人脸识别!先看运行效果:

上图用caffe训练了2000次人脸!

训练后,识别了一下,softmax处理后的结果是10左右

然后遮住摄像头,识别了一下,softmax处理后的结果是7左右,还行!

写程序,可能有bug,但首先要能跑!

caffe我们已经讲过了,lenet改的!这里我们把640*480的usb cam图像缩放到227*227的代码放出来:

我们打开电脑自带的摄像头软件,放置在left=0,top=0的地方,然后程序中创建一个timer,实时截屏rect=(0,0,640,480)区域,然后我们调用ZoomNormal函数处理成227*227图像:

private void timer1_Tick_1(object sender, EventArgs e)

{

Bitmap bmp = new Bitmap(640, 480, PixelFormat.Format32bppArgb);

// 创建一个画布

Graphics g = Graphics.FromImage(bmp);

Point hello = new Point(0, 80);

g.CopyFromScreen(hello, Point.Empty, new Size(640, 480));

// 释放画布资源

g.Dispose();

// pictureBox4.Image = bmp;//640*480转227*227

//从bitmap中获取图像

Rectangle rc = new Rectangle(0, 0, bmp.Width, bmp.Height);

System.Drawing.Imaging.BitmapData bmpdata = bmp.LockBits(rc,

System.Drawing.Imaging.ImageLockMode.ReadWrite,

bmp.PixelFormat);

IntPtr imageptr = bmpdata.Scan0;

int ww = bmp.Width;

int hh = bmp.Height;

int bytes=0;

if (bmp.PixelFormat == PixelFormat.Format24bppRgb)

{

bytes = ww * hh * 3;//此处针对的是24位位图

}

//if (bmp.PixelFormat == PixelFormat.Format8bppIndexed)PixelFormat.Format32bppRgb

if (bmp.PixelFormat == PixelFormat.Format32bppArgb)

{

bytes = ww * hh*4;

}

byte\[\] glob_rgbValues = new bytebytes;

glob_buffer8 = new byteww \* hh;

System.Runtime.InteropServices.Marshal.Copy(imageptr, glob_rgbValues, 0, bytes);

bmp.UnlockBits(bmpdata);

for (int j = 0; j < hh; j++)

{

for (int i = 0; i < ww; i = i + 1)

{

int nn = j * ww + i;

// glob_buffer8nn = glob_rgbValuesnn \* 4;//此处是32位灰度图像(黑白);以下注释掉的是32位argb图像(彩色)

glob_buffer8nn = (byte)(glob_rgbValuesnn \* 4*0.114+glob_rgbValuesnn \* 4+1*0.587+glob_rgbValuesnn \* 4+2*0.299);

//不用glob_rgbValuesnn \* 4+3alpha分量

}

}

//zoom

newimg = new byte227 \* 227; int neww = 0; int newh = 0;

ZoomNormal(glob_buffer8,ref newimg, 640, 480, ref neww, ref newh, 0.354, 0.473);

showbuffer2pictmod4(newimg,227,227,pictureBox4);

}

void ZoomNormal(byte\[\] image0, ref byte\[\] image1, int w, int h,

ref int outwidth, ref int outheight, double ZoomX, double ZoomY)

{

outwidth = (int)(w * ZoomX + 0.5);

outheight = (int)(h * ZoomY + 0.5);

int newbuffersize = outwidth * outheight;

image1 = new bytenewbuffersize;

int x = 0;

int y = 0;

int tempY;

int tempJ;

for (int j = 0; j < outheight; j++)

{

y = (int)(j / ZoomY + 0.5);

if (y >= h) y--;

tempY = y * w;

tempJ = j * outwidth;

for (int i = 0; i < outwidth; i++)

{

x = (int)(i / ZoomX + 0.5);

if (x >= w)

x--;

image1tempJ + i = image0tempY + x;

}

}

}

最后,我们把newimg图像喂给caffe做输入,可以训练,也可以用来测试

相关推荐
姗姗来迟了几秒前
前端传图片给多模态 Agent:压缩、预览、格式那些破事
人工智能
Sam092712 分钟前
Spec Coding 和 Vibe Coding 的区别:AI Coding 从感觉驱动到规格驱动
人工智能·ai
Kobebryant-Manba31 分钟前
学习RNN(简洁实现)
人工智能·rnn·学习
德迅--文琪39 分钟前
当前 2026 年 AI 狂潮时代,抗 DDoS 产品公司品牌推荐
人工智能·ddos
机器之心43 分钟前
Claude Fable 5四日惊魂
人工智能·openai
机器之心44 分钟前
打破SWE-bench唯分数论,首个独立测量harness的基准开源了
人工智能·openai
江畔柳前堤1 小时前
github实战指南07-CLI 与高级技巧
前端·人工智能·chrome·深度学习·github·caffe·issue
右耳朵猫AI1 小时前
GitHub周趋势2026W23 | last30days-skill AI搜索、headroom令牌压缩、apple/container开源
人工智能·开源·github
ZhengEnCi1 小时前
09ba-斯坦福CS336作业一-前馈网络
人工智能
武子康1 小时前
调查研究-175 Supermemory:AI 时代的 Memory API,不只是另一个向量数据库
人工智能·openai