def __init__(self): self.recognizer = SVC(C=10.0, kernel="rbf", gamma='auto', probability=True) self.le = LabelEncoder() #self.knownEmbeddings = [] #self.knownName = [] self.face_detect = face_detect()
def test_success_rate_higher_than_30_given_all_images(self, images): success_count = 0 image_list = images.get_all() for img in image_list: if face_detect(img.path) == img.face_count: success_count += 1 success_percentage = success_count / len(image_list) * 100 print(f'Actual success rate: {success_percentage}%') assert success_percentage > 30
def test_success_rate_higher_than_40_given_multiple_medium_images( self, images): success_count = 0 image_list = images.get_all(min_complexity=2, max_complexity=5) for img in image_list: if face_detect(img.path) == img.face_count: success_count += 1 success_percentage = success_count / len(image_list) * 100 print(f'Actual success rate: {success_percentage}%') assert success_percentage > 40
def handle_image_message(event): push_img_id = event.message.id # 投稿された画像IDを取得 message_content = line_bot_api.get_message_content( push_img_id) # LINEサーバー上に自動保存された画像を取得 push_img = b"" for chunk in message_content.iter_content(): push_img += chunk #画像をiter_contentでpush_imgに順次代入 push_img = base64.b64encode(push_img) # APIに通すためbase64エンコード msg = f.face_detect(push_img) line_bot_api.reply_message(event.reply_token, TextSendMessage(text=msg))
def predict(test_img): img = cv2.imread(test_img).copy() print "\n\n\n" print "Face Prediction Running -\-" face, rect, length = face_detect.face_detect(test_img) print len(face), "faces detected." for i in range(0, len(face)): labeltemp, confidence = face_recognizer.predict(face[i]) label.append(labeltemp) return img, label
def get_dataset(filestore=FILESTORE): result = [] folders = os.listdir(filestore) # print(folders) for folder in folders: image_items = [] for r, d, f in os.walk(os.path.join(filestore, folder)): for file in f: if ".jpg" in file: path = os.path.join(r, file) img = cv2.imread(path) faces = face_detect(img) if len(faces) > 0: image_items.append({"name": file, "path": path}) result.append({"name": folder, "images": image_items}) return result
def main(): # Get title title = sys.argv[2].split('\\')[-1].split('.')[0] face_data = fd.face_detect(sys.argv[1], sys.argv[2], title) image = cv2.imread(sys.argv[2]) image_after = image.copy() color_bgr = tuple(sys.argv[3:7]) for item in face_data: face_shape = item[2] upper_lips, lower_lips = get_points_lips(face_shape) mask = contours_lips(image.copy(), upper_lips, lower_lips) image_after = apply_lips(mask, image_after, color_bgr) # Save pic cv2.imwrite("{}_executed.jpg".format(title), image_after)
def prepare_training_data(data_folder_path): dirs = os.listdir(data_folder_path) faces = [] labels = [] for dir_name in dirs: if not dir_name.startswith("s"): continue label = int(dir_name.replace("s", "")) subject_dir_path = data_folder_path + "/" + dir_name subject_images_names = os.listdir(subject_dir_path) for image_name in subject_images_names: if image_name.startswith("."): continue image_path = subject_dir_path + "/" + image_name face, rect, length = face_detect.face_detect(image_path) if face is not None: faces.append(face[0]) labels.append(label) return faces, labels
def get_face_roi_task(self): roi_rate = rospy.Rate(1) while True: try: flag, image = self.liveview.read() self.roi = face_detect(image) self.detMsg.bbox.size_x = self.roi['left'] self.detMsg.bbox.size_y = self.roi['top'] self.detMsg.bbox.center.x = self.roi[ 'top'] + self.roi['height'] / 2 self.detMsg.bbox.center.y = self.roi[ 'left'] + self.roi['width'] / 2 self.detMsg.bbox.center.theta = 0.0 self.detMsg.source_img.width = self.roi['width'] self.detMsg.source_img.height = self.roi['height'] self.face_detect_pub.publish(self.detMsg) print("roi", self.roi['width']) except TypeError as e: flag, image = self.liveview.read() #print(e) roi_rate.sleep()
if 'no_sponsored' in sys.argv[1]: img_num = pickle.load(open(f'img_num_no_sponsored.p', 'rb')) else: img_num = pickle.load(open(f'img_num_sponsored.p', 'rb')) for i in range(int(sys.argv[2]), int(sys.argv[3])): dir_path = f'{in_path}{i}/' img_feature = {} img_feature['img_num'] = img_num[i] img_feature['face'] = [] img_feature['sharpness'] = [] if os.path.isdir(dir_path): for img_path in os.listdir(dir_path): try: img_path = dir_path + img_path img = cv2.imread(img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #im = Image.open(img_path).convert('L') array = np.asarray(img, dtype=np.int32) dx = np.diff(array)[1:,:] # remove the first row dy = np.diff(array, axis=0)[:,1:] # remove the first column dnorm = np.sqrt(dx**2 + dy**2) sharpness = float(np.average(dnorm)) img_feature['sharpness'].append(sharpness) face = face_detect(img) img_feature['face'].append(face) except Exception as e: print(e) pass pickle.dump(img_feature, open(f'{out_path}{i}.p', 'wb'))
def test_will_throw_any_exception_given_invalid_image_path(self): with pytest.raises(Exception) as e_info: face_detect('invalid/path/to/image.jpg')
# filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if confidence > args["confidence"]: # extract the index of the class label from the # `detections`, then compute the (x, y)-coordinates of # the bounding box for the object idx = int(detections[0, 0, i, 1]) box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # print('startX, startY, endX, endY', startX, startY, endX, endY) class_name = CLASSES[idx] if class_name in NEED_CLASSES: if class_name == 'person': face_detect.face_detect(frame[startY:endY, startX:endX]) # draw the prediction on the frame label = "{}: {:.2f}%".format(class_name, confidence * 100) cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2) # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break
def save_image(image, name): file_path = get_path_file(name) cv2.imwrite(file_path["path"], image) # time.sleep(1) # writeImageToGDrive(name, file_path["path"], getFolderfromGDrive(file_path["name"])) print("Save done", file_path["path"]) print("1. Collect data") print("2. Upload dataset to GDrive") you_choice = int(input("Select 1 or 2: ")) if you_choice == 1: while True: img = picam.get_image() # img = kinect.get_image() faces = face_detect(img) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), BORDER_WIDTH) # cv2.imshow("image", img) # Save the captured image into the datasets folder print("face detected ..., waitting for save") save_image( image=img[y + BORDER_WIDTH:y + h - BORDER_WIDTH, x + BORDER_WIDTH:x + w - BORDER_WIDTH, ], name="face." + str(current_milli_time()) + ".jpg", ) # cv2.imshow("image", img) k = cv2.waitKey(1) & 0xFF # Press 'ESC' for exiting video if k == 27:
def test_will_throw_correct_exception_given_invalid_image_path(self): with pytest.raises(FileNotFoundError) as e_info: face_detect('invalid/path/to/image.jpg')
def main(args): path_fd = args.face_path fd = face_detect('face detection', path_fd, args.device) fd.load_model() path_ld = args.landmark_path ld = landmark('landmark', path_ld, args.device) ld.load_model() path_hdps = args.headpose_path hp = head_pose('head pose', path_hdps, args.device) hp.load_model() gaze_path = args.gaze_path gz = gaze('Gaze', gaze_path, args.device) gz.load_model() if args.input_type == 'video': cap = cv2.VideoCapture('demo.mp4') elif args.input_type == 'cam': cap = cv2.VideoCapture(0) video_writer = cv2.VideoWriter('output1.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 10, (1920, 1080)) if not cap.isOpened(): logging.error('Video file not found. Check the path') while (cap.isOpened()): ret, frame = cap.read() if ret == True: #img = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) boxes, pre_img, crp_img = fd.predict(frame) keypoint_image, right_eye, left_eye, x_e, y_e = ld.predict(crp_img) hp_vector = hp.predict(crp_img) hp_vector = np.reshape(hp_vector, (1, 3)) mouse_points = gz.predict(left_eye, right_eye, hp_vector) # rotation vector rvec = np.array([0, 0, 0], np.float) # translation vector tvec = np.array([0, 0, 0], np.float) # camera matrix camera_matrix = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], np.float) result, _ = cv2.projectPoints(mouse_points, rvec, tvec, camera_matrix, None) result = result[0][0] res = (int(result[0] * 100), int(result[1] * 100)) e1 = (boxes[0][0] + x_e[0], boxes[0][1] + y_e[0]) e2 = (boxes[0][0] + x_e[1], boxes[0][1] + y_e[1]) cv2.arrowedLine(pre_img, e1, (e1[0] - res[0], e1[1] + res[1]), (0, 255, 0), 2) cv2.arrowedLine(pre_img, e2, (e2[0] - res[0], e2[1] + res[1]), (0, 255, 0), 2) #move_mouse = MouseController('medium','medium') #move_mouse.move((e1[0] - res[0], e1[1] + res[1])) if (args.inter_viz): cv2.imshow('frame', pre_img) video_writer.write(frame) cv2.waitKey(1) else: break cap.release() cv2.destroyAllWindows()
fig = plt.figure() for i, image in enumerate(images): fig.add_subplot(221 + i) plt.imshow(image) plt.show() shelf_plane_normal, products = read_calibration(args.calibration_filepath) gazenet = Gazenet(GAZENET_PATH) num_face_not_detected = 0 for i, (color, depth) in enumerate( read_bag(args.filepath, color_config=COLOR_CONFIG, depth_config=DEPTH_CONFIG) ): face = face_detect(color) if face is None: num_face_not_detected += 1 continue color_crop, depth_crop = crop(face, color, depth) if args.debug: show_images([color, color_crop, depth, depth_crop]) angles = gazenet.image_to_euler_angles( color, (face[0], face[1], face[0] + face[2], face[1] + face[3])) depth = depth_crop[(int(depth_crop.shape[0]/2), int(depth_crop.shape[1]/2))] face_origin_2d = (np.mean([face[0], face[2]]), np.mean([face[1], face[3]])) gaze_origin = np.array([ face_origin_2d[0],
import cv2 import numpy as np import os import face_detect as fr cam = cv2.VideoCapture(0) while True: ret, img = cam.read() faces_detected, gray_img = fr.face_detect(img) for (x, y, w, h) in faces_detected: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), thickness=2) cv2.imshow("Face", img) if (cv2.waitKey(1) == ord('q')): break cam.release() cv2.destroyAllWindows
def test_will_recognize_0_faces_given_specific_image(self, images): img = images.get(name='0_1_xp_background.jpg') assert face_detect(img.path) == 0
def handle_detections(detections, confidence=args["confidence"], x_offset=0, y_offset=0): global frame, W, H # loop over the detections for i in np.arange(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with # the prediction conf = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is if conf > confidence: # extract the index of the class label from the # `detections`, then compute the (x, y)-coordinates of # the bounding box for the object idx = int(detections[0, 0, i, 1]) box = detections[0, 0, i, 3:7] * np.array([W, H, W, H]) startX, startY, endX, endY = box.astype("int") startX = 0 if startX < 0 else startX startY = 0 if startY < 0 else startY class_name = CLASSES[idx] if class_name in NEED_CLASSES: # 继续检测人脸 if class_name == 'person': face_detect.face_detect(frame[startY:endY, startX:endX]) elif class_name == 'car': brand_region = car_brand_detect(frame[startY:endY, startX:endX]) # 用绿线画出车牌轮廓 for b_box in brand_region: x, y, w, h = b_box cv2.rectangle( frame, (x + startX + x_offset, y + startY + y_offset), (x + startX + x_offset + w, y + startY + y_offset + h)) # draw the prediction on the frame label = "{}: {:.2f}%".format(class_name, conf * 100) # 为每个目标创建跟踪器 tracker_box = (startX + x_offset, startY + y_offset, endX + x_offset - startX, endY + y_offset - startY) ignore_area.append(tracker_box) print('Create Tracker:', tracker_box) tracker = Tracker(frame, tracker_box, class_name, label) # tracker 状态管理 tracker_status[tracker.id] = tracker cv2.rectangle(frame, (startX + x_offset, startY + y_offset), (endX + x_offset, endY + y_offset), COLORS[idx], 2) ########################################### # # 标注文字 # y = startY + y_offset - 15 if startY + y_offset - 15 > 15 else startY + y_offset + 15 # cv2.putText(frame, label, (startX, y), # cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2) # # show the output frame # cv2.imshow("Frame", frame) # update the FPS counter fps.update()
def test_will_throw_correct_exception_given_invalid_type(self): with pytest.raises(ValueError) as e_info: face_detect(123)
# thread_mouse.start() print('[INFO] Press E for mouse enable') # Control of mouse is disabled at the beginning mouse_enabled = False while True: # Capturing images ret, frame = cam.read() # Mirror webcam frame = cv2.flip(frame, 1) blink_prev[1] = blink_prev[0] blink_prev[0] = blink # proces frame center_face_position, reference_point, blink = face_detect( frame, reference_point) # mouse click if mouse_enabled and (blink or blink_prev[0]): if t_blink == 0: t_blink = time.time() if (time.time() - t_blink) > 0.3: if not (left_b): print("[INFO] Left mouse button PRESSED") pyautogui.mouseDown() left_b = True if mouse_enabled and not (blink or blink_prev[0] or blink_prev[1]): t_blink = 0 if left_b: print("[INFO] Left mouse button RELEASED") pyautogui.mouseUp()
import numpy as np import imutils import pickle import cv2 import os import time import threading from PIL import ImageGrab from face_detect import face_detect from recognizer import recognizer from train_model import train_model recognizer = recognizer() face_detect = face_detect() train_model = train_model() train_model.train() print("Start") name = "" nameOutputFlag = 0 learningFlag = 0 unknownFlag = 0 nameinput = "" UnknownImage = "" def Thread_recognize(): global name global nameOutputFlag global UnknownImage global unknownFlag video_capture = cv2.VideoCapture(0)
from face_detect import face_detect model = face_detect().model() print(model)
def test_will_recognize_4_faces_given_specific_image(self, images): img = images.get(name='4_2_grayscale_abba.png') assert face_detect(img.path) == 4