HivisionIDPhoto功能挖掘

HivisionIDPhotos/ ├── app/ │ ├── __init__.py │ ├── app.py │ └── images/ │ └── test.jpg ├── demo_locales.py ├── demo_config.py ├── hivision/ │ ├── __init__.py │ ├── creator/ │ │ ├── __init__.py │ │ ├── choose_handler.py │ │ ├── photo_adjuster.py │ │ └── utils.py │ └── inference.py ├── test_create_id_photo.py └── requirements.txt

功能描述:用户上传照片后,系统对其进行初步处理,如调整大小,格式转换等。

实现代码:

  • inference.py:
import cv2 def preprocess_image(image_path): image = cv2.imread(image_path) # 读取本地文件路径中的图像 # 进行初步处理,如调整大小、格式转换等 return image
  • app.py:
from fastapi import FastAPI, File, Form, UploadFile import cv2 import numpy as np from hivision.utils import base64_2_numpy app = FastAPI() # 初始化FastAPI应用 @app.post("/upload") async def upload_image( input_image: UploadFile = File(None), # 接收上传的图像文件 input_image_base64: str = Form(None) # 接收Base64编码的图像字符串 ): if input_image_base64: img = base64_2_numpy(input_image_base64) # 将Base64编码的图像字符串转换为NumPy数组 else: image_bytes = await input_image.read() # 异步读取上传文件的内容 nparr = np.frombuffer(image_bytes, np.uint8) # 将二进制图像数据转换为NumPy数组 img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED) # 解码图像数据为OpenCV图像格式 return {"status": "success", "image_shape": img.shape} # 返回处理结果

功能描述:支持多种语言的用户界面,包括英语、中文、日语、韩语等。

实现代码:

  • demo_locales.py:
# LOCALES:存储不同语言的界面标签和提示信息,每个标签都有对应的多语言版本 LOCALES = { "face_model": { "en": {"label": "Face detection model"}, "zh": {"label": "人脸检测模型"}, "ja": {"label": "顔検出モデル"}, "ko": {"label": "얼굴 감지 모델"}, }, "matting_model": { "en": {"label": "Matting model"}, "zh": {"label": "抠图模型"}, "ja": {"label": "マッティングモデル"}, "ko": {"label": "매팅 모델"}, }, } # get_label(key,locale):根据键和语言获取对应的标签 def get_label(key, locale): return LOCALES[key][locale]["label"]

功能描述:提供多种证件照尺寸和背景颜色选择

实现代码:

  • demo_config.py:
import csv import os def csv_to_size_list(file_path): with open(file_path, 'r') as file: reader = csv.DictReader(file) # 读取CSV文件 return {row['name']: (int(row['width']), int(row['height'])) for row in reader} # 将尺寸信息转换为字典 def csv_to_color_list(file_path): with open(file_path, 'r') as file: reader = csv.DictReader(file) # 读取CSV文件 return {row['name']: (int(row['r']), int(row['g']), int(row['b'])) for row in reader} # 将颜色信息转换为字典 # 加载尺寸和颜色配置信息 def load_configuration(root_dir): size_list_dict_CN = csv_to_size_list(os.path.join(root_dir, "assets/size_list_CN.csv")) # 加载中文尺寸配置 size_list_dict_EN = csv_to_size_list(os.path.join(root_dir, "assets/size_list_EN.csv")) # 加载英文尺寸配置 color_list_dict_CN = csv_to_color_list(os.path.join(root_dir, "assets/color_list_CN.csv")) # 加载中文颜色配置 color_list_dict_EN = csv_to_color_list(os.path.join(root_dir, "assets/color_list_EN.csv")) # 加载英文颜色配置 return size_list_dict_CN, size_list_dict_EN, color_list_dict_CN, color_list_dict_EN # 返回配置信息

功能描述:自动检测人脸并进行抠图处理

实现代码:

  • hivision/creator/__init__.py:
class IDCreator: def __init__(self): self.matting_handler = extract_human # 设置默认的抠图处理函数 self.detection_handler = detect_face_mtcnn # 设置默认的人脸检测函数 def __call__(self, image, **kwargs): face = self.detection_handler(image) # 进行人脸检测 matting_image = self.matting_handler(image, face) # 进行抠图处理 return matting_image # 返回抠图后的图像
  • hivision/creator/choose_handler.py:
