matlab gendata,gen_data.py

# -*- coding: utf-8 -*-

"""

Created on Fri Apr 19 10:32:37 2019

@author: lyfeng

Make train/test datasets from raw datasets

input: raw original datasets

output: aligned datasets(customized size of images)

"""

import cv2

import os

import numpy as np

import torchvision.transforms as transforms

from matlab_cp2tform import get_similarity_transform_for_cv2

from src import detect_faces

from PIL import Image

from tqdm import tqdm

import matplotlib.pyplot as plt

def alignment(src_img,src_pts):

of = 0

ref_pts = [ [30.2946+of, 51.6963+of],[65.5318+of, 51.5014+of],

[48.0252+of, 71.7366+of],[33.5493+of, 92.3655+of],[62.7299+of, 92.2041+of] ]

crop_size = (96+of*2, 112+of*2) # 96,112

s = np.array(src_pts).astype(np.float32)

r = np.array(ref_pts).astype(np.float32)

tfm = get_similarity_transform_for_cv2(s, r) #

face_img = cv2.warpAffine(src_img, tfm, crop_size) # affine transformation

return face_img

# have no landmarks at initial

def produce_aligned_images(raw_data_root, new_data_root):

all_sequences = os.listdir(raw_data_root)

num_id = len(all_sequences)

for each_id in tqdm(all_sequences):

imgs_path = raw_data_root + '/' + each_id

for img_name in os.listdir(imgs_path):

img = Image.open(imgs_path +'/'+ img_name)

bounding_boxes, landmarks = detect_faces(img)

try:

landmarks = landmarks[0]

src_pts = [[landmarks[0],landmarks[5]],

[landmarks[1],landmarks[6]],

[landmarks[2],landmarks[7]],

[landmarks[3],landmarks[8]],

[landmarks[4],landmarks[9]]]

except: pass

img = alignment(np.array(img),src_pts)

try:

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # cv2 default:BGR

except:

pass

save_path = new_data_root + '/' + each_id

if not os.path.exists(save_path):

os.makedirs(save_path)

cv2.imwrite(save_path+'/'+img_name, img)

break

# have landmarks aleardy

def produce_aligned_images_from_txt(raw_data_root, new_data_root, landmark_file):

with open(landmark_file) as f:

for index, line in enumerate(f):

split = line.split('\t')

nameinzip = split[0] # eg: 0000159/266.jpg

classid = int(split[1])

src_pts = [] # store the landmarks

for i in range(5):

src_pts.append([int(split[2*i+2]),int(split[2*i+3])])

imgs_path = raw_data_root + '/' + nameinzip

img = Image.open(imgs_path)

img = alignment(np.array(img),src_pts)

try:

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # cv2 default:BGR

except:

pass

save_path = new_data_root + '/' + nameinzip.split('/')[0]

if not os.path.exists(save_path):

os.makedirs(save_path)

cv2.imwrite(save_path + '/' + nameinzip.split('/')[1], img)

#if __name__ =='__main__':

# produce_aligned_images('G:/数据资料/人脸数据集/CASIA-WebFace/CASIA-WebFace',

# 'D:/Face Recognition/CASIA-WebFace-aligned')

# produce_aligned_images_from_txt('G:/数据资料/人脸数据集/CASIA-WebFace/CASIA-WebFace',

# 'D:/Face Recognition/CASIA-WebFace-aligned',

# 'D:/Face Recognition/sphereface_pytorch-master/data/casia_landmark.txt')

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
运行代码: import scipy.io import mne from mne.bem import make_watershed_bem import random import string # Load .mat files inner_skull = scipy.io.loadmat('E:\MATLABproject\data\MRI\Visit1_040318\\tess_mri_COR_MPRAGE_RECON-mocoMEMPRAGE_FOV_220-298665.inner_skull.mat') outer_skull = scipy.io.loadmat('E:\MATLABproject\data\MRI\Visit1_040318\\tess_mri_COR_MPRAGE_RECON-mocoMEMPRAGE_FOV_220-298665.outer_skull.mat') scalp = scipy.io.loadmat('E:\MATLABproject\data\MRI\Visit1_040318\\tess_mri_COR_MPRAGE_RECON-mocoMEMPRAGE_FOV_220-298665.scalp.mat') print(inner_skull.keys()) # Assuming these .mat files contain triangulated surfaces, we will extract vertices and triangles # This might need adjustment based on the actual structure of your .mat files inner_skull_vertices = inner_skull['Vertices'] inner_skull_triangles = inner_skull['Faces'] outer_skull_vertices = outer_skull['Vertices'] outer_skull_triangles = outer_skull['Faces'] scalp_vertices = scalp['Vertices'] scalp_triangles = scalp['Faces'] subjects_dir = 'E:\MATLABproject\data\MRI\Visit1_040318' subject = ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase, k=8)) # Prepare surfaces for MNE # Prepare surfaces for MNE surfs = [ mne.make_bem_model(inner_skull_vertices, inner_skull_triangles, conductivity=[0.01], subjects_dir=subjects_dir), # brain mne.make_bem_model(outer_skull_vertices, outer_skull_triangles, conductivity=[0.016], subjects_dir=subjects_dir), # skull mne.make_bem_model(scalp_vertices, scalp_triangles, conductivity=[0.33], subjects_dir=subjects_dir), # skin ] # Create BEM solution model = make_watershed_bem(surfs) solution = mne.make_bem_solution(model) 时报错: Traceback (most recent call last): File "E:\pythonProject\MEG\头模型.py", line 30, in <module> mne.make_bem_model(inner_skull_vertices, inner_skull_triangles, conductivity=[0.01], subjects_dir=subjects_dir), # brain File "<decorator-gen-68>", line 12, in make_bem_model File "E:\anaconda\envs\pythonProject\lib\site-packages\mne\bem.py", line 712, in make_bem_model subject_dir = op.join(subjects_dir, subject) File "E:\anaconda\envs\pythonProject\lib\ntpath.py", line 117, in join genericpath._check_arg_types('join', path, *paths) File "E:\anaconda\envs\pythonProject\lib\genericpath.py", line 152, in _check_arg_types raise TypeError(f'{funcname}() argument must be str, bytes, or ' TypeError: join() argument must be str, bytes, or os.PathLike object, not 'ndarray' 进程已结束,退出代码1
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值