图像批处理脚本

作者: 少寨主的互联网洞察 | 来源:发表于2018-03-28 15:56 被阅读87次

以前在做图像识别处理算法实验的时候,基本都是使用别人处理好的数据集,今天想自己处理一些图像数据,于是自己写了一个小脚本程序(有一些冗余代码),主程序如下所示,其中需要opencv的一个人脸识别文件

haarcascade_frontalface_default.xml

需要用到的同学请自行寻找opencv源码中该文件
其中主要参考文章如下:

https://blog.csdn.net/Small_Mouse0/article/details/53995548

以下是我的代码:

#!/usr/bin/python
#coding=utf-8
''' face detect
https://github.com/seathiefwang/FaceRecognition-tensorflow
http://tumumu.cn/2017/05/02/deep-learning-face/
'''
# pylint: disable=invalid-name
import os
import random
import numpy as np
import cv2
import time

def createdir(*args):
    ''' create dir'''
    for item in args:
        if not os.path.exists(item):
            os.makedirs(item)

IMGSIZE = 64


def getpaddingSize(shape):
    ''' get size to make image to be a square rect '''
    h, w = shape
    longest = max(h, w)
    result = (np.array([longest]*4, int) - np.array([h, h, w, w], int)) // 2
    return result.tolist()

def dealwithimage(img, h=64, w=64):
    ''' dealwithimage '''
    #img = cv2.imread(imgpath)
    top, bottom, left, right = getpaddingSize(img.shape[0:2])
    img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[0, 0, 0])
    img = cv2.resize(img, (h, w))
    return img

def relight(imgsrc, alpha=1, bias=0):
    '''relight'''
    imgsrc = imgsrc.astype(float)
    imgsrc = imgsrc * alpha + bias
    imgsrc[imgsrc < 0] = 0
    imgsrc[imgsrc > 255] = 255
    imgsrc = imgsrc.astype(np.uint8)
    return imgsrc

def getfacefromcamera(indir,outdir):
    createdir(outdir)
    #camera = cv2.VideoCapture(0)
    haar = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    n = 1
    while 1:
        if (n <= 5):
            print('It`s processing %s image.' % n)
            # 读帧
            #success, img = camera.read()
            for file in os.listdir(indir):
                singlefilename=indir+"/"+file
                if(singlefilename.endswith('.png')):
                    img =cv2.imread(singlefilename)
                    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                    faces = haar.detectMultiScale(gray_img, 1.3, 5)
                    for f_x, f_y, f_w, f_h in faces:
                        face = img[f_y:f_y+f_h, f_x:f_x+f_w]
                        face = cv2.resize(face, (IMGSIZE, IMGSIZE))
                        cv2.imwrite(os.path.join(outdir, str(n)+'.png'), face)
                        img = cv2.rectangle(img, (f_x, f_y), (f_x + f_w, f_y + f_h), (255, 0, 0), 2)
                        n+=1
                    cv2.imshow('img', img)
                    time.sleep(2)
                    key = cv2.waitKey(30) & 0xff
                    if key == 27:
                        break
        else:
            break
if __name__ == '__main__':
    #name = input('please input yourename: ')
    getfacefromcamera('C:/Users/AUGUSTRUSH/Desktop/tangwei','C:/Users/AUGUSTRUSH/Desktop/tangwei/new1')


处理完效果如下图所示:


TIM截图20180328160036.png

相关文章

  • 图像批处理脚本

    以前在做图像识别处理算法实验的时候,基本都是使用别人处理好的数据集,今天想自己处理一些图像数据,于是自己写了一个小...

  • 第一节 运用批处理使程序在windows服务器下后台运行

    引言: 批处理(Batch),也称为批处理脚本[https://baike.baidu.com/item/%E8...

  • 关于CMD

    批处理 是一种简化的脚本语言 称作 宏 类似于 Unix中的Shell脚本 批处理 具有.bat或者.cmd的扩展...

  • 向大鱼致敬 (多图)

    Refs 大鱼 (豆瓣) 批处理图片用到的脚本:

  • 打包项目脚本

    1.批处理打包项目(war包) 2.分享一个批处理打war包脚本

  • 批处理脚本

    shell脚本和批处理,通常就是linux和windows下进行的一些简单脚本执行任务的途径。对于linux来说,...

  • Linux Shell 脚本编写学习

    shell脚本 Shell Script,Shell脚本与Windows/Dos下的批处理]相似,也就是用各类命令...

  • 批处理编写入门

    1.1、批处理作用 自上而下成批的处理每一条命令,直到执行最后一条! 一般批处理也叫脚本,如何该脚本实现的破坏功能...

  • linux扩展名表示

    *.sh : 脚本戒批处理文件 (scripts),因为批处理文件为使用 shell 写成的,所以扩展名就编成 ....

  • Windows批处理(一)

    一、概念批处理,也称为批处理脚本,英文译为BATCH,批处理文件后缀BAT就取的前三个字母。它的构成没有固定格式,...

网友评论

    本文标题:图像批处理脚本

    本文链接:https://www.haomeiwen.com/subject/xnxucftx.html