BossSensor项目地址:https://github.com/Hironsan/BossSensor/
一、使用方法:
1、下载项目并解压
2、项目主目录下创建一个store目录
训练生成模型会保存在这个目录下
3、安装相关依赖包
Install OpenCV, PyQt4, Anaconda.
conda create -n venv python=3.5
source activate venv
conda install -c https://conda.anaconda.org/menpo opencv3
conda install -c conda-forge tensorflow
pip install -r requirements.txt
Change Keras backend from Theano to TensorFlow.
4、训练特征识别模型
$ python boss_train.py
5、使用模型进行识别
$ python camera_reader.py
6、使用过程中可能遇到错误
可能是软件版本不匹配,查看错误提示信息,并处理好。
二、我修改后的代码
因为直接用出现了一些问题,所有进行了部分代码修改:
1、camera_reader.py 修改
做以下修改:
①、从ip摄像头或本地读取视频流。
②、每隔timeF帧进行一次采集识别 (对每一帧进行识别电脑速度跟不上)
③、实时显示图像,识别出来画框框
# -*- coding:utf-8 -*-
import cv2
from boss_train import Model
from image_show import show_image
import numpy as np
import time
if __name__ == '__main__':
cv2.namedWindow("camera",1)
#开启ip摄像头
video="http://172.20.11.42:8080/video" #此处@后的ipv4 地址需要修改为自己的地址
# video="./yfw.mp4" #从本地读取视频流
cap=cv2.VideoCapture(video)
cascade_path = "/usr/local/lib/python3.5/dist-packages/cv2/data/haarcascade_frontalface_default.xml" #
model = Model()
model.load()
timeF=30
c=1
while True:
success,frame = cap.read()
if (c % timeF == 0): # 每隔timeF帧进行一次图像识别
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 加载分离器
cascade = cv2.CascadeClassifier(cascade_path)
print('YFW----GET FACE!!')
# 分离器进行人脸识别,获取人脸位置信息
facerect = cascade.detectMultiScale(frame_gray, scaleFactor=1.2, minNeighbors=3, minSize=(10, 10))
#facerect = cascade.detectMultiScale(frame_gray, scaleFactor=1.01, minNeighbors=3, minSize=(3, 3))
if len(facerect) > 0: #对图像中的所有人脸进行特征识别,是否存在boss的人脸
print('face detected')
color = (255, 255, 255) # 白
for rect in facerect:
print('YFW----FOR!!')
# 検出した顔を囲む矩形の作成
#cv2.rectangle(frame, tuple(rect[0:2]), tuple(rect[0:2] + rect[2:4]), color, thickness=2)
print(rect)
x, y = rect[0:2]
width, height = rect[2:4]
image = frame[y: y + height, x: x + width] # 截取图像的人脸部分
image = cv2.resize(image,(64,64)) #修改图像大小为64*64 【这句很重要】
cv2.rectangle(frame, (x, y), (x+width, y+height), (0, 255, 0), 2) #画矩形
# print(image.shape)
result = model.predict(image) #人脸特征识别,识别到boss的人脸返回 0
result=0
if result == 0: # boss
print('Boss is approaching')
else:
print('Not boss')
cv2.imshow("camera",frame)
c = c + 1
#按键处理,注意,焦点应当在摄像头窗口,不是在终端命令行窗口
key = cv2.waitKey(10)
if key == 27:
#esc键退出
print("esc break...")
break
cap.release()
cv2.destroyWindow("camera")
image = cv2.resize(image,(64,64)) #修改图像大小为64*64 【这句很重要,因为后面代码只对64*64的头像进行处理,不一致会出错】
2、boss_train.py 修改
找到def predict(self, image):
增加一行image = image.reshape((1,IMAGE_SIZE, IMAGE_SIZE,3))
def predict(self, image):
if K.image_data_format() == 'channels_first' and ——image.shape != (3, IMAGE_SIZE, IMAGE_SIZE,1):
image = resize_with_pad(image)
image = image.reshape((1, 3,IMAGE_SIZE, IMAGE_SIZE))
elif K.image_data_format() == 'channels_first' and image.shape != (1,IMAGE_SIZE, IMAGE_SIZE, 3):
image = resize_with_pad(image)
image = image.reshape((1, IMAGE_SIZE, IMAGE_SIZE, 3))
+++++ image = image.reshape((1,IMAGE_SIZE, IMAGE_SIZE,3))
因为我打印image.shape可以看到图像数据是三维,而这里image.shape != (3, IMAGE_SIZE, IMAGE_SIZE,1)
只处理四维数据。所以增加image = image.reshape((1,IMAGE_SIZE, IMAGE_SIZE,3))
直接进行矩阵变化。
3、训练数据
①准备图片数据:
分别放入boss,和other。
②、修改图像大小
下载图像大小批量修改工具sizepic
③、开始训练
$ python boss_train.py
4、对训练结果进行测试