Esempio n. 1
0
class TestMotionDetect():

	framecnt = 0
	filePath = path+'/testcase/motionDetect/IMG_9083_small.m4v'

	imgReader = ImageReader(filePath, True)
	md = MotionDetector(0.4)

	(xmin, ymin, xmax, ymax) = (286, 98, 973, 561)
	while True: 
		(ret, frame, fname) = imgReader.read()
		if ret == False:
			break;
		framecnt = framecnt + 1

		#cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
		#key = basics.showResizeImg(frame, filePath, 1, width=800, height=600)

		roi = frame[ymin:ymax, xmin:xmax]
		key = basics.showResizeImg(roi, 'roi', 1, width=800, height=600)
		roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
		roi = basics.blur_img(roi, 'gussian', (7,7))
		#key = basics.showResizeImg(roi, 'roi', 1, x=0, width=800)
		md.update(roi)
		if (framecnt > 16):
			result = md.detect(roi)
			if (result == None):
				continue
			(diff, cnts) = result
			key = basics.showResizeImg(diff, 'diff', 0, x=800, width=400)


		if key == ord('q'):
			break
Esempio n. 2
0
def TestFrameDetectByDiffImages():

    img0 = cv2.imread(path + '/testcase/frameDetect/' +
                      'mainMenu_channel_focus.png')
    img1 = cv2.imread(path + '/testcase/frameDetect/' +
                      'mainMenu_channel_unfocus.png')
    img0Origin = img0.copy()
    img0 = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)
    img0 = basics.threshold_img(img0, 'OTSU', False)
    img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    img1 = basics.threshold_img(img1, 'OTSU', False)

    (rtn, (x, y, w, h)) = FrameDetectByDiffImages(img0Origin,
                                                  img0,
                                                  img1,
                                                  minW=200,
                                                  minH=60,
                                                  frameRatio=0.85)
    if rtn == True:
        print('diff frame detected {}'.format((x, y, w, h)))
        ch_focus_img = cv2.imread(path + '/testcase/frameDetect/' +
                                  'mainMenu_channel_focus.png')
        cv2.rectangle(ch_focus_img, (x, y), (x + w, y + h), (255, 255, 255), 3)
        basics.showResizeImg(ch_focus_img, 'result', 0, width=0)
    else:
        print('frame not detected')
Esempio n. 3
0
def TestFrameDetect(bExtract=False):

    img0 = cv2.imread(path + '/testcase/frameDetect/' + 'channel_00.png')
    img0Origin = img0.copy()
    img0 = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)
    (rtn, (x, y, w, h)) = FrameDetectByOneImage(img0Origin,
                                                img0,
                                                minW=200,
                                                minH=60,
                                                frameRatio=0.85)
    if rtn == True:
        print('frame detected {}'.format((x, y, w, h)))
        ch_focus_img = cv2.imread(path + '/testcase/frameDetect/' +
                                  'channel_00.png')
        cv2.rectangle(ch_focus_img, (x, y), (x + w, y + h), (255, 255, 255), 3)
        basics.showResizeImg(ch_focus_img, 'result', 0, width=0)
    else:
        print('frame not detected')
