Commit 86ab5239 authored by skysky's avatar skysky

complete

parents
This diff is collapsed.
This diff is collapsed.
img/1.jpg

143 KB

img/2.jpg

145 KB

img/3.jpg

674 KB

img/4.jpg

696 KB

img/5.jpg

3.04 MB

img/6.jpg

257 KB

img/7.jpg

14.2 KB

img/8.jpg

209 KB

import cv2
def imgCutting(filepath, magnification):
img = cv2.imread(filepath)
isCutting = False
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
classifier = cv2.CascadeClassifier("haarcascade_frontalcatface_extended.xml")
color = (0, 255, 0) # 定义绘制颜色
faceRects = classifier.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=1, minSize=(75, 75))
if len(faceRects): # 大于0则检测到猫脸
# print ("檢測到貓臉")
isCutting = True
for faceRect in faceRects: # 单独框出每一张猫脸
x, y, w, h = faceRect
centerX = x + 0.5*w
centerY = y + 0.5*h
w = int(w*magnification)
h = int(h*magnification)
x = int(centerX-0.5*w)
y = int(centerY-0.5*h)
if x <0 : x = 0
if y <0 : y = 0
# 框出猫脸
# cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)
cut_img = img[y:y+h, x:x+w]
if isCutting == True:
return cut_img, isCutting
else:
return img, isCutting
import cv2
import os
from imgCutting import imgCutting
filepath = "./img/" #檔案路徑
savepath = "./output/" #儲存路徑
saveName = "cutting_img.jpg" #儲存檔案名稱
magnification = 1.4 #放大倍率
#mode 0:顯示圖片 1:儲存圖片
mode = 1
if ".jpg" in filepath or ".png" in filepath:
img, isCutting = imgCutting(filepath, magnification)
if mode ==0 :
print("是否檢測到貓臉:" + str(isCutting))
cv2.imshow("image", img)
c = cv2.waitKey(10)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
if isCutting == True:
cv2.imwrite(savepath + saveName ,img )
else:
fileList = os.listdir(filepath)
for file in fileList:
singleFilePath = filepath + file
saveName = savepath + file
img, isCutting = imgCutting(singleFilePath, magnification)
if mode ==0 :
print("是否檢測到貓臉:" + str(isCutting))
cv2.imshow("image", img)
c = cv2.waitKey(10)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
if isCutting == True:
cv2.imwrite(saveName ,img )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment