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

get image info

parent ebf9d350
faiss-cpu==1.6.3 faiss-cpu==1.6.3
pandas==1.1.2 pandas==1.1.1
elasticsearch==7.9.1 elasticsearch==7.9.1
opencv-python==4.4.0.42 opencv-python==4.4.0.42
pillow==7.2.0 pillow==7.2.0
......
import os
import time 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(): def main():
...@@ -8,21 +11,30 @@ def main(): ...@@ -8,21 +11,30 @@ def main():
img = url_to_ndarray(img_url) img = url_to_ndarray(img_url)
print(img) 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(faces)
print("============") print("============")
for face in faces: for face in faces:
print(face["feature"] + "\n") print(face)
print("\n")
print("-------------")
if __name__ == "__main__": if __name__ == "__main__":
begin_time = time.time() begin_time = time.time()
print("base_dir: " + BASE_DIR)
print("models_dir: " + MODELS_DIR)
main() main()
print("total cost: " + str(time.time() - begin_time)) print("total cost: " + str(time.time() - begin_time))
import io import io
import json import json
import os
import time import time
import traceback import traceback
import cv2 import cv2
import dlib
import numpy as np import numpy as np
import requests import requests
from PIL import Image 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): def url_to_ndarray(url):
result = requests.get(url, timeout=30) result = requests.get(url, timeout=30)
...@@ -41,7 +28,7 @@ def file_to_ndarray(path): ...@@ -41,7 +28,7 @@ def file_to_ndarray(path):
return data 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() start = time.time()
orig_img = img orig_img = img
print("detect image...") print("detect image...")
...@@ -54,7 +41,7 @@ def face_to_vec(img, max_size=700): ...@@ -54,7 +41,7 @@ def face_to_vec(img, max_size=700):
img = cv2.resize(img, size, interpolation=cv2.INTER_AREA) img = cv2.resize(img, size, interpolation=cv2.INTER_AREA)
try: try:
dets = face_detector(img, 1) dets = face_detector_f(img, 1)
except Exception as e: except Exception as e:
print(e) print(e)
traceback.print_exc() traceback.print_exc()
...@@ -67,14 +54,14 @@ def face_to_vec(img, max_size=700): ...@@ -67,14 +54,14 @@ def face_to_vec(img, max_size=700):
for i, d in enumerate(dets): for i, d in enumerate(dets):
face = {} face = {}
shape = shape_predictor(img, d) shape = shape_predictor_f(img, d)
rect = shape.rect rect = shape.rect
landmark = extract_landmark(shape, scale=scale) landmark = extract_landmark(shape, scale=scale)
face["rect"] = check_rect(rect, orig_img.shape, scale=scale, landmark=landmark) face["rect"] = check_rect(rect, orig_img.shape, scale=scale, landmark=landmark)
face["landmark"] = json.dumps(landmark) face["landmark"] = json.dumps(landmark)
try: 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()) face["feature"] = json.dumps(np.array(face_descriptor).tolist())
faces.append(face) faces.append(face)
# del face_descriptor # 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