def choose_handler(creator, matting_model_option=None, face_detect_option=None): if matting_model_option == "modnet_photographic_portrait_matting": creator.matting_handler = extract_human_modnet_photographic_portrait_matting # 根据用户选择设置抠图模型 if face_detect_option == "retinaface-resnet50": creator.detection_handler = detect_face_retinaface # 根据用户选择设置人脸检测模型

功能描述:调整照片的大小、位置,并提供美艳功能

实现代码:

  • hivision/creator/photo_adjuster.py:
import cv2 import math class PhotoAdjuster: def __init__(self, params): self.params = params # 初始化参数 def adjust_photo(self, image, face_rect): standard_size = self.params.size # 获取目标尺寸 x, y = face_rect[0], face_rect[1] # 获取人脸矩形框的位置 w, h = face_rect[2], face_rect[3] # 获取人脸矩形框的宽度和高度 height, width = image.shape[:2] # 获取图像的高度和宽度 width_height_ratio = standard_size[0] / standard_size[1] # 计算目标尺寸的宽高比 face_center = (x + w / 2, y + h / 2) # 计算人脸中心点 face_measure = w * h # 计算人脸区域的面积 crop_measure = face_measure / self.params.head_measure_ratio # 计算裁剪区域的大小 resize_ratio = crop_measure / (standard_size[0] * standard_size[1]) # 计算缩放比例 resize_ratio_single = math.sqrt(resize_ratio) # 计算单个方向的缩放比例 crop_size = (int(standard_size[0] * resize_ratio_single), int(standard_size[1] * resize_ratio_single)) # 计算裁剪区域的尺寸 x1 = int(face_center[0] - crop_size[1] / 2) # 计算裁剪区域的左边界 y1 = int(face_center[1] - crop_size[0] * self.params.head_height_ratio) # 计算裁剪区域的上边界 y2 = y1 + crop_size[0] # 计算裁剪区域的下边界 x2 = x1 + crop_size[1] # 计算裁剪区域的右边界 cropped_image = image[y1:y2, x1:x2] # 裁剪图像 adjusted_image = cv2.resize(cropped_image, standard_size) # 缩放裁剪后的图像到目标尺寸 return adjusted_image # 返回调整后的图像

功能描述:支持用户选择不同的背景颜色或图片

实现代码:

  • hivision/inference.py:
import cv2 import numpy as np def add_background(image, background_color, render_mode=0): if render_mode == 0: background = np.full_like(image, background_color) # 创建与图像相同大小的背景图像,填充指定颜色 elif render_mode == 1: gradient = np.linspace(background_color, [255, 255, 255], image.shape[0]).astype(np.uint8) # 创建渐变背景 background = np.tile(gradient, (image.shape[1], 1, 1)).transpose(1, 0, 2) # 调整渐变背景的维度 elif render_mode == 2: center = (image.shape[1] // 2, image.shape[0] // 2) # 计算图像中心点 radius = min(center[0], center[1]) # 计算半径 y, x = np.ogrid[:image.shape[0], :image.shape[1]] # 创建网格坐标 distance = ((x - center[0])**2 + (y - center[1])**2)**0.5 # 计算距离中心点的距离 mask = distance <= radius # 创建圆形掩码 background = np.where(mask[:, :, np.newaxis], background_color, [255, 255, 255]) # 创建圆形背景 result = cv2.addWeighted(image, 1, background, 1, 0) # 将前景图像和背景图像混合 return result # 返回最终结果

功能描述:生成符合要求的证件照,并提供下载
实现代码:

  • test_create_id_photo.py:
from hivision.creator import IDCreator import cv2 import os now_dir = os.path.dirname(__file__) # 获取当前文件目录 image_path = os.path.join(os.path.dirname(now_dir), "app", "images", "test.jpg") # 构建测试图像路径 output_dir = os.path.join(now_dir, "temp") # 构建输出目录路径 image = cv2.imread(image_path) # 读取测试图像 creator = IDCreator() # 创建证件照生成器实例 result = creator(image) # 处理图像生成证件照 cv2.imwrite(os.path.join(output_dir, "result.png"), result.standard) # 保存标准尺寸的证件照 cv2.imwrite(os.path.join(output_dir, "result_hd.png"), result.hd) # 保存高清尺寸的证件照

