在图像/视频中裁剪出人脸区域

1. 在图像中裁剪人脸区域

import face_alignment
import skimage.io
import numpy
from argparse import ArgumentParser
from skimage import img_as_ubyte
from skimage.transform import resize
from tqdm import tqdm
import os
import numpy as np
import warnings
warnings.filterwarnings("ignore")# =================================================================================================
# Kun(20250306): 输出裁剪图像的命令
# =================================================================================================def extract_bbox(frame, fa):if max(frame.shape[0], frame.shape[1]) > 640:scale_factor =  max(frame.shape[0], frame.shape[1]) / 640.0frame = resize(frame, (int(frame.shape[0] / scale_factor), int(frame.shape[1] / scale_factor)))frame = img_as_ubyte(frame)else:scale_factor = 1frame = frame[..., :3]bboxes = fa.face_detector.detect_from_image(frame[..., ::-1])if len(bboxes) == 0:return []return np.array(bboxes)[:, :-1] * scale_factordef bb_intersection_over_union(boxA, boxB):xA = max(boxA[0], boxB[0])yA = max(boxA[1], boxB[1])xB = min(boxA[2], boxB[2])yB = min(boxA[3], boxB[3])interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)iou = interArea / float(boxAArea + boxBArea - interArea)return ioudef join(tube_bbox, bbox):xA = min(tube_bbox[0], bbox[0])yA = min(tube_bbox[1], bbox[1])xB = max(tube_bbox[2], bbox[2])yB = max(tube_bbox[3], bbox[3])return (xA, yA, xB, yB)def compute_bbox(tube_bbox, frame_shape, inp, image_shape, increase_area=0.1):left, top, right, bot = tube_bboxwidth = right - leftheight = bot - top#Computing aspect preserving bboxwidth_increase = max(increase_area, ((1 + 2 * increase_area) * height - width) / (2 * width))height_increase = max(increase_area, ((1 + 2 * increase_area) * width - height) / (2 * height))left = int(left - width_increase * width)top = int(top - height_increase * height)right = int(right + width_increase * width)bot = int(bot + height_increase * height)top, bot, left, right = max(0, top), min(bot, frame_shape[0]), max(0, left), min(right, frame_shape[1])h, w = bot - top, right - leftscale = f'{image_shape[0]}:{image_shape[1]}'return f'ffmpeg -i {inp} -filter:v "crop={w}:{h}:{left}:{top}, scale={scale}" crop.png'def process_image(args):device = 'cpu' if args.cpu else 'cuda'fa = face_alignment.FaceAlignment(face_alignment.LandmarksType.TWO_D, flip_input=False, device=device)frame = skimage.io.imread(args.inp)frame_shape = frame.shapebboxes = extract_bbox(frame, fa)if len(bboxes) == 0:print("No faces detected.")return []tube_bbox = bboxes[0]  # Assuming we take the first detected facecommand = compute_bbox(tube_bbox, frame_shape, inp=args.inp, image_shape=args.image_shape, increase_area=args.increase)return [command]if __name__ == "__main__":parser = ArgumentParser()parser.add_argument("--image_shape", default=(256, 256), type=lambda x: tuple(map(int, x.split(','))),help="Image shape")parser.add_argument("--increase", default=0.1, type=float, help='Increase bbox by this amount')parser.add_argument("--iou_with_initial", type=float, default=0.25, help="The minimal allowed iou with inital bbox")parser.add_argument("--inp", required=True, help='Input image')parser.add_argument("--cpu", dest="cpu", action="store_true", help="cpu mode.")args = parser.parse_args()commands = process_image(args)for command in commands:print(command)

2. 在视频中裁剪人脸区域

