資源簡介
【核心代碼】
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?=?“l(fā)ittle“
????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
評論
共有 條評論