资源简介
【核心代码】
faces = face_cascade.detectMultiScale(img, 1.1,5)
if len(faces):
for (x, y, w, h) in faces:
if w>=20 and h>=20:
listStr = [str(int(time.time())), str(count)]
fileName = ''.join(listStr)
X = int(x)
W = min(int(x w),img.shape[1])
Y = int(y)
H = min(int(y h),img.shape[0])
f = cv2.resize(img[Y:H, X:W], (W-X,H-Y))
cv2.imwrite(targetPath os.sep '%s.jpg' % fileName, f)
count = 1
print (imagePath " have face")
代码片段和文件信息
# -*- coding: utf-8 -*-
import os
import cv2
import time
import base64
def getAllPath(dirpath *suffix):
PathArray = []
for r ds fs in os.walk(dirpath):
for fn in fs:
if os.path.splitext(fn)[1] in suffix:
fname = os.path.join(r fn)
PathArray.append(fname)
return PathArray
def readPicSaveFace_1(sourcePathtargetPathinvalidPath*suffix):
try:
ImagePaths=getAllPath(sourcePath *suffix)
print(ImagePaths)
count = 1
face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_alt.xml‘)
for imagePath in ImagePaths:
try:
img = cv2.imread(imagePath)
if type(img) != str:
faces = face_cascade.detectMultiScale(img 1.15)
if len(faces):
for (x y w h) in faces:
if w>=20 and h>=20:
listStr = [str(int(time.time())) str(count)]
fileName = ‘‘.join(listStr)
X = int(x)
W = min(int(x + w)img.shape[1])
Y = int(y)
H = min(int(y + h)img.shape[0])
f = cv2.resize(img[Y:H X:W] (W-XH-Y))
cv2.imwrite(targetPath+os.sep+‘%s.jpg‘ % fileName f)
count += 1
print (imagePath + “ have face“)
except:
continue
except IOError:
print (“Error“)
else:
print (‘Find ‘+str(count-1)+‘ faces to Destination ‘+targetPath)
def getFace(imagePath):
face_cascade = cv2.CascadeClassifier()
try:
img = cv2.imread(imagePath)
if type(img) != str:
faces = face_cascade.detectMultiScale(img 1.1 5)
if len(faces):
for (x y w h) in faces:
if w>=20 and h>=20:
X = int(x)
W = min(int(x + w)img.shape[1])
Y = int(y)
H = min(int(y + h)img.shape[0])
f = cv2.resize(img[Y:H X:W] (W-XH-Y))
print(image_to_base64(f))
except:
pass
def image_to_base64(image_np):
image = cv2.imencode(‘.jpg‘ image_np)[1]
image = str(base64.b64encode(image))[2:-1]
return image
if __name__ == ‘__main__‘:
invalidPath = “test“
sourcePath = “big“
targetPath1 = “little“
readPicSaveFace_1(sourcePathtargetPath1invalidPath‘.jpg‘‘.jpeg‘‘.JPEG‘‘.JPG‘‘png‘‘PNG‘)
# img = cv2.imread(“C:\\Users\\intel\\Pictures\\big\\2.jpeg“)
# if type(img) != str:
# x = 1323
# y = 313
# w = 54
# h = 68
# listStr = [str(int(time.time())
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 124177 2020-12-10 17:55 big\timg.jpeg
文件 3347 2020-12-14 14:23 openCvTest.py
目录 0 2020-12-14 14:22 big
目录 0 2020-12-14 14:22 test
目录 0 2020-12-14 14:23 little
----------- --------- ---------- ----- ----
127524 5
相关资源
- 12306抢票代码(基于python2)
- python简单爬虫
- python 画黄花代码(基于Turtle)
- python 常用方法实现()
- python 经典款打飞机
- 知网爬虫软件(python)
- python实现一元线性回归.py
- python3环境搭建教程.ppt
- python合并多个mp4视频文件成一个mp4文
- 中山大学-自然语言处理-中文分词项目
- Python其它开发工具的安装与使用.ppt
- Computer Vision with Python 3
- python入门全套PPT
- a*算法的python版
- python爬虫爬取微博热搜
- python爬虫爬取旅游信息(附源码,c
- python爬虫爬取豆瓣电影信息
- 网页遥控小车 Python web (基于RPi.GPI
- python 采集360的图片地址到文本文件
- Python简单小游戏 五子棋
- python基础题库(附答案).docx(共54页
- Python RC4算法
- python实现的日历
- Python源代码:以web方式管理自己的常
- 基于Python实现的简易画气球
- python 井字棋 游戏源码
- 《Python从小白到大牛》源代码
- Python爬虫实战入门教程
- 机器学习numpy和pandas基础
- Python 3 Web Development. Beginners Guide
评论
共有 条评论