def dnn_detector(frame): gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame_height = frame.shape[0] frame_width = frame.shape[1] blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), [104, 117, 123], False, False) net.setInput(blob) detections = net.forward() bboxes = [] idx = 0 offset = 15 x_pos, y_pos = 10, 40 for i in range(detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > conf_threshold: idx += 1 x1 = int(detections[0, 0, i, 3] * frame_width) y1 = int(detections[0, 0, i, 4] * frame_height) x2 = int(detections[0, 0, i, 5] * frame_width) y2 = int(detections[0, 0, i, 6] * frame_height) bboxes.append([x1, y1, x2, y2]) face = [x1, y1, x2 - x1, y2 - y1] if hist_eq: gray_frame = cv2.equalizeHist(gray_frame) model_in = get_model_compatible_input(gray_frame, utils.bb_to_rect(face)) predicted_proba = model.predict(model_in) predicted_label = np.argmax(predicted_proba[0]) cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2) text = f"Person {idx}: {label2text[predicted_label]}" utils.draw_text_with_backgroud(frame, text, x1 + 5, y1, font_scale=0.4) text = f"Person {idx} : " y_pos = y_pos + 2 * offset utils.draw_text_with_backgroud(frame, text, x_pos, y_pos, font_scale=0.3, box_coords_2=(2, -2)) for k, v in label2text.items(): text = f"{v}: {round(predicted_proba[0][k]*100, 3)}%" y_pos = y_pos + offset utils.draw_text_with_backgroud(frame, text, x_pos, y_pos, font_scale=0.3, box_coords_2=(2, -2))
def cascade_detector(frame_orig, detector): if not detector in ["haar", "lbp"]: raise ValueError("Invalid cascade detector") face_detector = (haar_detector if detector == "haar" else lbp_detector) frame = frame_orig.copy() gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_detector.detectMultiScale(gray_frame, 1.32, 5) for face in faces: landmarks_coord = utils.get_landmarks(gray_frame, frame, utils.bb_to_rect(face), annotate=args["landmarks"]) if args["delaunay_triangulation"]: utils.annotate_delaunay_triangulation(frame, landmarks_coord, line_thickness=2) if args["region_of_interest"]: utils.annotate_ROI(frame, landmarks_coord) if args["bounding_box"]: x, y, w, h = face cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) return frame
def _raw_face_landmarks(face_image, face_locations=None): if face_locations is None: face_locations = _raw_face_locations(face_image) else: face_locations = [ utils.bb_to_rect(face_location) for face_location in face_locations ] return [ _post_predictor(face_image, face_location) for face_location in face_locations ]
def haar_detector(frame): gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) face_frame = np.zeros(gray_frame.shape, dtype="uint8") offset = 15 x_pos, y_pos = 10, 40 faces = cascade_detector.detectMultiScale(gray_frame, 1.32, 5) for idx, face in enumerate(faces): if hist_eq: gray_frame = cv2.equalizeHist(gray_frame) img_arr = utils.align_face(gray_frame, utils.bb_to_rect(face), desiredLeftEye) face_frame = cv2.resize(img_arr, (48, 48), interpolation=cv2.INTER_CUBIC) img_arr = utils.preprocess_img(img_arr, resize=False) predicted_proba = model.predict(img_arr) predicted_label = np.argmax(predicted_proba[0]) x, y, w, h = face cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) text = f"Person {idx+1}: {label2text[predicted_label]}" utils.draw_text_with_backgroud(frame, text, x + 5, y, font_scale=0.4) text = f"Person {idx+1} : " y_pos = y_pos + 2 * offset utils.draw_text_with_backgroud(frame, text, x_pos, y_pos, font_scale=0.3, box_coords_2=(2, -2)) for k, v in label2text.items(): text = f"{v}: {round(predicted_proba[0][k]*100, 3)}%" y_pos = y_pos + offset utils.draw_text_with_backgroud(frame, text, x_pos, y_pos, font_scale=0.3, box_coords_2=(2, -2)) return frame, face_frame
def dnn_detector(frame_orig): frame = frame_orig.copy() frame_height = frame.shape[0] frame_width = frame.shape[1] blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), [104, 117, 123], False, False) net.setInput(blob) detections = net.forward() bboxes = [] for i in range(detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > conf_threshold: x1 = int(detections[0, 0, i, 3] * frame_width) y1 = int(detections[0, 0, i, 4] * frame_height) x2 = int(detections[0, 0, i, 5] * frame_width) y2 = int(detections[0, 0, i, 6] * frame_height) bboxes.append([x1, y1, x2, y2]) face = [x1, y1, x2 - x1, y2 - y1] gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) landmarks_coord = utils.get_landmarks(gray_frame, frame, utils.bb_to_rect(face), annotate=args["landmarks"]) if args["delaunay_triangulation"]: utils.annotate_delaunay_triangulation(frame, landmarks_coord, line_thickness=2) if args["region_of_interest"]: utils.annotate_ROI(frame, landmarks_coord) if args["bounding_box"]: cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2) return frame
def dnn_detector(frame): frame_height = frame.shape[0] frame_width = frame.shape[1] blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), [104, 117, 123], False, False) net.setInput(blob) detections = net.forward() bboxes = [] idx = 0 offset = 15 x_pos, y_pos = 10, 40 gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) face_frame = np.zeros(gray_frame.shape, dtype="uint8") for i in range(detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > conf_threshold: idx += 1 x1 = int(detections[0, 0, i, 3] * frame_width) y1 = int(detections[0, 0, i, 4] * frame_height) x2 = int(detections[0, 0, i, 5] * frame_width) y2 = int(detections[0, 0, i, 6] * frame_height) bboxes.append([x1, y1, x2, y2]) face = [x1, y1, x2 - x1, y2 - y1] if hist_eq: gray_frame = cv2.equalizeHist(gray_frame) img_arr = utils.align_face(gray_frame, utils.bb_to_rect(face), desiredLeftEye) face_frame = cv2.resize(img_arr, (48, 48), interpolation=cv2.INTER_CUBIC) img_arr = utils.preprocess_img(img_arr, resize=False) predicted_proba = model.predict(img_arr) predicted_label = np.argmax(predicted_proba[0]) cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2) text = f"Person {idx}: {label2text[predicted_label]}" utils.draw_text_with_backgroud(frame, text, x1 + 5, y1, font_scale=0.4) text = f"Person {idx} : " y_pos = y_pos + 2 * offset utils.draw_text_with_backgroud(frame, text, x_pos, y_pos, font_scale=0.3, box_coords_2=(2, -2)) for k, v in label2text.items(): text = f"{v}: {round(predicted_proba[0][k]*100, 3)}%" y_pos = y_pos + offset utils.draw_text_with_backgroud(frame, text, x_pos, y_pos, font_scale=0.3, box_coords_2=(2, -2)) return frame, face_frame
utils.imshow_scaled("Images", imref) console.print(Rule()) console.print( "Press ENTER to validate, else press any other key to start again.", style="yellow bold blink") key = cv.waitKey(0) if key == 13: # if ENTER is pressed, exit loop break # Initial length and clamp ROI center l0 = utils.compute_length(ROI1, ROI2) center0 = utils.ROIcenter(ROIcorner) # Init trackers tracker1 = dlib.correlation_tracker() tracker1.start_track(imrefbw, utils.bb_to_rect(ROI1)) tracker2 = dlib.correlation_tracker() tracker2.start_track(imrefbw, utils.bb_to_rect(ROI2)) tracker3 = dlib.correlation_tracker() tracker3.start_track(imrefbw, utils.bb_to_rect(ROIcorner)) # Init strain and displacement plot plt.ion() fig, ax1 = plt.subplots() frames = [] strain = [] disp = [] line1, = ax1.plot(frames, strain, "ro") ax1.set_xlabel("Image") ax1.set_ylabel("Strain [%]", color="red") ax1.tick_params(axis="y", labelcolor="red")