Commit aa3e3738 authored by Bruce's avatar Bruce

add insert new face for training

parent 71fd32c2
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import tkinter as tk
import cv2
from PIL import Image,ImageTk
import time
import requests
import os
import threading
# In[2]:
# 接收攝影機串流影像,採用多執行緒的方式,降低緩衝區堆疊圖幀的問題。
class ipcamCapture:
def __init__(self, URL):
self.Frame = []
self.status = False
self.isstop = False
# 攝影機連接。
self.capture = cv2.VideoCapture(URL)
def start(self):
# 把程式放進子執行緒,daemon=True 表示該執行緒會隨著主執行緒關閉而關閉。
print('ipcam started!')
threading.Thread(target=self.queryframe, daemon=True, args=()).start()
def stop(self):
# 記得要設計停止無限迴圈的開關。
self.isstop = True
print('ipcam stopped!')
def getframe(self):
# 當有需要影像時,再回傳最新的影像。
return self.Frame
def queryframe(self):
while (not self.isstop):
self.status, self.Frame = self.capture.read()
self.capture.release()
# In[3]:
# api server environment
api_server='http://192.168.5.205:8000'
# In[4]:
# casecade 分類器載入
face_cascade = cv2.CascadeClassifier("../model/cv2/haarcascade_frontalface_alt2.xml")
# In[5]:
# camera = cv2.VideoCapture('rtsp://iCRc0en0w:VEZ3LCKK@192.168.5.1:2555/d878278e67636fbb')
window = tk.Tk()
window.title("人臉辨識打卡系統")
window.config(background="#FFFFFF")
# 連接攝影機
ipcam=ipcamCapture("rtsp://iCRc0en0w:VEZ3LCKK@192.168.5.1:2555/d878278e67636fbb")
# 啟動子執行緒
ipcam.start()
# 暫停1秒,確保影像已經填充
time.sleep(1)
# In[6]:
imageFrame = tk.Frame(window, width=1024, height=980)
imageFrame.grid(row=1, column=0,rowspan=4, padx=10, pady=2)
#root.protocol('WM_DELETE_WINDOW', detector)
lmain = tk.Label(imageFrame)
lmain.grid(row=0, column=0)
window.config(cursor="arrow")
# In[7]:
status_label=tk.Label(window,text='歡迎使用本系統',font=("標楷體", 20))
status_label.grid(row=0,column=0,columnspan=3)
time_label=tk.Label(window,text='時間:',font=("標楷體", 20))
time_label.grid(row=0,column=1)
t=tk.Label(window,text=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),font=("Times New Roman", 20))
t.grid(row=0,column=2)
name_label=tk.Label(window,text='姓名: ',font=("標楷體", 20))
name_label.grid(row=1,column=1)
english_name=tk.Label(window,text='英文名: ',font=("標楷體", 20))
english_name.grid(row=2,column=1,pady=10)
tel=tk.Label(window,text='電話: ',font=("標楷體",20))
tel.grid(row=3,column=1,pady=10)
since_year=tk.Label(window,text='入學年: ',font=("標楷體",20))
since_year.grid(row=4,column=1,pady=10)
# In[8]:
name_text=tk.Text(window,height=1,width=10,font=("標楷體", 20))
name_text.grid(row=1,column=2)
english_name_text=tk.Text(window,height=1,width=10,font=("標楷體", 20))
english_name_text.grid(row=2,column=2,pady=10)
phone_text=tk.Text(window,height=1,width=10,font=("標楷體", 20))
phone_text.grid(row=3,column=2,pady=10)
since_year_text=tk.Text(window,height=1,width=10,font=("標楷體", 20))
since_year_text.grid(row=4,column=2,pady=10)
# In[9]:
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
# In[10]:
def upload():
frame = ipcam.getframe()
imencoded = cv2.imencode(".jpg", frame)[1]
file = {'file': ('image.jpg', imencoded.tostring(), 'image/jpeg', {'Expires': '0'})}
engname=english_name_text.get(1.0,tk.END).split('\n')[0]
if(len(engname)==0):
status_label['text']='請先輸入英文名稱在上傳影像'
else:
data={'name':engname}
response=requests.post(os.path.join(api_server,'upload','image'),files=file,data=data)
def submit():
name=name_text.get(1.0,tk.END).split('\n')[0]
engname=english_name_text.get(1.0,tk.END).split('\n')[0]
phone=phone_text.get(1.0,tk.END).split('\n')[0]
years=since_year_text.get(1.0,tk.END).split('\n')[0]
if(is_number(phone) and is_number(years)):
data={'name':name,'engname':engname,'phone':phone,'years':years}
response=requests.post(os.path.join(api_server,'insert'),data=data)
else:
status_label['text']='輸入格式錯誤請重新輸入'
def video_loop():
t['text']=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
frame = ipcam.getframe()
h,w=frame.shape[:2]
img=cv2.resize(frame,(int(w/2),int(h/2)))
faces = face_cascade.detectMultiScale(frame,scaleFactor=1.1,minNeighbors=3)
if(len(faces)>0):
for f in faces:
x,y,w,h=f
margin=10
cv2.rectangle(frame,(x-margin//2,y-margin//2),(x+w+margin//2,y+h+margin//2),(0,0,255))
cv2image = cv2.cvtColor(img,cv2.COLOR_BGR2RGBA)
current_image = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=current_image)
lmain.imgtk = imgtk
lmain.config(image=imgtk)
window.after(10, video_loop)
# In[11]:
loginbtn = tk.Button(window, text="上傳照片",command=upload,height=1,font=("標楷體", 20))
loginbtn.grid(row=5,column=0,padx=10, pady=2)
logoutbtn = tk.Button(window, text="提交",command=submit,height=1,font=("標楷體", 20))
logoutbtn.grid(row=5,column=1,padx=10, pady=2)
video_loop()
window.mainloop()
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