예제 #1
0
def main(pixThreshold, frameRate, videoStream):

    expDuration = 600000  # duration of experiment, in seconds; only relevant for live feed
    saveFreq = 4500  # how often to save data, in frames
    i, m = imageTools.loadImageAndMask()
    m, w = imageTools.convertMaskToWeights(m)
    videoType, displayDiffs = imageTools.getVideoType(videoStream)
    cap = cv2.VideoCapture(videoStream)
    print 'Camera resolution is %s x %s' % (str(m.shape[1]), str(m.shape[0]))
    cap.set(3, m.shape[1])
    cap.set(4, m.shape[0])
    ret, frame = cap.read()
    storedFrame = imageTools.grayBlur(frame)
    pixThreshold = int(np.floor(pixThreshold * storedFrame.shape[0]))
    print('PixelThreshold is %i') % pixThreshold
    pixData = np.zeros([saveFreq, len(np.unique(w)) + 1])
    i = 0  # a counter for saving chunks of data
    totalFrames = 0
    startTime = datetime.now()
    oldTime = startTime
    elapsed = 0
    print('Analyzing motion data...')
    moviedeq = []
    while (cap.isOpened()):
        ret, frame = cap.read()
        if ret == False:
            print 'End of Video'
            break
        currentFrame = imageTools.grayBlur(frame)
        moviedeq.append(currentFrame)
        diff = imageTools.diffImage(storedFrame, currentFrame, pixThreshold,
                                    displayDiffs)
        timeDiff = 1. / frameRate
        elapsed = elapsed + timeDiff
        counts = np.bincount(w, weights=diff.ravel())
        pixData[i, :] = np.hstack((elapsed, counts))
        totalFrames += 1
        storedFrame = currentFrame  # comment out if nothing is in first frame
        i += 1
    pixData = pixData[:i, :]
    pixData = pixData[:, 2:]  # get rid of timing column and background column
    file = open(videoStream + ".motion2", 'w')
    file.write("12/8/2015" + '\015')
    for x in range(0, 285):
        #print "x", x
        for y in range(0, 96):
            file.write(str(int(pixData[x, :][y])) + '\n')

    cap.release()
    cv2.destroyAllWindows()
    vidInfo = {}
    return vidInfo
예제 #2
0
def imageMode(movielist, modename):
    moviedeq = []
    i2 = 0
    #print (movielist)
    #movielist = ["hsmovieTue, Nov 8, 2016_1.avi", "hsmovieTue, Nov 8, 2016_2.avi", "hsmovieTue, Nov 8, 2016_3.avi", "hsmovieTue, Nov 8, 2016_4.avi", "hsmovieTue, Nov 8, 2016_5.avi", "hsmovieTue, Nov 8, 2016_6.avi"]
    #modename = ''.join(map(str,movielist))
    for filenumber in movielist:
    #for file in glob.glob(movielist):
    #for file in glob.glob("*avi"):
        #if i2 == 100:
        #    break
       # print (filenumber)
        #file = "*_" + str(filenumber) + ".avi"
        #file2 = glob.glob("*_" + str(filenumber) + ".avi"[0])
        #print (file2)
        cap = cv2.VideoCapture(glob.glob("*_" + str(filenumber) + ".avi")[0])
        #cap = cv2.VideoCapture(file2)
        #cap = cv2.VideoCapture(videoStream.split('-')[x])
        ret,frame = cap.read()
        storedFrame = imageTools.grayBlur(frame)
        totalFrames = 0
        while(cap.isOpened()):
            ret,frame = cap.read()
            if ret == False:
                #print 'End of Video'
                break
            currentFrame = imageTools.grayBlur(frame)
            if totalFrames < 50:
                if totalFrames % 3 == 0:
                    #print "adding frames: ", totalFrames
                    moviedeq.append(currentFrame)
            totalFrames += 1
            storedFrame = currentFrame # comment out if nothing is in first frame
        i2 += 1
    testing = calc_mode(moviedeq, np.zeros([660,1088]))
    #print "saving mode.png"
    cv2.imwrite("mode_" + modename + ".png", testing)
    #cv2.imwrite("mode_"+ movielist[0] + "_to_" + movielist[len(movielist)] + ".png", testing)
    cap.release()
    cv2.destroyAllWindows()
예제 #3
0
def main(pixThreshold, frameRate, videoStream):
    row = {0: 0, 1: 12, 2: 24, 3: 36, 4: 48, 5: 60, 6: 72, 7: 84}
    saveFreq = 4500  # how often to save data, in frames, just making sure this is really big, so I don't have any issues, would be important for memory considerations if I was doing a long movie
    #i,m = imageTools.loadImageAndMask()
    filenumber = videoStream.split('.')[0].split('_')[
        len(videoStream.split('.')[0].split('_')) - 1]
    #print "testing: ", filenumber
    #	if 1 <= int(filenumber) <= 20:
    #		movielist = list(range(1,21))
    #	if 21 <= int(filenumber) <= 40:
    #		movielist = list(range(21,41))
    #	if 41 <= int(filenumber) <= 60:
    #		movielist = list(range(41,61))
    #	if 61 <= int(filenumber) <= 70:
    #		movielist = list(range(51,71))
    #	if 71 <= int(filenumber) <= 80:
    #		movielist = list(range(71,91))
    #	if 81 <= int(filenumber) <= 478:
    #		movielist = list(range(int(filenumber)-10,int(filenumber)+10))
    #	if 479 <= filenumber <= 488:
    #		movielist = list(range(468,488))
    #	if 489 <= int(filenumber) <= 498:
    #		movielist = list(range(489,509))
    #	if 499 <= int(filenumber) <= 598:
    #		movielist = list(range(int(filenumber)-10,int(filenumber)+10))
    #	if 599 <= int(filenumber) <= 608:
    #		movielist = list(range(599,609))
    #	if 609 <= int(filenumber) <= 631:
    #		movielist = list(range(609,632))
    #	if 632 <= int(filenumber) <= 661:
    #		movielist = list(range(632,662))
    #	if 662 <= int(filenumber) <= 691:
    #		movielist = list(range(662,692))
    #	if 692 <= int(filenumber) <= 721:
    #		movielist = list(range(692,722))
    #	if 722 <= int(filenumber) <= 741:
    #		movielist = list(range(722,742))
    #	if 742 <= int(filenumber) <= 781:
    #		movielist = list(range(742,782))
    #	if 782 <= int(filenumber) <= 821:
    #		movielist = list(range(782,822))
    #	if 822 <= int(filenumber) <= 862:
    #		movielist = list(range(822,862))
    #	if 862 <= int(filenumber) <= 901:
    #		movielist = list(range(862,901))
    if 1 <= int(filenumber) <= 20:
        movielist = list(range(1, 21))
    if 21 <= int(filenumber) <= 40:
        movielist = list(range(21, 41))
    if 41 <= int(filenumber) <= 60:
        movielist = list(range(41, 61))
    if 61 <= int(filenumber) <= 70:
        movielist = list(range(61, 71))
