示例#1
0
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 19 08:18:23 2019
@author: Joel Miller
"""

import cv2
from selector import areaSelector

usps_cascade = cv2.CascadeClassifier('data/cascade.xml')
cap = cv2.VideoCapture('../vids/usps.mp4')
cap.set(1, 24 * 30)

ret, frame = cap.read()
box = areaSelector(frame)
if box is not None:
    frame = frame[box]

while (cap.isOpened()):
    ret, frame = cap.read()
    if box is not None:
        frame = frame[box]

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    usps = usps_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in usps:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = frame[y:y + h, x:x + w]
# -*- coding: utf-8 -*-
"""
Created on Fri Nov  8 09:11:30 2019

@author: Joel Miller
"""
import cv2
from selector import areaSelector, mask2Box, box2Rect

video_location = '../vids/f15.mp4'
cap = cv2.VideoCapture(video_location)
success, refFrame = cap.read()

bbox = mask2Box(areaSelector(refFrame))
vidWIndow = "Video Window"

tracker_types = [
    'BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT'
]
tracker_type = tracker_types[7]

if tracker_type == 'BOOSTING':
    tracker = cv2.TrackerBoosting_create()
if tracker_type == 'MIL':
    tracker = cv2.TrackerMIL_create()
if tracker_type == 'KCF':
    tracker = cv2.TrackerKCF_create()
if tracker_type == 'TLD':
    tracker = cv2.TrackerTLD_create()
if tracker_type == 'MEDIANFLOW':
    tracker = cv2.TrackerMedianFLow_create()
示例#3
0
def selectPosNeg(video_location):
    makeDirs()
    bgList(video_location)
    count = pathCount('data/raw')
    i = 1
    posCount = 1
    negCount = 1
    posList = []
    negList = []
    init = 1
    yRatio = 1
    while i <= count:
        refImg = cv2.imread(f'data/raw/img{i}.jpg')
        modImg = refImg.copy()
        box = areaSelector(modImg, yRatio, init)

        if box[0] == -1:
            cv2.imwrite(f'data/neg/neg{negCount}.jpg', modImg)
            negList.append(f'neg/neg{negCount}.jpg\n')
            negCount += 1
        elif box[0] == -9:
            i -= 2
        elif box[0] == -21:
            i += 1
            continue
        elif box[0] == -31:
            break
        else:
            if init == 1:
                ratioInit = mask2Rect(box)
                h = ratioInit[1][1] - ratioInit[0][1]
                w = ratioInit[1][0] - ratioInit[0][0]
                yRatio = round(h / w, 2)
                init = 2
                print('height = ' + str(h))
                print('width = ' + str(w))

            modImg = modImg[box]
            if init == 1:
                cv2.imwrite(f'data/pos/pos{posCount}.jpg', modImg)
                w = modImg.shape[1]
                h = modImg.shape[0]
            else:
                resizedModImg = cv2.resize(modImg, (w, h))
                cv2.imwrite(f'data/pos/pos{posCount}.jpg', resizedModImg)
            posList.append(f'pos/pos{posCount}.jpg  1  0 0 {w} {h}\n')
            posCount += 1
            temp = mask2Rect(box)
            refImg = cv2.rectangle(refImg, temp[0], temp[1], (0, 0, 0),
                                   cv2.FILLED)
            cv2.imwrite(f'data/neg/neg{negCount}.jpg', refImg)
            negList.append(f'neg/neg{negCount}.jpg\n')
            negCount += 1
        print('Image: ' + str(i) + '/' + str(count))
        i += 1

    # Negative file list creation
    bgFile = open('data/bg.txt', 'w+')
    [bgFile.write(i) for i in negList]
    bgFile.close()
    print('Background text file written')

    # Positive file list creation
    datFile = open('data/info.dat', 'w+')
    [datFile.write(i) for i in posList]
    datFile.close()
    print('info.dat file written')

    negCount = pathCount('data/neg')
    print('Number of Negative pics: ' + str(negCount))
    posCount = pathCount('data/pos')
    print('Number of Positive pics: ' + str(posCount))
    return w, h
示例#4
0
def objTrack(videoLocation):
    makeDirs()
    cap = cv2.VideoCapture(videoLocation[0])
    vidNum = 0
    init = 1
    initScale = 1
    posCount = 0
    negCount = 0
    posList = []
    negList = []

    class Found(Exception):
        pass

    try:
        while True:
            success, frame = cap.read()
            if not success:
                print('didnt find video')
                break
            refFrame = frame.copy()
            refFrame2 = frame.copy()
            key = cv2.waitKey(1) & 0xFF

            # Pause the Video
            if key == 32:
                while True:
                    key2 = cv2.waitKey(1) or 0xFF
                    # Start obj tracking
                    if key2 == ord('w'):
                        if initScale:
                            yRatio = 1
                            box = areaSelector(frame, yRatio, initScale)
                            ratioInit = mask2Rect(box)
                            h = ratioInit[1][1] - ratioInit[0][1]
                            w = ratioInit[1][0] - ratioInit[0][0]
                            yRatio = round(h / w, 2)
                            initScale = 0
                            objFrame = refFrame[box]
                            w = objFrame.shape[1]
                            h = objFrame.shape[0]
                        else:
                            box = areaSelector(frame, yRatio, initScale)
                            objFrame = refFrame[box]
                            objFrame = cv2.resize(objFrame, (w, h))

                        init = 2
                        tracker = cv2.TrackerMedianFlow_create()
                        tracker.init(frame, mask2Box(box))
                        posCount += 1
                        cv2.imwrite(f'data/pos/pos{posCount}.jpg', objFrame)
                        posList.append(
                            f'pos/pos{posCount}.jpg  1  0 0 {w} {h}\n')
                        temp = mask2Rect(box)
                        refFrame = cv2.rectangle(refFrame, temp[0], temp[1],
                                                 (0, 0, 0), cv2.FILLED)
                        negCount += 1
                        cv2.imwrite(f'data/neg/neg{negCount}.jpg', refFrame)
                        negList.append(f'neg/neg{negCount}.jpg\n')
                        break

                    cv2.imshow('Video', frame)
                    # Play the Video
                    if key2 == 32:
                        break
                    if key2 == ord('s'):
                        init = 1
                        break
                    if key2 == ord('n'):
                        vidNum += 1
                        if len(videoLocation) > 1 and vidNum < len(
                                videoLocation):
                            cap.release()
                            cap = cv2.VideoCapture(videoLocation[vidNum])
                            break
                        else:
                            if len(videoLocation) == 1:
                                print('Only one video loaded')
                            else:
                                print('Last video in list')

                    if key2 == 27 or key2 == 113:
                        raise Found

            if success and init == 2:
                trackSuccess, box = tracker.update(frame)
                if trackSuccess:
                    rectPts = box2Rect(box)
                    objFrame = refFrame2[box2Mask(box)]
                    cv2.rectangle(frame, rectPts[0], rectPts[1], (255, 0, 0),
                                  2, 1)
                    posCount += 1
                    resizedModImg = cv2.resize(objFrame, (w, h))
                    cv2.imwrite(f'data/pos/pos{posCount}.jpg', resizedModImg)
                    posList.append(f'pos/pos{posCount}.jpg  1  0 0 {w} {h}\n')
                    refFrame = cv2.rectangle(refFrame, rectPts[0], rectPts[1],
                                             (0, 0, 0), cv2.FILLED)
                    negCount += 1
                    cv2.imwrite(f'data/neg/neg{negCount}.jpg', refFrame)
                    negList.append(f'neg/neg{negCount}.jpg\n')
                else:
                    init = 1

            if key == ord('s'):
                init = 1

            # Skip forward 3 seconds
            if key == ord('d'):
                skip = cap.get(cv2.CAP_PROP_POS_MSEC) + 3000
                cap.set(cv2.CAP_PROP_POS_MSEC, skip)
                success, frame = cap.read()

            # Skip Back 3 seconds
            if key == ord('a'):
                skip = cap.get(cv2.CAP_PROP_POS_MSEC) - 3000
                cap.set(cv2.CAP_PROP_POS_MSEC, skip)
                success, frame = cap.read()

            if key == ord('n'):
                vidNum += 1
                if len(videoLocation) > 1 and vidNum < len(videoLocation):
                    cap.release()
                    cap = cv2.VideoCapture(videoLocation[vidNum])
                else:
                    if len(videoLocation) == 1:
                        print('Only one video loaded')
                    else:
                        print('Last video in list')

            # Quit Video Playback by pressing 'q' or ESC
            if key == 113 or key == 27:
                break

            if success:
                cv2.imshow('Video', frame)

    except Found:
        print(
            'Finished making Pictures. Now review positives and delete false positives'
        )

    def reviewPics(posCount):
        count = posCount
        i = 1
        print('image: ' + str(i) + '/' + str(count))
        while i <= count:
            frame = cv2.imread(f'data/pos/pos{i}.jpg')
            while True:
                key = cv2.waitKey(1) or 0xFF
                cv2.imshow('Video', frame)

                if key == ord('a'):
                    try:
                        if i > 1:
                            i -= 1
                            frame = cv2.imread(f'data/pos/pos{i}.jpg')
                            print('image: ' + str(i) + '/' + str(count))
                    except:
                        break

                if key == ord('d'):
                    if i <= count:
                        i += 1
                        frame = cv2.imread(f'data/pos/pos{i}.jpg')
                        print('image: ' + str(i) + '/' + str(count))
                        break

                if key == ord('x'):
                    if i <= count:
                        os.remove(f'data/pos/pos{i}.jpg')
                        posList.remove(
                            f'pos/pos{posCount}.jpg  1  0 0 {w} {h}\n')
                        posCount -= 1
                        i += 1
                        if i <= count:
                            frame = cv2.imread(f'data/pos/pos{i}.jpg')
                        print('image: ' + str(i) + '/' + str(count))
                        break

                if key == 113 or key == 27:
                    i = count + 9000
                    break

        cv2.destroyAllWindows()
        retPath = os.getcwd()
        os.chdir('data/pos')
        renameList = [file for file in glob.glob('*.jpg')]

        def posSort(name):
            a = re.search('pos(\d+).jpg', name)
            return int(a.groups(0)[0])

        renameList.sort(key=posSort)

        renameNum = 1
        for j in renameList:
            os.rename(j, f'pos{renameNum}.jpg')
            renameNum += 1
        os.chdir(retPath)
        return posCount

    posCount = reviewPics(posCount)

    # write out background file
    bgFile = open('data/bg.txt', 'w+')
    [bgFile.write(i) for i in negList]
    bgFile.close()
    # write out dat file
    datFile = open('data/info.dat', 'w+')
    [datFile.write(i) for i in posList]
    datFile.close()
    print("Positive Count: " + str(posCount))
    print("Negative Count: " + str(negCount))
    cap.release()
    cv2.destroyAllWindows()
    return w, h
示例#5
0
    def selectPosNeg(self):
        self.makeDirs()
        #video_location = self.filePaths
        '''relocating bgList'''
        vidPath = self.tcVars.filePaths
        if os.path.isdir('data/raw') == False:
            os.mkdir('data/raw')
        count = pathCount('data/raw')
        # it will capture image in each 5 seconds
        try:
            frameRate = int(self.tcVars.frameRateInput.GetValue())
        except:
            frameRate = 3
            self.tcVars.statusBox.AppendText(
                "Error reading frame rate, defaulting to 3\n")

        for i in list(range(0, len(vidPath))):
            sec = 2
            vidcap = cv2.VideoCapture(vidPath[i])
            success, image = vidcap.read()
            while success:
                vidcap.set(cv2.CAP_PROP_POS_MSEC, sec * 1000)
                success, image = vidcap.read()
                if success:
                    cv2.imwrite(f'data/raw/img{count+1}.jpg', image)
                    count += 1
                sec += frameRate
                if (count % 50) == 0:
                    self.tcVars.statusBox.AppendText("converting images...\n")

        vidcap.release()
        cv2.destroyAllWindows()
        self.tcVars.statusBox.AppendText("Finished making pictures\n")

        count = pathCount('data/raw')
        i = 1
        posCount = 1
        negCount = 1
        posList = []
        negList = []
        init = 1
        yRatio = 1
        self.tcVars.statusBox.AppendText('a -- to go to previous picture\n')
        self.tcVars.statusBox.AppendText('d -- to go to next picture\n')
        self.tcVars.statusBox.AppendText(
            's -- to skip picture from training arena\n')
        self.tcVars.statusBox.AppendText('q or Esc -- to skip review\n\n')

        while i <= count:
            self.tcVars.statusBox.AppendText('Image: ' + str(i) + '/' +
                                             str(count) + '\n')
            refImg = cv2.imread(f'data/raw/img{i}.jpg')
            modImg = refImg.copy()
            box = areaSelector(modImg, yRatio, init)

            if box[0] == -1:
                cv2.imwrite(f'data/neg/img{i}.jpg', modImg)
                negList.append(f'neg/img{i}.jpg\n')
                negCount += 1
                i += 1
            elif box[0] == -9:  # previous picturew
                if i > 1:
                    i -= 1
            elif box[0] == -21:  # next picture
                cv2.imwrite(f'data/neg/img{i}.jpg', modImg)
                negList.append(f'neg/img{i}.jpg\n')
                negCount += 1
                i += 1
            elif box[0] == -41:  # skip picture
                i += 1
                continue
            elif box[0] == -31:  # quit
                break
            else:
                if init == 1:
                    ratioInit = mask2Rect(box)
                    h = ratioInit[1][1] - ratioInit[0][1]
                    w = ratioInit[1][0] - ratioInit[0][0]
                    yRatio = round(h / w, 2)
                    init = 2
                    self.tcVars.statusBox.AppendText('height = ' + str(h) +
                                                     '\n')
                    self.tcVars.statusBox.AppendText('width = ' + str(w) +
                                                     '\n')

                modImg = modImg[box]
                if init == 1:
                    cv2.imwrite(f'data/pos/img{i}.jpg', modImg)
                    w = modImg.shape[1]
                    h = modImg.shape[0]
                else:
                    resizedModImg = cv2.resize(modImg, (w, h))
                    cv2.imwrite(f'data/pos/img{i}.jpg', resizedModImg)
                posList.append(f'pos/img{i}.jpg  1  0 0 {w} {h}\n')
                posCount += 1
                temp = mask2Rect(box)
                refImg = cv2.rectangle(refImg, temp[0], temp[1], (0, 0, 0),
                                       cv2.FILLED)
                cv2.imwrite(f'data/neg/img{i}.jpg', refImg)
                negList.append(f'neg/img{i}.jpg\n')
                negCount += 1
                i += 1

        # Negative file list creation
        bgFile = open('data/bg.txt', 'w+')
        [bgFile.write(i) for i in negList]
        bgFile.close()
        self.tcVars.statusBox.AppendText('Background text file written\n')

        # Positive file list creation
        datFile = open('data/info.dat', 'w+')
        [datFile.write(i) for i in posList]
        datFile.close()
        self.tcVars.statusBox.AppendText('info.dat file written\n')

        negCount = pathCount('data/neg')
        self.tcVars.statusBox.AppendText('Number of Negative pics: ' +
                                         str(negCount) + '\n')
        posCount = pathCount('data/pos')
        self.tcVars.statusBox.AppendText('Number of Positive pics: ' +
                                         str(posCount) + '\n')
        self.tcVars.w = w
        self.tcVars.h = h