def get_central_face_attributes(full_path): img = cv.imread(full_path, cv.IMREAD_COLOR) bboxes, landmarks = detect_faces(img) if len(landmarks) > 0: i = select_significant_face(bboxes) return [bboxes[i]], [landmarks[i]] return None, None
def get_face_attributes(full_path): try: img = Image.open(full_path).convert('RGB') bounding_boxes, landmarks = detect_faces(img) if len(landmarks) > 0: landmarks = [int(round(x)) for x in landmarks[0]] return True, landmarks except KeyboardInterrupt: raise except: pass return False, None
def get_face_attributes(full_path): try: img = cv.imread(full_path, cv.IMREAD_COLOR) bounding_boxes, landmarks = detect_faces(img) if len(landmarks) > 0: landmarks = [int(round(x)) for x in landmarks[0]] return True, landmarks except KeyboardInterrupt: raise except Exception as err: print(err) return False, None
def get_central_face_attributes(full_path): try: img = Image.open(full_path).convert('RGB') bounding_boxes, landmarks = detect_faces(img) if len(landmarks) > 0: i = select_significant_face(bounding_boxes) return True, [bounding_boxes[i]], [landmarks[i]] except KeyboardInterrupt: raise except ValueError: pass except IOError: pass return False, None, None
def get_central_face_attributes(full_path): from retinaface.detector import detect_faces from utils import select_central_face try: img = Image.open(full_path).convert('RGB') bounding_boxes, landmarks = detect_faces(img) if len(landmarks) > 0: i = select_central_face(img.size, bounding_boxes) return True, [bounding_boxes[i]], [landmarks[i]] except KeyboardInterrupt: raise except ValueError: pass except IOError: pass return False, None, None
def detect_face(data): from retinaface.detector import detect_faces from utils import select_significant_face, align_face src_path = data['src_path'] dst_path = data['dst_path'] img_raw = cv.imread(src_path) if img_raw is not None: img = resize(img_raw) bboxes, landmarks = detect_faces(img, top_k=5, keep_top_k=5) if len(bboxes) > 0: i = select_significant_face(bboxes) bbox, landms = bboxes[i], landmarks[i] img = align_face(img, [landms]) dirname = os.path.dirname(dst_path) os.makedirs(dirname, exist_ok=True) cv.imwrite(dst_path, img) return True
def download(tokens, idx, num): url = tokens[0].replace('"', '').strip() left = float(tokens[1].strip()) right = float(tokens[2].strip()) top = float(tokens[3].strip()) bottom = float(tokens[4].strip()) filename = url[url.rfind("/") + 1:].strip() fullname = os.path.join(download_folder, filename) # if not os.path.isfile(fullname): # process = Popen(["wget", '-N', url, "-P", download_folder], stdout=PIPE) # (output, err) = process.communicate() # exit_code = process.wait() filename = '{}_{}.jpg'.format(idx, num) filename = os.path.join(image_folder, filename) # print(filename) if os.path.isfile(filename) and os.path.getsize(filename) > 1000: img = cv.imread(filename) if img is not None: h, w = img.shape[:2] if h == 224 and w == 224: return filename if os.path.isfile(fullname) and os.path.getsize(fullname) > 1000: img = cv.imread(fullname) if img is not None: height, width = img.shape[:2] left, right = int(round(left * width)), int(round(right * width)) top, bottom = int(round(top * height)), int(round(bottom * height)) img = img[top:bottom, left:right, :] _, landmarks = detect_faces(img, top_k=1, keep_top_k=1) if len(landmarks) < 1: return None img = align_face(img, landmarks) cv.imwrite(filename, img) return filename return None
import time import cv2 as cv from retinaface.detector import detect_faces if __name__ == '__main__': # testing begin image_path = "images/test.jpg" img_raw = cv.imread(image_path, cv.IMREAD_COLOR) start = time.time() bboxes, landmarks = detect_faces(img_raw) end = time.time() elapsed = end - start print('avg time: {:5f} ms'.format(elapsed * 1000)) num_faces = bboxes.shape[0] # show image for i in range(num_faces): b = bboxes[i] text = "{:.4f}".format(b[4]) b = list(map(int, b)) cv.rectangle(img_raw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 2) cx = b[0] cy = b[1] + 12 cv.putText(img_raw, text, (cx, cy), cv.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))
cv.putText(img_raw, text, (cx, cy), cv.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255)) # landms landms = landmarks[i] cv.circle(img_raw, (landms[0], landms[5]), 1, (0, 0, 255), 4) cv.circle(img_raw, (landms[1], landms[6]), 1, (0, 255, 255), 4) cv.circle(img_raw, (landms[2], landms[7]), 1, (255, 0, 255), 4) cv.circle(img_raw, (landms[3], landms[8]), 1, (0, 255, 0), 4) cv.circle(img_raw, (landms[4], landms[9]), 1, (255, 0, 0), 4) # save image cv.imwrite('images/result.jpg', img_raw) cv.imshow('image', img_raw) cv.waitKey(0) if __name__ == "__main__": full_path = 'test/Jason Behr_27968.JPG' img = Image.open(full_path).convert('RGB') bboxes, landmarks = mtcnn.detect_faces(img) print(bboxes) print(landmarks) show_bboxes(full_path, bboxes, landmarks) bboxes, landmarks = retinaface.detect_faces(img) print(bboxes) print(landmarks) show_bboxes(full_path, bboxes, landmarks)
def get_all_face_attributes(full_path): from retinaface.detector import detect_faces img = cv.imread(full_path, cv.IMREAD_COLOR) bounding_boxes, landmarks = detect_faces(img) return bounding_boxes, landmarks
def get_all_face_attributes(full_path): img = Image.open(full_path).convert('RGB') bounding_boxes, landmarks = detect_faces(img) return bounding_boxes, landmarks
cv.circle(img_raw, (landms[4], landms[9]), 1, (255, 0, 0), 4) if __name__ == '__main__': folder = 'data/Aaron Eckhart' files = [f for f in os.listdir(folder) if f.endswith('.jpg')] np.random.shuffle(files) for file in files: filename = os.path.join(folder, file) # print(filename) img = cv.imread(filename) if img is not None: img_raw = resize(img) bboxes, landmarks = detect_faces(img_raw, top_k=5, keep_top_k=5) i = select_significant_face(img.shape[:2], bboxes) bbox, landms = bboxes[i], landmarks[i] img = img_raw.copy() draw_bbox(img, bbox, landms) cv.namedWindow(file) # Create a named window cv.moveWindow(file, 150, 30) cv.imshow(file, img) img = align_face(img_raw, [landms]) cv.imshow('aligned', img) cv.waitKey(0) print('Completed!')