Esempio n. 4
0
    def testSlidingWindowHOGMatch(self):

        return

        test_img = [
            ('img77.jpg', 'title_pic_foc.png', 0.7, 1, (0, 0, 0, 0), (578, 168,
                                                                      80, 74)),
            #('img77.jpg', 'title_pic_audio_foc.png', 0.7, 1, (0, 0, 0, 0), (578, 168, 80, 74)),
            #('img77.jpg', 'title_pic_app_foc.png', 0.7, 1, (0, 0, 0, 0), (578, 168, 80, 74)),
            #('img77.jpg', 'title_pic_sx_foc.png', 0.7, 1, (0, 0, 0, 0), (578, 168, 80, 74)),
            #('img77.jpg', 'title_pic_setup_foc.png', 0.7, 1, (0, 0, 0, 0), (578, 168, 80, 74)),
            #('img77.jpg', 'template0.png', 0.66, 1, (0, 0, 0, 0), (324, 152, 61, 73)),
            #('img77.jpg', 'template1.png', 0.66, 1, (0, 0, 0, 0), (324, 152, 61, 73)),
            #('img77.jpg', 'template2.png', 0.66, 1, (0, 0, 0, 0), (324, 152, 61, 73)),
            #('img77.jpg', 'template3.png', 0.66, 1, (0, 0, 0, 0), (324, 152, 61, 73)),
            #('img77.jpg', 'template4.png', 0.66, 1, (0, 0, 0, 0), (324, 152, 61, 73)),
            #('img77.jpg', 'template5.png', 0.66, 1, (0, 0, 0, 0), (324, 152, 61, 73)),
            #('img77.jpg', 'template6.png', 0.66, 1, (0, 0, 0, 0), (324, 152, 61, 73)),
        ]
        for (targetName, templateName, targetRatio, templateRatio,
             searchRegion, realLoc) in test_img:
            targetImg = cv2.imread(path + '/testcase/searchSlidingWindowHOG/' +
                                   targetName)
            (targetH, targetW) = targetImg.shape[:2]
            targetImg = basics.resizeImg(targetImg, int(targetW * targetRatio),
                                         int(targetH * targetRatio))
            targetImgColor = targetImg.copy()
            targetImg = cv2.cvtColor(targetImg, cv2.COLOR_BGR2GRAY)
            targetImg = basics.blur_img(targetImg, 'gussian', (5, 5))
            templateImg = cv2.imread(path +
                                     '/testcase/searchSlidingWindowHOG/' +
                                     templateName)
            templateImg = cv2.cvtColor(templateImg, cv2.COLOR_BGR2GRAY)
            templateImg = basics.resizeImg(
                templateImg, int(templateImg.shape[1] * templateRatio),
                int(templateImg.shape[0] * templateRatio))
            hogParam = HOGParam(orientations=9,
                                pixels_per_cell=(8, 8),
                                cells_per_block=(2, 2),
                                transform_sqrt=True,
                                block_norm="L2")
            templateAR = float(templateImg.shape[1]) / float(
                templateImg.shape[0])
            stepSize = (10, int(10 / templateAR))
            (bFound, val, (x, y, w, h)) = searchImageByHOG(templateImg,
                                                           targetImg,
                                                           searchRegion,
                                                           0.3,
                                                           hogParam,
                                                           stepSize,
                                                           bVisualize=False)
            print(
                'testSlidingWindowHOGMatch search {} in {} - val = {}, loc = {}'
                .format(templateName, targetName, val, (x, y, w, h)))
            cv2.rectangle(targetImgColor, (x, y), (x + w, y + h), (0, 255, 0),
                          2)
            key = basics.showResizeImg(targetImgColor, 'targe', 0, 0, 200)
