Commit b01125a6 authored by 赵威's avatar 赵威

try get image info

parent 8ca40746
import time
from utils.images import (BASE_DIR, MODELS_DIR, face_to_vec, file_to_ndarray, url_to_ndarray)
def main():
img_url = "https://pic.igengmei.com/2020/07/04/1802/1fd23e101178-w"
img = url_to_ndarray(img_url)
print(img)
faces = face_to_vec(img)
print(faces)
print("============")
for face in faces:
print(face["feature"] + "\n")
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))
from elasticsearch import Elasticsearch as Es
def es_index_adapt(index_prefix, doc_type, rw=None):
"""get the adapted index name
"""
assert rw in [None, "read", "write"]
index = "-".join((index_prefix, doc_type))
if rw:
index = "-".join((index, rw))
return index
def get_es():
init_args = {
"sniff_on_start": False,
"sniff_on_connection_fail": False,
}
new_hosts = [{
"host": "172.16.31.17",
"port": 9000,
}, {
"host": "172.16.31.11",
"port": 9000,
}, {
"host": "172.16.31.13",
"port": 9000,
}]
new_es = Es(hosts=new_hosts, **init_args)
return new_es
def es_query(doc, body, offset=0, size=100, es=None, rw="read"):
if es is None:
es = get_es()
index = es_index_adapt(index_prefix="gm-dbmw", doc_type=doc, rw=rw)
res = es.search(index=index, timeout="10s", body=body, from_=offset, size=size)
return res
import io
import json
import os
import time
import traceback
import cv2
import dlib
import requests
from numpy import np
from PIL import Image
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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")
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)
if result.ok:
img = Image.open(io.BytesIO(result.content))
img = img.convert("RGB")
data = np.array(img)
return data
else:
print("http get: {}".format(result.status_code))
return None
def file_to_ndarray(path):
result = cv2.imread(path)
img = Image.fromarray(result).convert("RGB")
data = np.array(img)
return data
def face_to_vec(img, max_size=700):
start = time.time()
orig_img = img
print("detect image...")
scale = 1
height, width = img.shape[:2]
if max(height, width) > max_size:
scale = max_size / float(max(height, width))
size = (int(width * scale), int(height * scale))
img = cv2.resize(img, size, interpolation=cv2.INTER_AREA)
try:
dets = face_detector(img, 1)
except Exception as e:
print(e)
traceback.print_exc()
return []
print("Number of faces detected: {}".format(len(dets)))
faces = []
for i, d in enumerate(dets):
face = {}
shape = shape_predictor(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["feature"] = json.dumps(np.array(face_descriptor).tolist())
faces.append(face)
# del face_descriptor
except Exception as e:
print(e)
traceback.print_exc()
# del shape
print("Compute face cost: {}".format(time.time() - start))
# del dets
return faces
def check_rect(rect, shape, scale=1, landmark=[]):
w, h = shape[1], shape[0]
# 面部区域最大化
top = rect.top() / scale
right = rect.right() / scale
bottom = rect.bottom() / scale
left = rect.left() / scale
# print
top = max(min([top] + [p[1] for p in landmark]), 0)
right = min(max([right] + [p[0] for p in landmark]), w)
bottom = min(max([bottom] + [p[1] for p in landmark]), h)
left = max(min([left] + [p[0] for p in landmark]), 0)
# 检测框转换成正方形
squareY = bottom - top
squareX = right - left
diff = abs(squareY - squareX) / 2
if squareX > squareY:
bottom = min(bottom + diff, h)
top = max(top - diff, 0)
elif squareX < squareY:
right = min(right + diff, w)
left = max(left - diff, 0)
# 检测框放大
rect_scale = min((h - bottom) / squareY, top / squareY, (w - right) / squareX, left / squareX, 0.1)
top -= squareY * rect_scale
bottom += squareY * rect_scale
left -= squareX * rect_scale
right += squareX * rect_scale
return int(top), int(right), int(bottom), int(left)
def extract_landmark(shape, scale=1):
"""特征点提取"""
ret = []
for i in range(68):
point = shape.part(i)
ret.append((int(point.x / scale), int(point.y / scale)))
return ret
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