Commit 4a9d8f16 authored by 赵威's avatar 赵威

get image info

parent ebf9d350
import os
import time
from utils.images import (BASE_DIR, MODELS_DIR, face_to_vec, file_to_ndarray, url_to_ndarray)
import dlib
from utils.images import face_to_vec, file_to_ndarray, url_to_ndarray
def main():
......@@ -8,21 +11,30 @@ def main():
img = url_to_ndarray(img_url)
print(img)
faces = face_to_vec(img)
base_dir = os.getcwd()
print("base_dir: " + base_dir)
model_diry = os.path.join(base_dir, "_models")
facerec_model_path = os.path.join(model_diry, "dlib_face_recognition_resnet_model_v1.dat")
shape_model_path = os.path.join(model_diry, "shape_predictor_68_face_landmarks.dat")
face_rec = dlib.face_recognition_model_v1(facerec_model_path)
face_detector = dlib.get_frontal_face_detector()
shape_predictor = dlib.shape_predictor(shape_model_path)
faces = face_to_vec(img, face_rec, face_detector, shape_predictor)
print(faces)
print("============")
for face in faces:
print(face["feature"] + "\n")
print(face)
print("\n")
print("-------------")
if __name__ == "__main__":
begin_time = time.time()
print("base_dir: " + BASE_DIR)
print("models_dir: " + MODELS_DIR)
main()
print("total cost: " + str(time.time() - begin_time))
import io
import json
import os
import time
import traceback
import cv2
import dlib
import numpy as np
import requests
from PIL import Image
BASE_DIR = os.getcwd()
MODELS_DIR = os.path.join(BASE_DIR, "_models")
FACEREC_PATH = os.path.join(MODELS_DIR, "dlib_face_recognition_resnet_model_v1.dat")
SHAPE_PATH = os.path.join(MODELS_DIR, "shape_predictor_68_face_landmarks.dat")
dlib.DLIB_USE_CUDA = False
face_rec = dlib.face_recognition_model_v1(FACEREC_PATH)
face_detector = dlib.get_frontal_face_detector()
shape_predictor = dlib.shape_predictor(SHAPE_PATH)
def url_to_ndarray(url):
result = requests.get(url, timeout=30)
......@@ -41,7 +28,7 @@ def file_to_ndarray(path):
return data
def face_to_vec(img, max_size=700):
def face_to_vec(img, face_rec_f, face_detector_f, shape_predictor_f, max_size=700):
start = time.time()
orig_img = img
print("detect image...")
......@@ -54,7 +41,7 @@ def face_to_vec(img, max_size=700):
img = cv2.resize(img, size, interpolation=cv2.INTER_AREA)
try:
dets = face_detector(img, 1)
dets = face_detector_f(img, 1)
except Exception as e:
print(e)
traceback.print_exc()
......@@ -67,14 +54,14 @@ def face_to_vec(img, max_size=700):
for i, d in enumerate(dets):
face = {}
shape = shape_predictor(img, d)
shape = shape_predictor_f(img, d)
rect = shape.rect
landmark = extract_landmark(shape, scale=scale)
face["rect"] = check_rect(rect, orig_img.shape, scale=scale, landmark=landmark)
face["landmark"] = json.dumps(landmark)
try:
face_descriptor = face_rec.compute_face_descriptor(img, shape)
face_descriptor = face_rec_f.compute_face_descriptor(img, shape)
face["feature"] = json.dumps(np.array(face_descriptor).tolist())
faces.append(face)
# del face_descriptor
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment