def store_frame(self, vid): while True: ret, frame = vid.read() if not ret: log.sys("Camera disconnected or doesn't online!") self.closed = True self.frames.append(frame)
def init(): cam_list = [] for i in range(10): vs = cv2.VideoCapture(i) if vs.isOpened(): cam_list.append(i) for cams in cam_list: log.sys("Cam {} online".format(cams)) return cam_list
def save(image, dirs, number, formats): # if int(samples) != 0: # for i in range(samples): # name = "{}{}.{}".format(number,i,formats) # path_name = os.path.join(dirs,name) # cv2.imwrite(path_name,image) # log.sys("Image created : {}".format(path_name)) # else: name = "{}.{}".format(number, formats) path_name = os.path.join(dirs, name) cv2.imwrite(path_name, image) log.sys("Image created : {}".format(path_name))
def start(vid_num): log.sys("Starting video stream...") vs = cv2.VideoCapture(vid_num) unlocked = False step = 1 while unlocked == False: ret,frame = vs.read() frame = imutils.resize(frame, width=1920) frame = adjust_gamma(frame, gamma=float(args["gamma"])) (h, w) = frame.shape[:2] imageBlob = cv2.dnn.blobFromImage( cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False) detector.setInput(imageBlob) detections = detector.forward() for i in range(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > args["confidence"]: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") face = frame[startY:endY, startX:endX] (fH, fW) = face.shape[:2] if fW < 20 or fH < 20: continue faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False) embedder.setInput(faceBlob) vec = embedder.forward() preds = recognizer.predict_proba(vec)[0] j = np.argmax(preds) proba = preds[j] name = le.classes_[j] text = "{}: {:.2f}%".format(name, proba * 100) y = startY - 10 if startY - 10 > 10 else startY + 10 cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2) cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2) if (proba*100) >= 92 and name != "Guest": datas.put({"user_id":name,"camera_id":"5d4c276616171e2938004c72"}) t = threading.Thread(target=background) t.daemon = True t.start() frame = imutils.resize(frame, width=1000) cv2.imshow("frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break cv2.destroyAllWindows()
def start(vid_num): log.sys("Starting video stream...") vs = cv2.VideoCapture(vid_num) unlocked = False step = 1 while unlocked == False: start = time.time() ret, frame = vs.read() # frame = imutils.resize(frame, width=1920) frame = adjust_gamma(frame, 2) detect_face(frame) for text in data: y = startY - 10 if startY - 10 > 10 else startY + 10 cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2) cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2) # frame = imutils.resize(frame, width=1000) # print(int(1.0 / (time.time() - start))) cv2.imshow("frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break cv2.destroyAllWindows()
size=(15, 15), key='Resolution') ], # [sg.Text('Confidence'), sg.Slider(range=(0,1),orientation='h', resolution=.1, default_value=0.5, size=(15,15), key='Confidence')], # [sg.Text('Output:')], # [sg.Output(size=(80, 10))], [sg.Button("Register"), sg.Cancel()] ] win = sg.Window('Register Faces', default_element_size=(21, 1), text_justification='right', auto_size_text=False).Layout(layout) os.system('cls' if os.name == 'nt' else 'clear') log.sys("Initliazing components..") restart = False name = "" currentdir = os.getcwd() datasetdir = os.path.join(currentdir, "dataset") # detector = cv2.CascadeClassifier(os.path.join(currentdir,"haarcascade_frontalface_default.xml")) directory = os.path.join(datasetdir, name) protoPath = os.path.join("face_detection_model", "deploy.prototxt") modelPath = os.path.join("face_detection_model", "res10_300x300_ssd_iter_140000.caffemodel") detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) total = 0 start_time = time.time() xs = 1
help="path to model trained to recognize faces") ap.add_argument("-l", "--le", required=True, help="path to label encoder") ap.add_argument("-g", "--gamma", type=float, default=1.5, help="set gamma intensity") ap.add_argument("-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections") args = vars(ap.parse_args()) # load our serialized face detector from disk log.sys("Loading face detector...") protoPath = os.path.sep.join([args["detector"], "deploy.prototxt"]) modelPath = os.path.sep.join( [args["detector"], "res10_300x300_ssd_iter_140000.caffemodel"]) detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) # detector_face = dlib.get_frontal_face_detector() # predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # fa = FaceAligner(predictor, desiredFaceWidth=128) # load our serialized face embedding model from disk log.sys("Loading face recognizer...") embedder = cv2.dnn.readNetFromTorch(args["embedding_model"]) unlocked = False thumbnail_created = False # load the actual face recognition model along with the label encoder
from imutils import paths import numpy as np import argparse import imutils import pickle import cv2 import os import system_logging as log import send import datetime import sys import json import threading log.sys("Initializing Systems..") protoPath = os.path.join("face_detection_model", "deploy.prototxt") modelPath = os.path.join("face_detection_model", "res10_300x300_ssd_iter_140000.caffemodel") detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) embedder = cv2.dnn.readNetFromTorch("openface_nn4.small2.v1.t7") start = datetime.datetime.now() log.sys("Completed!") log.sys("Processing faces...") imagePaths = list(paths.list_images("dataset")) knownEmbeddings = [] knownNames = [] bad = [] model = os.path.join("output", "embeddings.pickle") total = 0
# python train_model.py --embeddings output/embeddings.pickle --recognizer output/recognizer.pickle --le output/le.pickle # import the necessary packages from sklearn.preprocessing import LabelEncoder from sklearn.svm import SVC import pickle import system_logging as log import time import os import datetime model = os.path.join("output", "embeddings.pickle") # def main(): start = datetime.datetime.now() log.sys("Initializing systems..") count = 0 total = 0 data = pickle.loads(open(model, "rb").read()) le = LabelEncoder() labels = le.fit_transform(data["names"]) recognizer = SVC(C=1.0, kernel="linear", probability=True) recognizer.fit(data["embeddings"], labels) f = open(os.path.join("output", "recognizer.pickle"), "wb") f.write(pickle.dumps(recognizer)) f.close() f = open(os.path.join("output", "le.pickle"), "wb") f.write(pickle.dumps(le)) f.close()
ap.add_argument("-d", "--detector", required=True, help="path to OpenCV's deep learning face detector") ap.add_argument("-m", "--embedding-model", required=True, help="path to OpenCV's deep learning face embedding model") ap.add_argument("-r", "--recognizer", required=True, help="path to model trained to recognize faces") ap.add_argument("-ca", "--cam", required=True,help="number of cam") ap.add_argument("-g", "--gamma", required=True, help="amount of gamma") ap.add_argument("-l", "--le", required=True, help="path to label encoder") ap.add_argument("-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections") args = vars(ap.parse_args()) start_time = time.time() log.sys("Loading face detector...") protoPath = os.path.sep.join([args["detector"], "deploy.prototxt"]) modelPath = os.path.sep.join([args["detector"], "res10_300x300_ssd_iter_140000.caffemodel"]) detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) log.sys("Loading face recognizer...") embedder = cv2.dnn.readNetFromTorch(args["embedding_model"]) recognizer = pickle.loads(open(args["recognizer"], "rb").read()) le = pickle.loads(open(args["le"], "rb").read()) tracked = queue.Queue(maxsize=0) datas=queue.Queue(maxsize=0) def background(): while datas.not_empty: tracked.put(send.track(datas.get())) tracked.task_done()
import queue import os import numpy as np import pickle import sys import system_logging as log from datetime import datetime import psutil import redis import imutils import send from flask_opencv_streamer.streamer import Streamer # import vlib os.system('cls' if os.name == 'nt' else 'clear') log.sys("Initializing System Core") class VCORE: def __init__(self): continue def check_core(self): try: if not os.path.isfile(self.core): self.create_model() self.recognizer_model.read(self.core) except: pass def check_cam(self):
def main(imagePath): start = datetime.datetime.now() log.sys("Loading face detector and recognizer") protoPath = os.path.join("face_detection_model", "deploy.prototxt") modelPath = os.path.join("face_detection_model", "res10_300x300_ssd_iter_140000.caffemodel") detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) embedder = cv2.dnn.readNetFromTorch("openface_nn4.small2.v1.t7") log.sys("Completed!") log.sys("Processing faces...") imagePaths = list(paths.list_images(os.path.join(dataset, imagePath))) knownEmbeddings = [] knownNames = [] total = 0 for (i, imagePath) in enumerate(imagePaths): log.log("Processing image {}/{}".format(i + 1, len(imagePaths))) name = imagePath.split(os.path.sep)[-2] image = cv2.imread(imagePath) image = imutils.resize(image, width=1920) # details = json.loads(send.get_identity(name)) # if details["status"] == "0": # continue # elif details["status"] == "1" and details["name"] != "": # name = details["name"] (h, w) = image.shape[:2] imageBlob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False) detector.setInput(imageBlob) detections = detector.forward() if len(detections) > 0: i = np.argmax(detections[0, 0, :, 2]) confidence = detections[0, 0, i, 2] if confidence > 0.5: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") face = image[startY:endY, startX:endX] (fH, fW) = face.shape[:2] if fW < 20 or fH < 20: continue faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False) embedder.setInput(faceBlob) vec = embedder.forward() knownNames.append(name) knownEmbeddings.append(vec.flatten()) total += 1 log.sys("Serializing {} encodings...".format(total)) end = datetime.datetime.now() log.sys("Training Completed!") log.sys("Time Consumed : {} Seconds".format(end - start)) if os.path.isfile(model): file = pickle.load(open(model, "rb")) embdeddings = file["embeddings"] names = file["names"] file.close() for embedding, name in zip(embdeddings, names): knownEmbeddings.append(embedding) knownNames.append(name) data = {"embeddings": knownEmbeddings, "names": knownNames} f = open(model, "wb") f.write(pickle.dumps(data)) f.close() elif not os.path.isfile(model): os.mkdir("models") data = {"embeddings": knownEmbeddings, "names": knownNames} f = open(model, "wb") f.write(pickle.dumps(data)) f.close()
import os import os import system_logging as log import datetime start = datetime.datetime.now() os.system('cls' if os.name == 'nt' else 'clear') log.sys("Extracting embeddings from all dataset images...") os.system( 'python embedding.py --embeddings output/embeddings.pickle --detector face_detection_model --embedding-model openface_nn4.small2.v1.t7' ) log.sys("Training Model...") os.system( 'python train_model.py --embeddings output/embeddings.pickle --recognizer output/recognizer.pickle --le output/le.pickle --steps=100' ) end = datetime.datetime.now() log.sys("Task Sucessfully Completed!") log.sys("Total Time Consumed : {}".format(end - start)) log.sys("Running Face Recognition...") os.system("python absen.py")
def save(image, dirs, number, formats): name = "{}.{}".format(number, formats) path_name = os.path.join(dirs, name) cv2.imwrite(path_name, image) log.sys("Image created : {}".format(path_name))
import imutils # r = redis.Redis("localhost") labels = [] faces = [] boxes = [] predictions = [] dataset = "dataset" pickles = os.path.join("model", "alpha.resist") core = os.path.join("model", "core.resist") protoPath = os.path.join("model", "deploy.prototxt") modelPath = os.path.join("model", "face_detector.caffemodel") detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) recognizer_model = cv2.face.LBPHFaceRecognizer_create() log.sys("Initializing System Core") def check_core(): try: if not os.path.isfile(core): create_model() recognizer_model.read(core) except: pass def check_cam(): online_cams = [] for i in range(psutil.cpu_count(logical=True)): vid = cv2.VideoCapture(i)