#	if 1 <= int(filenumber) <= 70:
#		movielist = list(range(71,111))
    if 71 <= int(filenumber) <= 100:
        movielist = list(range(71, 111))
    if 101 <= int(filenumber) <= 448:
        movielist = list(range(int(filenumber) - 20, int(filenumber) + 20))
    if 449 <= int(filenumber) <= 488:
        movielist = list(range(448, 488))
    if 489 <= int(filenumber) <= 528:
        movielist = list(range(489, 529))
    if 529 <= int(filenumber) <= 568:
        movielist = list(range(529, 569))
        #movielist = list(range(int(filenumber)-20,int(filenumber)+20))
    if 569 <= int(filenumber) <= 608:
        movielist = list(range(569, 609))
    if 609 <= int(filenumber) <= 631:
        movielist = list(range(609, 632))
    if 632 <= int(filenumber) <= 661:
        movielist = list(range(632, 662))
    if 662 <= int(filenumber) <= 691:
        movielist = list(range(662, 692))
    if 692 <= int(filenumber) <= 721:
        movielist = list(range(692, 722))
    if 722 <= int(filenumber) <= 741:
        movielist = list(range(722, 742))
    #if 632 <= int(filenumber) <= 741:
    #	movielist = list(range(609,632))
    if 742 <= int(filenumber) <= 781:
        movielist = list(range(742, 782))
    if 782 <= int(filenumber) <= 821:
        movielist = list(range(782, 822))
    if 822 <= int(filenumber) <= 862:
        movielist = list(range(822, 862))
    if 862 <= int(filenumber) <= 901:
        movielist = list(range(862, 901))

    modename = str(movielist[0]) + "to" + str(movielist[len(movielist) - 1])
    #modename = ''.join(map(str,movielist))
    imageMode(movielist, modename)
    modefilename = "mode_" + modename + ".png"
    #print movielist
    try:
        mpimg.imread(modefilename)
        #print "mode file already generated"
    except:
        imageMode(movielist, modename)
    e = imageTools.loadmodeImage(modefilename)
    #e = imageTools.loadModeImage()
    roimask = np.zeros((660, 1088))
    (maxxysnp, minxysnp) = max_min()
    maxxs = []
    minxs = []
    maxys = []
    minys = []
    for j in range(0, numberofwells * 2, 2):
        if maxxysnp[j] == -100:
            # if nothing ever moved in this well and there is no max or min value (could happen with a totally empty well)
            maxxs.append(np.nan)
            maxys.append(np.nan)
            minxs.append(np.nan)
            minys.append(np.nan)
        else:
            maxxs.append(maxxysnp[j])
            maxys.append(maxxysnp[j + 1])
            minxs.append(minxysnp[j])
            minys.append(minxysnp[j + 1])
    npmaxxs = np.asarray(maxxs)
    npmaxys = np.asarray(maxys)
    npminxs = np.asarray(minxs)
    npminys = np.asarray(minys)
    npmaxxs = np.reshape(npmaxxs, (numberofcols, numberofrows))
    npmaxys = np.reshape(npmaxys, (numberofcols, numberofrows))  #12,8
    npminxs = np.reshape(npminxs, (numberofcols, numberofrows))
    npminys = np.reshape(npminys, (numberofcols, numberofrows))
    #print npmaxxs
    #print npmaxys
    #print npminxs
    #print npminys
    cmaxxs = []
    cminxs = []
    cmaxys = []
    cminys = []
    for j2 in range(0, numberofwells):
        maxx = maxxs[well_conversion[j2]]
        maxy = maxys[well_conversion[j2]]
        miny = minys[well_conversion[j2]]
        minx = minxs[well_conversion[j2]]
        #print "j2: ", j2, well_conversion[j2], maxx, maxy, minx, miny
        #print "wcj2/8: ", well_conversion[j2]/8
        #print npmaxxs[well_conversion[j2]/8,:]
        #print npminxs[well_conversion[j2]/8,:]
        #print np.nanmean(npmaxxs[well_conversion[j2]/8,:])
        #print np.nanmean(npminxs[well_conversion[j2]/8,:])
        #print "j2/12: ", j2/12
        #print npmaxys[:,j2/12]
        #print npminys[:,j2/12]
        #print np.nanmean(npmaxys[:,j2/12])
        #print np.nanmean(npminys[:,j2/12])
        if minx == maxx:
            maxx = maxx + 2
        if miny == maxy:
            maxy = maxy + 2
        if math.isnan(
                float(maxx)
        ):  # could also add a condition if min and max are equal to each other
            #print "very first if statement"
            maxx = np.nanmean(npmaxxs[well_conversion[j2] / numberofrows, :])
            minx = np.nanmean(npminxs[well_conversion[j2] / numberofrows, :])
            maxy = np.nanmean(npmaxys[:, j2 / numberofcols])
            miny = np.nanmean(npminys[:, j2 / numberofcols])
            #print "new means: ", maxx, minx, maxy, miny
            # In the case that the entire row never gets any values in any wells and the mean is still NaN
            # not 100% sure that this is going to work, will get a runtime warning, so then can check it out??
            # mostly not sure about the well_conversions for the Xs
            if math.isnan(float(maxx)) and math.isnan(float(minx)):
                #		print "first if statement"
                if well_conversion[j2] < 8:
                    #print "2nd if statement"
                    minx = well_conversion[j2]
                    maxx = well_conversion[j2] + 85
                else:
                    #			print "2nd else statement"
                    minx = cminxs[well_conversion[j2] - 8] + 85
                    maxx = cmaxxs[well_conversion[j2] - 8] + 85
                ###if j2 <= 11:
        #			print "3rd if statement"
        ###	miny = j2
        ###	maxy = j2+60
        ###	minx = j2
            if math.isnan(float(maxy)) and math.isnan(float(miny)):
                #print "4th if statement"
                if j2 < 12:
                    #	print "5th if statement"
                    miny = j2
                    maxy = j2 + 85
                else:
                    #	print "5th else statement"
                    miny = cminys[j2 - 12] + 85
                    maxy = cmaxys[j2 - 12] + 85
        # End of untested section
        cmaxxs.append(maxx)
        cmaxys.append(maxy)
        cminxs.append(minx)
        cminys.append(miny)
        #print miny, maxy, minx, maxx, j2, j2+1
        roimask[miny:maxy, minx:maxx] = j2 + 1
    np.set_printoptions(threshold=np.nan)  # printing entire array
    cmaxxs.sort()
    cmaxys.sort()
    cminxs.sort()
    cminys.sort()
    rm, roimaskweights = imageTools.convertMaskToWeights(roimask)

    # start camera or open video
    videoType, displayDiffs = imageTools.getVideoType(videoStream)
    cap = cv2.VideoCapture(videoStream)

    # adjust video resolution if necessary (sized to mask)
    print 'Camera resolution is %s x %s' % (str(
        roimask.shape[1]), str(roimask.shape[0]))
    cap.set(3, roimask.shape[1])
    cap.set(4, roimask.shape[0])
    # Set Pixel Threshold
    ret, frame = cap.read()
    storedImage = np.array(e * 255, dtype=np.uint8)
    # have to convert the float32 to uint8
    storedMode = imageTools.Blur(storedImage)
    storedFrame = imageTools.grayBlur(frame)
    #pixThreshold = int(np.floor( pixThreshold * storedFrame.shape[0] ))
    print('PixelThreshold is %i') % pixThreshold
    #cenData = np.zeros([ saveFreq, len(np.unique(roimaskweights))*2])
    cenData = np.zeros([saveFreq, len(np.unique(roimaskweights)) * 2 - 2])
    #print "cenData shape: ", np.shape(cenData)
    pixData = np.zeros([saveFreq, len(np.unique(roimaskweights))])
    #pixData = np.zeros([ saveFreq, len(np.unique(roimaskweights))])
    i = 0  # a counter for saving chunks of data
    totalFrames = 0
    startTime = datetime.now()
    print('Analyzing motion data...')
    frame_roi = []
    while (cap.isOpened()):
        ret, frame = cap.read()
        if ret == False:
            print 'End of Video'
            break
        currentFrame = imageTools.grayBlur(frame)
        currentFrame2 = imageTools.grayBlur(frame)
        diffpix = imageTools.diffImage(storedFrame, currentFrame2,
                                       pixThreshold, displayDiffs)
        diff = imageTools.trackdiffImage(storedMode, currentFrame,
                                         pixThreshold, displayDiffs)
        #cv2.imwrite(videoStream + '_diffimage_' + str(i) + ".png", diff)
        diff.dtype = np.uint8
        _, contours, hierarchy = cv2.findContours(diff, cv2.RETR_TREE,
                                                  cv2.CHAIN_APPROX_NONE)
        MIN_THRESH = 20.0
        MIN_THRESH_P = 20.0
        roi_dict = {}
        for r in range(0, numberofwells):
            roi_dict[r + 1] = []
        for cs in range(0, len(contours)):
            #print "area and lenght: ", cv2.contourArea(contours[cs]), cv2.arcLength(contours[cs], True)
            if cv2.contourArea(contours[cs]) < 1.0:
                continue
            if cv2.arcLength(contours[cs], True) < 1.0:
                continue
            if cv2.contourArea(contours[cs]) > MIN_THRESH or cv2.arcLength(
                    contours[cs], True) > MIN_THRESH_P:
                M = cv2.moments(contours[cs])
                #print M
                cX = int(M["m10"] / M["m00"])
                cY = int(M["m01"] / M["m00"])
                #print "cX, cY :", cX, cY
                r = 1
                c = 1
                for x in range(0, len(cmaxxs)):
                    #print "cmaxxs: ", cmaxxs[x]
                    if cX > cmaxxs[x]:
                        r = x + 1
                        #print "r: ", r
                #print "r: ", r
                for y in range(0, len(cmaxys)):
                    #print "cmaxys: ", cmaxys[x]
                    if cY > cmaxys[y]:
                        c = y + 1
                    #	print "c: ", c
                if c == numberofwells:
                    c = c - 1
                if r == numberofwells:
                    r = r - 1
                area = cv2.contourArea(contours[cs])
                perim = cv2.arcLength(contours[cs], True)
                #print c, numberofcols, c/numberofcols
                if not roi_dict[r / numberofrows + 1 + row[c / numberofcols]]:
                    roi_dict[r / numberofrows + 1 +
                             row[c / numberofcols]].append(
                                 (area * perim, cX, cY))
                else:
                    if roi_dict[r / numberofrows + 1 +
                                row[c / numberofcols]] < area * perim:
                        roi_dict[r / numberofrows + 1 +
                                 row[c / numberofcols]][0] = (area * perim, cX,
                                                              cY)
        frame_roi.append(roi_dict)

        pixcounts = []
        pixcounts = np.bincount(roimaskweights, weights=diffpix.ravel())
        pixData[i, :] = np.hstack((pixcounts))
        counts = []
        keys = roi_dict.keys()
        keys.sort()
        for k in keys:
            #print "k: ", k
            x = -10000
            y = -10000
            if roi_dict[k]:
                x = roi_dict[k][0][1]
                y = roi_dict[k][0][2]
            counts.append(x)
            counts.append(y)
            cv2.line(storedImage, (x, y), (x, y), (255, 255, 255), 2)
        if i == 284:
            cv2.imwrite(
                videoStream + '_trackedimagewithlines_' + str(i) + ".png",
                storedImage)
        cenData[i, :] = np.asarray(counts)
        totalFrames += 1
        storedFrame = currentFrame
        i += 1

    file = open(videoStream + ".centroid2", 'w')
    for x in range(0, 285):
        for y in range(0, 192):
            file.write(str(int(cenData[x, :][y])) + '\n')
    pixData = pixData[:i, :]
    pixData = pixData[:, 1:]  # get rid of background column
    file = open(videoStream + ".motion2", 'w')
    #file.write("12/8/2015" + '\015')
    for x in range(0, 285):
        for y in range(0, numberofwells):
            file.write(str(int(pixData[x, :][y])) + '\n')
