From 2a2a3b30cebd504445e2b57a43386f15ecf35a8c Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 23 Sep 2023 02:59:24 +0700 Subject: [PATCH] Update face_analyzer.py --- access_control_python/face_analyzer.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/access_control_python/face_analyzer.py b/access_control_python/face_analyzer.py index f4271af..e99e1a2 100644 --- a/access_control_python/face_analyzer.py +++ b/access_control_python/face_analyzer.py @@ -3,21 +3,38 @@ import cv2 import base64 import requests import json -cam = cv2.VideoCapture(1) +import threading +import time +cam = cv2.VideoCapture(0) + +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: - ret, img = cam.read() + while not frame_ready: + time.sleep(1) + ret, jpg_buffer = cv2.imencode('.jpg',img) b64_img = base64.b64encode(jpg_buffer) data = {"image":b64_img.decode("utf-8")} res = requests.post("https://racist.siwatsystem.com/process_image",json=data) res = res.content.decode("utf-8") res = json.loads(res) - #print(req) + print(res) if len(res) >0: if (res[0]['dominant_race']=="asian"): print("Ching Chong") elif(res[0]['dominant_race']=="black"): print("CRIMINAL!!") elif (res[0]['dominant_race']=="white"): - print("Ding Dong") \ No newline at end of file + print("Ding Dong")