人生苦短,我用python~
import cv2
import os
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import shutil
import csv
def check_landmark():
os.system('cls')
path = os.getcwd()
path = 'dataset\\'
student_id = input("please input the student id:\n")
if student_id == 'q': return
while 1:
frame = input("please input the frame:\n")
if frame == 'q': return
im = cv2.imread(path + 'faces\\' + student_id + '\\' + frame + '.jpg')
print(path + 'faces\\' + student_id + '\\' + frame + '.jpg')
im = im[:, :, (2, 1, 0)]
fig,ax = plt.subplots(figsize=(4, 4))
ax.imshow(im, aspect = 'equal', extent = (0, 255, 255, 0))
lm = np.loadtxt(path + 'landmarks\\' + student_id + '\\' + frame + '.txt', delimiter = ',')
plt.plot(lm[:,0], lm[:,1], '*')
plt.axis('off')
#plt.tight_layout()
#plt.draw()
plt.show()
def remove_0_file_in_dataset():
path = os.getcwd()
path += '\\dataset\\'
for dir in os.listdir(path):
if dir == 'AU':
continue
if '0' in os.listdir(path + dir):
path_of_0_file = path + dir + '\\0'
shutil.rmtree(path_of_0_file)
print(dir + " done")
else:
print(dir + " not exist")
input()
def remove_specific_file():
path = os.getcwd()
path += '\\dataset\\'
student_id = input("please input the student id:\n")
if student_id == 'q': return
while 1:
frame = input("please input the frame:\n")
if frame == 'q': return
for dir in os.listdir(path):
if dir == 'AU':
continue
if frame + '.jpg' in os.listdir(path + dir + '\\' + student_id + '\\'):
path2rmv = path + dir + '\\' + student_id + '\\' + frame + '.jpg'
os.remove(path2rmv)
print('the file ' + path2rmv + " has been remove!")
elif frame + '.txt' in os.listdir(path + dir + '\\' + student_id + '\\'):
path2rmv = path + dir + '\\' + student_id + '\\' + frame + '.txt'
os.remove(path2rmv)
print('the file ' + path2rmv + " has been remove!")
else:
print("the file doesn't exist in " + path + dir + '\\' + student_id + '\\')
def check_reduntant_files():
os.system('cls')
pat_b = input("Please enter the path of your big folder: ")
pat_s = input("Please enter the path to your small folder: ")
root = os.getcwd()
path_b = root + '\\dataset\\' + pat_b
path_s = root + '\\dataset\\' + pat_s
flag = True
for f in os.listdir(path_b):
ff = path_b + '\\' + f
for name in os.listdir(ff):
if os.path.exists(path_s + '\\' + f + '\\' + name)==False:
print('Path:' + pat_b + '>>' + f + '>>' + name + 'is reduntant\n')
flag = False
if flag:
print('The two folders are the same \n')
input()
def calculate_the_files_number():
os.system('cls')
path = os.getcwd()
path += '\\dataset\\'
for f in os.listdir(path):
sum = 0
if f == 'AU':
for ff in os.listdir(path + '\\' + f):
csv_reader = csv.reader(open(path + '\\' + f + '\\' + ff, encoding='utf-8')) # encoding='utf-8' is required !!
tmp = np.array(list(csv_reader)).shape[0] - 1
sum += tmp
print(os.path.splitext(ff)[0] + ':' + str(tmp), end = ' ')
else:
for ff in os.listdir(path + '\\' + f):
if ff == '0':
continue
else:
sum += len(os.listdir(path + '\\' + f + '\\' + ff))
print(ff + ':' + str(len(os.listdir(path + '\\' + f + '\\' + ff))), end = ' ')
print('%12s totally: '%f + str(sum))
input()
if __name__ == '__main__':
while True:
os.system('cls')
string1 = "----Welcome to the auxiliary system!----"
print('\n')
print(string1.center(80))
print(" 1. check landmark")
print(" 2. remove 0 file in dataset")
print(" 3. remove specific files")
print(" 4. check reduntant files")
print(" 5. calculate the files number")
index = input("\nSelect your operation with corresponding number:")
if index == '1':
check_landmark()
if index == '2':
remove_0_file_in_dataset()
if index == '3':
remove_specific_file()
if index == '4':
check_reduntant_files()
if index == '5':
calculate_the_files_number()
if index == 'q':
quit()