def MyImageReadMSH(index): global g """ common.DebugPrint("MyImageReadMSH(): initFrame = %d)" % \ (config.initFrame[g.indexVideo])); index += config.initFrame[g.indexVideo]; """ """ common.DebugPrint("Entered MyImageReadMSH(capture=%s, index=%s)" % \ (str(capture), str(index))); """ common.DebugPrint("Entered MyImageReadMSH(index=%s)" % \ (str(index))) """ We must reopen the capture device in each different process, otherwise the program blocks at the first operation in the "global" capture device. """ if g.harlocsFolder.endswith(config.HARRIS_QUERY_FOLDER_NAME): if g.captureQ == None: capture = cv2.VideoCapture(sys.argv[1]) g.captureQ = capture common.DebugPrint("MyImageReadMSH(): new capture=%s" % \ (str(capture))) else: capture = g.captureQ elif g.harlocsFolder.endswith(config.HARRIS_REFERENCE_FOLDER_NAME): if g.captureR == None: capture = cv2.VideoCapture(sys.argv[2]) g.captureR = capture common.DebugPrint("MyImageReadMSH(): new capture=%s" % \ (str(capture))) else: capture = g.captureR else: assert False assert (g.indexVideo == 0) or (g.indexVideo == 1) if config.OCV_OLD_PY_BINDINGS: capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, \ index) else: """ From http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get: <<CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.>> """ capture.set(cv2.CAP_PROP_POS_FRAMES, \ index) common.DebugPrint("MyImageReadMSH(): after capture.set()") # This is only for (paranoid) testing purposes: if config.OCV_OLD_PY_BINDINGS: indexCrt = capture.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) else: """ From http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get: <<CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.>> """ indexCrt = capture.get(cv2.CAP_PROP_POS_FRAMES) #assert int(indexCrt) == index; #!!!!TODO: think if OK if int(indexCrt) != index: common.DebugPrint( \ "MyImageReadMSH(): indexCrt != index --> returning black frame") ret = False else: #common.DebugPrint("Alex: frameR = %d" % frameR); #if myIndex > numFramesR: # break; #ret, img = r_path.read(); ret, img = capture.read() #if ret == False: # break; #!!!!TODO: think if well #assert ret == True; if ret == False: common.DebugPrint( "MyImageReadMSH(index=%d): ret == False --> returning None" % index) img = None else: common.DebugPrint("MyImageReadMSH(): img.shape = %s" % str(img.shape)) common.DebugPrint("MyImageReadMSH(): img.dtype = %s" % str(img.dtype)) #!!!!TODO: I suggest to do the gray conversion at reading, not in multi_scale_harris.py if False: # In the Matlab code he reads gray/8bpp JPEGs imgGray = common.ConvertImgToGrayscale(img) if config.VIDEO_FRAME_RESIZE_SCALING_FACTOR != 1: # We resize the image img = Matlab.imresize(img, \ scale=config.VIDEO_FRAME_RESIZE_SCALING_FACTOR) common.DebugPrint("Exiting MyImageReadMSH()") if False: return imgGray return img
def MyImageReadMSH(index): global g; """ common.DebugPrint("MyImageReadMSH(): initFrame = %d)" % \ (config.initFrame[g.indexVideo])); index += config.initFrame[g.indexVideo]; """ """ common.DebugPrint("Entered MyImageReadMSH(capture=%s, index=%s)" % \ (str(capture), str(index))); """ common.DebugPrint("Entered MyImageReadMSH(index=%s)" % \ (str(index))); """ We must reopen the capture device in each different process, otherwise the program blocks at the first operation in the "global" capture device. """ if g.harlocsFolder.endswith(config.HARRIS_QUERY_FOLDER_NAME): if g.captureQ == None: capture = cv2.VideoCapture(sys.argv[1]); g.captureQ = capture; common.DebugPrint("MyImageReadMSH(): new capture=%s" % \ (str(capture))); else: capture = g.captureQ; elif g.harlocsFolder.endswith(config.HARRIS_REFERENCE_FOLDER_NAME): if g.captureR == None: capture = cv2.VideoCapture(sys.argv[2]); g.captureR = capture; common.DebugPrint("MyImageReadMSH(): new capture=%s" % \ (str(capture))); else: capture = g.captureR; else: assert False; assert (g.indexVideo == 0) or (g.indexVideo == 1); if config.OCV_OLD_PY_BINDINGS: capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, \ index); else: """ From http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get: <<CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.>> """ capture.set(cv2.CAP_PROP_POS_FRAMES, \ index); common.DebugPrint("MyImageReadMSH(): after capture.set()"); # This is only for (paranoid) testing purposes: if config.OCV_OLD_PY_BINDINGS: indexCrt = capture.get(cv2.cv.CV_CAP_PROP_POS_FRAMES); else: """ From http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get: <<CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.>> """ indexCrt = capture.get(cv2.CAP_PROP_POS_FRAMES); #assert int(indexCrt) == index; #!!!!TODO: think if OK if int(indexCrt) != index: common.DebugPrint( \ "MyImageReadMSH(): indexCrt != index --> returning black frame"); ret = False; else: #common.DebugPrint("Alex: frameR = %d" % frameR); #if myIndex > numFramesR: # break; #ret, img = r_path.read(); ret, img = capture.read(); #if ret == False: # break; #!!!!TODO: think if well #assert ret == True; if ret == False: common.DebugPrint( "MyImageReadMSH(index=%d): ret == False --> returning None" % index); img = None; else: common.DebugPrint("MyImageReadMSH(): img.shape = %s" % str(img.shape)); common.DebugPrint("MyImageReadMSH(): img.dtype = %s" % str(img.dtype)); #!!!!TODO: I suggest to do the gray conversion at reading, not in multi_scale_harris.py if False: # In the Matlab code he reads gray/8bpp JPEGs imgGray = common.ConvertImgToGrayscale(img); if config.VIDEO_FRAME_RESIZE_SCALING_FACTOR != 1: # We resize the image img = Matlab.imresize(img, \ scale=config.VIDEO_FRAME_RESIZE_SCALING_FACTOR); common.DebugPrint("Exiting MyImageReadMSH()"); if False: return imgGray; return img;
def ComputeHarlocs(capture, counterStep, folderName, fileNamePrefix, fileNameExtension=".csv", indexVideo=-1): print( \ "Entered ComputeHarlocs(capture=%s, counterStep=%d, folderName=%s, " \ "indexVideo=%d)" % \ (str(capture), counterStep, folderName, indexVideo)) harlocsFolder = config.VIDEOS_FOLDER + "/" + folderName t1 = float(cv2.getTickCount()) harlocs = [] if config.OCV_OLD_PY_BINDINGS: numFrames = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) else: numFrames = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) common.DebugPrint("ComputeHarlocs(): numFrames = %d" % numFrames) if not os.path.exists(harlocsFolder): os.makedirs(harlocsFolder) else: #!!!!TODO: check that the loaded Harlocs are complete - same frame numbers as in the videos # Folder with precomputed Harris features exists folderContent = os.listdir(harlocsFolder) sortedFolderContent = sorted(folderContent) for fileName in sortedFolderContent: pathFileName = harlocsFolder + "/" + fileName """ common.DebugPrint("ComputeHarlocs(): pathFileName = %s" % pathFileName); common.DebugPrint("ComputeHarlocs(): fileName = %s" % fileName); """ if os.path.isfile(pathFileName) and \ fileName.startswith(fileNamePrefix) and \ pathFileName.endswith(fileNameExtension): common.DebugPrint("ComputeHarlocs(): Loading %s" % pathFileName) harrisFeatures = multi_scale_harris.LoadMultiScaleHarrisFeatures( pathFileName) harlocs.append(harrisFeatures) if config.endFrame[indexVideo] == -1: assert (len(harlocs) + config.initFrame[indexVideo]) == numFrames #!!!!TODO: if condition is NOT met, give a nicer error, or redo computations of Harlocs else: assert (len(harlocs) + config.initFrame[indexVideo] ) == config.endFrame[indexVideo] + 1 #!!!!TODO: if condition is NOT met, give a nicer error, or redo computations of Harlocs return harlocs if config.USE_MULTITHREADING == True: global g g.captureQ = None # We need to reopen the capture device in each process, separately g.captureR = None # We need to reopen the capture device in each process, separately #g.capture = capture; g.harlocsFolder = harlocsFolder g.fileNamePrefix = fileNamePrefix g.fileNameExtension = fileNameExtension g.indexVideo = indexVideo if config.OCV_OLD_PY_BINDINGS: frameCount = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) else: frameCount = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) #listParams = range(frameCount); listParams = range(config.initFrame[indexVideo], frameCount, counterStep) common.DebugPrint("ComputeHarlocs(): frameCount = %d" % frameCount) print("ComputeHarlocs(): Spawning a pool of %d workers" % \ config.numProcesses) """ # DEBUG purposes ONLY - since when we use Pool() and call function, if # we have an error in the function the exception reported is very # vague... for i in listParams: IterationStandaloneMSH(i); #import time #time.sleep(1000); """ """ Start worker processes to use on multi-core processor (circumvent also the GIL issue). """ pool = multiprocessing.Pool(processes=config.numProcesses) print("ComputeHarlocs(): Spawned a pool of %d workers" % \ config.numProcesses) #res = pool.map(IterationStandaloneMSH, listParams); # See https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.pool res = pool.map(func=IterationStandaloneMSH, iterable=listParams, \ chunksize=1) print("Pool.map returns %s" % str(res)) """ From https://medium.com/building-things-on-the-internet/40e9b2b36148 close the pool and wait for the work to finish """ pool.close() pool.join() #!!!!TODO: do more efficient - don't load the results from the CSV files return ComputeHarlocs(capture, counterStep, folderName, \ fileNamePrefix, fileNameExtension, indexVideo) #return []; #indexHarloc = 0; while True: if config.OCV_OLD_PY_BINDINGS: framePos = capture.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) else: """ From http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get: <<CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.>> """ framePos = capture.get(cv2.CAP_PROP_POS_FRAMES) common.DebugPrint("ComputeHarlocs(): framePos = %d" % framePos) counter = int(framePos) #0 common.DebugPrint("ComputeHarlocs(): counter = %d" % counter) ret, img = capture.read() if common.MY_DEBUG_STDOUT: common.DebugPrint("ComputeHarlocs(): img = %s" % str(img)) if False and config.SAVE_FRAMES: fileName = config.IMAGES_FOLDER + "/img_%05d.png" % counter if not os.path.exists(fileName): #print "dir(img) = %s"% str(dir(img)) """ imgCV = cv.fromarray(img) cv2.imwrite(fileName, imgCV) """ cv2.imwrite(fileName, img) #if ret == False: #MatchFrames.counterQ == 3: if (ret == False) or ((counter > numFrames) or \ (config.endFrame[indexVideo] != -1 and \ counter > config.endFrame[indexVideo])): break if config.VIDEO_FRAME_RESIZE_SCALING_FACTOR != 1: img = Matlab.imresize(img, \ scale=config.VIDEO_FRAME_RESIZE_SCALING_FACTOR) im = img pp = multi_scale_harris.multi_scale_harris(im, nos, disp=0) # n=0:nos-1 #harlocs = pp harlocs.append(pp) multi_scale_harris.StoreMultiScaleHarrisFeatures( \ harlocsFolder + "/" + fileNamePrefix + "%05d%s" % \ (counter, fileNameExtension), pp) counter += counterStep # If we try to seek to a frame out-of-bounds frame it gets to the last one if config.OCV_OLD_PY_BINDINGS: capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, counter) else: capture.set(cv2.CAP_PROP_POS_FRAMES, counter) #indexHarloc += 1; t2 = float(cv2.getTickCount()) myTime = (t2 - t1) / cv2.getTickFrequency() common.DebugPrint("ComputeHarlocs(): computing the multiscale harlocs " \ "took %.6f [sec]" % myTime) #common.DebugPrint("ComputeHarlocs(): len(harlocs) = %s" % str(len(harlocs))); if False: for i, h in enumerate(harlocs): #common.DebugPrint("ComputeHarlocs(): len(harlocs[%d]) = %d" % \ # (i, len(h))); multi_scale_harris.StoreMultiScaleHarrisFeatures( harlocsFolder + "/" + fileNamePrefix + "%05d.txt" % i, h) #if False: if common.MY_DEBUG_STDOUT: common.DebugPrint("ComputeHarlocs(): harlocs = %s" % str(harlocs)) return harlocs
def ComputeHarlocs(capture, counterStep, folderName, fileNamePrefix, fileNameExtension=".csv", indexVideo=-1): print( \ "Entered ComputeHarlocs(capture=%s, counterStep=%d, folderName=%s, " \ "indexVideo=%d)" % \ (str(capture), counterStep, folderName, indexVideo)); harlocsFolder = config.VIDEOS_FOLDER + "/" + folderName; t1 = float(cv2.getTickCount()); harlocs = []; if config.OCV_OLD_PY_BINDINGS: numFrames = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)); else: numFrames = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)); common.DebugPrint("ComputeHarlocs(): numFrames = %d" % numFrames); if not os.path.exists(harlocsFolder): os.makedirs(harlocsFolder); else: #!!!!TODO: check that the loaded Harlocs are complete - same frame numbers as in the videos # Folder with precomputed Harris features exists folderContent = os.listdir(harlocsFolder); sortedFolderContent = sorted(folderContent); for fileName in sortedFolderContent: pathFileName = harlocsFolder + "/" + fileName; """ common.DebugPrint("ComputeHarlocs(): pathFileName = %s" % pathFileName); common.DebugPrint("ComputeHarlocs(): fileName = %s" % fileName); """ if os.path.isfile(pathFileName) and \ fileName.startswith(fileNamePrefix) and \ pathFileName.endswith(fileNameExtension): common.DebugPrint("ComputeHarlocs(): Loading %s" % pathFileName); harrisFeatures = multi_scale_harris.LoadMultiScaleHarrisFeatures(pathFileName); harlocs.append(harrisFeatures); if config.endFrame[indexVideo] == -1: assert (len(harlocs) + config.initFrame[indexVideo]) == numFrames; #!!!!TODO: if condition is NOT met, give a nicer error, or redo computations of Harlocs else: assert (len(harlocs) + config.initFrame[indexVideo]) == config.endFrame[indexVideo] + 1; #!!!!TODO: if condition is NOT met, give a nicer error, or redo computations of Harlocs return harlocs; if config.USE_MULTITHREADING == True: global g; g.captureQ = None; # We need to reopen the capture device in each process, separately g.captureR = None; # We need to reopen the capture device in each process, separately #g.capture = capture; g.harlocsFolder = harlocsFolder; g.fileNamePrefix = fileNamePrefix; g.fileNameExtension = fileNameExtension; g.indexVideo = indexVideo; if config.OCV_OLD_PY_BINDINGS: frameCount = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)); else: frameCount = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)); #listParams = range(frameCount); listParams = range(config.initFrame[indexVideo], frameCount, counterStep); common.DebugPrint("ComputeHarlocs(): frameCount = %d" % frameCount); print("ComputeHarlocs(): Spawning a pool of %d workers" % \ config.numProcesses); """ # DEBUG purposes ONLY - since when we use Pool() and call function, if # we have an error in the function the exception reported is very # vague... for i in listParams: IterationStandaloneMSH(i); #import time #time.sleep(1000); """ """ Start worker processes to use on multi-core processor (circumvent also the GIL issue). """ pool = multiprocessing.Pool(processes=config.numProcesses); print("ComputeHarlocs(): Spawned a pool of %d workers" % \ config.numProcesses); #res = pool.map(IterationStandaloneMSH, listParams); # See https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.pool res = pool.map(func=IterationStandaloneMSH, iterable=listParams, \ chunksize=1); print("Pool.map returns %s" % str(res)); """ From https://medium.com/building-things-on-the-internet/40e9b2b36148 close the pool and wait for the work to finish """ pool.close(); pool.join(); #!!!!TODO: do more efficient - don't load the results from the CSV files return ComputeHarlocs(capture, counterStep, folderName, \ fileNamePrefix, fileNameExtension, indexVideo); #return []; #indexHarloc = 0; while True: if config.OCV_OLD_PY_BINDINGS: framePos = capture.get(cv2.cv.CV_CAP_PROP_POS_FRAMES); else: """ From http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get: <<CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.>> """ framePos = capture.get(cv2.CAP_PROP_POS_FRAMES); common.DebugPrint("ComputeHarlocs(): framePos = %d" % framePos); counter = int(framePos); #0 common.DebugPrint("ComputeHarlocs(): counter = %d" % counter); ret, img = capture.read(); if common.MY_DEBUG_STDOUT: common.DebugPrint("ComputeHarlocs(): img = %s" % str(img)); if False and config.SAVE_FRAMES: fileName = config.IMAGES_FOLDER + "/img_%05d.png" % counter; if not os.path.exists(fileName): #print "dir(img) = %s"% str(dir(img)) """ imgCV = cv.fromarray(img) cv2.imwrite(fileName, imgCV) """ cv2.imwrite(fileName, img); #if ret == False: #MatchFrames.counterQ == 3: if (ret == False) or ((counter > numFrames) or \ (config.endFrame[indexVideo] != -1 and \ counter > config.endFrame[indexVideo])): break; if config.VIDEO_FRAME_RESIZE_SCALING_FACTOR != 1: img = Matlab.imresize(img, \ scale=config.VIDEO_FRAME_RESIZE_SCALING_FACTOR); im = img; pp = multi_scale_harris.multi_scale_harris(im, nos, disp=0); # n=0:nos-1 #harlocs = pp harlocs.append(pp); multi_scale_harris.StoreMultiScaleHarrisFeatures( \ harlocsFolder + "/" + fileNamePrefix + "%05d%s" % \ (counter, fileNameExtension), pp); counter += counterStep; # If we try to seek to a frame out-of-bounds frame it gets to the last one if config.OCV_OLD_PY_BINDINGS: capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, counter); else: capture.set(cv2.CAP_PROP_POS_FRAMES, counter); #indexHarloc += 1; t2 = float(cv2.getTickCount()); myTime = (t2 - t1) / cv2.getTickFrequency(); common.DebugPrint("ComputeHarlocs(): computing the multiscale harlocs " \ "took %.6f [sec]" % myTime); #common.DebugPrint("ComputeHarlocs(): len(harlocs) = %s" % str(len(harlocs))); if False: for i, h in enumerate(harlocs): #common.DebugPrint("ComputeHarlocs(): len(harlocs[%d]) = %d" % \ # (i, len(h))); multi_scale_harris.StoreMultiScaleHarrisFeatures( harlocsFolder + "/" + fileNamePrefix + "%05d.txt" % i, h); #if False: if common.MY_DEBUG_STDOUT: common.DebugPrint("ComputeHarlocs(): harlocs = %s" % str(harlocs)); return harlocs;