import face_alignment
import skimage.io
import numpy
from argparse import ArgumentParser
from skimage import img_as_ubyte
from skimage.transform import resize
from tqdm import tqdm
import os
import imageio
import numpy as np
import warnings
warnings.filterwarnings("ignore")# =================================================================================================
# Kun(20250306): 输出裁剪视频的命令
# =================================================================================================def extract_bbox(frame, fa):if max(frame.shape[0], frame.shape[1]) > 640:scale_factor =  max(frame.shape[0], frame.shape[1]) / 640.0frame = resize(frame, (int(frame.shape[0] / scale_factor), int(frame.shape[1] / scale_factor)))frame = img_as_ubyte(frame)else:scale_factor = 1frame = frame[..., :3]bboxes = fa.face_detector.detect_from_image(frame[..., ::-1])if len(bboxes) == 0:return []return np.array(bboxes)[:, :-1] * scale_factordef bb_intersection_over_union(boxA, boxB):xA = max(boxA[0], boxB[0])yA = max(boxA[1], boxB[1])xB = min(boxA[2], boxB[2])yB = min(boxA[3], boxB[3])interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)iou = interArea / float(boxAArea + boxBArea - interArea)return ioudef join(tube_bbox, bbox):xA = min(tube_bbox[0], bbox[0])yA = min(tube_bbox[1], bbox[1])xB = max(tube_bbox[2], bbox[2])yB = max(tube_bbox[3], bbox[3])return (xA, yA, xB, yB)def compute_bbox(start, end, fps, tube_bbox, frame_shape, inp, image_shape, increase_area=0.1):left, top, right, bot = tube_bboxwidth = right - leftheight = bot - top#Computing aspect preserving bboxwidth_increase = max(increase_area, ((1 + 2 * increase_area) * height - width) / (2 * width))height_increase = max(increase_area, ((1 + 2 * increase_area) * width - height) / (2 * height))left = int(left - width_increase * width)top = int(top - height_increase * height)right = int(right + width_increase * width)bot = int(bot + height_increase * height)top, bot, left, right = max(0, top), min(bot, frame_shape[0]), max(0, left), min(right, frame_shape[1])h, w = bot - top, right - leftstart = start / fpsend = end / fpstime = end - startscale = f'{image_shape[0]}:{image_shape[1]}'return f'ffmpeg -i {inp} -ss {start} -t {time} -filter:v "crop={w}:{h}:{left}:{top}, scale={scale}" crop.mp4'def compute_bbox_trajectories(trajectories, fps, frame_shape, args):commands = []for i, (bbox, tube_bbox, start, end) in enumerate(trajectories):if (end - start) > args.min_frames:command = compute_bbox(start, end, fps, tube_bbox, frame_shape, inp=args.inp, image_shape=args.image_shape, increase_area=args.increase)commands.append(command)return commandsdef process_video(args):device = 'cpu' if args.cpu else 'cuda'fa = face_alignment.FaceAlignment(face_alignment.LandmarksType.TWO_D, flip_input=False, device=device)video = imageio.get_reader(args.inp)trajectories = []previous_frame = Nonefps = video.get_meta_data()['fps']commands = []try:for i, frame in tqdm(enumerate(video)):frame_shape = frame.shapebboxes =  extract_bbox(frame, fa)## For each trajectory check the criterionnot_valid_trajectories = []valid_trajectories = []for trajectory in trajectories:tube_bbox = trajectory[0]intersection = 0for bbox in bboxes:intersection = max(intersection, bb_intersection_over_union(tube_bbox, bbox))if intersection > args.iou_with_initial:valid_trajectories.append(trajectory)else:not_valid_trajectories.append(trajectory)commands += compute_bbox_trajectories(not_valid_trajectories, fps, frame_shape, args)trajectories = valid_trajectories## Assign bbox to trajectories, create new trajectoriesfor bbox in bboxes:intersection = 0current_trajectory = Nonefor trajectory in trajectories:tube_bbox = trajectory[0]current_intersection = bb_intersection_over_union(tube_bbox, bbox)if intersection < current_intersection and current_intersection > args.iou_with_initial:intersection = bb_intersection_over_union(tube_bbox, bbox)current_trajectory = trajectory## Create new trajectoryif current_trajectory is None:trajectories.append([bbox, bbox, i, i])else:current_trajectory[3] = icurrent_trajectory[1] = join(current_trajectory[1], bbox)except IndexError as e:raise (e)commands += compute_bbox_trajectories(trajectories, fps, frame_shape, args)return commandsif __name__ == "__main__":parser = ArgumentParser()parser.add_argument("--image_shape", default=(256, 256), type=lambda x: tuple(map(int, x.split(','))),help="Image shape")parser.add_argument("--increase", default=0.1, type=float, help='Increase bbox by this amount')parser.add_argument("--iou_with_initial", type=float, default=0.25, help="The minimal allowed iou with inital bbox")parser.add_argument("--inp", required=True, help='Input image or video')parser.add_argument("--min_frames", type=int, default=150,  help='Minimum number of frames')parser.add_argument("--cpu", dest="cpu", action="store_true", help="cpu mode.")args = parser.parse_args()commands = process_video(args)for command in commands:print (command)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/898316.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【软考-架构】11.3、设计模式-新

