调整具有精​​确面部位置的非均匀图像的大小

如何解决调整具有精​​确面部位置的非均匀图像的大小

我在一家处理学校照片的工作室里工作,我们正在尝试编写脚本以消除将每张照片裁剪为模板的工作。我们使用的照片相当统一,但分辨率和头部位置略有不同。我花了很多力气,试图用我相当有限的Python知识来编写脚本,并通过大量的试验和错误以及在线资源,我认为我已经掌握了大部分方法。

目前,我正在尝试找出从NumPy数组中裁剪出想要的图像的最佳方法,但我找不到合适的灵活解决方案。对于姿势1和姿势2,头部的位置需要稍有不同,因此它需要易于随时更改(可能将实现某种简单的GUI来输入类似的内容,但是现在我可以更改代码)。

我还需要能够更改照片的输出分辨率,以便它们都是统一的(2000x2500)。有人有什么想法吗?

这是我当前的代码,它只是保存检测到的脸部正方形:

import cv2
import os.path
import glob

# Cascade path
cascPath = 'haarcascade_frontalface_default.xml'

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

#Check for output folder and create if its not there
if not os.path.exists('output'):
    os.makedirs('output')

# Read Images
images = glob.glob('*.jpg')
for c,i in enumerate(images):
    image = cv2.imread(i,1)

    # Convert to grayscale
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

    # Find face(s) using cascade
    faces = faceCascade.detectMultiScale(
        gray,scaleFactor=1.1,# size of groups
        minNeighbors=5,# How many groups around are detected as face for it to be valid
        minSize=(500,500)  # Min size in pixels for face
    )

    # Outputs number of faces found in image
    print('Found {0} faces!'.format(len(faces)))

    # Places a rectangle on face
    for (x,y,w,h) in faces:
        imgCrop = image[y:y+h,x:x+w]

    if len(faces) > 0:
        #Saves Images to output folder with OG name
        cv2.imwrite('output/'+ i,imgCrop)

我可以像这样使用它来裁剪

    # Crop Padding
    left = 300
    right = 300
    top = 400
    bottom = 1000
    
    for (x,h) in faces:
        imgCrop = image[y-top:y+h+bottom,x-left:x+w+right]

但这会输出相当随机的分辨率并根据图像分辨率进行更改

解决方法

TL; DR

  • 要使用尺寸设置新的分辨率,可以使用cv2.resize。可能会有像素损失,因此您可以使用插值方法。

  • 新调整大小的图像可能为BGR格式,因此您可能需要转换为RGB格式。

cv2.resize(src=crop,dsize=(2000,2500),interpolation=cv2.INTER_LANCZOS4)
crop = cv2.cvtColor(crop,cv2.COLOR_BGR2RGB)  # Make sure the cropped image is in RGB format
cv2.imwrite("image-1.png",crop)

建议:

  • 一种方法是使用python的face-recognition库。

  • 该方法使用两个样本图像进行训练。

  • 根据训练图像预测下一幅图像。

例如,以下是训练图像:

enter image description here enter image description here

我们要在下图中预测人脸:

enter image description here

当我们获得训练图像的面部编码并应用于下一张图像时:

import face_recognition
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image,ImageDraw

# Load a sample picture and learn how to recognize it.
first_image = face_recognition.load_image_file("images/ex.jpg")
first_face_encoding = face_recognition.face_encodings(first_image)[0]

# Load a second sample picture and learn how to recognize it.
second_image = face_recognition.load_image_file("images/index.jpg")
sec_face_encoding = face_recognition.face_encodings(second_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    first_face_encoding,sec_face_encoding
]

print('Learned encoding for',len(known_face_encodings),'images.')

# Load an image with an unknown face
unknown_image = face_recognition.load_image_file("images/babes.jpg")

# Find all the faces and face encodings in the unknown image
face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image,face_locations)

# Convert the image to a PIL-format image so that we can draw on top of it with the Pillow library
# See http://pillow.readthedocs.io/ for more about PIL/Pillow
pil_image = Image.fromarray(unknown_image)
# Create a Pillow ImageDraw Draw instance to draw with
draw = ImageDraw.Draw(pil_image)

# Loop through each face found in the unknown image
for (top,right,bottom,left),face_encoding in zip(face_locations,face_encodings):
    matches = face_recognition.compare_faces(known_face_encodings,face_encoding)
    face_distances = face_recognition.face_distance(known_face_encodings,face_encoding)
    best_match_index = np.argmin(face_distances)
    draw.rectangle(((left,top),(right,bottom)),outline=(0,255),width=5)

# Remove the drawing library from memory as per the Pillow docs
del draw

# Display the resulting image
plt.imshow(pil_image)
plt.show()

输出将是:

enter image description here

以上是我的建议。当您使用当前图像创建新的分辨率时,会有像素损失。因此,您需要使用 插值 方法。

例如:找到面部位置后,在原始图像中选择坐标。

# Add after draw.rectangle function.
crop = unknown_image[top:bottom,left:right]

设置大小为2000 x 2500的新分辨率,并使用CV2.INTERN_LANCZOS4进行插值。

可能的问题:为什么选择CV2.INTERN_LANCZOS4

当然,您可以选择任何内容,但建议使用in this post CV2.INTERN_LANCZOS4

cv2.resize(src=crop,interpolation=cv2.INTER_LANCZOS4)

保存图像

crop = cv2.cvtColor(crop,crop)

输出约为4.3 MB,因此我无法在此处显示。

从最终结果中,我们可以清楚地看到并识别出面孔。该库可在图像中精确找到人脸。

您可以做什么:

  • 您可以使用自己设置的训练图像,也可以使用上面的示例。

  • 使用经过训练的脸部位置为每张图像应用脸部识别功能,并将结果保存在目录中。

,

这就是我想要的裁剪方式,将其添加到“输出面部数量”功能的下方

const [darkState,setDarkState] = useState(Boolean(!cookies.darkTheme));

一团糟,但它的工作方式完全符合我的要求。 最终由于速度原因而选择了openCV而不是切换到python-Recognition,但是如果我可以让多线程在python-recognition中工作,我可能会切换。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-