def main(args):
    global testJsonPath
    global outputPath
    if args.pydev:
        print("Enabling debugging with pydev")
        import pydevd
        pydevd.settrace(suspend=False)

    inputPath = args.inputJsonPath
    #outputPath = args.outputPath
    outputPath = "-"
    testJsonPath = args.testJsonPath
    numThreads = args.numThreads
    recursive = args.recursive
    fileFilter = args.filter

    print("Input path: {}\nOutput path: {}\n\n".format(inputPath, outputPath))

    # Initialize the Vam window
    vamWindow = VamWindow(idx=args.vamWindow)

    # Locating the buttons via image comparison does not reliably work. These coordinates are the 'Window' coordinates
    # found via AutoHotKey's Window Spy, cooresponding to the Load Preset button and the location of the test file
    vamWindow.setClickLocations([(130, 39), (248, 178)])

    print("Initializing worker processes...")
    poolWorkQueue = multiprocessing.Queue(maxsize=200)
    doneEvent = multiprocessing.Event()
    if numThreads > 1:
        pool = []
        for idx in range(numThreads):
            proc = multiprocessing.Process(target=worker_process_func,
                                           args=(idx, poolWorkQueue, doneEvent,
                                                 args))
            proc.start()
            pool.append(proc)
    else:
        pool = None
        doneEvent.set()

    angles = [0, 35]
    skipCnt = 0
    screenshots = deque(maxlen=2)
    for root, subdirs, files in os.walk(inputPath):
        print("Entering directory {}".format(root))
        for file in fnmatch.filter(files, fileFilter):
            try:
                anglesToProcess = [] + angles
                for angle in angles:
                    fileName = "{}_angle{}.png".format(
                        os.path.splitext(file)[0], angle)
                    fileName = os.path.join(root, fileName)
                    if os.path.exists(fileName) or os.path.exists(
                            "{}.failed".format(fileName)):
                        anglesToProcess.remove(angle)

                if len(anglesToProcess) == 0:
                    skipCnt += 1
                    #print("Nothing to do for {}".format(file))
                    continue
                print("Processing {} (after skipping {})".format(
                    file, skipCnt))
                skipCnt = 0

                if (GetKeyState(VK_CAPITAL) or GetKeyState(VK_SCROLL)):
                    print(
                        "WARNING: Suspending script due to Caps Lock or Scroll Lock being on. Push CTRL+PAUSE/BREAK or mash CTRL+C to exit script."
                    )
                    while GetKeyState(VK_CAPITAL) or GetKeyState(VK_SCROLL):
                        time.sleep(1)
                # Get screenshots of face and submit them to worker threads
                inputFile = os.path.join(root, file)
                face = VamFace(inputFile)
                for angle in anglesToProcess:
                    face.setRotation(angle)
                    face.save(testJsonPath)
                    vamWindow.loadLook()
                    start = time.time()

                    threshold = 850000000
                    minTime = .3
                    screenshots.append(vamWindow.getScreenShot())

                    while True:
                        screenshots.append(vamWindow.getScreenShot())
                        diff = ImageChops.difference(screenshots[0],
                                                     screenshots[1])
                        imageStat = ImageStat.Stat(diff)
                        s = sum(imageStat.sum2)
                        # Todo: Calculate where blue box will be
                        pix = diff.getpixel((574, 582))
                        if s < threshold and sum(
                                pix) < 50 and time.time() - start >= minTime:
                            break
                        #print( "{} < {}: {}, {} {}".format(s, threshold, s < threshold, pix, sum(pix) ) )
                    outputFileName = "{}_angle{}.png".format(
                        os.path.splitext(os.path.basename(inputFile))[0],
                        angle)
                    outputFileName = os.path.join(root, outputFileName)
                    poolWorkQueue.put((screenshots.pop(), outputFileName))

                    if pool is None:
                        worker_process_func(0, poolWorkQueue, doneEvent, args)
            except Exception as e:
                print("Failed to process {} - {}".format(file, str(e)))

        if not recursive:
            break

    print("Generator done!")
    doneEvent.set()
    if pool:
        for proc in pool:
            proc.join()
Exemple #2
0
def main( args ):
    global testJsonPath
    global outputPath
    if args.pydev:
        print("Enabling debugging with pydev")
        import pydevd
        pydevd.settrace(suspend=False)

    inputPath = args.inputJsonPath
    #outputPath = args.outputPath
    outputPath = "-"
    testJsonPath = args.testJsonPath
    numThreads = args.numThreads
    recursive = args.recursive
    fileFilter = args.filter


    print( "Input path: {}\nOutput path: {}\n\n".format( inputPath, outputPath ) )

    # Initialize the Vam window
    vamWindow = VamWindow()

    # Locating the buttons via image comparison does not reliably work. These coordinates are the 'Window' coordinates
    # found via AutoHotKey's Window Spy, cooresponding to the Load Preset button and the location of the test file
    vamWindow.setClickLocations([(130,39), (248,178)])

    print("Initializing worker processes...")
    poolWorkQueue = multiprocessing.Queue(maxsize=200)
    doneEvent = multiprocessing.Event()
    if numThreads > 1:
        pool = []
        for idx in range(numThreads):
            proc = multiprocessing.Process(target=worker_process_func, args=(idx, poolWorkQueue, doneEvent) )
            proc.start()
            pool.append( proc )
    else:
        pool = None
        doneEvent.set()


    angles = [0, 35, 65]
    for root, subdirs, files in os.walk(inputPath):
        print("Entering directory {}".format(root))
        for file in fnmatch.filter(files, fileFilter):
            print("Processing {}".format(file))
            try:
                alreadyDone = False
                for angle in angles:
                    fileName = "{}_angle{}.png".format( os.path.splitext(file)[0], angle)
                    fileName = os.path.join( root,fileName)
                    if os.path.exists(fileName):
                        alreadyDone = True
                        print("Output file already exists. Skipping.")
                        break

                if alreadyDone:
                    continue

                if (GetKeyState(VK_CAPITAL) or GetKeyState(VK_SCROLL)):
                    print("WARNING: Suspending script due to Caps Lock or Scroll Lock being on. Push CTRL+PAUSE/BREAK or mash CTRL+C to exit script.")
                    while GetKeyState(VK_CAPITAL) or GetKeyState(VK_SCROLL):
                        time.sleep(1)

                # Get screenshots of face and submit them to worker threads
                inputFile = os.path.join( root, file )
                face = VamFace(inputFile)
                for angle in angles:
                    face.setRotation(angle)
                    face.save( testJsonPath )
                    vamWindow.loadLook()
                    time.sleep(.3)
                    img = vamWindow.getScreenShot()

                    outputFileName = "{}_angle{}.png".format( os.path.splitext(os.path.basename(inputFile))[0], angle)
                    outputFileName = os.path.join( root, outputFileName )
                    poolWorkQueue.put( (img, outputFileName))

                    if pool is None:
                        worker_process_func(0, poolWorkQueue, doneEvent)
            except:
                print("Failed to process {}".format(file))

        if not recursive:
            break

    print("Generator done!")
    doneEvent.set()
    if pool:
        for proc in pool:
            proc.join()