用python能做什么牛逼的程序_你用 Python 写过哪些牛逼的程序/脚本?

import cv2

import sys

import os,random,string

#choices=['Add a name']

import os

current_directory=os.path.dirname(os.path.abspath(__file__))

from Tkinter import Tk

from easygui import *

import numpy as np

x= os.listdir(current_directory)

new_x=[]

testing=[]

for i in x:

if i.find('.')==-1:

new_x+=[i]

else:

testing+=[i]

x=new_x

g=x

choices=['Add a name']+x

y= range(1,len(x)+1)

def get_images_and_labels():

global current_directory,x,y,g

if x==[]:

return (False,False)

image_paths=[]

for i in g:

path=current_directory+''+i

for filename in os.listdir(path):

final_path=path+''+filename

image_paths+=[final_path]

# images will contains face images

images = []

# labels will contains the label that is assigned to the image

labels = []

for image_path in image_paths:

# Read the image and convert to grayscale

img = cv2.imread(image_path,0)

# Convert the image format into numpy array

image = np.array(img, 'uint8')

# Get the label of the image

backslash=image_path.rindex('')

underscore=image_path.index('_',backslash)

nbr = image_path[backslash+1:underscore]

t=g.index(nbr)

nbr=y[t]

# If face is detected, append the face to images and the label to labels

images.append(image)

labels.append(nbr)

#cv2.imshow("Adding faces to traning set...", image)

#cv2.waitKey(50)

# return the images list and labels list

return images, labels

# Perform the tranining

def train_recognizer():

recognizer = cv2.createLBPHFaceRecognizer()

images, labels = get_images_and_labels()

if images==False:

return False

cv2.destroyAllWindows()

recognizer.train(images, np.array(labels))

return recognizer

def get_name(image_path,recognizer):

global x,choices

#if recognizer=='':

# recognizer=train_recognizer()

cascadePath = "haarcascade_frontalface_default.xml"

faceCascade = cv2.CascadeClassifier(cascadePath)

#recognizer=train_recognizer()

x1=testing

global g

print image_path

image = cv2.imread(image_path)

img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

predict_image = np.array(img, 'uint8')

faces = faceCascade.detectMultiScale(

img,

scaleFactor=1.3,

minNeighbors=5,

minSize=(30, 30),

flags = http://cv2.cv.CV_HAAR_SCALE_IMAGE

)

for (x, y, w, h) in faces:

f= image[y:y+w,x:x+h]

cv2.imwrite('temp.jpg',f)

im='temp.jpg'

nbr_predicted, conf = recognizer.predict(predict_image[y: y + h, x: x + w])

predicted_name=g[nbr_predicted-1]

print "{} is Correctly Recognized with confidence {}".format(predicted_name, conf)

if conf>=140:

continue

msg='Is this '+predicted_name

reply = buttonbox(msg, image=im, choices=['Yes','No'])

if reply=='Yes':

reply=predicted_name

directory=current_directory+''+reply

if not os.path.exists(directory):

os.makedirs(directory)

random_name=''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(7))

path=directory+''+random_name+'.jpg'

cv2.imwrite(path,f)

else:

msg = "Who is this?"

reply = buttonbox(msg, image=im, choices=choices)

if reply == 'Add a name':

name=enterbox(msg='Enter the name', title='Training', strip=True)

print name

choices+=[name]

reply=name

directory=current_directory+''+reply

if not os.path.exists(directory):

os.makedirs(directory)

random_name=''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(7))

path=directory+''+random_name+'.jpg'

print path

cv2.imwrite(path,f)

# calculate window position

root = Tk()

pos = int(root.winfo_screenwidth() * 0.5), int(root.winfo_screenheight() * 0.2)

root.withdraw()

WindowPosition = "+%d+%d" % pos

# patch rootWindowPosition

rootWindowPosition = WindowPosition

def detect_faces(img):

global choices,current_directory

imagePath = img

faceCascade = cv2.CascadeClassifier(cascPath)

image = cv2.imread(imagePath)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(

gray,

scaleFactor=1.3,

minNeighbors=5,

minSize=(30, 30),

flags = http://cv2.cv.CV_HAAR_SCALE_IMAGE

)

print "Found {0} faces!".format(len(faces))

m=0

for (x, y, w, h) in faces:

m+=1

padding=0

f= image[y-padding:y+w+padding,x-padding:x+h+padding]

cv2.imwrite('temp.jpg',f)

im='temp.jpg'

msg = "Who is this?"

reply = buttonbox(msg, image=im, choices=choices)

if reply == 'Add a name':

name=enterbox(msg='Enter the name', title='Training', strip=True)

print name

choices+=[name]

reply=name

directory=current_directory+''+reply

if not os.path.exists(directory):

os.makedirs(directory)

random_name=''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(7))

path=directory+''+random_name+'.jpg'

print path

cv2.imwrite(path,f)

def new(img,recognizer):

imagePath = current_directory+''+img

print imagePath

get_name(imagePath,recognizer)

cascPath = 'haarcascade_frontalface_default.xml'

b=0

os.system("change_name.py")

for filename in os.listdir("."):

b+=1

if b%10==0 or b==1:

os.system("change_name.py")

recognizer=train_recognizer()

if filename.endswith('.jpg') or filename.endswith('.png'):

print filename

imagePath=filename

#detect_faces(imagePath)

new(imagePath,recognizer)

os.remove(filename)

raw_input('Done with this photograph')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值