From 277d84cdbec93ea8ba2d65771295a1e794f0fe9b Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Wed, 28 Aug 2024 09:11:21 -0400 Subject: [PATCH] fix control api --- CHANGELOG.md | 1 + modules/api/control.py | 6 ++++-- modules/control/run.py | 24 +++++++++++++++--------- wiki | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63f3926f4..144887f88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ To use and of the new models, simply select model from *Networks -> Reference* a - fix Stable Cascade with custom schedulers, thanks @Disty0! - fix LoRA apply with force-diffusers - fix LoRA scales with force-diffusers +- fix control API ## Update for 2024-07-08 diff --git a/modules/api/control.py b/modules/api/control.py index 7c0607b34..b90dba049 100644 --- a/modules/api/control.py +++ b/modules/api/control.py @@ -143,7 +143,7 @@ class APIControl(): # prepare args args = req.copy(update={ # Override __init__ params "sampler_index": processing_helpers.get_sampler_index(req.sampler_name), - "is_generator": False, + "is_generator": True, "inputs": [helpers.decode_base64_to_image(x) for x in req.inputs] if req.inputs else None, "inits": [helpers.decode_base64_to_image(x) for x in req.inits] if req.inits else None, "mask": helpers.decode_base64_to_image(req.mask) if req.mask else None, @@ -164,8 +164,10 @@ class APIControl(): output_images += item[0] if item[0] is not None else [] output_processed += [item[1]] if item[1] is not None else [] output_info += item[2] if len(item) > 2 and item[2] is not None else '' - else: + elif isinstance(item, str): output_info += item + else: + pass shared.state.end(api=False) # return diff --git a/modules/control/run.py b/modules/control/run.py index ba736a4a8..2debcedec 100644 --- a/modules/control/run.py +++ b/modules/control/run.py @@ -380,7 +380,8 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini try: video = cv2.VideoCapture(inputs) if not video.isOpened(): - yield terminate(f'Control: video open failed: path={inputs}') + if is_generator: + yield terminate(f'Control: video open failed: path={inputs}') return [], '', '', 'Error: video open failed' frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) fps = int(video.get(cv2.CAP_PROP_FPS)) @@ -392,7 +393,8 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) shared.log.debug(f'Control: input video: path={inputs} frames={frames} fps={fps} size={w}x{h} codec={codec}') except Exception as e: - yield terminate(f'Control: video open failed: path={inputs} {e}') + if is_generator: + yield terminate(f'Control: video open failed: path={inputs} {e}') return [], '', '', 'Error: video open failed' while status: @@ -409,7 +411,8 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini continue if shared.state.interrupted: shared.state.interrupted = False - yield terminate('Control interrupted') + if is_generator: + yield terminate('Control interrupted') return [], '', '', 'Interrupted' # get input if isinstance(input_image, str): @@ -497,7 +500,8 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini except Exception: pass if any(img is None for img in processed_images): - yield terminate('Control: attempting process but output is none') + if is_generator: + yield terminate('Control: attempting process but output is none') return [], '', '', 'Error: output is none' if len(processed_images) > 1 and len(active_process) != len(active_model): processed_image = [np.array(i) for i in processed_images] @@ -515,7 +519,8 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini debug(f'Control: inputs match: input={len(processed_images)} models={len(selected_models)}') p.init_images = processed_images elif isinstance(selected_models, list) and len(processed_images) != len(selected_models): - yield terminate(f'Control: number of inputs does not match: input={len(processed_images)} models={len(selected_models)}') + if is_generator: + yield terminate(f'Control: number of inputs does not match: input={len(processed_images)} models={len(selected_models)}') return [], '', '', 'Error: number of inputs does not match' elif selected_models is not None: p.init_images = processed_image @@ -529,7 +534,8 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini p.task_args['ref_image'] = p.ref_image debug(f'Control: process=None image={p.ref_image}') if p.ref_image is None: - yield terminate('Control: attempting reference mode but image is none') + if is_generator: + yield terminate('Control: attempting reference mode but image is none') return [], '', '', 'Reference mode without image' elif unit_type == 'controlnet' and input_type == 1 and has_models: # Init image same as control p.task_args['control_image'] = p.init_images # switch image and control_image @@ -589,7 +595,8 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini # final check if has_models: if unit_type in ['controlnet', 't2i adapter', 'lite', 'xs'] and p.task_args.get('image', None) is None and getattr(p, 'init_images', None) is None: - yield terminate(f'Control: mode={p.extra_generation_params.get("Control mode", None)} input image is none') + if is_generator: + yield terminate(f'Control: mode={p.extra_generation_params.get("Control mode", None)} input image is none') return [], '', '', 'Error: Input image is none' # resize mask @@ -659,8 +666,7 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini msg = f'Control output | {index} of {frames} skip {video_skip_frames} | Frame {image_txt}' else: msg = f'Control output | {index} of {len(inputs)} | Image {image_txt}' - if is_generator: - yield (output_image, blended_image, msg) # result is control_output, proces_output + yield (output_image, blended_image, msg) # result is control_output, proces_output if video is not None and frame is not None: status, frame = video.read() diff --git a/wiki b/wiki index 205a79397..ffc3bed14 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit 205a79397883bfb77dc1a9b3616a7d6e7da4cd7f +Subproject commit ffc3bed145ff007908ebb65d95759aa3c3016838