Commit 564d3055 authored by YONG-LIN SU's avatar YONG-LIN SU

add save error detention somebody to the local folder for retrain

parent c13b3fd0
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"from sklearn.externals import joblib\n",
"from skimage.transform import resize\n",
"from sklearn.preprocessing import LabelEncoder\n",
"from keras.models import model_from_json\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 載入人臉偵測cascade分類器\n",
"face_cascade = cv2.CascadeClassifier(\"../model/cv2/haarcascade_frontalface_alt2.xml\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 載入Facenet預測模型\n",
"model=model_from_json(open(\"../model/keras/facenet_model.json\",\"r\").read())\n",
"model.load_weights(\"../model/keras/facenet_weights.h5\")\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 載入SVM分類器\n",
"clf=joblib.load('../model/20190618224324/20190618224324.pkl')\n",
"# 載入LabelEncoder\n",
"le=LabelEncoder()\n",
"le.classes_ =np.load('../model/20190618224324/classes.npy')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 影像預處理\n",
"def prewhiten(x):\n",
" if x.ndim == 4:\n",
" axis = (1, 2, 3)\n",
" size = x[0].size\n",
" elif x.ndim == 3:\n",
" axis = (0, 1, 2)\n",
" size = x.size\n",
" else:\n",
" print(x.ndim)\n",
" raise ValueError('Dimension should be 3 or 4')\n",
"\n",
" mean = np.mean(x, axis=axis, keepdims=True)\n",
" std = np.std(x, axis=axis, keepdims=True)\n",
" std_adj = np.maximum(std, 1.0/np.sqrt(size))\n",
" y = (x - mean) / std_adj\n",
" return y\n",
"\n",
"def l2_normalize(x, axis=-1, epsilon=1e-10):\n",
" output = x / np.sqrt(np.maximum(np.sum(np.square(x), axis=axis, keepdims=True), epsilon))\n",
" return output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 人臉偵測處理回傳結果矩陣\n",
"image_size=160\n",
"def face_cropped(img,faces, margin): \n",
" aligned_images = []\n",
" for f in faces:\n",
" (x, y, w, h) = f\n",
" cropped = img[y-margin//2:y+h+margin//2,x-margin//2:x+w+margin//2, :]\n",
" aligned = resize(cropped, (image_size, image_size), mode='reflect')\n",
" aligned_images.append(aligned)\n",
" \n",
" return np.array(aligned_images)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 取得人臉Facenet預測之特徵值\n",
"def calc_embs(faces, margin=10, batch_size=1):\n",
" aligned_images = prewhiten(faces)\n",
" pd = []\n",
" for start in range(0, len(aligned_images), batch_size):\n",
" pd.append(model.predict_on_batch(aligned_images[start:start+batch_size]))\n",
" embs = l2_normalize(np.concatenate(pd))\n",
"\n",
" return embs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 人臉辨識推斷\n",
"def infer(le, clf, img):\n",
" faces = face_cascade.detectMultiScale(img,scaleFactor=1.1,minNeighbors=3)\n",
" if(len(faces)==0):\n",
" return '偵測不到人臉請重新調整'\n",
" \n",
" embs = calc_embs(face_cropped(img,faces,10))\n",
"# pred = le.inverse_transform(clf.predict(embs))\n",
" pred=get_labels(le,clf,embs)\n",
" return [faces,pred]\n",
"# Labels 解析\n",
"def get_labels(le,clf,embs):\n",
" socres=clf.predict_proba(embs)\n",
" print(socres)\n",
" results=[]\n",
" for s in socres:\n",
" if(s[s.argmax()]>0.5):\n",
" results.append(le.inverse_transform([s.argmax()])[0])\n",
" else:\n",
" results.append('Unknow')\n",
" return results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"img=cv2.imread('../data/Test/Kevin04.jpg')\n",
"# img2=cv2.imread('../data/Test/test6.jpg')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"result=infer(le,clf,img)\n",
"# result2=infer(le,clf,img2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 顯示結果\n",
"if(len(result)==2): \n",
" fa=result[0]\n",
" na=result[1]\n",
" for f,n in zip(fa,na):\n",
" x,y,w,h=f\n",
" margin=10\n",
" cv2.rectangle(img,(x-margin//2,y-margin//2),(x+w+margin//2,y+h+margin//2),(0,0,255))\n",
" cv2.putText(img, n, (x-margin//2,y-margin//2), cv2.FONT_HERSHEY_PLAIN,1, (0, 255, 255), 1, cv2.LINE_AA)\n",
" cv2.imshow('frame',img)\n",
" cv2.waitKey(0)\n",
" cv2.destroyAllWindows()\n",
"else:\n",
" print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"faces = face_cascade.detectMultiScale(img,scaleFactor=1.1,minNeighbors=3)\n",
"if(len(faces)!=0):\n",
" err,embs = calc_embs(face_cropped(faces,10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"socre=clf.predict_proba(embs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"get_labels(le,clf,embs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fa=result1[0]\n",
"na=result1[1]\n",
"print(f,n)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for f,n in zip(fa,na):\n",
" print(str(f) + n)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment