def test_faces(capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/face_no_surprise.jpg') detect.detect_faces(file_name) out, _ = capsys.readouterr() assert 'Likelihood.POSSIBLE' in out
def app(): file_input_column = [[sg.Text('Выберите изображение')], [ sg.In(key='-file_name-', enable_events=True), sg.FileBrowse('Найти', file_types=(("PNG files", "*.png"), ("JPG files", "*.jpg"), ("ALL files", "*")), target='-file_name-') ]] layout = [[sg.Column(file_input_column, justification='center')], [sg.Column([[sg.Image(key='-image-')]], justification='center')], [ sg.Button('Поиск лиц', key='-detect-'), sg.SaveAs('Сохранить', target='-save-'), sg.InputText(key='-save-', do_not_clear=False, enable_events=True, visible=False) ]] window = sg.Window('Нахождение лиц', layout, resizable=True, finalize=True) while True: event, values = window.read() if event == '-file_name-': file_name = values['-file_name-'] try: window["-image-"].update(data=load_image(file_name)) except: pass elif event == '-detect-': if window["-image-"].get_size() != (2, 2): temp_path = os.path.join(os.getcwd(), "temp/temp.png") detect_faces(values['-file_name-'], temp_path) window["-image-"].update(data=load_image(temp_path)) elif event == '-save-': if window["-image-"].get_size() != (2, 2): path = values['-save-'] if path and os.path.exists("temp/temp.png"): im = Image.open("temp/temp.png") im.save(values['-save-']) elif event == sg.WIN_CLOSED: try: os.remove("temp/temp.png") except: pass break window.close()
def is_correct(frame, previous=None): if _change_scene_threshold is not None and previous is not None: score, _ = structural_similarity(_small_img(frame), _small_img(previous), full=True, multichannel=True) if 1 - score > _change_scene_threshold: raise CheckFrameException("large difference with previous frame, probably another scene") faces = detect.detect_faces(frame, threshold=_face_detect_threshold) if len(faces) != 1: raise CheckFrameException("detected %d faces, expected 1" % len(faces))
def oreintation(tilt, pan): out = False if tilt >= 3 and tilt <= 12: if pan <= 7 and pan >= 1: out = 1 elif pan >= -7 and pan <= -1: out = 2 else: out = out elif tilt <= -3 and tilt >= 12: if pan <= 7 and pan >= 1: out = 3 elif pan >= -7 and pan <= -1: out = 4 else: out = out else: out = out return out detect_faces(path)
def recognize_face(recognizer, image, face_cascade, eye_cascade, face_size, threshold): found_faces = [] gray, faces = detect_faces(image, face_cascade, eye_cascade, return_gray=1) # If faces are found, try to recognize them for ((x, y, w, h), eyedim) in faces: label, confidence = recognizer.predict(cv2.resize(level_face(gray, ((x, y, w, h), eyedim)), face_size)) if confidence < threshold: found_faces.append((label, confidence, (x, y, w, h))) return found_faces
def _extract_faces(a_dir, folder, do_level_face=False): faceCascade = cv2.CascadeClassifier(config.FACE_CASCADE_FILE) eyeCascade = cv2.CascadeClassifier(config.EYE_CASCADE_FILE) the_path = join(a_dir, folder) result = [] for img in [f for f in os.listdir(the_path) if _supported_img(f)]: img_path = join(the_path, img) image, faces = detect_faces(cv2.imread(img_path), faceCascade, eyeCascade, True) if len(faces) == 0: print("No face found in " + img_path) for ((x, y, w, h), eyedim) in faces: if not do_level_face: result.append(image[y:y+h, x:x+w]) else: result.append(level_face(image, ((x, y, w, h), eyedim))) #result.append(image[y:y+h, x:x+w]) return result
import cv2 import matplotlib.pyplot as plt import time import config from detect import detect_faces # opencv loads an image into BGR color space by default def read_img(imgpath): return cv2.imread(imgpath) if config.test: detect_faces(read_img('data/test/test2.jpg'), 1.1, True)
from detect import detect_faces from detect import detect_faces_uri if __name__ == "__main__": file = 'image002.jpg' if len(sys.argv) > 1: for i in range(1, len(sys.argv)): file = sys.argv[i] response = detect_faces(file)