def tracknSave_accWeight(imData, nFrame, weight, avg, imDir, displayRate, flyFirstFrame, extension): ''' tracks object in the given frame on the basis of parameters set in params tracking is done by cv2.accumulateWeighted Input parameters: imData: image array nFrame: frame sequence number weight: weight for the acuumulated weight for image averaging avg: the averaged image from previous frames imDir: directory for saving the image flyFirstFrame: boolean value for checking the first frame in the directory Output parameters: avg: the averaged image produced by cv2.accumulateWeighted using previous avg image np.std(frameDelta): standard deviation of the image array, this is used to determine movement in the image flyFirstFrame: boolean to determine if the image is first in the directory ''' small = cv2.resize(imData, (0,0), fx = processScaleFactor, fy = processScaleFactor) cv2.accumulateWeighted(small, avg, weight) frameDelta = cv2.absdiff(small, cv2.convertScaleAbs(avg)) if nFrame%displayRate==0: showIm(frameDelta, displayScale, imShowBorders, borderValue) if flyFirstFrame == True: startNT(cv2.imwrite,(imDir+str(nFrame)+extension,imData)) return avg, np.std(frameDelta), flyFirstFrame
def main(tiffStep): """Main function for multi object tracking """ #tiffStep = 512 # Create Object Detector detector = Detectors() # Create Object Tracker tracker = Tracker(200, 50, 25, 100) # Variables initialization pause = False track_colors = [random_color() for x in xrange(256)] # Infinite loop to process video frames stTmAv = time.time() outFName = imFolder + '_traced_0-' + str(tiffStep) + '.tiff' #memmap_image = tifffile.memmap(outFName, shape=(tiffStep, newx, newy, 3), dtype='uint8') imgs = np.zeros((tiffStep, newy, newx, 3), dtype=np.uint8) tTm = 0 stTm = time.time() for fr in xrange(len(flyContours[0])): # Capture frame-by-frame frame = getBgSubIm((flyContours[0][fr], flyContours[1])) frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) outFrame = cv2.cvtColor(flyContours[0][fr], cv2.COLOR_GRAY2BGR) # Detect and return centeroids of the objects in the frame centers = detector.DetectCM(frame) # If centroids are detected then track them if (len(centers) > 0): # Track object using Kalman Filter tracker.Update(centers) # For identified object tracks draw tracking line # Use various colors to indicate different track_id for i in range(len(tracker.tracks)): if (len(tracker.tracks[i].trace) > 1): for j in range(len(tracker.tracks[i].trace) - 1): # Draw trace line x1 = tracker.tracks[i].trace[j][0][0] y1 = tracker.tracks[i].trace[j][1][0] x2 = tracker.tracks[i].trace[j + 1][0][0] y2 = tracker.tracks[i].trace[j + 1][1][0] clr = tracker.tracks[i].track_id cv2.line(outFrame, (int(x1), int(y1)), (int(x2), int(y2)), track_colors[clr], 2) cv2.circle(outFrame, (int(x2), int(y2)), 2, track_colors[clr], 2) #cv2.circle(outFrame, (int(x1), int(y1)), 2, (255,25,255), 2) cv2.circle(outFrame, (int(x2), int(y2)), 2, track_colors[clr], 1) ## Display the resulting tracking frame #cv2.imshow('Tracking', outFrame) #cv2.waitKey(1) # outFName = imFolder+'_traced/'+flist[fr].split('/')[-1] # cv2.imwrite(outFName, outFrame) img = cv2.resize(outFrame, (newx, newy)) imN = (fr % tiffStep) if (imN == 0 and fr > 0): outFName = imFolder + '_traced_' + str( fr - tiffStep) + '-' + str(fr) + '.tiff' startNT(imageio.mimwrite, (outFName, imgs)) imgs = np.zeros((tiffStep, newy, newx, 3), dtype=np.uint8) #memmap_image = tifffile.memmap(outFName, shape=(tiffStep, newx, newy, 3), dtype='uint8') #memmap_image[imN] = img tm = time.time() fps = (tiffStep / (tm - stTm)) tTm += tm - stTm print('FPS: %0.3f (frame# %d)' % (fps, fr)) stTm = tm #else: # #print fr, imN imgs[imN] = img imageio.mimwrite( imFolder + '_traced_' + str( (fr / tiffStep) * tiffStep) + '-' + str(fr) + '.tiff', imgs[:imN]) print('Tracking average FPS: %0.3f' % (float(fr) / (time.time() - stTmAv)) ) #(1.0/(tm-stTm))) cv2.destroyAllWindows()
def getTrackedIms(dirname): saveFolder = dirname + '_tracked' try: os.mkdir(saveFolder) except: pass images, flyContours = getFlycontour(dirname, blurkernel=blurKernel, blk=block, cutOff=cutoff, ellaxisRatioMin=ellAxisRatioMin, ellaxisRatioMax=ellAxisRatioMax, flyareaMin=flyAreaMin, flyareaMax=flyAreaMax) print("done tracking, now displaying") displayMovie(images, flyContours, saveFolder, False, True, tFPS=10) from thread import start_new_thread as startNT print "Started processing directories at " + present_time() for rawDir in rawdirs: # print "----------Processing directoy: "+os.path.join(baseDir,rawDir)+'--------' d = os.path.join(baseDir, rawDir, imgDatafolder) imdirs = natural_sort([ os.path.join(d, name) for name in os.listdir(d) if os.path.isdir(os.path.join(d, name)) ]) for imdir in imdirs: startNT(getTrackedIms, (imdir, ))
header = '' for i in xrange(len(headers)): spacer+=',' header+=headers[i]+', ' startString = spacer+'Parameters' endString = spacer+'Done Processing' rawdirs = natural_sort([ name for name in os.listdir(baseDir) if os.path.isdir(os.path.join(baseDir, name)) ]) print "Started processing directories at "+present_time() for rawDir in rawdirs: # processRawDirs(rawDir) startNT(processRawDirs, (rawDir,)) ''' im1 = flist[0] img = cv2.imread(im1, cv2.IMREAD_GRAYSCALE) img = cv2.medianBlur(img,blurKernel) ret,th1 = cv2.threshold(img,90,190,cv2.THRESH_BINARY) th2 = cv2.adaptiveThreshold(img,200,cv2.ADAPTIVE_THRESH_MEAN_C,\ cv2.THRESH_BINARY,block,cutoff) th3 = cv2.adaptiveThreshold(img,200,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
def CapNProc(c, im, nFrames, imDir, baseDir, roiDir, saveIm, dirTime, framedelThresh, avgWeight, dirThresh, displayRate, extension): ''' Function for capturing images from the camera and then tracking already defined templates. The template images are updated (using ROIs defined earlier) everytime the function is called. Pressing 'Ctrl+c' pauses the tracking loop and starts displaying live images from the camera. This can be used to select new templates while the function is running. Input parameters: c: camera context, used for getting image buffer from the camera im: nFrames: total number of frames to be saved imDir: directory where all imaging folders will be saved baseDir: imaging directory roiDir: directory where all ROI images are saved saveIm: boolean to confirm whether to save images or not dirTime: present_time when this function was called and all images are saved in directory with this as directory name framedelThresh: threshold to determine whether the standard deviation of the frame is enough to detect movement and hence a walking fly avgWeight: weight for the funciton cv2.accummulatedWeighted to average over previous frames. Higher value means low number of previous frames are averaged dirThresh: Minimum number of files to be present in a folder, if number of files in an imaging folder is less than dirThresh, the folder is deleted ''' print present_time()+" Press 'Ctrl+C' to pause analysis and start live display" logFileWrite(present_time()) tick = datetime.now() prevFrame = 0 flyFirstFrame = False #variable to keep track when the fly entered the imaging arena avg = None # variable to hold the averaged image currDir = imDir+present_time()+'/' for nFrame in range (0,nFrames): try: if nFrame%250==0: tock = datetime.now() currFPS = (nFrame-prevFrame)/((tock -tick).total_seconds()) sys.stdout.write("\rAverage FPS: %0.4f"%(currFPS)) sys.stdout.flush() prevFrame = nFrame tick=tock c.retrieve_buffer(im) imData = np.array(im) if saveIm==True: if avg is None: print "\n[INFO] starting background model..." avg = cv2.resize(imData, (0,0), fx=processScaleFactor, fy=processScaleFactor).copy().astype("float") avg,frDelta, flyFirstFrame = tracknSave_accWeight(imData, nFrame, avgWeight,\ avg, currDir, displayRate, flyFirstFrame, extension) elif saveIm==False: if nFrame%1000==0: startNT(cv2.imwrite,(currDir+str(nFrame)+'.jpeg',imData,)) if frDelta > framedelThresh and flyFirstFrame == False: flyFirstFrame = True saveTick = datetime.now() currDir = imDir+present_time()+'/' try: os.mkdir(currDir) os.chdir(currDir) cv2.imwrite(imDir+ '../roi/' + os.getcwd().split('/')[-1] +\ '_in_'+str(nFrame)+'.jpeg',imData) # print "\rCreated new directory: "+ currDir except: pass if frDelta < framedelThresh and flyFirstFrame == True: flyFirstFrame = False saveTock = datetime.now() fList = os.listdir(currDir) try: cv2.imwrite(imDir+ '../roi/' + os.getcwd().split('/')[-1] +\ '_out_'+str(nFrame)+'_'+str(len(fList))+'.jpeg',imData) except: pass # if len(fList) < dirThresh: ## print str(len(fList)) + ' less frames' # try: # [os.remove(currDir+x) for x in fList] # rmDir = os.getcwd().split('/')[-1] # os.chdir(imDir) ## print imDir+rmDir # try: # roiList = glob(imDir+ '../roi/'+rmDir+'*.jpeg') # [os.remove(x) for x in roiList] # except: # print '\ncould not remove rois' # pass # #os.removedirs(imDir+rmDir) # sys.stdout.write('\rRemoved %s with %d frames\n'%(rmDir, len(fList))) # sys.stdout.flush() # except: # print "\rNot able to remove the dir with less images" # pass # else: # saveFPS = (float(len(fList))/((saveTock -saveTick).total_seconds())) # print ("\nNumber of frames saved in last dir (%s): %d at %0.2f FPS"\ # %(os.getcwd().split('/')[-1], len(fList), saveFPS)) # logFileWrite("\nNumber of frames saved in last dir (%s): %d at %0.2f FPS"\ # %(os.getcwd().split('/')[-1], len(fList), saveFPS)) saveFPS = (float(len(fList))/((saveTock -saveTick).total_seconds())) print ("\nNumber of frames saved in last dir (%s): %d at %0.2f FPS"\ %(os.getcwd().split('/')[-1], len(fList), saveFPS)) logFileWrite("\nNumber of frames saved in last dir (%s): %d at %0.2f FPS"\ %(os.getcwd().split('/')[-1], len(fList), saveFPS)) except KeyboardInterrupt: print "\nCamera display started on "+present_time() logFileWrite("Camera display started on "+present_time()) avgDisplay = displayCam(c, im) avg = cv2.resize(avgDisplay, (0,0), fx=processScaleFactor, fy=processScaleFactor).copy().astype("float") print "Camera display exited on "+present_time() logFileWrite("Camera display exited on "+present_time()) logFileWrite(present_time()) tick = datetime.now() logFileWrite('----------------------')
for i in range(0, nCam): c.connect(*c.get_camera_from_index(i)) if c.get_camera_info()['serial_number'] == camNumber: camSerial = i c.connect(*c.get_camera_from_index(camSerial)) p = c.get_property(fc2.FRAME_RATE) print "Frame Rate: "+str(p['abs_value']) logFileWrite("Camera Frame Rate: "+str(p['abs_value'])) im = fc2.Image() c.start_capture() imData = displayCam(c, im) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(imData, 'Starting Image', (400, 300), font, 2, (0,255,100),5) startNT(cv2.imwrite,(roiDir+'start.jpeg',imData)) for nLoop in range (startSegment,totalLoops): dirTime = present_time() print('========== Starting loop #%i =========='%(nLoop+1)) # saveDir = imDir+dirTime+'/' # os.mkdir(saveDir) CapNProc(c, im, nFrames, imDir, baseDir, roiDir, saveImDel, dirTime,\ framedelThresh, avgWeight, dirThresh, displayRate, extension) print "\r\nWaiting for loop number: "+str(nLoop+1) c.stop_capture() c.disconnect() logFileWrite('----------------------') # #