2016年科大网络程序设计课程即将结束,来总结一下这门课程给我带来的收获。首先在这里要感谢孟老师的教导,孟宁老师授课方式很特别,采用项目驱动方式,由学生在老师的带动下主动开发,在这门课程中,班里70多个学生共同完成了这个项目。很惭愧,我对这个项目并无什么直接的贡献。我学习了python和一些机器学习算法,对CNN,RNN,深度学习框架及图像处理有了些了解。python
项目介绍mysql
基于深度学习神经网络等机器学习技术实现一个医学辅助诊断的专家系统原型,体现为对血常规检验报告的OCR识别,而后将识别的数据传入神经网络中预测年龄和性别。算法
项目要求sql
1.将血常规检验报告的图片识别出年龄、性别及血常规检验的各项数据,获得一个json数据存储到了MongoDB数据库中,如识别出的结果与图片不一致,可人工修正。mongodb
2.搭建神经网络学习血常规检验的各项数据及对应的年龄性别,而且最后能根据血常规数据预测年龄和性别。数据库
安装包:json
# 安装numpy sudo apt-get install python-numpy # http://www.numpy.org/ # 安装opencv sudo apt-get install python-opencv # http://opencv.org/ ##安装OCR和预处理相关依赖 sudo apt-get install tesseract-ocr sudo pip install pytesseract sudo apt-get install python-tk sudo pip install pillow # 安装Flask框架、mongo sudo pip install Flask sudo apt-get install mongodb # 若是找不到能够先sudo apt-get update sudo service mongodb started sudo pip install pymongo #安装pandas和 scikit-learn sudo apt-get install python-numpy cython python-scipy python-matplotlib pip install -U scikit-learn(若是不行就加sudo) pip install pandas
view.py --Web 端上传图片到服务器,存入mongodb并获取oid。 imageFilter.py --对图像透视裁剪和OCR进行了简单的封装,以便于模块间的交互,规定适当的接口.是整个ocr中最重要的模块. classifier.py --用于断定裁剪矫正后的报告和裁剪出检测项目的编号 imgproc.py --将识别的图像进行处理二值化等操做,提升识别率 包括对中文和数字的处理
先对整张图片作预处理。 浏览器
#灰度化 img_gray = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY) #高斯平滑 img_gb = cv2.GaussianBlur(img_gray, (gb_param,gb_param), 0) #闭运算 closed = cv2.morphologyEx(img_gb, cv2.MORPH_CLOSE, kernel) #开运算 opened = cv2.morphologyEx(closed, cv2.MORPH_OPEN, kernel) #canny算子边缘检测 edges = cv2.Canny(opened, canny_param_lower , canny_param_upper)
因为整张图有英文,中文,数字,符号等混杂在一块儿,因此要直接识别实现难度至关大。因此首先要对图片进行裁剪成单个的小方块。定位图片是关键。服务器
将黑线看成识别的特征网络
# 调用findContours提取轮廓 contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 求得最小外接矩形 def getbox(i): rect = cv2.minAreaRect(contours[i]) box = cv2.cv.BoxPoints(rect) box = np.int0(box) return box
# 由三条线来肯定表头的位置和表尾的位置 line_upper, line_lower = findhead(line[2],line[1],line[0]) # 由表头和表尾肯定目标区域的位置 # 利用叉乘的不可交换性肯定起始点 total_width = line_upper[1]-line_upper[0] total_hight = line_lower[0]-line_upper[0] cross_prod = cross(total_width, total_hight) if cross_prod <0: temp = line_upper[1] line_upper[1] = line_upper[0] line_upper[0] = temp temp = line_lower[1] line_lower[1] = line_lower[0] line_lower[0] = temp
随机森林是一个树型分类器{h(x,k),k=1,…}的集合。其中元分类器h(x,k)是用CART算法构建的没有剪枝的分类回归树;x是输入向量;k是独立同分布的随机向量,决定了单颗树的生长过程;森林的输出采用简单多数投票法(针对分类)或单颗树输出结果的简单平均(针对回归)获得。
利用随机森林训练模型
年龄模型:
min_max_scaler = preprocessing.MinMaxScaler() x_train, x_test, y_train, y_test = train_test_split(train_data, train_labels, test_size=0.2) #clf = RandomForestRegressor(n_estimators=25) #clf.fit(x_train, y_train) clf = joblib.load("./age/age_model.m") y_pred = clf.predict(x_test) print "MAE:",metrics.mean_absolute_error(y_test, y_pred) joblib.dump(clf, "./age/age_model.m")
性别模型:
min_max_scaler = preprocessing.MinMaxScaler() x_train, x_test, y_train, y_test = train_test_split(train_data, train_labels, test_size=0.2) #clf = RandomForestClassifier(n_estimators=25) clf = joblib.load("./sex/sex_model.m") #clf.fit(x_train, y_train) y_pred = clf.predict(x_test) # clf = RandomForestClassifier(n_estimators=25) # clf.fit(x_train, y_train) # y_pred = clf.predict(x_test) p = np.mean(y_pred == y_test) print precision_score(y_test, y_pred, average='macro') print recall_score(y_test, y_pred, average='macro') print f1_score(y_test, y_pred, average='macro')
将模型集成到诊断系统中
elif app.config['MODEL'] == 'rf': clf_age = joblib.load("./age/age_model.m") clf_sex = joblib.load("./sex/sex_model.m") age = clf_age.predict(arr) sex = clf_sex.predict(arr) result = { "sex": int(sex), "age": int(age) }
cd BloodTestReportOCR python view.py # upload图像,在浏览器打开http://yourip:8080
选择图片上传
生成报告
进行预测
机器时是面对一个具体问题,从给定的数据中产生模型的算法,感悟到在学习时,要有意识甚至刻意的的创建起知识架构,从而在面对一个问题时,才能有完整的求解思路。机器学习才开始接触,还要好多
算法须要学习,须要知道这些算法在什么场合下使用,以及这些算法的优缺点,以及调参经验等等。