Forum

Author Topic: How to show Python script progression to user?  (Read 2518 times)

bmartin

  • Newbie
  • *
  • Posts: 15
    • View Profile
How to show Python script progression to user?
« on: February 09, 2018, 12:15:55 AM »
Hello,

I have a script which masks automatically all the images in a chunk.  On my computer, it runs at about 3 images per seconds so masking all the images in a project with 2315 images takes more than 12 minutes. If I can't inform the user of the masking progress, there are very good chances that he will think that the program is stuck and will simply kill it...

I tried adding a print statement into my mask loop but what happens is that ALL the printing is done AFTER the function finishes, defeating the purpose of printing to inform the user of the processing progress. See below:

def CreateMasks(chunk):
   imagesMaskedCount = 0
   for cam in chunk.cameras:
      print("Image(" + str(imagesMaskedCount) + ") masked...") #Just printing after returning!!!
      src = cam.photo.image()

      openCVImg = AgisoftOpenCVUtils.AgisoftToOpenCVImage(src)
      gray = cv2.cvtColor(openCVImg, cv2.COLOR_BGR2GRAY)

      whiteColor = (255,255,255)

      # Create a mask supposed to cover everything that is white.
      mask = PhotoScan.Utils.createDifferenceMask(src, whiteColor, tolerance=5, fit_colors=False )
      m = PhotoScan.Mask()
      m.setImage(mask)

      # Assign this mask to our camera.
      cam.mask = m
      imagesMaskedCount = imagesMaskedCount+1


Is there a way to make the printing work "real time" instead of just in "batch" at the end of my long function?

Thanks,

Bruno
« Last Edit: February 12, 2018, 10:15:21 PM by bmartin »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14860
    • View Profile
Re: How to show Python script progression to user?
« Reply #1 on: February 13, 2018, 08:31:47 PM »
Hello Bruno,

You can modify the function as following:

Code: [Select]
from PySide2 import QtGui, QtCore, QtWidgets
app = QtWidgets.QApplication.instance()

def CreateMasks(chunk):
   imagesMaskedCount = 0
   print("Starting masking")
   app.processEvents()

   for cam in chunk.cameras:
      src = cam.photo.image()
      openCVImg = AgisoftOpenCVUtils.AgisoftToOpenCVImage(src)
      gray = cv2.cvtColor(openCVImg, cv2.COLOR_BGR2GRAY)
      whiteColor = (255,255,255)

      # Create a mask supposed to cover everything that is white.
      mask = PhotoScan.Utils.createDifferenceMask(src, whiteColor, tolerance=5, fit_colors=False )
      m = PhotoScan.Mask()
      m.setImage(mask)

      # Assign this mask to our camera.
      cam.mask = m
      imagesMaskedCount = imagesMaskedCount+1
      print("Image(" + str(imagesMaskedCount) + ") masked...") #Just printing after returning!!!

      app.processEvents()
Best regards,
Alexey Pasumansky,
Agisoft LLC

bmartin

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to show Python script progression to user?
« Reply #2 on: February 16, 2018, 10:56:35 PM »
Thanks!  That's exactly what I needed!

Bruno