当前位置: 首页 > wzjs >正文

玉林建设信息网站湖南seo排名

玉林建设信息网站,湖南seo排名,开发网站的基本流程,用php做的博客网站# 主要筛选人脸信息(比如:0 这个人的文件夹里有很多张属于0的人脸照片,但是同时又参杂一些非常模糊或者其他人的照片,那么可以通过这个方法把参杂的模糊的和其他人的人脸排序到最后,那样清理的时候就不需要到处找那些不…

# 主要筛选人脸信息(比如:0 这个人的文件夹里有很多张属于0的人脸照片,但是同时又参杂一些非常模糊或者其他人的照片,那么可以通过这个方法把参杂的模糊的和其他人的人脸排序到最后,那样清理的时候就不需要到处找那些不合格的照片)

import os
import shutilimport numpy as np
from sklearn.metrics.pairwise import euclidean_distances
from PIL import Image
import torch
import torchvision.transforms as transforms
from facenet_pytorch import InceptionResnetV1
import sqlite3
import threading# 1. 加载预训练的人脸特征提取模型
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = InceptionResnetV1(pretrained='vggface2').eval().to(device)# 2. 图像预处理
transform = transforms.Compose([transforms.Resize((160, 160)),  # FaceNet 输入尺寸transforms.ToTensor(),transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])# 3. 提取单张图像的特征向量
def extract_feature(image_path):image = Image.open(image_path).convert('RGB')image = transform(image).unsqueeze(0).to(device)with torch.no_grad():feature = model(image).cpu().numpy().flatten()return feature# 4. 创建 SQLite 数据库
def create_database(db_path):conn = sqlite3.connect(db_path)cursor = conn.cursor()cursor.execute('''CREATE TABLE IF NOT EXISTS features (person_id TEXT,image_path TEXT,feature_vector BLOB,PRIMARY KEY (person_id, image_path))''')conn.commit()conn.close()# 5. 将特征向量保存到数据库
def save_feature_to_db(db_path, person_id, image_path, feature):conn = sqlite3.connect(db_path)cursor = conn.cursor()# 检查是否有相同的person_id 和 image_path 存在 (目的是为例防止程序中断 后 又重新运行 数据插入冲突导致报错)cursor.execute("""SELECT COUNT(*) FROM featuresWHERE person_id = ? AND image_path = ? """, (person_id, image_path))count = cursor.fetchone()[0]# 如果不存在if count == 0:feature_blob = feature.tobytes()  # 将特征向量转换为二进制格式cursor.execute('''INSERT INTO features (person_id, image_path, feature_vector)VALUES (?, ?, ?)''', (person_id, image_path, feature_blob))conn.commit()conn.close()else:print(f"Feature for {person_id} - {image_path} already exists,  skipping")# 6. 处理每个文件夹,提取特征并保存到数据库
def process_folder(db_path, folder_path, person_id):for image_name in os.listdir(folder_path):image_path = os.path.join(folder_path, image_name)# 避免处理非图片文件if image_path.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):# 防止因图片损坏导致提取特侦失败致使程序中断try:feature = extract_feature(image_path)save_feature_to_db(db_path, person_id, image_path, feature)except Exception as e:print(e)# 7. 从数据库中获取某个人的平均特征向量
def get_avg_feature(db_path, person_id):conn = sqlite3.connect(db_path)cursor = conn.cursor()cursor.execute('''SELECT feature_vector FROM features WHERE person_id = ?''', (person_id,))rows = cursor.fetchall()conn.close()# 将所有特征向量转换为 numpy 数组features = [np.frombuffer(row[0], dtype=np.float32) for row in rows]avg_feature = np.mean(features, axis=0)return avg_feature# 8. 根据欧氏距离排序并重命名图像
def sort_and_rename_images(db_path, out_path, person_id):avg_feature = get_avg_feature(db_path, person_id)conn = sqlite3.connect(db_path)cursor = conn.cursor()cursor.execute('''SELECT image_path, feature_vector FROM features WHERE person_id = ?''', (person_id,))rows = cursor.fetchall()conn.close()# 计算欧氏距离并排序distances = []for row in rows:image_path, feature_blob = rowfeature = np.frombuffer(feature_blob, dtype=np.float32)distance = euclidean_distances([feature], [avg_feature])[0][0]distances.append((image_path, distance))# 按距离排序distances.sort(key=lambda x: x[1])# 重命名文件for idx, (image_path, _) in enumerate(distances):new_name = f"{idx:04d}.jpg"  # 按距离排序后的新文件名# new_path = os.path.join(folder_path, new_name)new_path = rf'{out_path}/{person_id}/{new_name}'# 如果目标文件夹不存在,则创建os.makedirs(os.path.dirname(new_path), exist_ok=True)shutil.copy(image_path, new_path)# os.rename(image_path, new_path)# 9. 主函数
def main():# 数据库路径db_path = r'D:\FS_project2\Feature_extraction\sql_database\features.db2'create_database(db_path)# 基础路径base_path = r'D:\FS_project2\Feature_extraction\peopel_crop'out_path = r'D:\FS_project2\Feature_extraction\out'# 第一步:提取特征并保存到数据库for folder in os.listdir(base_path):folder_path = os.path.join(base_path, folder)if os.path.isdir(folder_path):process_folder(db_path, folder_path, folder)print(f"Processed folder: {folder}")# 第二步:排序并重命名图像for folder in os.listdir(base_path):folder_path = os.path.join(base_path, folder)if os.path.isdir(folder_path):sort_and_rename_images(db_path, out_path, folder)print(f"Sorted and renamed folder: {folder}")if __name__ == "__main__":main()

http://www.dtcms.com/wzjs/214041.html

相关文章:

  • 镇江高端网站建设工作室电商平台怎么做
  • 天津手机版建站系统鹤壁seo
  • 荣盛科技网站建设北京、广州最新发布
  • 网站建设与管理好找工作吗某企业网站的分析优化与推广
  • 网站开发html5技术怎么做一个网站出来
  • 网站侧栏软件排行榜怎么做的网站权重怎么看
  • 网页页面建设方案济南seo优化公司
  • 做推广比较好的网站网络营销案例分享
  • 租号网站怎么做的建网站一般需要多少钱
  • 网站报价方案怎么做大数据技术主要学什么
  • 武汉设计工程学院代码上海网站seo公司
  • 永久免费手机建站安卓排名优化
  • 公司有多少做网站东莞百度推广排名
  • 玩具网站 下载西安网站建设方案优化
  • 深圳做营销网站的公司哪家好长治seo顾问
  • 网站代码在哪里修改百度入驻
  • 微信3g网站模板好看的网页设计作品
  • 懂得都懂晚上正能量seo首页排名优化
  • 许昌建设委员会网站衡阳seo优化推荐
  • 网站备案密码查询论坛软文案例
  • 湖南省人民政府热线电话seo自学
  • 很看好未来做生鲜的网站世界十大搜索引擎排名
  • 中小企业网站制作哪家好100个商业经典案例
  • 做网站最主要是什么seo流量排名软件
  • 济南网站建设公司排名成都网站seo服务
  • 旅游最好的网站建设seo营销
  • 泰州做兼职的网站俄罗斯搜索引擎yandex官网入口
  • wordpress改模板seo专业培训需要多久
  • 做网站的公司都有哪些岗位推广普通话宣传语
  • 在网站留外链怎么做网络营销制度课完整版