Beispiel #1
0
def enhance(f, file, *args):
  try:
    result = f(file, *args)
  except Exception as msg:
    print('错误内容=='+str(msg))
    traceback.print_exc()
    result = 'Fail'
  finally:
    imageProcess.clean()
  return jsonify(result=result)
Beispiel #2
0
def SR_vid(video, *steps):
    commandIn, commandOut, outputPath, width, height, start, stop, root, process = prepare(
        video, steps)
    pipeIn = sp.Popen(commandIn,
                      stdout=sp.PIPE,
                      stderr=sp.PIPE,
                      bufsize=bufsize)
    pipeOut = sp.Popen(commandOut,
                       stdin=sp.PIPE,
                       stdout=sp.PIPE,
                       stderr=sp.PIPE,
                       bufsize=bufsize,
                       shell=True)

    def p(raw_image=None):
        bufs = process((raw_image, height, width))
        if type(bufs) != type(None) and len(bufs):
            for buffer in bufs:
                if buffer:
                    pipeOut.stdin.write(buffer)
        if raw_image:
            root.trace()

    try:
        createEnqueueThread(pipeOut.stdout, 0)
        createEnqueueThread(pipeIn.stderr, 1)
        createEnqueueThread(pipeOut.stderr, 1)
        i = 0
        while i <= stop and not context.stopFlag.is_set():
            raw_image = pipeIn.stdout.read(
                width * height *
                pixBytes)  # read width*height*6 bytes (= 1 frame)
            if len(raw_image) == 0:
                break
            readSubprocess(qOut)
            if i >= start:
                p(raw_image)
            i += 1
            idle()
        p()

        pipeOut.communicate()
    finally:
        pipeIn.terminate()
        pipeOut.terminate()
        clean()
        try:
            os.remove(video)
        except:
            log.warning(
                'Timed out waiting ffmpeg to terminate, need to remove {} manually.'
                .format(video))
    readSubprocess(qOut)
    return outputPath, i
Beispiel #3
0
def batchSR(images, srpath, scale, mode, dnmodel, dnseq):
    count = 0
    process = imageProcess.genProcess(scale, mode, dnmodel, dnseq, 'file')
    for image in images:
        count += 1
        print('processing image {}'.format(image.filename))
        fileName = '{}{}.png'.format(srpath, count)
        try:
            process(image, fileName)
        except Exception as msg:
            print('错误内容==' + str(msg))
        finally:
            imageProcess.clean()
    return 'Success'
Beispiel #4
0
def SR_vid(video, by, *steps):
    def p(raw_image=None):
        bufs = process((raw_image, height, width))
        if (not bufs is None) and len(bufs):
            for buffer in bufs:
                if buffer:
                    procOut.stdin.write(buffer)
        if raw_image:
            root.trace()
        return 0 if bufs is None else len(bufs)

    context.stopFlag.clear()
    outputPath, process, *args = prepare(video, by, steps)
    start, stop, refs, root = args[:4]
    width, height, *more = getVideoInfo(video, by, *args[-3:])
    root.callback(root, dict(shape=[height, width], fps=more[0]))
    commandIn, commandVideo, commandOut = setupInfo(by, outputPath, *args[3:9],
                                                    start, width, height,
                                                    *more)
    procIn = popen(commandIn)
    procOut = sp.Popen(commandVideo,
                       stdin=sp.PIPE,
                       stdout=sp.PIPE,
                       stderr=sp.PIPE,
                       bufsize=0)
    procMerge = 0
    err = 0

    try:
        createEnqueueThread(procOut.stdout)
        createEnqueueThread(procIn.stderr)
        createEnqueueThread(procOut.stderr)
        i = 0
        frameBytes = width * height * pixBytes  # read 1 frame
        while (stop < 0 or i <= stop + refs) and not context.stopFlag.is_set():
            raw_image = procIn.stdout.read(frameBytes)
            if len(raw_image) == 0:
                break
            readSubprocess(qOut)
            if i >= start:
                p(raw_image)
            elif (i + 1) % 10 == 0:
                root.callback(root, dict(skip=i + 1))
            i += 1
            idle()
        os.kill(procIn.pid, sigint)
        if len(raw_image) == 0:  # tell VSR to pad frames
            arefs = 0 if stop <= 0 or i < stop else i - stop
            for step in steps:
                if arefs >= refs:
                    break
                if step['op'] == 'slomo':
                    refs = refs * step['sf'] + step[
                        'opt'].outEnd  # outEnd is negative
                    step['opt'].outEnd = 0
                    arefs = arefs * step['sf']
                elif step['op'] in padOp:
                    step['opt'].end = -min(refs - arefs, lookahead[step['op']])
                    refs += step['opt'].end
        p()

        procOut.communicate(timeout=300)
        procIn.terminate()
        readSubprocess(qOut)
        procMerge, err = mergeAV(commandOut)
    finally:
        log.info('Video processing end at frame #{}.'.format(i - refs))
        procIn.terminate()
        procOut.terminate()
        if procMerge:
            procMerge.terminate()
        clean()
        try:
            if not by:
                removeFile(video)
        except Exception:
            log.warning(
                'Timed out waiting ffmpeg to terminate, need to remove {} manually.'
                .format(video))
        if err:
            log.warning(
                'Unable to merge video and other tracks with exit code {}.'.
                format(err))
        else:
            outputPath = cleanAV(commandOut, outputPath)
    readSubprocess(qOut)
    return outputPath, i - refs
Beispiel #5
0
def SR_vid(video, by, *steps):
    def p(raw_image=None):
        bufs = process((raw_image, height, width))
        if (not bufs is None) and len(bufs):
            for buffer in bufs:
                if buffer:
                    procOut.stdin.write(buffer)
        if raw_image:
            root.trace()

    context.stopFlag.clear()
    outputPath, process, *args = prepare(video, by, steps)
    start, stop, root = args[:3]
    width, height, *more = getVideoInfo(video, by, *args[-3:])
    commandIn, commandVideo, commandOut = setupInfo(by, outputPath, *args[2:8],
                                                    start, width, height,
                                                    *more)
    procIn = popen(commandIn)
    procOut = sp.Popen(commandVideo,
                       stdin=sp.PIPE,
                       stdout=sp.PIPE,
                       stderr=sp.PIPE,
                       bufsize=0)
    procMerge = 0
    err = 0

    try:
        createEnqueueThread(procOut.stdout)
        createEnqueueThread(procIn.stderr)
        createEnqueueThread(procOut.stderr)
        i = 0
        while (stop < 0 or i <= stop) and not context.stopFlag.is_set():
            raw_image = procIn.stdout.read(
                width * height *
                pixBytes)  # read width*height*6 bytes (= 1 frame)
            if len(raw_image) == 0:
                break
            readSubprocess(qOut)
            if i >= start:
                p(raw_image)
            i += 1
            idle()
        os.kill(procIn.pid, sigint)
        p()

        procOut.communicate(timeout=300)
        procIn.terminate()
        readSubprocess(qOut)
        procMerge, err = mergeAV(commandOut)
    finally:
        log.info('Video processing end at frame #{}.'.format(i))
        procIn.terminate()
        procOut.terminate()
        if procMerge:
            procMerge.terminate()
        clean()
        try:
            if not by:
                removeFile(video)
        except:
            log.warning(
                'Timed out waiting ffmpeg to terminate, need to remove {} manually.'
                .format(video))
        if err:
            log.warning(
                'Unable to merge video and other tracks with exit code {}.'.
                format(err))
        else:
            outputPath = cleanAV(commandOut, outputPath)
    readSubprocess(qOut)
    return outputPath, i