def detect(self): model = BlazeFace() model.load_weights(self.model_weights) model.load_anchors(self.model_anchors) # img = cv2.cvtColor(cv2.imread(self.img_path), cv2.COLOR_BGR2RGB) # img_res = cv2.resize(img, (self.img_size, self.img_size)) img_res = cv2.resize(self.img_arr, (self.img_size, self.img_size)) results = model.predict_on_image(img_res) self.detections = results return results
net.load_anchors("anchors.npy") # let's start the capture now print("starting camera now....") #adjust based on your device. For most cases, normally 0 cap = cv2.VideoCapture(0) while (True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (128, 128)) detections = net.predict_on_image(img) if isinstance(detections, torch.Tensor): detections = detections.cpu().numpy() if detections.ndim == 1: detections = np.expand_dims(detections, axis=0) print("Found %d faces" % detections.shape[0]) if detections.shape[0] > 0: for i in range(detections.shape[0]): # adjust the coordinates to the original image ymin = detections[i, 0] * frame.shape[0] xmin = detections[i, 1] * frame.shape[1] ymax = detections[i, 2] * frame.shape[0] xmax = detections[i, 3] * frame.shape[1] cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (255, 0, 0), 2)
xscale, yscale = 192 / face_img.shape[1], 192 / face_img.shape[0] for i in range(detections.shape[0]): x, y = int(detections[i, 0] / xscale), int(detections[i, 1] / yscale) image = cv2.circle(image, (xmin + x, ymin + y), 1, (255, 0, 0), 1) videoCapture = cv2.VideoCapture(0) while True: ret, image = videoCapture.read() if not ret: break # preprocess image face_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) face_img = cv2.resize(face_img, (128, 128)) # get face detection boxes detections = blaze_net.predict_on_image(face_img).numpy() xmin, ymin, face_img = get_crop_face(detections, image) # get face mesh mesh_img = cv2.resize(face_img, (192, 192)) detections = mesh_net.predict_on_image(mesh_img).numpy() get_mesh_face(detections, face_img, image, xmin, ymin) # show processed image cv2.imshow("capture", image) if cv2.waitKey(3) & 0xFF == 27: break
hasFrame, frame = capture.read() frame_ct = 0 else: hasFrame = False while hasFrame: frame_ct += 1 if mirror_img: frame = np.ascontiguousarray(frame[:, ::-1, ::-1]) else: frame = np.ascontiguousarray(frame[:, :, ::-1]) # BRG to RGB img1, img2, scale, pad = resize_pad(frame) normalized_face_detections = face_detector.predict_on_image(img1) face_detections = denormalize_detections(normalized_face_detections, scale, pad) xc, yc, scale, theta = face_detector.detection2roi(face_detections.cpu()) img, affine, box = face_regressor.extract_roi(frame, xc, yc, theta, scale) flags, normalized_landmarks = face_regressor(img.to(gpu)) landmarks = face_regressor.denormalize_landmarks( normalized_landmarks.cpu(), affine) #frame = cv2.rectangle(frame, (0,0), (400,400), (120, 120, 120), 400) for i in range(len(flags)): landmark, flag = landmarks[i], flags[i] if flag > .5:
cap = cv2.VideoCapture(0) net = BlazeFace().to(device) net.load_weights("blazeface.pth") net.load_anchors("anchors.npy") # Optionally change the thresholds: net.min_score_thresh = 0.75 net.min_suppression_threshold = 0.3 while True: ret, frame = cap.read() frame = cv2.resize(frame, (128, 128)).astype(np.float32) print(frame.shape) detections = net.predict_on_image(frame) img = plot_detections(frame, detections) print(detections.shape) # #detections = detections.squeeze() # top_left = tuple((detections[0][0], detections[0][1])) # bottom_right = tuple((detections[0][1], detections[0][3])) # frame = frame/255.0 # cv2.rectangle(frame, top_left, bottom_right, (0, 0, 255)) # cv2.imshow('frame', frame) cv2.namedWindow("output", cv2.WINDOW_NORMAL) cv2.resizeWindow("output", 600, 600) cv2.imshow('output', img) cv2.waitKey(1)