Esempio n. 5
0
def searchImageByMatchTemplate(template,
                               target,
                               searchRegion,
                               threshold,
                               bVisualize=False):

    (x0, y0, x1, y1) = searchRegion
    if (x0, y0, x1, y1) == (0, 0, 0, 0):
        x1 = target.shape[1]
        y1 = target.shape[0]
    target = target[y0:y1, x0:x1]

    (templateH, templateW) = template.shape[:2]
    (tH, tW) = target.shape[:2]

    bFound = False

    edged = cv2.Canny(target, 50, 200)
    try:
        result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF_NORMED)
        (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
    except:
        print('target= ', (tW, tH))
        print('Template= ', (templateW, templateH))
        return (False, 0, (0, 0, 0, 0))

    r = 1
    if maxVal > threshold:
        #print('found: maxval = ', maxVal, 'r=', r)
        bFound = True

    (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
    (endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))

    crop_W = int(templateW * r)
    crop_H = int(templateH * r)

    if bVisualize == True:
        cv2.rectangle(scaledTarget, (x, y), (x + w - 1, y + h - 1),
                      (255, 0, 0), 5)
        basics.showResizeImg(scaledTarget, targetName, 0, 0, 0, 0, 0)

    return (bFound, maxVal, (startX, startY, crop_W, crop_H))
Esempio n. 6
0
class TestImgReader():
	filePath = path+'/testcase/imgReader'

	imgReader = ImageReader(filePath, False)

	while True: 
		(ret, frame, fname) = imgReader.read()
		if ret == False:
			break;
		print('fname = {}'.format(fname))

	videoReader = ImageReader('webcam', True)
	while True: 
		(ret, frame, fname) = videoReader.read()
		if ret == False:
			break;
		key = basics.showResizeImg(frame, 'webcam', 1)
		if key == ord('q'):
			break
Esempio n. 7
0
def searchImageByHOG(template,
                     target,
                     searchRegion,
                     threshold,
                     hogParam,
                     SearchStep,
                     bVisualize=False):

    (x0, y0, x1, y1) = searchRegion
    if (x0, y0, x1, y1) == (0, 0, 0, 0):
        x1 = target.shape[1]
        y1 = target.shape[0]
    target = target[y0:y1, x0:x1]

    (templateH, templateW) = template.shape[:2]
    (tH, tW) = target.shape[:2]

    print('hogParam = {}'.format(hogParam))
    (hogTemplate,
     hogImage) = feature.hog(template,
                             orientations=hogParam.orientations,
                             pixels_per_cell=hogParam.pixels_per_cell,
                             cells_per_block=hogParam.cells_per_block,
                             transform_sqrt=hogParam.transform_sqrt,
                             block_norm=hogParam.block_norm,
                             visualise=True)

    if (bVisualize == True):
        hogImage = exposure.rescale_intensity(hogImage, out_range=(0, 255))
        hogImage = hogImage.astype("uint8")
        basics.showResizeImg(hogImage, 'template HOG', 1, 0, 0, 0, 0)

    minDist = 999.0
    for (x, y, window) in sliding_window(target,
                                         SearchStep,
                                         windowSize=(templateW, templateH)):
        clone = target.copy()

        winImg = target[y:y + templateH, x:x + templateW]
        if (y + templateH > tH) or (x + templateW > tW):
            continue

        if bVisualize == True:
            (hogWindow,
             hogImage) = feature.hog(winImg,
                                     orientations=hogParam.orientations,
                                     pixels_per_cell=hogParam.pixels_per_cell,
                                     cells_per_block=hogParam.cells_per_block,
                                     transform_sqrt=hogParam.transform_sqrt,
                                     block_norm=hogParam.block_norm,
                                     visualise=True)
        else:
            hogWindow = feature.hog(winImg,
                                    orientations=hogParam.orientations,
                                    pixels_per_cell=hogParam.pixels_per_cell,
                                    cells_per_block=hogParam.cells_per_block,
                                    transform_sqrt=hogParam.transform_sqrt,
                                    block_norm=hogParam.block_norm,
                                    visualise=False)

        if (bVisualize == True):
            hogImage = exposure.rescale_intensity(hogImage, out_range=(0, 255))
            hogImage = hogImage.astype("uint8")
            basics.showResizeImg(hogImage, 'window HOG', 1, 500, 0, 0, 0)
            cv2.rectangle(clone, (x, y), (x + templateW, y + templateH),
                          (0, 255, 0), 2)
            key = basics.showResizeImg(clone, 'targe', 1, 0, 200)
        else:
            key = ord(' ')
        if (len(hogTemplate) != len(hogWindow)):
            continue
        dist = distance.euclidean(hogTemplate, hogWindow)
        if (minDist > dist):
            minDist = dist
            minLoc = [x, y, x + templateW, y + templateH]
        if (key == ord('q')):
            break

    print('minDist = {}, minLoc = {}'.format(minDist, minLoc))
    if (bVisualize == True):
        clone = target.copy()
        cv2.rectangle(clone, (minLoc[0], minLoc[1]), (minLoc[2], minLoc[3]),
                      (0, 255, 0), 2)
        key = basics.showResizeImg(clone, 'targe', 0, 0, 200)

    return (True, minDist, (minLoc[0], minLoc[1], templateW, templateH))
Esempio n. 8
0
def main():

    ap = argparse.ArgumentParser()
    ap.add_argument("-v",
                    "--video",
                    required=True,
                    help="Path to the video file")
    ap.add_argument("-m",
                    "--main",
                    required=True,
                    help="json file main menu configuration")
    ap.add_argument("-i",
                    "--item",
                    required=True,
                    help="json file menu item configuration")
    ap.add_argument("-w",
                    "--write",
                    required=False,
                    help="output path for the result")
    args = vars(ap.parse_args())

    mainMenuConf = Conf(args['main'])
    menuItemConf = Conf(args['item'])
    (mainMenufeatureList,
     mainMenulabels) = h5_load_dataset(mainMenuConf['feature_file'],
                                       mainMenuConf['dataset_feature_name'])

    # read main menu class
    classInfo = []
    mainMenuClassName = None
    if (mainMenuConf['class'] != None):
        for name in open(mainMenuConf['class']).read().split("\n"):
            classInfo.append(name)
    if len(classInfo) != 0:
        mainMenuClassName = classInfo[0]
    else:
        mainMenuClassName = 'mainMenu'

    voc = pacasl_voc_reader(mainMenuConf['dataset_xml'])
    objectList = voc.getObjectList()
    for (className, mainMenuBox) in objectList:
        if (className == mainMenuClassName):
            break

    # read menu item class
    itemClassInfo = []
    if (menuItemConf['class'] != None):
        for name in open(menuItemConf['class']).read().split("\n"):
            itemClassInfo.append(name)

    voc = pacasl_voc_reader(menuItemConf['dataset_xml'])
    objectList = voc.getObjectList()
    for (className, itemBox) in objectList:
        if (className == itemClassInfo[0]):
            break

    itemClassifier = Classifier(menuItemConf['classifier_path'], "SVC")
    itemHOG = HOG(menuItemConf['orientations'],
                  menuItemConf['pixels_per_cell'],
                  menuItemConf['cells_per_block'],
                  True if menuItemConf['transform_sqrt'] == 1 else False,
                  menuItemConf['normalize'])

    imgReader = ImageReader(args['video'], True)
    hogParam = HOGParam(
        orientations=mainMenuConf['orientations'],
        pixels_per_cell=mainMenuConf['pixels_per_cell'],
        cells_per_block=mainMenuConf['cells_per_block'],
        transform_sqrt=True if mainMenuConf['transform_sqrt'] == 1 else False,
        block_norm=mainMenuConf['normalize'])

    if args['write'] != None:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(args['write'], fourcc, 30.0, (1280, 720))

    mainMenuLoc = None
    mainMenuImg = None
    bFound = False
    frameCnt = 0
    searchRegion = None
    while True:
        (ret, frame, fname) = imgReader.read()
        if ret == False:
            break
        templateShape = [
            mainMenuBox[3] - mainMenuBox[1] + 1,
            mainMenuBox[2] - mainMenuBox[0] + 1
        ]
        frameOrigin = frame.copy()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        key = ' '
        if (bFound == True):

            testImg = frame[mainMenuLoc[1]:mainMenuLoc[3],
                            mainMenuLoc[0]:mainMenuLoc[2]]
            e1 = cv2.getTickCount()
            (diff, ratio) = imgDiffRatio(testImg, mainMenuImg)
            e2 = cv2.getTickCount()
            time = (e2 - e1) / cv2.getTickFrequency()
            #print('[{}] ratio {}'.format(frameCnt, ratio))
            if (ratio < 0.1):
                bFound = True
                (x, y, w, h) = (mainMenuLoc[0], mainMenuLoc[1],
                                mainMenuLoc[2] - mainMenuLoc[0],
                                mainMenuLoc[3] - mainMenuLoc[1])
            else:
                bFound = False
        else:
            if searchRegion == None:
                searchRegion = tuple(mainMenuConf['mainMenuSearchRegion'])
            e1 = cv2.getTickCount()
            (bFound, val, (x, y, w, h)) = searchImageByHOGFeature(
                mainMenufeatureList[0],
                templateShape,
                frame,
                searchRegion,
                mainMenuConf['mainMenuHOGDistanceThreshold'],
                hogParam, (10, 10),
                bMP=False,
                bVisualize=False)
            e2 = cv2.getTickCount()
            time = (e2 - e1) / cv2.getTickFrequency()
            if bFound == True:
                frameDetectImg = frame[y:y + h, x:x + w]

        if bFound == True:
            print('[{}] search result time {}, loc = {}'.format(
                frameCnt, time, (x, y, w, h)))
            searchRegion = (x, y, x + w, y + h)
            mainMenuLoc = (x, y, x + w, y + h)
            mainMenuImg = frame[y:y + h, x:x + w]
            frameDetectImg = mainMenuImg
            cv2.rectangle(frameOrigin, (x, y), (x + w - 1, y + h - 1),
                          (0, 255, 0), 2)

            e1 = cv2.getTickCount()
            (rtn, (fx, fy, fw, fh)) = FrameDetectByOneImage(
                frameDetectImg,
                frameDetectImg,
                minW=200,
                minH=60,
                frameRatio=mainMenuConf['mainMenuFrameRectRatio'])
            e2 = cv2.getTickCount()
            time = (e2 - e1) / cv2.getTickFrequency()
            if rtn == True:
                fx = fx + x
                fy = fy + y
                cv2.rectangle(frameOrigin, (fx - 5, fy - 5),
                              (fx + fw + 5, fy + fh + 5), (255, 0, 0), 2)
                bh = itemBox[3] - itemBox[1] + 1
                bw = itemBox[2] - itemBox[0] + 1
                roi = frame[fy:fy + bh, fx:fx + bw]
                e1 = cv2.getTickCount()
                (feature, _) = itemHOG.describe(roi)
                predictIdx = itemClassifier.predict(feature)
                e2 = cv2.getTickCount()
                time = (e2 - e1) / cv2.getTickFrequency()
                print('    predict {} takes {}'.format(predictIdx, time))
                cv2.putText(frameOrigin, str(predictIdx),
                            (fx + fw + 10, fy + fh), cv2.FONT_HERSHEY_SIMPLEX,
                            2, (0, 0, 255), 3, cv2.LINE_AA)

            key = basics.showResizeImg(frameOrigin, 'result', 1)
            if args['write'] != None:
                out.write(frameOrigin)
        else:
            print('[{}] Not found, takes  {}'.format(frameCnt, time))
            key = basics.showResizeImg(frameOrigin, 'result', 1)
            if args['write'] != None:
                out.write(frameOrigin)

        if key == ord('q'):
            break
        frameCnt = frameCnt + 1

    if args['write'] != None:
        out.release()