✨资料&文章更新✨ GitHub地址&#xff1a;https://github.com/tyronczt/system_architect 文章目录 项目中的应用设计模式创建型设计模式结构型设计模式行为型设计模式 &#x1f4af;考试真题题外话 项目中的应用 在实际项目中&#xff0c;我应用过多种设计模式来解决不同…

使用Redis如何实现分布式锁?(超卖)

分布式锁概念 在多线程环境下&#xff0c;为了保证数据的线程安全&#xff0c;锁保证同一时刻&#xff0c;只有一个可以访问和更新共享数据。在单机系统我们可以使用 synchronized 锁、Lock 锁保证线程安全。 synchronized 锁是 Java 提供的一种内置锁&#xff0c;在单个 JVM …

Linux的Shell编程

一、什么是Shell 1、为什么要学习Shell Linux运维工程师在进行服务器集群管理时&#xff0c;需要编写Shell程序来进行服务器管理。 对于JavaEE和Python程序员来说&#xff0c;工作的需要。Boss会要求你编写一些Shell脚本进行程序或者是服务器的维护&#xff0c;比如编写一个…

使用React和google gemini api 打造一个google gemini应用

实现一个简单的聊天应用&#xff0c;用户可以通过输入问题或点击“Surprise me”按钮获取随机问题&#xff0c;并从后端API获取回答。 import { useState } from "react"; function App() {const [ value, setValue] useState(""); // 存储用户输入的问题…

深入探讨TK矩阵系统:创新的TikTok运营工具

TK矩阵的应用场景 TK矩阵系统适用于多个场景&#xff0c;尤其是在以下几个方面有显著优势&#xff1a; 批量账号管理与内容发布&#xff1a;对于需要管理多个TikTok账号的内容创作者或营销人员&#xff0c;TK矩阵提供了高效的账号管理工具&#xff0c;支持批量发布视频、评论、…

MTK Android12 应用在最顶端时,禁止拉起其他某个应用(一)

1、需求 近期&#xff0c;客户要求应用在最顶端时&#xff0c;禁止拉起其他某个应用2、解决方法 diff --git a/frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java b/frameworks/base/services/core/java/com/android/server/wm/ActivityStarte…

论文阅读笔记:Deep Unsupervised Learning using Nonequilibrium Thermodynamics

1、来源 论文连接1&#xff1a;http://ganguli-gang.stanford.edu/pdf/DeepUnsupDiffusion.pdf 论文连接2(带appendix)&#xff1a;https://arxiv.org/pdf/1503.03585v7 代码链接&#xff1a;https://github.com/Sohl-Dickstein/Diffusion-Probabilistic-Models 代码的环境配置…

7种数据结构

7种数据结构 顺序表sqlite.hseqlite.c 单链表linklist.clinklist.h 双链表doulinklist.cdoulinklist.h 链式栈linkstack.clinkstack.h 队列SeqQueue.cSeqQueue.h 树tree.c 哈希表hash.c 顺序表 sqlite.h #ifndef __SEQLIST_H__ #define __SEQLIST_H__ typedef struct person…

Linux 查看及测试网络命令

使用 ifconfig 命令查看网络接口地址 查看指定的网络接口信息 执行 ifconfig ens33 命令可以只查看网卡 ens33 的配置信息

ABAP语言的动态编程(4) - 综合案例:管理费用明细表

本篇来实现一个综合案例&#xff1a;管理费用明细表。报表在实际项目中&#xff0c;也有一定的参考意义&#xff0c;一方面展示类似的报表&#xff0c;比如管理费用、研发费用等费用的明细&#xff0c;使用业务比较习惯的展示格式&#xff1b;另一方面正好综合运用前面学习的动…

