working face identify function

This commit is contained in:
Siwat Sirichai 2023-09-25 01:30:29 +07:00
parent 5bd4b4c100
commit 3ee1e00519
18 changed files with 9516 additions and 9334 deletions

View file

@ -3,23 +3,26 @@ import cv2
import base64
import requests
#Capture Image
cam = cv2.VideoCapture(1)
ret, img = cam.read()
# #Capture Image
# cam = cv2.VideoCapture(1)
# ret, img = cam.read()
# #Save Image
# cv2.imwrite("cam_test_img_pre.jpg",img)
# #Load Image
# img = cv2.imread("cam_test_img_pre.jpg")
#Load Image
img = cv2.imread("cam_test_img_pre.jpg")
#Encode Image
ret, jpg_buffer = cv2.imencode('.jpg',img)
b64_img = base64.b64encode(jpg_buffer)
#Send Image to Server
api_endpoint = "https://racist.siwatsystem.com/process_image"
api_endpoint = "http://localhost:5000/identify_face"
data = {"image":b64_img.decode("utf-8")}
req = requests.post("https://racist.siwatsystem.com/process_image",json=data)
req = requests.post(api_endpoint,json=data)
print(req.content)
#Decode Image

View file

@ -0,0 +1,28 @@
import numpy as np
import cv2
import base64
import requests
import json
api_server = "http://localhost:5000"
#api_server = "https://racist.siwatsystem.com"
def analyze_face(img):
endpoint = api_server +"/process_image"
ret, jpg_buffer = cv2.imencode('.jpg',img)
b64_img = base64.b64encode(jpg_buffer)
data = {"image":b64_img.decode("utf-8")}
req = requests.post(endpoint,json=data)
res = res.content.decode("utf-8")
res = json.loads(res)
return res
def identify_face(img, target_condidence: float):
endpoint = api_server +"/identify_face"
ret, jpg_buffer = cv2.imencode('.jpg',img)
b64_img = base64.b64encode(jpg_buffer)
data = {"image":b64_img.decode("utf-8"),"target_confidence": target_condidence}
req = requests.post(endpoint,json=data)
res = req.content.decode("utf-8")
res = json.loads(res)
return res

View file

@ -0,0 +1,8 @@
import numpy as np
import cv2
import face_processing as fp
cam = cv2.VideoCapture(0)
while True:
#Capture Image
ret, img = cam.read()
print(fp.identify_face(img,0.4))

View file

@ -1,21 +1,37 @@
import cv2
import threading
import time
import face_processing as fp
from access_control import access_control
from line_notify import LineNotify
import time
stm32 = access_control("COM12")
time.sleep(1)
stm32.lock_door()
#stm32.unlock_door()
time.sleep(1)
#door_state = False
#while True:
#print(stm32._in_payloads)
#if(door_state != stm32.get_door_state()):
# door_state = stm32.get_door_state()
# notify = LineNotify("olK1QXriiuKgfxB6xkj7SIFfj9jsXfpl2PqmjCDuBRw")
# notify.send(f'door is {door_state}')
#if(stm32.get_door_state() == True):
# stm32.lock_door()
#else:
# stm32.unlock_door()
#time.sleep(0.01)
SERIAL_PORT = "COM12"
CAMERA_INDEX = 0
cam = cv2.VideoCapture(CAMERA_INDEX)
stm32 = access_control(SERIAL_PORT)
global img
global frame_ready
frame_ready = False
def read_webcam():
global img
global frame_ready
while True:
ret, img = cam.read()
frame_ready = True
threading.Thread(target=read_webcam).start()
while True:
while not frame_ready:
time.sleep(1)
#Try to identify face
faces = fp.identify_face(img, target_condidence=0.4)
if(len(faces)>0):
print("Door Unlocked!, Locking in 5 seconds")
stm32.unlock_door()
time.sleep(5)
stm32.lock_door()
print("Door Locked!")

View file

@ -0,0 +1,26 @@
from access_control import access_control
from line_notify import LineNotify
import time
stm32 = access_control("COM12")
time.sleep(1)
while True:
cmd = input("Enter Command : ")
if(cmd == 'lock'):
stm32.lock_door()
if(cmd == 'unlock'):
stm32.unlock_door()
#stm32.unlock_door()
time.sleep(1)
#door_state = False
#while True:
#print(stm32._in_payloads)
#if(door_state != stm32.get_door_state()):
# door_state = stm32.get_door_state()
# notify = LineNotify("olK1QXriiuKgfxB6xkj7SIFfj9jsXfpl2PqmjCDuBRw")
# notify.send(f'door is {door_state}')
#if(stm32.get_door_state() == True):
# stm32.lock_door()
#else:
# stm32.unlock_door()
#time.sleep(0.01)