10-2Cascade.py

000: import cv2
001:
002: def __main():
003: img = cv2.imread(‘DSCF0769.JPG’)
004: img = getResize(img)
005:
006: # ここから処理を実行する
007: img = getHuman(img)
008:
009: cv2.imshow(‘Final result’, img)
010: cv2.waitKey(0)
011: cv2.destroyAllWindows()
012:
013: def getHuman(img):
014: global cascade
015:
016: gray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)
017: bodyRect = cascade.detectMultiScale(image=gray, scaleFactor=1.01,minNeighbors=10)
018: for x, y, w, h in bodyRect:
019: cv2.rectangle(img=img, pt1=(x, y), pt2=(x + w, y + h), color=(0, 255, 255), thickness=3)
020:
021: return img
022:
023: def getResize(img):
024: basePixSize = 1280 # 縦横で大きい辺の変更したいサイズ
025: height = img.shape[0]
026: width = img.shape[1]
027:
028: largeSize = max(height, width) # 大きい方の辺のサイズ
029: resizeRate = basePixSize / largeSize # 変更比率を計算
030: img = cv2.resize(img, (int(width * resizeRate), int(height * resizeRate)))
031:
032: return img
033:
034: if __name__ == ‘__main__’:
035: print(cv2.__version__)
036:
037: cascadeFile = ‘haarcascade_fullbody.xml’
038: cascade = cv2.CascadeClassifier(cascadeFile)
039: __main()