100、网页端的人脸识别
最近在做人臉識(shí)別的一個(gè)需求,要給客戶看Demo證明我們公司可以做
其實(shí)很多時(shí)候,別的同事一聽到這個(gè)需求就先說要直接調(diào)用微軟云上面的服務(wù)。
其實(shí)他不懂什么叫做神經(jīng)網(wǎng)絡(luò),也不知道什么叫做Res-net 更不知道什么叫做VGG16 , VGG19
其實(shí)也很對(duì),知道和不知道,并沒有什么區(qū)別。
即使知道了也不能怎么滴呢。也不會(huì)給你漲工資、
所以知道的意義何在。
不扯這些了,下面開始進(jìn)入正題
1、前端使用的是python 的 django web框架。
2、后端使用的是face_recognition的人臉識(shí)別庫(kù)
3、通過websocket 進(jìn)行視頻人臉識(shí)別
?
?
第一部分websocket
前端js部分代碼如下所示
主要目的是建立Websocket鏈接,然后每200毫秒截一下視頻,并且將截好的圖片通過websocket發(fā)送到后臺(tái)
var ws = null;var video = $("#live").get()[0];var canvas = $("#canvas");var ctx = canvas.get()[0].getContext('2d');if (window.location.protocol === "https:") {wsproto = "wss";} else {wsproto = "ws";}if (window.location.protocol === "file:") {wsuri = wsproto + "://127.0.0.1:8080/ws";} else {wsuri = wsproto + "://" + window.location.hostname + ":9001/ws";}if ("WebSocket" in window) {ws = new WebSocket(wsuri);} else if ("MozWebSocket" in window) {ws = new MozWebSocket(wsuri);} else {console.log("Browser does not support WebSocket!");}if (ws) {ws.onopen = function () {console.log("Openened connection to websocket");}// 收到WebSocket服務(wù)器數(shù)據(jù)ws.onmessage = function(e) {var name = eval('('+e.data+')');var labelName = $("#recognise_name");// e.data contains received string.console.log("onmessage: " + name["face_found_in_image"]);if(name["face_found_in_image"]!=""&&name["face_found_in_image"]!=null){stop();labelName.text("Hi dear " + name["face_found_in_image"] + " we find you");labelName.show();ws.close();}};// 關(guān)閉WebSocket連接ws.onclose = function(e) {console.log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')");ws = null;};}navigator.webkitGetUserMedia({video:true, audio:true},function(stream) {video.src = window.webkitURL.createObjectURL(stream);},function(err) {console.log("Unable to get video stream!")});var dataURI = function dataURIToBlob(dataurl){var arr = dataurl.split(',');var mime = arr[0].match(/:(.*?);/)[1];var bstr = atob(arr[1]);var n = bstr.length;var u8arr = new Uint8Array(n);while(n--){u8arr[n] = bstr.charCodeAt(n);}return new Blob([u8arr], {type:mime});}start();timer = setInterval(function () {ctx.drawImage(video, 0, 0, 480, 360);var data = canvas.get()[0].toDataURL('image/png', 1.0);newblob = dataURI(data);if (ws.readyState===1) {ws.send(newblob);console.log(newblob)}else{//do something}}, 200);?
?
下面部分是python的后臺(tái)代碼。
主要目的是通過Websocket接收前臺(tái)傳遞過來的圖片。并且將圖片存儲(chǔ)到指定目錄下面
class EchoServerProtocol(WebSocketServerProtocol):def __init__(self):super(WebSocketServerProtocol, self).__init__()def onMessage(self, payload, isBinary):result = {}face_rec_rsult=""if isBinary:#self.detect_faces_in_image(img_b64decode)if len(payload) >0:byte_stream = io.BytesIO(payload)roiImg = Image.open(byte_stream)imgByteArr = io.BytesIO()roiImg.save(imgByteArr, format='png')imgByteArr = imgByteArr.getvalue()now = datetime.datetime.now()tempPicturePath = "./"+now.strftime('%Y-%m-%d-%H-%M-%S')+".png"tempPicture = open(tempPicturePath, "wb")tempPicture.write(imgByteArr)tempPicture.close()face_rec_rsult = self.detect_faces_in_image(tempPicturePath)os.remove(tempPicturePath)print("temp picture removed")print(face_rec_rsult)self.sendMessage(json.dumps(face_rec_rsult).encode(encoding="utf-8"))else:#print("Text message received: {}".format(payload))self.sendMessage(payload)?
?
再下面是調(diào)用face_recognition包進(jìn)行人臉識(shí)別的部分。
這里要說明,人臉的向量,是在websocket server端代碼啟動(dòng)的時(shí)候,加載完成的。這樣就可以在新的請(qǐng)求到來的時(shí)候
直接進(jìn)行人臉識(shí)別的計(jì)算,不需要再進(jìn)行Res-net的前向encoding了。提高了web的識(shí)別時(shí)間
當(dāng)然下面只是代碼片段。完整的代碼可以去本文最下面github的鏈接中查看。
要說明的是開發(fā)環(huán)境是在linux下面,centos 7 。如果要在windows上面運(yùn)行需要裝docker或者虛擬機(jī)。之后再虛擬環(huán)境中運(yùn)行本Demo
def detect_faces_in_image(self,file_stream):#load unknow face image and encode itimgunknow = face_recognition.load_image_file(file_stream)unknown_face_encodings = face_recognition.face_encodings(imgunknow)imgknow = know_user_encodingknown_face_names = ["LiHui","Dehua","GuoMeng","WeiZhen"]face_found = Falseis_weizhen = Falseface_names = []if len(unknown_face_encodings) > 0:face_found = True# See if the first face in the uploaded image matches the known face of Obamastart = time.time()match_results = face_recognition.compare_faces(imgknow, unknown_face_encodings[0],tolerance=0.45)end = time.time()print(time.ctime(), "face_recognition_compare_faces : ", end - start)print(match_results)if True in match_results:first_match_index = match_results.index(True)name = known_face_names[first_match_index]face_names.append(name)# Return the result as jsonresult = {"face_found_in_image": face_names}return result?
總體的效果如下圖所示。可以看出,在光線合適,角度合適,距離合適的情況下,基本能在2秒以內(nèi)識(shí)別出人
完整的代碼請(qǐng)參考如下的github鏈接:
https://github.com/weizhenzhao/Face_Recognise
?
Thanks
WeiZhen
總結(jié)
以上是生活随笔為你收集整理的100、网页端的人脸识别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: IxChariot 带破解
- 下一篇: Python selenium自动化刷问
