Skip to content

Commit 9de0eb9

Browse files
committed
face-gensui
1 parent 0356d5c commit 9de0eb9

File tree

22 files changed

+266
-0
lines changed

22 files changed

+266
-0
lines changed

automatic_weather/1.天气查询.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def mainHandle(self):
3636

3737
print(output)
3838

39+
fileHandle = open ( 'weather.txt', 'r' )
40+
begin = fileHandle.read()
41+
print(begin)
3942
fileHandle = open ( 'weather.txt', 'w' )
4043
fileHandle.write(output)
4144
fileHandle.close()

face_gensui/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
1.py是测试文件
2+
2.py是成品,能够对目录下的5.jpg进行人脸识别,并且画出一个绿色的框,重点是
3+
能够获取脸的距离位置,目的是通过获取相对位置,不断的重复读取的过程,存储到
4+
文件中,然后达到跟随的目的。
5+
6+
使用指南,
7+
请先安装好环境:
8+
linux
9+
python 2.7.3
10+
opencv 2.3.1-7
11+
安装依赖
12+
sudo apt-get install libopencv-*
13+
sudo apt-get install python-opencv
14+
sudo apt-get install python-numpy
15+
16+
进入start运行2.py可以单独测试人脸识别的功能,欲测试整体不断识别相对位置功能
17+
请运行lianxuzhibo.py文件 python lianxuzhibo.py,如果环境上没有大问题的话
18+
应该能在该目录下出现5.jpg图片文件,如果存在人脸,则会进行识别,建立一个
19+
weizhi.txt文件,存储每次人脸位置的重心,
20+
21+
剩下的部分就是根据这个txt进行摄像头的转换了,这个之后再写,别闲我懒。。过年嘛。。

face_gensui/opencv_in.jpg

7.9 KB
Loading

face_gensui/opencv_in_face/1.jpg

1.48 KB
Loading

face_gensui/opencv_in_face/out.jpg

8.95 KB
Loading

face_gensui/start/15.jpg

7.9 KB
Loading

face_gensui/start/2.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#coding=utf-8
2+
import cv2
3+
import cv2.cv as cv
4+
5+
img = cv2.imread("5.jpg")
6+
7+
def detect(img, cascade):
8+
'''detectMultiScale函数中smallImg表示的是要检测的输入图像为smallImg,
9+
faces表示检测到的人脸目标序列,1.3表示每次图像尺寸减小的比例为1.3,
10+
4表示每一个目标至少要被检测到3次才算是真的目标(因为周围的像素和不同的窗口大小都可以检测到人脸),
11+
CV_HAAR_SCALE_IMAGE表示不是缩放分类器来检测,而是缩放图像,Size(20, 20)为目标的最小最大尺寸'''
12+
rects = cascade.detectMultiScale(img, scaleFactor=1.3,
13+
minNeighbors=5, minSize=(30, 30), flags = cv.CV_HAAR_SCALE_IMAGE)
14+
if len(rects) == 0:
15+
return []
16+
print rects
17+
rects[:,2:] += rects[:,:2]
18+
print rects
19+
return rects
20+
21+
#在img上绘制矩形
22+
def draw_rects(img, rects, color):
23+
for x1, y1, x2, y2 in rects:
24+
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
25+
26+
27+
#转换为灰度图
28+
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
29+
#直方图均衡处理
30+
gray = cv2.equalizeHist(gray)
31+
32+
#脸部特征分类地址,里面还有其他
33+
cascade_fn = '/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml'
34+
35+
#读取分类器,CascadeClassifier下面有一个detectMultiScale方法来得到矩形
36+
cascade = cv2.CascadeClassifier(cascade_fn)
37+
38+
#通过分类器得到rects
39+
rects = detect(gray, cascade)
40+
41+
#vis为img副本
42+
'''
43+
以下文件操作部分可以删除,功能只是要读取上一次的人脸位置重心
44+
'''
45+
vis = img.copy()
46+
txt = str(rects)
47+
fileHandle = open ( 'weizhi.txt', 'r' )
48+
begin = fileHandle.read()
49+
fileHandle.close()
50+
51+
fileHandle = open ( 'weizhi.txt', 'w' )
52+
fileHandle.write(txt)
53+
fileHandle.close()
54+
55+
x =int(begin[2:5:])
56+
y =int(begin[6:9:])
57+
z =int(begin[10:13])/2
58+
x = x + z
59+
y = y + z
60+
print("x=",x,"y=",y,"z=",z)
61+
f = open('zhongxin.txt','w')
62+
f.write(str(x)+" "+str(y)+" "+str(z))
63+
f.close()
64+
65+
#画矩形
66+
draw_rects(vis, rects, (0, 255, 0))
67+
68+
cv2.imshow('facedetect', vis)
69+
70+
cv2.waitKey(0)
71+
cv2.destroyAllWindows()

face_gensui/start/5 .png

39.7 KB
Loading

face_gensui/start/5.jpg

39.7 KB
Loading

face_gensui/start/lianxuzhibo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import subprocess
2+
import time
3+
4+
file_number = 0
5+
6+
while True:
7+
file_name = dir + format(file_number,"05d")+".jpg"
8+
file_number = file_number + 1
9+
subprocess.call(["raspistill","-w","400","-h","400","-e","jpg","-n","-t","1","-o",file_name])
10+
subprocess.call(["cp","-f",file_name,"5.jpg"])
11+
subprocess.call(["python","2.py"])
12+
time.sleep(1)
13+

0 commit comments

Comments
 (0)