#	vidInfo = {}
# release camera
    cap.release()
    cv2.destroyAllWindows()
예제 #4
0
def main(pixThreshold, frameRate, videoStream):

    expDuration = 600000  # duration of experiment, in seconds; only relevant for live feed
    saveFreq = 4500  # how often to save data, in frames

    i, m = imageTools.loadImageAndMask()

    # convert mask to integer values for bincount weights
    m, w = imageTools.convertMaskToWeights(m)
    #print m,w

    # start camera or open video
    videoType, displayDiffs = imageTools.getVideoType(videoStream)
    cap = cv2.VideoCapture(videoStream)

    # adjust video resolution if necessary (sized to mask)
    print 'Camera resolution is %s x %s' % (str(m.shape[1]), str(m.shape[0]))
    cap.set(3, m.shape[1])
    cap.set(4, m.shape[0])

    # Set Pixel Threshold
    ret, frame = cap.read()
    storedFrame = imageTools.grayBlur(frame)
    pixThreshold = int(np.floor(pixThreshold * storedFrame.shape[0]))
    print('PixelThreshold is %i') % pixThreshold

    # Acquire data
    if saveFreq / frameRate > expDuration:  # do shorter of expDuration vs. saveFreq
        saveFreq = expDuration * frameRate

    pixData = np.zeros([saveFreq, len(np.unique(w)) + 1])

    i = 0  # a counter for saving chunks of data
    totalFrames = 0
    startTime = datetime.now()
    oldTime = startTime
    elapsed = 0

    print('Analyzing motion data...')

    moviedeq = []

    while (cap.isOpened()):

        ret, frame = cap.read()

        if ret == False:
            print 'End of Video'
            break

        currentFrame = imageTools.grayBlur(frame)
        moviedeq.append(currentFrame)

        # check if i bigger than saveFreq. If yes, save and reset values
        ##	if i >= saveFreq:
        # save data
        ##	timeStamp = datetime.now()
        # make a directory to save stuff in? %m%d%y%H%M
        ##	fname = 'data' + videoStream + timeStamp.strftime('%m%d%y%H%M%S') + '.npy'
        #fname = 'data' + timeStamp.strftime('%m%d%y%H%M%S') + '.npy'
        ##		np.save(fname,pixData)
        # reset pixData and i
        ##	pixData = np.zeros([ saveFreq, len(np.unique(w)) + 1])
        ##	i = 0

        # stop experiment if user presses 'q' or if experiment duration is up
        if (cv2.waitKey(1) & 0xFF == ord('q') or len(sys.argv) == 1 and
                datetime.now() > startTime + timedelta(seconds=expDuration)):
            break

        # record pixel differences in all of the ROIs
        diff = imageTools.diffImage(storedFrame, currentFrame, pixThreshold,
                                    displayDiffs)

        timeDiff = 1. / frameRate
        elapsed = elapsed + timeDiff
        #print elapsed

        # calculate and record pixel differences
        counts = np.bincount(w, weights=diff.ravel())
        #print counts # output
        pixData[i, :] = np.hstack((elapsed, counts))
        totalFrames += 1

        storedFrame = currentFrame  # comment out if nothing is in first frame
        #oldTime = newTime
        i += 1

    # done recording. Remove empty rows (those bigger than i) from PixData
    pixData = pixData[:i, :]
    #print np.shape(pixData)
    #print pixData

    #testing = calc_mode(moviedeq, np.zeros([660,1088]))
    #cv2.imwrite('test_img.jpg', testing)

    # Write data to file with timestamp:
    # np.save(movieName[:-4] + '.npy', pixData)
    ##timeStamp = datetime.now()
    ##fname = 'data' + videoStream + timeStamp.strftime('%m%d%y%H%M%S') + '.npy'
    #fname = 'data' + timeStamp.strftime('%m%d%y%H%M%S') + '.npy'
    ##np.save(fname,pixData)
    pixData = pixData[:, 2:]  # get rid of timing column and background column
    #print np.shape(pixData)
    #print pixData
    #pixData = deltaPix[:,1:] # get rid of background column
    file = open(videoStream + ".motion2", 'w')
    file.write("12/8/2015" + '\015')
    for x in range(0, 285):
        #print "x", x
        for y in range(0, 96):
            #print "y", y
            #print str(int(pixData[x,:][y]))
            file.write(str(int(pixData[x, :][y])) + '\n')

    # Save info (elapsed time and framerate) for later use
    vidInfo = {}
    #analysisTime = timeStamp - startTime
    #vidInfo['analysisTime'] = float(str(analysisTime.seconds) + '.' + str(analysisTime.microseconds))
    vidInfo['TotalFrames'] = totalFrames
    #vidInfo['fps'] = int(totalFrames/vidInfo['analysisTime'])
    vidInfo['pixThreshold'] = pixThreshold
    vidInfo['CameraResolution'] = '%s x %s' % (str(m.shape[1]), str(
        m.shape[0]))
    vidInfo['NamePrefix'] = videoStream
    #vidInfo['NamePrefix'] =  videoStream.split('.')[0]

    #print ('Analyzed %i frames in %f seconds') % (vidInfo['TotalFrames'],vidInfo['analysisTime'])
    #print('FrameRate is about %i fps') % vidInfo['fps']
    print 'Motion threshold is %i pixels' % int(pixThreshold)
    print 'Camera resolution is %s' % vidInfo['CameraResolution']

    # release camera
    cap.release()
    cv2.destroyAllWindows()

    return vidInfo
