提交 8b8b9e0c 编写于 作者: H hypox64

change find_face model to cnn

上级 e199e83a
......@@ -127,3 +127,9 @@ dmypy.json
# Pyre type checker
.pyre/
# myrules
#all
__pycache__/
tmp/
output/
\ No newline at end of file
import face_recognition
import os
import shutil
import datetime
import concurrent.futures
import cv2
import numpy as np
import random
import string
def random_str(length=12):
"""
Generate secret key from alpha and digit.
:param length: length of secret key.
:return: [length] long secret key.
"""
key = ''
while length:
key += random.choice(string.ascii_letters + string.digits)
length -= 1
return key
def get_bar(percent,num = 25):
# graphs = ' ▏▎▍▋▊▉'
percent = round(percent)
bar = '['
for i in range(num):
if i < round(percent/int((100/num))):
bar += '#'
else:
bar += '-'
bar += ']'
return bar
def lapulase(resImg):
img2gray = cv2.cvtColor(resImg, cv2.COLOR_BGR2GRAY) # 将图片压缩为单通道的灰度图
res = cv2.Laplacian(img2gray, cv2.CV_64F)
score = res.var()
return score
def resize(img,size):
h, w = img.shape[:2]
if min(h, w) ==size:
return img
if w >= h:
res = cv2.resize(img,(int(size*w/h), size),interpolation=cv2.INTER_LANCZOS4)
else:
res = cv2.resize(img,(size, int(size*h/w)),interpolation=cv2.INTER_LANCZOS4)
return res
def Traversal(filedir):
file_list=[]
for root,dirs,files in os.walk(filedir):
for file in files:
file_list.append(os.path.join(root,file))
for dir in dirs:
Traversal(dir)
return file_list
def is_img(ext):
ext = ext.lower()
if ext in ['.jpg','.png','.jpeg','.bmp']:
return True
else:
return False
def picture_select(file_list):
imgpath_list=[]
for pic in file_list:
if is_img(os.path.splitext(pic)[1]):
imgpath_list.append(pic)
return imgpath_list
def del_noface_image(input_path):
try:
image = cv2.imread(input_path)
h,w = image.shape[:2]
face_locations = face_recognition.face_locations(image)
if len(face_locations)>0 and lapulase(image)>Del_Blur_Score:
return 0
else:
try:
os.remove(input_path)
return 1
except Exception as e:
return 0
except Exception as e:
try:
os.remove(input_path)
return 1
except Exception as e:
return 0
filedir = (input("filedir:").strip()).replace("'","")
WORKERS = int((input("cpu_workers(defult=4):").strip()).replace("'",""))
Del_Blur_Score = 50
file_list = Traversal(filedir)
imgpath_list = picture_select(file_list)
random.shuffle(imgpath_list)
all_length = len(imgpath_list)
print("Find picture:"+" "+str(all_length))
print('Begining......')
print('Finished/del images % Bar Usedtime/Totaltime')
starttime = datetime.datetime.now()
del_cnt=0
with concurrent.futures.ProcessPoolExecutor(max_workers=WORKERS) as executor:
for i,imgpath,count in zip(range(1,len(imgpath_list)+1),
imgpath_list,
executor.map(del_noface_image,imgpath_list)):
del_cnt+=count
if i%100==0:
endtime = datetime.datetime.now()
used_time = (endtime-starttime).seconds
percent = round(100*i/all_length,1)
print('\r','',str(i)+'/'+str(del_cnt)+' ',
str(percent)+'%'+get_bar(percent,30),
' '+str(int(used_time))+'s/'+str(int(used_time/i*all_length))+'s',end= " ")
print('\nFinished!','Finall del image:',del_cnt,' Cost time:',(datetime.datetime.now()-starttime).seconds,'s')
\ No newline at end of file
......@@ -40,14 +40,14 @@ def lapulase(resImg):
score = res.var()
return score
def resize(img,size):
def resize(img,size,interpolation=cv2.INTER_LINEAR):
h, w = img.shape[:2]
if min(h, w) ==size:
if np.min((w,h)) ==size:
return img
if w >= h:
res = cv2.resize(img,(int(size*w/h), size),interpolation=cv2.INTER_LANCZOS4)
res = cv2.resize(img,(int(size*w/h), size),interpolation=interpolation)
else:
res = cv2.resize(img,(size, int(size*h/w)),interpolation=cv2.INTER_LANCZOS4)
res = cv2.resize(img,(size, int(size*h/w)),interpolation=interpolation)
return res
def Traversal(filedir):
......@@ -77,44 +77,56 @@ def find_save_resize_face(input_path):
try:
filename,extension = os.path.splitext(os.path.split(input_path)[1])
# image = face_recognition.load_image_file(input_path)
image = cv2.imread(input_path)
h,w = image.shape[:2]
#print(image.dtype)
# Find all the faces in the image and print
face_locations = face_recognition.face_locations(image)
# print("found {} face(s) in this photograph.".format(len(face_locations)))
origin_image = cv2.imread(input_path)
h,w = origin_image.shape[:2]
mask = np.zeros(origin_image.shape[:2],dtype = "uint8")
rat = min(origin_image.shape[:2])/LOADSIZE
image = resize(origin_image, LOADSIZE,interpolation = cv2.INTER_AREA)
face_locations = face_recognition.face_locations(image,number_of_times_to_upsample=1,model=MODEL)
count=0
mask_count = 0
for face_location in face_locations:
# Print the location of each face in this image
top, right, bottom, left = face_location
#print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# You can access the actual face itself like this:
top, right, bottom, left = int(top*rat), int(right*rat), int(bottom*rat), int(left*rat)
# EX
if IS_random_EXTEND:
ex=int(((EXTEND-1+0.2*random.random())*(bottom-top))/2)
else:
ex=int(((EXTEND-1)*(bottom-top))/2)
# ex_face=int((1*(bottom-top))/2)
if ((bottom-top)>MINSIZE) and 0.95<abs((bottom-top)/(left-right))<1.05 and (top-ex)>0 and (bottom+ex)<h and (left-ex)>0 and (right+ex)<w:
face = origin_image[top-ex:bottom+ex, left-ex:right+ex]
face = cv2.resize(face, (512,512),interpolation=cv2.INTER_LANCZOS4)
# cv2.imwrite(os.path.join(outdir_face,outname+filename+'_'+str(count)+'.jpg'),face)
if lapulase(face)>Del_Blur_Score:
#print(os.path.join(outdir_face,random_str()+'.jpg'))
cv2.imwrite(os.path.join(outdir_face,random_str()+'.jpg'),face)
count = count+1
#mask = cv2.rectangle(mask,(left-ex,top-ex),(right+ex,bottom+ex),255,-1)
#mask
# if count > 0:
# mask = resize(mask,256)
# origin_image = resize(origin_image,1024)
# cv2.imwrite(os.path.join(outdir_ori,outname+filename+'.jpg'),origin_image)
# cv2.imwrite(os.path.join(outdir_mask,outname+filename+'.png'),mask)
# print(output_path)
if SAVE_MASK:
try:
if MASK_TYPE=='contour':
_ex = int((bottom - top)*0.5)
face_get_landmark = origin_image[top-_ex:bottom+_ex, left-_ex:right+_ex]
face_landmark = face_recognition.face_landmarks(face_get_landmark)[0]
face_point = []
face_point = face_point+face_landmark['left_eyebrow']+face_landmark['right_eyebrow']+face_landmark['chin'][::-1]
face_point = np.array(face_point) + np.array([left-_ex,top-_ex])
face_point[:10] = face_point[:10] - np.array([0,int((bottom-top)*HIGH_MASK)])
mask = cv2.fillPoly(mask,[face_point],(255))
elif MASK_TYPE=='rect':
_ex = int((bottom - top)*0.05)
mask = cv2.rectangle(mask,(int(left-_ex*0.5),top-_ex),(int(right+_ex*0.5),bottom+_ex),(255),-1)
mask_count += 1
except Exception as e:
pass
if SAVE_MASK:
if count == mask_count and count > 0:
mask = resize(mask,512,interpolation = cv2.INTER_AREA)
origin_image = resize(origin_image,512,interpolation = cv2.INTER_AREA)
cv2.imwrite(os.path.join(outdir_ori,outname+filename+'.jpg'),origin_image)
cv2.imwrite(os.path.join(outdir_mask,outname+filename+'.png'),mask)
return count
except Exception as e:
......@@ -129,10 +141,13 @@ WORKERS = int((input("cpu_workers(defult=4):").strip()).replace("'",""))
# EXTEND=1.4
EXTEND = 1.6
Del_Blur_Score = 50
Del_Blur_Score = 20 # normal-> 20 | clear -> recommed 50
IS_random_EXTEND = False
# WORKERS = 4
# MINSIZE = 256
MODEL = 'cnn' # 'hog' | 'cnn'
SAVE_MASK = True
MASK_TYPE = 'rect' # rect | contour
HIGH_MASK = 0.2 # more vertical mask
LOADSIZE = 512 # load to this size and process
outdir='./output/'+outname
......
import numpy
import cv2
import os
img_dir = '/media/hypo/Hypo_U/桂林一调/理综'
out_dir = os.path.join(img_dir,'threshold')
THR = 12
img_names = os.listdir(img_dir)
if not os.path.isdir(out_dir):
os.makedirs(out_dir)
for img_name in img_names:
img = cv2.imread(os.path.join(img_dir,img_name), 0)
img = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,21,THR)
cv2.imwrite(os.path.join(out_dir,img_name), img)
[toc]
[TOC]
### 安装显卡驱动
```bash
#卸载旧的驱动
sudo apt-get purge nvidia*
sudo apt-get autoremove #这个命令有时候不用也可以
#
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
ubuntu-drivers devices
sudo apt install nvidia-430
```
### 挂载U盘
```bash
root@lthpc:/home/hypo# fdisk -l
......@@ -8,12 +19,29 @@ Device Start End Sectors Size Type
root@lthpc:/home/hypo# cd /media
root@lthpc:/media# mkdir usb
root@lthpc:/media# mount -t ntfs-3g /dev/sdc1 /media/usb
#卸载挂载点
$ umount /dev/hda2
$ umount /usr
#参数可以是设备文件或安装点
```
### shell 中运行基本应用
```bash
nautilus #文件管理器
firefox #火狐浏览器
```
### 批量杀死应用
```bash
ps aux|grep python|grep -v grep|cut -c 9-15|xargs kill -15
# 管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。下面说说用管道符联接起来的
#几个命令:
#“ps aux”是linux 里查看所有进程的命令。这时检索出的进程将作为下一条命令“grep python”的输入。
#“grep python”的输出结果是,所有含有关键字“python”的进程,这是python程序
#“grep -v grep”是在列出的进程中去除含有关键字“grep”的进程。
#“cut -c 9-15”是截取输入行的第9个字符到第15个字符,而这正好是进程号PID。
#“xargs kill -15”中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -15”命令的参数,并执行该令。
#“kill -15”会正常退出指定进程,-9强行杀掉
```
### 常用系统命令
* 删除文件
```bash
......@@ -51,7 +79,7 @@ dpkg -L <package> #列出 <package> 安装的所有文件清单。同时请看 d
dpkg -s <package> #显示已安装包裹的信息。同时请看 apt-cache 显示 Debian 存档中的包裹信息,以及 dpkg -I 来显示从一个 .deb 文件中提取的包裹信息。
dpkg-reconfigure <package> #重新配制一个已经安装的包裹,如果它使用的是 debconf (debconf 为包裹安装提供了一个统一的配制界面)。
```
* 调整CPU性能模式
### 调整CPU性能模式
```bash
sudo apt-get install cpufrequtils
cpufreq-info
......@@ -60,6 +88,16 @@ sudo cpufreq-set -g performance
sudo apt-get install sysfsutils
sudo gedit /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
```
### 添加或禁用开机启动项
```bash
#禁用开机启动项
$ systemctl list-unit-files --type=service|grep enabled #查看开机启动的服务
$ sudo systemctl disable apache2.service #禁用掉该服务
#添加开机启动项
$ systemctl list-unit-files --type=service|grep team
teamviewerd.service enabled
$ sudo systemctl enable teamviewerd.service
```
### 压缩解压
```bash
tar xvf filename.tar #.tar 解包
......@@ -128,6 +166,7 @@ firefox &
```bash
#压缩+解压流传输 把本地的文件复制到远程主机上
tar -c './dir' |pigz |ssh hypo@172.31.73.116 "gzip -d|tar -xC /home/hypo/MyProject"
tar -c './DeepMosaics' |pigz |ssh hypo@172.30.194.156 "gzip -d|tar -xC /media/hypo/Project/MyProject/DeepMosaics"
#把远程的文件复制到本地
scp root@www.test.com:/val/test/test.tar.gz /val/test/test.tar.gz
......
[toc]
[TOC]
# Python
## base
### main
......@@ -265,7 +265,23 @@ cv2.IMREAD_UNCHANGED:加载图像,包括alpha通道
您可以简单地分别传递整数1,0或-1,而不是这三个标志。
'''
```
### resize
```python
#CV2.INTER_NEAREST      最邻近插值点法
#CV2.NTER_LINEAR         双线性插值法
#CV2.INTER_AREA             邻域像素再取样插补
#CV2.INTER_CUBIC           双立方插补,4*4大小的补点
#CV2.INTER_LANCZOS4 8x8像素邻域的Lanczos插值
#當我們縮小影像時,使用CV_INTER_AREA會有比較好的效果,當我們放大影像,CV_INTER_CUBIC會有最好的效果,但是計算花費時間較多,CV_INTER_LINEAR能在影像品質和花費時間上取得不錯的平衡。 CV_INTER_LANCZOS4    Lanczos插补,8*8大小的补点
```
### imshow
```python
cv2.imshow('image',img)
'''
......
......@@ -38,7 +38,12 @@ def get_cpu_freq():
# Cpu freq
def get_cpu_temp():
temp_str = os.popen('cat /sys/class/hwmon/hwmon0/device/hwmon/hwmon0/temp1_input').read()
if os.path.isfile('/sys/class/hwmon/hwmon0/device/hwmon/hwmon0/temp1_input'):
temp_str = os.popen('cat /sys/class/hwmon/hwmon0/device/hwmon/hwmon0/temp1_input').read()
elif os.path.isfile('/sys/class/hwmon/hwmon0/device/hwmon0/temp1_input'):
temp_str = os.popen('cat /sys/class/hwmon/hwmon0/device/hwmon0/temp1_input').read()
else:
return -1
return (float(temp_str)/1000)
# Mem
......@@ -71,9 +76,10 @@ def get_swap_use():
###############################################################
gpus_str = os.popen('nvidia-smi -L').read()
gpus =[]
while gpus_str.find('\n') != -1:
gpus.append(gpus_str[gpus_str.find(':')+2:gpus_str.find('(')-1])
gpus_str=gpus_str[gpus_str.find('\n')+1:]
if 'communicate with the NVIDIA driver' not in gpus_str:
while gpus_str.find('\n') != -1:
gpus.append(gpus_str[gpus_str.find(':')+2:gpus_str.find('(')-1])
gpus_str=gpus_str[gpus_str.find('\n')+1:]
def get_gpu_use():
......@@ -85,7 +91,10 @@ def get_gpu_use():
infos_str = infos_str[:infos_str.find('\n')+1]
infos_str = infos_str.split()
#['|', '50%', '42C', 'P0', '19W', '/', '75W', '|', '929MiB', '/', '5050MiB', '|', '14%', 'Default', '|']
fan = int(infos_str[1].replace('%','')) # %
if infos_str[1].replace('%','') == 'N/A':
fan = -1
else:
fan = int(infos_str[1].replace('%','')) # %
temp = int(infos_str[2].replace('C','')) # C
if infos_str[4] == 'N/A':
power_used = -1
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册