【Redis】Redis的数据删除(过期)策略,数据淘汰策略。

如果问到&#xff1a;假如Redis的key过期之后&#xff0c;会立即删除吗&#xff1f; 其实就是想问数据删除(过期)策略。 如果面试官问到&#xff1a;如果缓存过多&#xff0c;内存是有限的&#xff0c;内存被占满了怎么办&#xff1f; 其实就是问&#xff1a;数据的淘汰策略。…

Linux配置yum仓库,服务控制,防火墙

一、yum仓库 1.在安装软件时&#xff0c;首先第一步就是要考虑软件的版本的问题&#xff01; 2.软件的安装&#xff1a;最安全可靠的方法就是去软件对应的官网上查看安装手册&#xff08;包括的软件的下载&#xff09; 红帽系软件安装的常见的3种方式 &#xff08;1&#x…

[从零开始学习JAVA] Stream流

前言&#xff1a; 本文我们将学习Stream流&#xff0c;他就像流水线一样&#xff0c;可以对我们要处理的对象进行逐步处理&#xff0c;最终达到我们想要的效果&#xff0c;是JAVA中的一大好帮手&#xff0c;值得我们了解和掌握。&#xff08;通常和lambda 匿名内部类 方法引用相…

设计模式(创建型)-抽象工厂模式

摘要 在软件开发的复杂世界中,设计模式作为解决常见问题的最佳实践方案,一直扮演着至关重要的角色。抽象工厂模式,作为一种强大的创建型设计模式,在处理创建一系列或相关依赖对象的场景时,展现出了独特的优势和灵活性。它通过提供一个创建对象的接口,让开发者能够在不指定…

【JavaEE】-- SpringBoot快速上手

文章目录 1. Maven1.1 什么是Maven1.2 为什么要学Maven1.3 创建一个Maven项目1.4 Maven核心功能1.4.1 项目创建1.4.2 依赖管理1.4.3 Maven Help插件 1.5 Maven仓库1.5.1 本地仓库1.5.2 中央仓库1.5.3 私有服务器&#xff08;私服&#xff09; 1.6 Maven设置国内源1.6.1 配置当前…

TCP/IP 协议精讲-精华总结版本

序言 本文旨在介绍一下TCP/IP涉及得所有基础知识&#xff0c;为大家从宏观上俯瞰TCP/IP提供一个基石&#xff0c;文档属于《TCP/IP图解&#xff08;第五版&#xff09;》的精简版本。 专业术语 缩写 全称 WAN Wide area network广域网 LAN Local area network局域网 TC…

Leetcode 160 Intersection of Two Linked Lists

题意 给定两个链表&#xff0c;找这两个链表第一个公共节点&#xff0c;如果没有返回nullptr 题目链接 https://leetcode.com/problems/intersection-of-two-linked-lists/description/ 题解 两个指针分别从两个链表&#xff08;记录为表A&#xff0c;表B&#xff09;的表…

C语言——结构体、联合、枚举

C语言中自定义类型 结构体结构体变量的创建和初始化结构体传参结构体内存对齐(如何存储) 联合体(共用体)联合体创建和初始化联合体大小(如何存储) 枚举类型枚举类型创建枚举类型初始化枚举的优点(相较于define) 前言 C语言中有内置类型和自定义类型&#xff0c;内置类型就像int…

利用pprof对golang进行性能分析

利用pprof进行性能分析 pprof性能分析的5个方面 一、性能分析的五个核心维度 CPU分析 - 剖析程序的CPU使用情况&#xff0c;定位高耗时函数 内存分析 - 追踪内存分配与泄露&#xff0c;优化内存使用模式 IO分析 - 监控文件/网络IO操作&#xff0c;发现瓶颈资源 Goroutine分…

IntelliJ IDEA 调试技巧指南

在日常开发中&#xff0c;调试是不可或缺的一部分。掌握调试工具的使用可以让我们更高效地定位和解决问题。本文将介绍一些在 IntelliJ IDEA 中常用的调试技巧&#xff0c;希望能帮助你在开发过程中更顺畅地解决问题。 1. 方法断点&#xff1a;快速定位实现类 方法断点可以帮…