2019年7月17日水曜日

mkCascadeDetect.py

import cv2
import glob
import os
import os.path
import re
import numpy as np

ocvIcoDirPath="data/ico"ocvMdlDirPath="data/mdl"ocvRstDirPath="result"ocvTgtDirPath="data/_img/tokyometro/floormap"# ocvTgtDirPath="data/pos/atm.jpg.d"# ocvTgtDirPath="data/pos/baby.jpg.d"# ocvTgtDirPath="data/pos/elv.jpg.d"# ocvTgtDirPath="data/pos/esc.jpg.d"# ocvTgtDirPath="data/pos/001275320-baby.jpg.d"
ocvTgtDirPaths = [
    "data/_img/jreast/floormap",    "data/_img/keikyu/floormap",    "data/_img/tokyometro/floormap",]

# def binary_threshold(img):#     # img = cv2.imread(path)#     grayed = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#     # grayed = img#     under_thresh = 105#     upper_thresh = 145#     maxValue = 255#     th, drop_back = cv2.threshold(grayed, under_thresh, maxValue, cv2.THRESH_BINARY)#     th, clarify_born = cv2.threshold(grayed, upper_thresh, maxValue, cv2.THRESH_BINARY_INV)#     merged = np.minimum(drop_back, clarify_born)#     return merged
# def mask_blue(img):#     # img = cv2.imread(path)#     hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)##     blue_min = np.array([210*180/240, 170*100/240, 200*100/240], np.uint8)#     blue_max = np.array([230*180/240, 190*100/240, 230*100/240], np.uint8)##     blue_region = cv2.inRange(hsv, blue_min, blue_max)#     white = np.full(img.shape, 255, dtype=img.dtype)#     background = cv2.bitwise_and(white, white, mask=blue_region)  # detected blue area becomes white##     inv_mask = cv2.bitwise_not(blue_region)  # make mask for not-blue area#     extracted = cv2.bitwise_and(img, img, mask=inv_mask)##     masked = cv2.add(extracted, background)##     return masked
# def morph(img):#     kernel = np.ones((3, 3),np.uint8)#     opened = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=2)#     return opened
def detectTgtIco(tgtFilePath, icoFilePath):
    if os.name == 'nt':
        os.system("title " + tgtFilePath + " " + icoFilePath)
    icoFileName = re.sub('^.*[/\\\\]', "", icoFilePath)
    tgtFileName = re.sub('^.*[/\\\\]', "", tgtFilePath)
    cascadeXml = str(ocvMdlDirPath) + '/' + icoFileName + '.d/cascade.xml'    detectedFilePath = ocvRstDirPath + "/" + str(tgtFileName) + "." + str(icoFileName) + "." + "jpg"    if os.path.isfile(cascadeXml):
        print("# " + detectedFilePath)
        Cascade = cv2.CascadeClassifier(cascadeXml)
        root, ext = os.path.splitext(tgtFilePath)
        if ext == "gif":
            return        else:
            img = cv2.imread(tgtFilePath, cv2.IMREAD_COLOR)
        # gray = img        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)        # gray = cv2.cvtColor(img, cv2.IMREAD_COLOR)        # gray = mask_blue(gray)        # gray = morph(gray)        # gray = binary_threshold(img)        # cv2.imwrite(detectedFilePath, gray)        point = Cascade.detectMultiScale(gray, 1.1, 3)
        if len(point) > 0:
            print("points: " + str(len(point)))
            for rect in point:
                print(rect)
                (x, y, w, h) = rect
                cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 5)
            cv2.imwrite(detectedFilePath, img)
        # else:        #     print("no detect")
# def icoFile(ocvTgtDirPath, icoFilePath):#     tgtFilePaths = glob.glob(str(ocvTgtDirPath) + "/*")#     for tgtFilePath in tgtFilePaths:#         tgtFilePath = re.sub('[/\\\\]', "/", tgtFilePath)#         tgtFileFile(tgtFilePath, icoFilePath)
# def icoFiles(ocvTgtDirPath):#     icoFilePaths = glob.glob(str(ocvIcoDirPath) + "/*")#     for icoFilePath in icoFilePaths:#         ocvTgtDirPaths.append(icoFilePath)#         icoFilePath = re.sub('[/\\\\]', "/", icoFilePath)#         icoFile(ocvTgtDirPath, icoFilePath)
# def icoFilePaths(ocvTgtFilePath, ocvIcoFilePath):#     icoFilePaths = glob.glob(str(ocvIcoDirPath) + "/*")#     for icoFilePath in icoFilePaths:#         detectTgtIco(tgtFilePath, icoFilePath)
def tgtFilePath(ocvTgtFilePath):
    ocvIcoFilePaths = glob.glob(str(ocvIcoDirPath) + "/*")
    for ocvIcoFilePath in ocvIcoFilePaths:
        detectTgtIco(ocvTgtFilePath, ocvIcoFilePath)

    # for ocvTgtDirPath in tgtFilePaths:    #     icoFiles(ocvTgtFilePath)

# def tgtFilePath(ocvTgtFilePath):#     for ocvTgtDirPath in tgtFilePaths:#         icoFiles(ocvTgtDirPath)
# for tgtFilePath in ocvTgtDirPaths:#     tgtFilePath = re.sub('[/\\\\]', "/", tgtFilePath)#     icoDir(tgtFilePath)

def tgtDirPath(ocvTgtDirPath):
    ocvTgtFilePaths = glob.glob(str(ocvTgtDirPath) + "/*")
    for ocvTgtFilePath in ocvTgtFilePaths:
            tgtFilePath(ocvTgtFilePath)

def tgtDirPaths(ocvTgtDirPaths):
    for ocvTgtDirPath in ocvTgtDirPaths:
        tgtDirPath(ocvTgtDirPath)

tgtDirPaths(ocvTgtDirPaths)

0 件のコメント:

コメントを投稿