功能描述:提供API接口供外部调用,实现照片处理功能

实现代码:

  • app.py:
from fastapi import FastAPI, File, Form, UploadFile from fastapi.responses import JSONResponse from hivision.utils import base64_2_numpy, bytes_2_base64, resize_image_to_kb, save_image_dpi_to_bytes from hivision.creator import IDCreator from hivision.creator.choose_handler import choose_handler app = FastAPI() # 初始化FastAPI应用 @app.post("/idphoto_crop") async def idphoto_crop_inference( input_image: UploadFile = File(None), # 接收上传的图像文件 input_image_base64: str = Form(None), # 接收Base64编码的图像字符串 height: int = Form(413), # 目标高度 width: int = Form(295), # 目标宽度 face_detect_model: str = Form("mtcnn"), # 人脸检测模型 hd: bool = Form(True), # 是否生成高清图像 dpi: int = Form(300), # 图像分辨率 head_measure_ratio: float = 0.2, # 头部测量比例 head_height_ratio: float = 0.45, # 头部高度比例 top_distance_max: float = 0.12, # 头部顶部最大距离 top_distance_min: float = 0.10, # 头部顶部最小距离 ): if input_image_base64: img = base64_2_numpy(input_image_base64) # 将Base64编码的图像字符串转换为NumPy数组 else: image_bytes = await input_image.read() # 异步读取上传文件的内容 nparr = np.frombuffer(image_bytes, np.uint8) # 将二进制图像数据转换为NumPy数组 img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED) # 解码图像数据为OpenCV图像格式 creator = IDCreator() # 创建证件照生成器实例 choose_handler(creator, face_detect_option=face_detect_model) # 根据用户选择的模型设置相应的处理函数 size = (int(height), int(width)) # 设置目标尺寸 try: result_image = creator( img, size=size, change_bg_only=False, crop_only=False, head_measure_ratio=head_measure_ratio, head_height_ratio=head_height_ratio, head_top_range=(top_distance_max, top_distance_min), face=None, whitening_strength=0, brightness_strength=0, contrast_strength=0, sharpen_strength=0, saturation_strength=0, face_alignment=False, ) # 处理图像生成证件照 result_image_bytes = save_image_dpi_to_bytes(result_image, None, dpi=dpi) # 将生成的图像转换为字节流 result_image_base64 = bytes_2_base64(result_image_bytes) # 将字节流转换为Base64编码的字符串 result_message = { "status": True, "image_base64": result_image_base64, } # 返回处理结果 except Exception as e: result_message = { "status": False, "error": str(e), } # 返回错误信息 return result_message # 返回最终结果

依赖文件:

  • requirements.txt:
fastapi==0.68.0 # FastAPI框架 uvicorn==0.15.0 # ASGI服务器 opencv-python==4.5.3.56 # OpenCV库 numpy==1.21.2 # NumPy库 Pillow==8.3.2 # 图像处理库

测试脚本:

  • test_create_id_photo.py:
from hivision.creator import IDCreator import cv2 import os now_dir = os.path.dirname(__file__) # 获取当前文件目录 image_path = os.path.join(os.path.dirname(now_dir), "app", "images", "test.jpg") # 构建测试图像路径 output_dir = os.path.join(now_dir, "temp") # 构建输出目录路径 image = cv2.imread(image_path) # 读取测试图像 creator = IDCreator() # 创建证件照生成器实例 result = creator(image) # 处理图像生成证件照 cv2.imwrite(os.path.join(output_dir, "result.png"), result.standard) # 保存标准尺寸的证件照 cv2.imwrite(os.path.join(output_dir, "result_hd.png"), result.hd) # 保存高清尺寸的证件照

原文链接:https://blog.csdn.net/weixin_66037896/article/details/143651458?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522ae02de49329aeede0fa4af416cb2dbf3%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=ae02de49329aeede0fa4af416cb2dbf3&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-20-143651458-null-null.nonecase&utm_term=%E9%A3%9E%E7%89%9BOS

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享