예제 #5
0
def main(pixThreshold,frameRate,videoStream):
	#elate_loc={(1,1):1, (1,2):2, (1,3):3, (1,4):4, (1:5):5, (1,6):6, (1,7):7, (1,8):8, (1,9):9,(1,10):10,(1,11):11,(1,12):12,(2,1):13,(2,2):14,(2,3):15,(2,4):16,(2:5):17,(2,6):18,(2,7):19,(2,8):20,(2,9):21,(2,10):22,(2,11):23,(2,12):24,(3,1):25,(3,2):26,(3,3):27,(3,4):28,(3,5):29,(3,6):30,(3,7)}
	rowold={1:0,2:12,3:24,4:36,5:48,6:60,7:72,8:84}
	row={0:0,1:12,2:24,3:36,4:48,5:60,6:72,7:84}

	expDuration = 600000 # duration of experiment, in seconds; only relevant for live feed
	saveFreq = 4500 # how often to save data, in frames

	i,m = imageTools.loadImageAndMask()
	e = imageTools.loadModeImage()
	roimask = np.zeros((660,1088))
	(maxxysnp, minxysnp) = max_min()
	print "maxxysnp: ", maxxysnp
	print "minxysnp: ", minxysnp
	maxxs = []
	minxs = []
	maxys = []
	minys = []
	for j in range (0, numberofwells*2,2):
		maxx = maxxysnp[j]
		maxxs.append(maxxysnp[j])
		maxy = maxxysnp[j+1]
		maxys.append(maxxysnp[j+1])
		minx = minxysnp[j]
		minxs.append(minxysnp[j])
		miny = minxysnp[j+1]
		minys.append(minxysnp[j+1])
		roimask[miny:maxy,minx:maxx] = j+1
	maxxs.sort()
	maxys.sort()
	minxs.sort()
	minys.sort()
	#smaxxs = []
	#sminxs = []
	#smaxys = []
	#sminys = []
	#rx = 8
	#cy = 12
	#realmaxx = 0
	#realmaxxs = []
	#for z in range(0, len(maxxs)):
	#	if z == cy - 1:
	#		realmaxxs.append(realmaxx)
	#		realmaxx = 0
	#	if maxxs[z] > realmaxx:
	#		realmaxx = maxxs[z]
	np.set_printoptions(threshold=np.nan) # printing entire array
	print roimask
	#	print maxx,maxy,minx,miny
	#print e
	#cv2.imwrite('testinge.jpg', e)
	#cv2.imwrite('testingm.jpg', m)
	#cv2.imwrite('testingi.jpg', i)
	# convert mask to integer values for bincount weights
	#print "mask1: ", np.shape(m), m
	m,w = imageTools.convertMaskToWeights(m)
	rm,roimaskweights = imageTools.convertMaskToWeights(roimask)
	#print "mask2: ", np.shape(m), m
	#print "weights: ", np.shape(w), w
	unique = np.unique(m)
	print "unique: ", unique
	unique2 = np.unique(rm)
	print "unique2: ", unique2
	#print np.shape(m)
	rminr = set()
	rminc = set()
	rmaxr = set()
	rmaxc = set()

	for x in unique2:
		#print "x: ", x
		#if x == 0:
		#	continue
		#print np.shape(np.where(m==x))
		#print np.where(m==x)
		rmaxdimc = np.amax(np.where(rm==x)[0])
		rmaxc.add(rmaxdimc)
		#print "max dimc: ", np.amax(np.where(m==x)[0])
		rmaxdimr = np.amax(np.where(rm==x)[1])
		rmaxr.add(rmaxdimr)
		#print "max dimr: ", np.amax(np.where(m==x)[1])
		rmindimc = np.amin(np.where(rm==x)[0])
		rminc.add(rmindimc)
		#print "min dimc: ", np.amin(np.where(m==x)[0])
		rmindimr = np.amin(np.where(rm==x)[1])
		rminr.add(rmindimr)
		#print "min dimr: ", np.amin(np.where(m==x)[1])
	rlminx = list(rminr)
	rlminx.sort()
	rlmaxx = list(rmaxr)
	rlmaxx.sort()
	rlminy = list(rminc)
	rlminy.sort()
	rlmaxy = list(rmaxc)
	rlmaxy.sort()
	print rlminx, rlmaxx, rlminy, rlmaxy


	minr = set()
	minc = set()
	maxr = set()
	maxc = set()
	for x in unique:
		#print "x: ", x
		if x == 0:
			continue
		#print np.shape(np.where(m==x))
		#print np.where(m==x)
		maxdimc = np.amax(np.where(m==x)[0])
		maxc.add(maxdimc)
		#print "max dimc: ", np.amax(np.where(m==x)[0])
		maxdimr = np.amax(np.where(m==x)[1])
		maxr.add(maxdimr)
		#print "max dimr: ", np.amax(np.where(m==x)[1])
		mindimc = np.amin(np.where(m==x)[0])
		minc.add(mindimc)
		#print "min dimc: ", np.amin(np.where(m==x)[0])
		mindimr = np.amin(np.where(m==x)[1])
		minr.add(mindimr)
		#print "min dimr: ", np.amin(np.where(m==x)[1])
	#print minr, maxr, minc, maxc
	lminx = list(minr)
	lminx.sort()
	lmaxx = list(maxr)
	lmaxx.sort()
	lminy = list(minc)
	lminy.sort()
	lmaxy = list(maxc)
	lmaxy.sort()
	print "real mask: ", lminx, lmaxx, lminy, lmaxy
	#print lminr, lmaxr, lminc, lmaxc
	#print np.where(m==75)
	#for x in len(m):
		#print m[x]
	#print "mask2: ", np.shape(m), m
	#print "mask: ", np.nonzero(m)
	#print m,w


	# start camera or open video
	videoType, displayDiffs = imageTools.getVideoType(videoStream)
	cap = cv2.VideoCapture(videoStream)

	# adjust video resolution if necessary (sized to mask)
	print 'Camera resolution is %s x %s' % (str(m.shape[1]),str(m.shape[0]))
	cap.set(3,m.shape[1])
	cap.set(4,m.shape[0])
	# Set Pixel Threshold
	ret,frame = cap.read()
	storedImage = np.array(e * 255, dtype = np.uint8)
	# have to convert the float32 to uint8
	storedMode = imageTools.Blur(storedImage)
	storedFrame = imageTools.grayBlur(frame)
	pixThreshold = int(np.floor( pixThreshold * storedFrame.shape[0] ))
	print('PixelThreshold is %i') % pixThreshold
	cenData = np.zeros([ saveFreq, len(np.unique(w))*2 -2])
	pixData = np.zeros([ saveFreq, len(np.unique(w)) + 1])
	i = 0 # a counter for saving chunks of data
	totalFrames = 0
	startTime = datetime.now()
	oldTime = startTime
	elapsed = 0
	print('Analyzing motion data...')
	frame_roi = []
	while(cap.isOpened()):
		#print "frames", totalFrames
		ret,frame = cap.read()
		if ret == False:
			print 'End of Video'
			break
		currentFrame = imageTools.grayBlur(frame)
		currentFrame2 = imageTools.grayBlur(frame)
		diffpix = imageTools.diffImage(storedFrame,currentFrame2,pixThreshold,displayDiffs)
		#print np.shape(diffpix)
		#print diffpix # This is 660x1088
		diff = imageTools.trackdiffImage(storedMode,currentFrame,pixThreshold,displayDiffs)
		diff.dtype = np.uint8
		_,contours,hierarchy = cv2.findContours(diff, cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
		MIN_THRESH = 20.0
		MIN_THRESH_P = 20.0
		#if cv2.contourArea(contours[0]) > MIN_THRESH:
		#	M = cv2.moments(contours[0])
		#	cX = int(M["m10"] / M["m00"])
		#	cY = int(M["m01"] / M["m00"])
	#		print cX,cY
		roi_dict = {}
		for r in range(0,numberofwells):
			roi_dict[r+1] = []
		for cs in range(0,len(contours)):
			if cv2.contourArea(contours[cs]) > MIN_THRESH or cv2.arcLength(contours[cs],True) > MIN_THRESH_P:
			#if cv2.contourArea(contours[cs]) > MIN_THRESH and cv2.arcLength(contours[cs],True) > MIN_THRESH_P:
				M = cv2.moments(contours[cs])
				cX = int(M["m10"] / M["m00"])
				cY = int(M["m01"] / M["m00"])
				print i, " cX and cY:", cX, cY
				#print lmaxx
				#print lmaxy
				r=1
				c=1
				for x in range(0,len(lmaxx)):
					if cX > lmaxx[x]:
						r=x+2
						print "Lx,cX,lmaxx[x],lmin[x] ",x, cX, lmaxx[x], lminx[x]
				for y in range(0, len(lmaxy)):
					if cY > lmaxy[y]:
						c=y+2
						print "Ly,cY,maxy[y],lmin[x],r,c ",y, cY, lmaxy[y], lminy[y],r,c
				area = cv2.contourArea(contours[cs])
				perim = cv2.arcLength(contours[cs],True)
				perim = cv2.arcLength(contours[cs],True)
				print "L r + c + row[c]: ", r, c, rowold[c]," final well: ", r + rowold[c]
				if not roi_dict[r+rowold[c]]:
				#	roi_dict[r+row[c]].append((area*perim))
					roi_dict[r+rowold[c]].append((area*perim,cX,cY))
					#roi_dict[r+rowold[c]].append((area*perim,contours[cs]))
				else:
					if roi_dict[r+rowold[c]] < area*perim:
						roi_dict[r+rowold[c]][0] = (area*perim,cX,cY)
				print len(maxxs), maxxs, maxys, minxs, minys
				for x in range(0,len(maxxs)):
					if cX > maxxs[x]:
						r=x+1 # maybe DONT ADD TWO?
						#r=x+2 # maybe DONT ADD TWO?
						print "x,cX,maxx[x],minx[x],r,c: ",x, cX, maxxs[x], minxs[x],r,c
				for y in range(0, len(maxys)):
					if cY > maxys[y]:
						c=y+1
						#c=y+2
						print "y,cY,maxy[y].miny[y],r,c", y, cY, maxys[y], minys[y],r,c
				area = cv2.contourArea(contours[cs])
				perim = cv2.arcLength(contours[cs],True)
				print "r + c + r/8+1 + c/12: ", r, c, r/8+1, c/12, " row[c/12]: ", row[c/12], " final well: ", r/8 + 1 + row[c/12]
				if not roi_dict[r/8+1+row[c/12]]:
				#	roi_dict[r+row[c]].append((area*perim))
					roi_dict[r/8+1+row[c/12]].append((area*perim,cX,cY))
					#roi_dict[r+row[c]].append((area*perim,contours[cs]))
				else:
					if roi_dict[r/8+1+row[c/12]] < area*perim:
						roi_dict[r/8+1+row[c/12]][0] = (area*perim,cX,cY)
		frame_roi.append(roi_dict)

		timeDiff = 1. / frameRate
		elapsed = elapsed + timeDiff
		pixcounts = []
		pixcounts = np.bincount(w, weights=diffpix.ravel())
		pixData[i,:] = np.hstack((elapsed,pixcounts))

		counts = []
		keys = roi_dict.keys()
		keys.sort()
		for k in keys:
			x = -10000
			y = -10000
			if roi_dict[k]:
				x = roi_dict[k][0][1]
				y = roi_dict[k][0][2]
			counts.append(x)
			counts.append(y)
			cv2.line(storedImage,(x,y),(x,y),(255,255,255),2)
		if i == 284:
			cv2.imwrite('trackedimagewithlines_' + str(i) + ".png", storedImage)
		cenData[i,:] = np.asarray(counts)
		totalFrames += 1
		storedFrame = currentFrame
		i += 1

	file = open(videoStream + ".centroid2",'w')
	for x in range(0,285):
		for y in range(0,192):
			file.write(str(int(cenData[x,:][y])) + '\n')
	pixData = pixData[:i,:]
	pixData = pixData[:,2:] # get rid of timing column and background column
	file = open(videoStream + ".motion2",'w')
	file.write("12/8/2015" + '\015')
	for x in range(0,285):
		#print "x", x
		for y in range(0,numberofwells):
			file.write(str(int(pixData[x,:][y])) + '\n')
	vidInfo = {}
	# release camera
	cap.release()
	cv2.destroyAllWindows()
	return vidInfo
예제 #6
0
def main(pixThreshold, frameRate, videoStream):

    #elate_loc={(1,1):1, (1,2):2, (1,3):3, (1,4):4, (1:5):5, (1,6):6, (1,7):7, (1,8):8, (1,9):9,(1,10):10,(1,11):11,(1,12):12,(2,1):13,(2,2):14,(2,3):15,(2,4):16,(2:5):17,(2,6):18,(2,7):19,(2,8):20,(2,9):21,(2,10):22,(2,11):23,(2,12):24,(3,1):25,(3,2):26,(3,3):27,(3,4):28,(3,5):29,(3,6):30,(3,7)}
    col = {1: 0, 2: 12, 3: 24, 4: 36, 5: 48, 6: 60, 7: 72, 8: 84}

    expDuration = 600000  # duration of experiment, in seconds; only relevant for live feed
    saveFreq = 4500  # how often to save data, in frames

    i, m = imageTools.loadImageAndMask()
    e = imageTools.loadModeImage()

    #print e
    #cv2.imwrite('testinge.jpg', e)
    #cv2.imwrite('testingm.jpg', m)
    #cv2.imwrite('testingi.jpg', i)
    # convert mask to integer values for bincount weights
    np.set_printoptions(threshold=np.nan)  # printing entire array
    #print "mask1: ", np.shape(m), m
    m, w = imageTools.convertMaskToWeights(m)
    #print "mask2: ", np.shape(m), m
    unique = np.unique(m)
    #print np.shape(m)
    minr = set()
    minc = set()
    maxr = set()
    maxc = set()
    for x in unique:
        #print "x: ", x
        if x == 0:
            continue
        #print np.shape(np.where(m==x))
        #print np.where(m==x)
        maxdimc = np.amax(np.where(m == x)[0])
        maxc.add(maxdimc)
        #print "max dimc: ", np.amax(np.where(m==x)[0])
        maxdimr = np.amax(np.where(m == x)[1])
        maxr.add(maxdimr)
        #print "max dimr: ", np.amax(np.where(m==x)[1])
        mindimc = np.amin(np.where(m == x)[0])
        minc.add(mindimc)
        #print "min dimc: ", np.amin(np.where(m==x)[0])
        mindimr = np.amin(np.where(m == x)[1])
        minr.add(mindimr)
        #print "min dimr: ", np.amin(np.where(m==x)[1])
    #print minr, maxr, minc, maxc
    lminx = list(minr)
    lminx.sort()
    lmaxx = list(maxr)
    lmaxx.sort()
    lminy = list(minc)
    lminy.sort()
    lmaxy = list(maxc)
    lmaxy.sort()
    #print lminr, lmaxr, lminc, lmaxc
    #print np.where(m==75)
    #for x in len(m):
    #print m[x]
    #print "mask2: ", np.shape(m), m
    #print "mask: ", np.nonzero(m)
    #print m,w

    # start camera or open video
    videoType, displayDiffs = imageTools.getVideoType(videoStream)
    cap = cv2.VideoCapture(videoStream)

    # adjust video resolution if necessary (sized to mask)
    print 'Camera resolution is %s x %s' % (str(m.shape[1]), str(m.shape[0]))
    cap.set(3, m.shape[1])
    cap.set(4, m.shape[0])

    # Set Pixel Threshold
    ret, frame = cap.read()
    storedImage = np.array(e * 255, dtype=np.uint8)
    # have to convert the float32 to uint8, hopefully this is doing it correctly (found online)
    storedFrame = imageTools.Blur(storedImage)
    #storedFrame = imageTools.grayBlur(frame)
    pixThreshold = int(np.floor(pixThreshold * storedFrame.shape[0]))
    print('PixelThreshold is %i') % pixThreshold

    # Acquire data
    if saveFreq / frameRate > expDuration:  # do shorter of expDuration vs. saveFreq
        saveFreq = expDuration * frameRate

    pixData = np.zeros([saveFreq, len(np.unique(w)) * 2 - 2])

    i = 0  # a counter for saving chunks of data
    totalFrames = 0
    startTime = datetime.now()
    oldTime = startTime
    elapsed = 0

    print('Analyzing motion data...')

    moviedeq = []

    frame_roi = []

    while (cap.isOpened()):

        ret, frame = cap.read()

        if ret == False:
            print 'End of Video'
            break

        currentFrame = imageTools.grayBlur(frame)
        moviedeq.append(currentFrame)

        # stop experiment if user presses 'q' or if experiment duration is up
        if (cv2.waitKey(1) & 0xFF == ord('q') or len(sys.argv) == 1 and
                datetime.now() > startTime + timedelta(seconds=expDuration)):
            break

        diff = imageTools.trackdiffImage(storedFrame, currentFrame,
                                         pixThreshold, displayDiffs)
        diff.dtype = np.uint8
        _, contours, hierarchy = cv2.findContours(diff, cv2.RETR_TREE,
                                                  cv2.CHAIN_APPROX_NONE)
        MIN_THRESH = 20.0
        MIN_THRESH_P = 20.0
        #if cv2.contourArea(contours[0]) > MIN_THRESH:
        #	M = cv2.moments(contours[0])
        #	cX = int(M["m10"] / M["m00"])
        #	cY = int(M["m01"] / M["m00"])
        #		print cX,cY
        roi_dict = {}
        for r in range(0, 96):
            roi_dict[r + 1] = []
        for cs in range(0, len(contours)):
            if cv2.contourArea(contours[cs]) > MIN_THRESH or cv2.arcLength(
                    contours[cs], True) > MIN_THRESH_P:
                #if cv2.contourArea(contours[cs]) > MIN_THRESH and cv2.arcLength(contours[cs],True) > MIN_THRESH_P:
                M = cv2.moments(contours[cs])
                cX = int(M["m10"] / M["m00"])
                cY = int(M["m01"] / M["m00"])
                #print i, " cX and cY:", cX, cY
                #print lmaxx
                #print lmaxy
                r = 1
                c = 1
                for x in range(0, len(lmaxx)):
                    if cX > lmaxx[x]:
                        r = x + 2
                #		print x, cX, lmaxx[x], lminx[x]
                for y in range(0, len(lmaxy)):
                    if cY > lmaxy[y]:
                        c = y + 2
                #		print y, cY, lmaxy[y], lminy[y]
                area = cv2.contourArea(contours[cs])
                perim = cv2.arcLength(contours[cs], True)
                if not roi_dict[r + col[c]]:
                    #	roi_dict[r+col[c]].append((area*perim))
                    roi_dict[r + col[c]].append((area * perim, cX, cY))
                    #roi_dict[r+col[c]].append((area*perim,contours[cs]))
                else:
                    if roi_dict[r + col[c]] < area * perim:
                        roi_dict[r + col[c]][0] = (area * perim, cX, cY)
        frame_roi.append(roi_dict)

        timeDiff = 1. / frameRate
        elapsed = elapsed + timeDiff

        counts = []
        keys = roi_dict.keys()
        keys.sort()
        for k in keys:
            x = -10000
            y = -10000
            if roi_dict[k]:
                x = roi_dict[k][0][1]
                y = roi_dict[k][0][2]
            counts.append(x)
            counts.append(y)
            cv2.line(storedImage, (x, y), (x, y), (255, 255, 255), 2)
        cv2.imwrite('withlines' + str(i) + ".png", storedImage)
        pixData[i, :] = np.asarray(counts)
        totalFrames += 1

    file = open(videoStream + ".centroid2", 'w')
    for x in range(0, 285):
        for y in range(0, 192):
            file.write(str(int(pixData[x, :][y])) + '\n')

    # Save info (elapsed time and framerate) for later use
    vidInfo = {}
    analysisTime = timeStamp - startTime
    vidInfo['analysisTime'] = float(
        str(analysisTime.seconds) + '.' + str(analysisTime.microseconds))
    vidInfo['TotalFrames'] = totalFrames
    vidInfo['fps'] = int(totalFrames / vidInfo['analysisTime'])
    vidInfo['pixThreshold'] = pixThreshold
    vidInfo['CameraResolution'] = '%s x %s' % (str(m.shape[1]), str(
        m.shape[0]))
    vidInfo['NamePrefix'] = videoStream
    #vidInfo['NamePrefix'] =  videoStream.split('.')[0]

    print('Analyzed %i frames in %f seconds') % (vidInfo['TotalFrames'],
                                                 vidInfo['analysisTime'])
    print('FrameRate is about %i fps') % vidInfo['fps']
    print 'Motion threshold is %i pixels' % int(pixThreshold)
    print 'Camera resolution is %s' % vidInfo['CameraResolution']

    # release camera
    cap.release()
    cv2.destroyAllWindows()

    return vidInfo
예제 #7
0
def main(pixThreshold,frameRate,videoStream):
    row={0:0,1:12,2:24,3:36,4:48,5:60,6:72,7:84}
    saveFreq = 4500 # how often to save data, in frames, just making sure this is really big, so I don't have any issues, would be important for memory considerations if I was doing a long movie
    #print (videoStream)
    filenumber = videoStream.split('.')[0].split('_')[len(videoStream.split('.')[0].split('_'))-1]
   # print (filenumber)
#    if 1 <= int(filenumber) <= 20:
#        movielist = list(range(1,21))
#    if 21 <= int(filenumber) <= 40:
#        movielist = list(range(21,41))
#    if 41 <= int(filenumber) <= 60:
#        movielist = list(range(41,61))
#    if 61 <= int(filenumber) <= 70:
#        movielist = list(range(51,71))
#    if 71 <= int(filenumber) <= 80:
#        movielist = list(range(71,91))
#    if 81 <= int(filenumber) <= 478:
#        movielist = list(range(int(filenumber)-10,int(filenumber)+10))
#    if 479 <= filenumber <= 488:
#        movielist = list(range(468,488))
#    if 489 <= int(filenumber) <= 498:
#        movielist = list(range(489,509))
#    if 499 <= int(filenumber) <= 598:
#        movielist = list(range(int(filenumber)-10,int(filenumber)+10))
#    if 599 <= int(filenumber) <= 608:
#        movielist = list(range(599,609))
#    if 609 <= int(filenumber) <= 631:
#        movielist = list(range(609,632))
#    if 632 <= int(filenumber) <= 661:
#        movielist = list(range(632,662))
#    if 662 <= int(filenumber) <= 691:
#        movielist = list(range(662,692))
#    if 692 <= int(filenumber) <= 721:
#        movielist = list(range(692,722))
#    if 722 <= int(filenumber) <= 741:
#        movielist = list(range(722,742))
#    if 742 <= int(filenumber) <= 781:
#        movielist = list(range(742,782))
#    if 782 <= int(filenumber) <= 821:
#        movielist = list(range(782,822))
#    if 822 <= int(filenumber) <= 862:
#        movielist = list(range(822,862))
#    if 862 <= int(filenumber) <= 901:
#        movielist = list(range(862,901))
#    if 241 <= int(filenumber) <= 270:
#        movielist = list(range(241,271))
#    if 271 <= int(filenumber) <= 275:
#        movielist = list(range(271,276))
#    if 276 <= int(filenumber) <= 292:
#        movielist = list(range(276,293))
#    if 306 <= int(filenumber) <= 335:
#        movielist = list(range(306,336))
#    if 336 <= int(filenumber) <= 365:
#        movielist = list(range(336,366))
#    if 366 <= int(filenumber) <= 395:
#        movielist = list(range(366,396))
#    if 396 <= int(filenumber) <= 425:
#        movielist = list(range(396,426))
#    if 426 <= int(filenumber) <= 455:
#        movielist = list(range(426,456))
#    if 456 <= int(filenumber) <= 485:
#        movielist = list(range(456,486))
#    if 486 <= int(filenumber) <= 503:
#        movielist = list(range(486,504))

#    f = open(eventsfile, 'r')
#    lines = f.readlines()
#    numcounter = 0
#    counter = 0
#    fullcounter = 0
    #1:06:24\tPM\t0\t2\n'
#    movielist = []
#    movielists =[]
#    timestamp_list = []
#    filteredlist = [] 
#    startdate = "2020-02-26"
    
#    for line in lines:
#        TAPES = line.split('\t')
        #print(TAPES)
#        if int(TAPES[2]) == 1 or int(TAPES[2]) == 2:
#            filteredlist.append(line)
    
#    for newline in filteredlist:
#        print(line)
#        TAPES = newline.split('\t')
#        print(line)
#        something = TAPES[2]
#        fullcounter +=1
        #print "early", TAPES[2], TAPES[3]
        # need to be splitting on tab for the new format of the input file with the spaces in last column
        #TAPES = line.split('')
#        if int(TAPES[2]) == 2:
#             timestamp_list.append(0)
#             continue
#        startdate2 = startdate.split("-")[1] + "/" + startdate.split("-")[2] + "/" + startdate.split("-")[0]
#        dateplustime = startdate2 + TAPES[0][0:len(TAPES[0])]
#        thistime = faststrptime(dateplustime)
#        unixtimestamp = dt.timestamp(thistime)
#        timestamp_list.append(int(unixtimestamp))
     
#    i = 0    
#    for element in timestamp_list:

#        if i < (len(timestamp_list)-1) and timestamp_list[i+(counter - i)]-timestamp_list[i] >= 3600:
#           counter += 1
#           i = counter
#           movielist.append(counter)
#           
#           if len(movielist) <= 15:
#                numcounter = 0
#                j = 0
#                for step in movielist:
#                    movielists[len(movielists)-1].append(movielist[j])
#                    j += 1
#                movielist = []
#                continue   
 #          else:
  #              movielists.append(movielist)
   #             movielist = []
#                numcounter = 0
#                continue
            
#        if i < (len(timestamp_list)-1) and timestamp_list[i+1]-timestamp_list[i] >= 3600:
#           counter += 1
#           i = counter
#           movielist.append(counter)

#           if len(movielist) <= 15:
#                numcounter = 0
#                j = 0
#                for step in movielist:
#                    movielists[len(movielists)-1].append(movielist[j])
#                    j += 1
#                movielist = []
#                continue
#           else:
#                movielists.append(movielist)
#                movielist = []
#                numcounter = 0
#                continue
    
#        counter += 1
#        numcounter += 1
#        if element != 0:
#             movielist.append(counter)
#             i += 1

#        if numcounter == 30:
#            numcounter = 0
#            movielists.append(movielist)
#            movielist = []
        
#        if i > (len(timestamp_list)-1):
#            movielists.append(movielist)
#            movielist = []
#            numcounter = 0
#    print(movielists)        
#    numendlists = counter - fullcounter
#    first = len(movielists)-numendlists
#    last = len(movielists)
#    del movielists[first:last]

#    for x in movielists:
#        for y in x:
#            if int(filenumber) == y:
#                movielist = x
#    print(movielists)
    movielist = [1,2]
    modename = str(movielist[0]) + "to" + str(movielist[len(movielist)-1])
    #imageMode(movielist, modename)
    modefilename = "mode_" + modename + ".png"
    #print (movielist)
    try:
#        mpimg.imread(modefilename)
         imageTools.loadmodeImage(modefilename)
        #cv2.imread(modefilename)
    except:
         imageMode(movielist, modename)
    e = imageTools.loadmodeImage(modefilename)
    #e = imageTools.loadModeImage()
    roimask = np.zeros((660,1088))
    (maxxysnp, minxysnp) = max_min()
    maxxs = []
    minxs = []
    maxys = []
    minys = []
    for j in range (0, numberofwells*2,2):
        if maxxysnp[j] == -100:
            # if nothing ever moved in this well and there is no max or min value (could happen with a totally empty well)
            maxxs.append(np.nan)
            maxys.append(np.nan)
            minxs.append(np.nan)
            minys.append(np.nan)
        else:
            maxxs.append(maxxysnp[j])
            maxys.append(maxxysnp[j+1])
            minxs.append(minxysnp[j])
            minys.append(minxysnp[j+1])
    npmaxxs = np.asarray(maxxs)
    npmaxys = np.asarray(maxys)
    npminxs = np.asarray(minxs)
    npminys = np.asarray(minys)
    npmaxxs = np.reshape(npmaxxs, (numberofcols,numberofrows))
    npmaxys = np.reshape(npmaxys, (numberofcols,numberofrows)) #12,8
    npminxs = np.reshape(npminxs, (numberofcols,numberofrows))
    npminys = np.reshape(npminys, (numberofcols,numberofrows))
    #print npmaxxs
    #print npmaxys
    #print npminxs
    #print npminys
    cmaxxs = []
    cminxs = []
    cmaxys = []
    cminys = []
    for j2 in range (0, numberofwells):
        maxx = maxxs[well_conversion[j2]]
        maxy = maxys[well_conversion[j2]]
        miny = minys[well_conversion[j2]]
        minx = minxs[well_conversion[j2]]
        #print "j2: ", j2, well_conversion[j2], maxx, maxy, minx, miny
        #print "wcj2/8: ", well_conversion[j2]/8
        #print npmaxxs[well_conversion[j2]/8,:]
        #print npminxs[well_conversion[j2]/8,:]
        #print np.nanmean(npmaxxs[well_conversion[j2]/8,:])
        #print np.nanmean(npminxs[well_conversion[j2]/8,:])
        #print "j2/12: ", j2/12
        #print npmaxys[:,j2/12]
        #print npminys[:,j2/12]
        #print np.nanmean(npmaxys[:,j2/12])
        #print np.nanmean(npminys[:,j2/12])
        if minx == maxx:
            maxx = maxx + 2
        if miny == maxy:
            maxy = maxy + 2
        if math.isnan(float(maxx)): # could also add a condition if min and max are equal to each other
            #print "very first if statement"
            maxx = np.nanmean(npmaxxs[int(well_conversion[j2]/numberofrows),:])
            minx = np.nanmean(npminxs[int(well_conversion[j2]/numberofrows),:])
            maxy = np.nanmean(npmaxys[:,int(j2/numberofcols)])
            miny = np.nanmean(npminys[:,int(j2/numberofcols)])
            #print "new means: ", maxx, minx, maxy, miny
            # In the case that the entire row never gets any values in any wells and the mean is still NaN
            # not 100% sure that this is going to work, will get a runtime warning, so then can check it out??
            # mostly not sure about the well_conversions for the Xs
            if math.isnan(float(maxx)) and math.isnan(float(minx)):
        #        print "first if statement"
                if well_conversion[j2] < 8:
                    #print "2nd if statement"
                    minx = well_conversion[j2]
                    maxx = well_conversion[j2]+85
                else:
        #            print "2nd else statement"
                    minx = cminxs[well_conversion[j2]-8] + 85
                    maxx = cmaxxs[well_conversion[j2]-8] + 85
                ###if j2 <= 11:
        #            print "3rd if statement"
                ###    miny = j2
                ###    maxy = j2+60
                ###    minx = j2
            if math.isnan(float(maxy)) and math.isnan(float(miny)):
                #print "4th if statement"
                if j2 < 12:
                #    print "5th if statement"
                    miny = j2
                    maxy = j2+85
                else:
                #    print "5th else statement"
                    miny = cminys[j2-12] + 85
                    maxy = cmaxys[j2-12] + 85
        # End of untested section
        # hack, 06/11/18
        if miny == maxy:
            maxy = maxy+1
        if minx == maxx:
            maxx = maxx+1
        # end of hack

        cmaxxs.append(maxx)
        cmaxys.append(maxy)
        cminxs.append(minx)
        cminys.append(miny)
        #print (miny, maxy, minx, maxx, j2, j2+1)
        roimask[int(miny):int(maxy),int(minx):int(maxx)] = j2+1
    #np.set_printoptions(threshold=np.nan) # printing entire array
    cmaxxs.sort()
    cmaxys.sort()
    cminxs.sort()
    cminys.sort()
    rm,roimaskweights = imageTools.convertMaskToWeights(roimask)

    # start camera or open video
    videoType, displayDiffs = imageTools.getVideoType(videoStream)
    cap = cv2.VideoCapture(videoStream)

    # adjust video resolution if necessary (sized to mask)
    #print 'Camera resolution is %s x %s' % (str(roimask.shape[1]),str(roimask.shape[0]))
    cap.set(3,roimask.shape[1])
    cap.set(4,roimask.shape[0])
    # Set Pixel Threshold
    ret,frame = cap.read()
    storedImage = np.array(e * 255, dtype = np.uint8)
    # have to convert the float32 to uint8
    storedMode = imageTools.Blur(storedImage)
    storedFrame = imageTools.grayBlur(frame)
    #pixThreshold = int(np.floor( pixThreshold * storedFrame.shape[0] ))
    #print('PixelThreshold is %i') % pixThreshold
    #cenData = np.zeros([ saveFreq, len(np.unique(roimaskweights))*2])
#    print "roimaskweights: "
#    print np.shape(roimaskweights)
#    print roimaskweights
#    print "Unique: "
#    print np.unique(roimaskweights)
#    print (len(np.unique(roimaskweights))*2 - 2)
#    print np.shape(roimaskweights)
    cenData = np.zeros([ saveFreq, len(np.unique(roimaskweights))*2 -2])
#    print "cenData shape: ", np.shape(cenData)
    #print "testing hack below!"
    #cenData = np.zeros([4500, 192])
    pixData = np.zeros([ saveFreq, len(np.unique(roimaskweights))])
    #print "testing hack below!"
    #pixData = np.zeros([4500, 95])
    #pixData = np.zeros([ saveFreq, len(np.unique(roimaskweights))])
    i = 0 # a counter for saving chunks of data
    totalFrames = 0
#    startTime = datetime.now()
    #print('Analyzing motion data...')
    frame_roi = []
   # storedImage = cv2.cvtColor(e*255, cv2.COLOR_GRAY2BGR)
    completed_t = []
    f_t = []
    while(cap.isOpened()):
        ret,frame = cap.read()
        if ret == False:
            #print 'End of Video'
            break
        currentFrame = imageTools.grayBlur(frame)
        currentFrame2 = imageTools.grayBlur(frame)
        diffpix = imageTools.diffImage(storedFrame,currentFrame2,pixThreshold,displayDiffs)
        diff = imageTools.trackdiffImage(storedMode,currentFrame,pixThreshold,displayDiffs)
        #cv2.imwrite(videoStream + '_diffimage_' + str(i) + ".png", diff)
        diff.dtype = np.uint8
        contours,hierarchy = cv2.findContours(diff, cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
        area = 20
        fishfilter_top = []
        for t in contours:
            if area <= cv2.contourArea(t) < 200:
               (x,y), r = cv2.minEnclosingCircle(t)
               center = (int(x),int(y))
               fishfilter_top.append(center)
       # fishfiltertop = [cv2.minEnclosingCircle(t)[0] for t in contours if area <= cv2.contourArea(t) < 50]
        if i == 0:
           f_t = [Fish(i, fish) for fish in fishfilter_top]
          # print([pr.location for pr in p_t])
           number_of_colors = len(f_t)
        else:
           for n in f_t:
               n.nearby(fishfilter_top)
           newfish_t = [Fish(i, cord) for cord in fishfilter_top]
           #if i == 1:
             # print([pr.location for pr in newfish_t])
          # print(len(newfish_t))
           f_t = f_t + newfish_t
           xy_complete = list(filter(lambda x: x.completed, f_t))
           completed_t = completed_t + xy_complete
           f_t = list(filter(lambda x: not x.completed, f_t))
       # if i == 100:
        #   cv2.imwrite(videoStream + '_frame_' + str(i) + ".png", frame)
       # if i == 284:
        #   cv2.imwrite(videoStream + '_frame_' + str(i) + ".png", frame)
       # if i == 1:
        #   cv2.imwrite(videoStream + '_frame_' + str(i) + ".png", frame)
         #  img = cv2.drawContours(frame, contours, -1, (0, 255, 0), 2) 
          # cv2.imwrite(videoStream + '_contourimage_' + str(i) + ".png", img)
        MIN_THRESH = 20.0
        MIN_THRESH_P = 20.0
        roi_dict = {}
        for r in range(0,numberofwells):
            roi_dict[r+1] = []
        for cs in range(0,len(contours)):
            #print "area and lenght: ", cv2.contourArea(contours[cs]), cv2.arcLength(contours[cs], True)
            if cv2.contourArea(contours[cs]) < 1.0:
                continue
            if cv2.arcLength(contours[cs],True) < 1.0:
                continue
            if cv2.contourArea(contours[cs]) > MIN_THRESH or cv2.arcLength(contours[cs],True) > MIN_THRESH_P:
                M = cv2.moments(contours[cs])
                #print M
                cX = int(M["m10"] / M["m00"])
                cY = int(M["m01"] / M["m00"])
                #print "cX, cY :", cX, cY
                r=1
                c=1
                for x in range(0,len(cmaxxs)):
                    #print "cmaxxs: ", cmaxxs[x]
                    if cX > cmaxxs[x]:
                        r=x+1
                        #print "r: ", r
                #print "r: ", r
                for y in range(0, len(cmaxys)):
                    #print "cmaxys: ", cmaxys[x]
                    if cY > cmaxys[y]:
                        c=y+1
                    #    print "c: ", c
                if c == numberofwells:
                    c = c-1
                if r == numberofwells:
                    r = r-1
                area = cv2.contourArea(contours[cs])
                perim = cv2.arcLength(contours[cs],True)
                #print (c, numberofcols, c/numberofcols, r, numberofrows, r/numberofrows)
                #print ("TEST: ", roi_dict[int(r/numberofrows)+1+row[int(c/numberofcols)]], area*perim)
               # if not roi_dict[int(r/numberofrows)+1+row[int(c/numberofcols)]]:
               #     roi_dict[int(r/numberofrows)+1+row[int(c/numberofcols)]].append((area*perim,cX,cY))
               # else:
                    #print ("TEST2")
               #     if roi_dict[int(r/numberofrows)+1+row[int(c/numberofcols)]][0][0] < area*perim:
               #         roi_dict[int(r/numberofrows)+1+row[int(c/numberofcols)]][0] = (area*perim,cX,cY)
        frame_roi.append(roi_dict)

        pixcounts = []
        pixcounts = np.bincount(roimaskweights, weights=diffpix.ravel())
        pixData[i,:] = np.hstack((pixcounts))
        counts = []
        keys = roi_dict.keys()
        keys = sorted(keys)
        for k in keys:
            #print "k: ", k
            x = -10000
            y = -10000
            if roi_dict[k]:
                x = roi_dict[k][0][1]
                y = roi_dict[k][0][2]
            counts.append(x)
            counts.append(y)
            cv2.line(storedImage,(x,y),(x,y),(255,255,255),2)
          #  cv2.line(storedImage,(x,y),(x,y),(233,178,101),2)
        if i == 749:
            cv2.imwrite(videoStream + '_trackedimagewithlines_' + str(i) + ".png", storedImage)
        #print len(counts)
        cenData[i,:] = np.asarray(counts)
        totalFrames += 1
        storedFrame = currentFrame
        i += 1
   # print(len(p_t))
    all_xy = []
    all_xy = completed_t + f_t
    all_xy = sorted(all_xy, key=lambda x: len(x.location))
    all_xy.reverse()
    for n in all_xy:
        n.calcdistance()
       # print(n.totaldistance)
        print(n.displacement)
    all_xy = [fish for fish in all_xy if fish.timestamp + len(fish.location) > 745 and int(fish.displacement) > 10]
    
    myFrameNumber = 749
    cap = cv2.VideoCapture(videoStream)
    totalFrames = cap.get(cv2.CAP_PROP_FRAME_COUNT)

    if myFrameNumber >= 0 & myFrameNumber <= totalFrames:
       cap.set(cv2.CAP_PROP_POS_FRAMES,myFrameNumber)

    ret, frame = cap.read()
   # cv2.imwrite("lastframe.png", frame)
    image = plt.imread('lastframefilter.png')
   # implot = plt.imshow(image)
   # colours = []
   # for i in range(number_of_colors):
   #     r = random.randint(0,255)
   #     g = random.randint(0,255)
   #     b = random.randint(0,255)
   #     rgb = tuple([r,g,b])
   #     colours.append(rgb)
    plt.figure(figsize=(20,20))
    implot = plt.imshow(image)
   # fig,ax = plt.subplots(1)
  #  ax.set_aspect('equal')
  #  ax.axis('off')
    for i,fish in enumerate(all_xy):
      #  print(fish.location)
        xylist = [list(num) for num in fish.location]
        newlist = []
        newlist.append(xylist[0])
        for j in range(0,len(xylist)-1):
            distance =  math.sqrt((xylist[j+1][0]-xylist[j][0])*(xylist[j+1][0]-xylist[j][0]) + (xylist[j+1][1]-xylist[j][1])*(xylist[j+1][1]-xylist[j][1]))
            if distance > -1:
               newlist.append(xylist[j+1])
        xlist = [x[0] for x in newlist]
        ylist = [y[1] for y in newlist]
        r = float(random.randint(0,255)/255)
        g = float(random.randint(0,255)/255)
        b = float(random.randint(0,255)/255)
        num = round(random.uniform(0, 3), 2)
        palette = sns.cubehelix_palette(n_colors=6, start=num, rot=0, gamma=0.3, hue=0.8, light=1, dark=0, reverse=False, as_cmap=True)
       # palette = sns.light_palette((r, g, b), n_colors=1, reverse=False, as_cmap=True)
        plt.scatter(xlist,ylist, s=6, c=list(range(0,len(newlist))), cmap=palette)
       # for j,num in fish.location:
        #    print(type(j))
        #    coord = list(num)
        #    plt.scatter(coord[0],coord[1], s=1, cmap=palette
           # cv2.line(image,num,num,cmap[j],2)
       # if i == number_of_colors:
       #    r = random.randint(0,255)
       #    g = random.randint(0,255)
       #    b = random.randint(0,255)
       #    rgb = tuple([r,g,b])
       #    colours.append(rgb)
       #    number_of_colors += 1
   # cv2.imwrite("newimage.png", image)
    plt.savefig("omrlighttodark_forward.png")
   # print(len(all_xy))
    file = open(videoStream + ".centroid2",'w')
    for x in range(0,750):
        for y in range(0,1):
            file.write(str(int(cenData[x,:][y])) + '\n')
    pixData = pixData[:i,:]
    pixData = pixData[:,1:] # get rid of background column
    file = open(videoStream + ".motion2",'w')
    #file.write("12/8/2015" + '\015')
    for x in range(0,750):
        for y in range(0,numberofwells):
            file.write(str(int(pixData[x,:][y])) + '\n')
#    vidInfo = {}
    # release camera
    cap.release()
    cv2.destroyAllWindows()
예제 #8
0
def main(pixThreshold, frameRate, videoStream):

    expDuration = 600000  # duration of experiment, in seconds; only relevant for live feed
    saveFreq = 4500  # how often to save data, in frames

    i, m = imageTools.loadImageAndMask()

    # convert mask to integer values for bincount weights
    m, w = imageTools.convertMaskToWeights(m)
    #print m,w

    moviedeq = []
    i2 = 0
    for file in glob.glob("*_4*avi"):
        if i2 == 15:
            break
    #for x in range(0,5):

    # start camera or open video
        videoType, displayDiffs = imageTools.getVideoType(videoStream)
        #	print "testing: ", file
        #print "testing: ", videoStream.split('-')[x]
        cap = cv2.VideoCapture(file)
        #cap = cv2.VideoCapture(videoStream.split('-')[x])

        # adjust video resolution if necessary (sized to mask)
        #	print 'Camera resolution is %s x %s' % (str(m.shape[1]),str(m.shape[0]))
        #	cap.set(3,m.shape[1])
        #	cap.set(4,m.shape[0])

        # Set Pixel Threshold
        ret, frame = cap.read()
        storedFrame = imageTools.grayBlur(frame)
        #pixThreshold = int(np.floor( pixThreshold * storedFrame.shape[0] ))
        #print('PixelThreshold is %i') % pixThreshold

        # Acquire data
        if saveFreq / frameRate > expDuration:  # do shorter of expDuration vs. saveFreq
            saveFreq = expDuration * frameRate

        pixData = np.zeros([saveFreq, len(np.unique(w)) + 1])

        #i = 0 # a counter for saving chunks of data
        totalFrames = 0
        startTime = datetime.now()
        oldTime = startTime
        elapsed = 0

        print('Analyzing motion data...')

        #moviedeq = []

        while (cap.isOpened()):

            ret, frame = cap.read()

            if ret == False:
                print 'End of Video'
                break

            currentFrame = imageTools.grayBlur(frame)
            moviedeq.append(currentFrame)

            # stop experiment if user presses 'q' or if experiment duration is up
            if (cv2.waitKey(1) & 0xFF == ord('q')
                    or len(sys.argv) == 1 and datetime.now() >
                    startTime + timedelta(seconds=expDuration)):
                break

            # record pixel differences in all of the ROIs
            #diff = imageTools.diffImage(storedFrame,currentFrame,pixThreshold,0)

            timeDiff = 1. / frameRate
            elapsed = elapsed + timeDiff
            #	print elapsed

            # calculate and record pixel differences
            #counts = np.bincount(w, weights=diff.ravel())
            #	print counts # output
            #pixData[i,:] = np.hstack((elapsed,counts))
            totalFrames += 1

            storedFrame = currentFrame  # comment out if nothing is in first frame
            #oldTime = newTime
        i2 += 1

        # done recording. Remove empty rows (those bigger than i) from PixData
        #pixData = pixData[:i,:]
        #print pixData

    testing = calc_mode(moviedeq, np.zeros([660, 1088]))
    print "saving mode.png"
    cv2.imwrite('mode.png', testing)

    # Save info (elapsed time and framerate) for later use
    vidInfo = {}
    #	analysisTime = timeStamp - startTime
    #	vidInfo['analysisTime'] = float(str(analysisTime.seconds) + '.' + str(analysisTime.microseconds))
    #	vidInfo['TotalFrames'] = totalFrames
    #	vidInfo['fps'] = int(totalFrames/vidInfo['analysisTime'])
    #	#vidInfo['pixThreshold']=pixThreshold
    #	vidInfo['CameraResolution']='%s x %s' % (str(m.shape[1]),str(m.shape[0]))
    #vidInfo['NamePrefix'] =  videoStream.split('-')[x]
    #vidInfo['NamePrefix'] =  videoStream.split('.')[0]

    #	print ('Analyzed %i frames in %f seconds') % (vidInfo['TotalFrames'],vidInfo['analysisTime'])
    #	print('FrameRate is about %i fps') % vidInfo['fps']
    #	print 'Motion threshold is %i pixels' % int(pixThreshold)
    #	print 'Camera resolution is %s' % vidInfo['CameraResolution']

    # release camera
    cap.release()
    cv2.destroyAllWindows()

    return vidInfo