尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
03.01.01.ComfyUI Ubuntu:环境搭建篇(集成 调用ComfyUI的API 使用Pythin 中间件方式)
总操作流程1、下载安装2、写代码3、测试下载安装pipinstallwebsocket-client uuid Pillowapi: https://docs.comfy.org/zh/development/comfyui-server/api-examples写代码方法一提交即忘仅 HTTPcat/usr/local/software/ComfyUI/script_examples/test_basic_api.pyEOF import json from urllib import request prompt_text { 3: { class_type: KSampler, inputs: { cfg: 8, denoise: 1, latent_image: [ 5, 0 ], model: [ 4, 0 ], negative: [ 7, 0 ], positive: [ 6, 0 ], sampler_name: euler, scheduler: normal, seed: 8566257, steps: 20 } }, 4: { class_type: CheckpointLoaderSimple, inputs: { ckpt_name: stabilityai/stable-diffusion-3-medium/sd3_medium_incl_clips_t5xxlfp16.safetensors } }, 5: { class_type: EmptyLatentImage, inputs: { batch_size: 1, height: 512, width: 512 } }, 6: { class_type: CLIPTextEncode, inputs: { clip: [ 4, 1 ], text: masterpiece best quality girl } }, 7: { class_type: CLIPTextEncode, inputs: { clip: [ 4, 1 ], text: bad hands } }, 8: { class_type: VAEDecode, inputs: { samples: [ 3, 0 ], vae: [ 4, 2 ] } }, 9: { class_type: SaveImage, inputs: { filename_prefix: ComfyUI, images: [ 8, 0 ] } } } def queue_prompt(prompt): p {prompt: prompt} data json.dumps(p).encode(utf-8) req request.Request(http://10.3.11.173:8188/prompt, datadata) request.urlopen(req) prompt json.loads(prompt_text) prompt[6][inputs][text] 画一只小猫咪 prompt[3][inputs][seed] 5 queue_prompt(prompt) EOF方法二WebSocket History监控执行完成cat/usr/local/software/ComfyUI/script_examples/test_websockets_api.pyEOF import websocket import uuid import json import urllib.request import urllib.parse server_address 10.3.11.173:8188 client_id str(uuid.uuid4()) def queue_prompt(prompt, prompt_id): p {prompt: prompt, client_id: client_id, prompt_id: prompt_id} data json.dumps(p).encode(utf-8) req urllib.request.Request(http://{}/prompt.format(server_address), datadata) urllib.request.urlopen(req).read() def get_image(filename, subfolder, folder_type): data {filename: filename, subfolder: subfolder, type: folder_type} url_values urllib.parse.urlencode(data) with urllib.request.urlopen(http://{}/view?{}.format(server_address, url_values)) as response: return response.read() def get_history(prompt_id): with urllib.request.urlopen(http://{}/history/{}.format(server_address, prompt_id)) as response: return json.loads(response.read()) def get_images(ws, prompt): prompt_id str(uuid.uuid4()) queue_prompt(prompt, prompt_id) output_images {} while True: out ws.recv() if isinstance(out, str): message json.loads(out) if message[type] executing: data message[data] if data[node] is None and data[prompt_id] prompt_id: break #Execution is done else: # If you want to be able to decode the binary stream for latent previews, here is how you can do it: # bytesIO BytesIO(out[8:]) # preview_image Image.open(bytesIO) # This is your preview in PIL image format, store it in a global continue #previews are binary data history get_history(prompt_id)[prompt_id] for node_id in history[outputs]: node_output history[outputs][node_id] images_output [] if images in node_output: for image in node_output[images]: image_data get_image(image[filename], image[subfolder], image[type]) images_output.append(image_data) output_images[node_id] images_output return output_images prompt_text { 3: { class_type: KSampler, inputs: { cfg: 8, denoise: 1, latent_image: [ 5, 0 ], model: [ 4, 0 ], negative: [ 7, 0 ], positive: [ 6, 0 ], sampler_name: euler, scheduler: normal, seed: 8566257, steps: 20 } }, 4: { class_type: CheckpointLoaderSimple, inputs: { ckpt_name: stabilityai/stable-diffusion-3-medium/sd3_medium_incl_clips_t5xxlfp16.safetensors } }, 5: { class_type: EmptyLatentImage, inputs: { batch_size: 1, height: 512, width: 512 } }, 6: { class_type: CLIPTextEncode, inputs: { clip: [ 4, 1 ], text: masterpiece best quality girl } }, 7: { class_type: CLIPTextEncode, inputs: { clip: [ 4, 1 ], text: bad hands } }, 8: { class_type: VAEDecode, inputs: { samples: [ 3, 0 ], vae: [ 4, 2 ] } }, 9: { class_type: SaveImage, inputs: { filename_prefix: ComfyUI, images: [ 8, 0 ] } } } prompt json.loads(prompt_text) #set the text prompt for our positive CLIPTextEncode prompt[6][inputs][text] 画一只大闸蟹 prompt[3][inputs][seed] 5 ws websocket.WebSocket() ws.connect(ws://{}/ws?clientId{}.format(server_address, client_id)) images get_images(ws, prompt) ws.close() EOF方法三WebSocket 配合 SaveImageWebsocket实时获取图片cat/usr/local/software/ComfyUI/script_examples/test_websockets_api_example_ws_images.pyEOF import websocket import uuid import json import urllib.request import urllib.parse server_address 10.3.11.173:8188 client_id str(uuid.uuid4()) def queue_prompt(prompt): p {prompt: prompt, client_id: client_id} data json.dumps(p).encode(utf-8) req urllib.request.Request(http://{}/prompt.format(server_address), datadata) return json.loads(urllib.request.urlopen(req).read()) def get_image(filename, subfolder, folder_type): data {filename: filename, subfolder: subfolder, type: folder_type} url_values urllib.parse.urlencode(data) with urllib.request.urlopen(http://{}/view?{}.format(server_address, url_values)) as response: return response.read() def get_history(prompt_id): with urllib.request.urlopen(http://{}/history/{}.format(server_address, prompt_id)) as response: return json.loads(response.read()) def get_images(ws, prompt): prompt_id queue_prompt(prompt)[prompt_id] output_images {} current_node while True: out ws.recv() if isinstance(out, str): message json.loads(out) if message[type] executing: data message[data] if data[prompt_id] prompt_id: if data[node] is None: break #Execution is done else: current_node data[node] else: if current_node save_image_websocket_node: images_output output_images.get(current_node, []) images_output.append(out[8:]) output_images[current_node] images_output return output_images prompt_text { 3: { class_type: KSampler, inputs: { cfg: 8, denoise: 1, latent_image: [ 5, 0 ], model: [ 4, 0 ], negative: [ 7, 0 ], positive: [ 6, 0 ], sampler_name: euler, scheduler: normal, seed: 8566257, steps: 20 } }, 4: { class_type: CheckpointLoaderSimple, inputs: { ckpt_name: stabilityai/stable-diffusion-3-medium/sd3_medium_incl_clips_t5xxlfp16.safetensors } }, 5: { class_type: EmptyLatentImage, inputs: { batch_size: 1, height: 512, width: 512 } }, 6: { class_type: CLIPTextEncode, inputs: { clip: [ 4, 1 ], text: masterpiece best quality girl } }, 7: { class_type: CLIPTextEncode, inputs: { clip: [ 4, 1 ], text: bad hands } }, 8: { class_type: VAEDecode, inputs: { samples: [ 3, 0 ], vae: [ 4, 2 ] } }, 9: { class_type: SaveImage, inputs: { filename_prefix: ComfyUI, images: [ 8, 0 ] } } } prompt json.loads(prompt_text) prompt[6][inputs][text] 画一只毛毛虫 prompt[3][inputs][seed] 5 ws websocket.WebSocket() ws.connect(ws://{}/ws?clientId{}.format(server_address, client_id)) images get_images(ws, prompt) ws.close() EOF测试# 本身就有的代码cd/usr/local/software/ComfyUI/script_exampleschmod0777-R*chown$USER:$USER-R*方法一提交即忘仅 HTTP# 运行时候查看ComfyUI输出的日志python /usr/local/software/ComfyUI/script_examples/test_basic_api.py# 自动生成的图片在如下文件夹ls/usr/local/software/ComfyUI/outputWebSocket History监控执行完成# 运行时候查看ComfyUI输出的日志python /usr/local/software/ComfyUI/script_examples/test_websockets_api.py# 自动生成的图片在如下文件夹ls/usr/local/software/ComfyUI/output方法三WebSocket 配合 SaveImageWebsocket实时获取图片# 运行时候查看ComfyUI输出的日志python /usr/local/software/ComfyUI/script_examples/test_websockets_api_example_ws_images.py# 自动生成的图片在如下文件夹ls/usr/local/software/ComfyUI/output
RELATED

相关推荐

基于 STM32 的智能防走失腰带设计与实现

基于 STM32 的智能防走失腰带设计与实现

一、系统概述 智能防走失腰带针对老人、儿童及认知障碍人群设计,以 STM32 为主控核心,集成 GPS 定位、GSM 通信、蓝牙近场告警、一键 SOS 求救等功能,当佩戴者超出安全范围或主动求助时,立即向监护人手机发送位置信息与告警信号。 二、硬件选型与接线 2.1 主控芯片选型 …

📅 2026/9/11 14:24:09
如何构建一个基于CNN+BiLSTM+Attention模型来处理滚动轴承的故障诊断和寿命预测问题 结合CNN(卷积神经网络)、BiLSTM(双向长短期记忆网络)以及注意力机制Attention

如何构建一个基于CNN+BiLSTM+Attention模型来处理滚动轴承的故障诊断和寿命预测问题 结合CNN(卷积神经网络)、BiLSTM(双向长短期记忆网络)以及注意力机制Attention

如何构建一个基于CNNBiLSTMAttention模型来处理滚动轴承的故障诊断和寿命预测问题 结合CNN(卷积神经网络)、BiLSTM(双向长短期记忆网络)以及注意力机制Attention 文章目录 数据准备数据预处理模型构建CNNBiLSTMAttention模型 模型…

📅 2026/9/11 14:23:50
CycleGAN 图像风格迁移:PyTorch 实现马到斑马转换,PSNR 达 28.5

CycleGAN 图像风格迁移:PyTorch 实现马到斑马转换,PSNR 达 28.5

CycleGAN实战:从马到斑马的图像风格迁移与PSNR优化1. 当计算机学会"艺术创作":CycleGAN的核心突破在计算机视觉领域,图像到图像的转换一直是个令人着迷的挑战。传统方法需要成对的训练数据——比如同一场景下马的图片和斑马的图片&…

📅 2026/7/11 13:22:49
MORE NEWS

更多资讯

📰

Jackett 种子站代理搜索:一个入口搜遍 700 个站,30 秒跑通

Jackett 种子站代理搜索:一个入口搜遍 700 个站,30 秒跑通 【免费下载链接】Jackett API Support for your favorite torrent trackers 项目地址: https://gitcode.com/GitHub_Trending/ja/Jackett 你正用 Sonarr 或 Radarr 管着媒体库&#xff0…

📰

ArduPilot飞控系统完整指南:一套代码如何自主驾驭5类无人平台?

ArduPilot飞控系统完整指南:一套代码如何自主驾驭5类无人平台? 【免费下载链接】ardupilot ArduPlane, ArduCopter, ArduRover, ArduSub source 项目地址: https://gitcode.com/GitHub_Trending/ar/ardupilot ArduPilot 是一款开源的飞控系统&…

📰

SystemInformer完整入门教程:5个实战技巧快速定位Windows资源占用与文件锁定

SystemInformer完整入门教程:5个实战技巧快速定位Windows资源占用与文件锁定 【免费下载链接】systeminformer A free, powerful, multi-purpose tool that helps you monitor system resources, debug software and detect malware. Brought to you by Winsider Se…

📰

CesiumJS体素渲染如何实现:从体积数据到实时可视化的完整实践指南

CesiumJS体素渲染如何实现:从体积数据到实时可视化的完整实践指南 【免费下载链接】cesium An open-source JavaScript library for world-class 3D globes and maps :earth_americas: 项目地址: https://gitcode.com/GitHub_Trending/ce/cesium 很多三维可视…

📰

Web端开源ER图工具选型:三款实战推荐

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

基于MATLAB的MIMO-OFDM仿真程序解析:从发射链路到误码率曲线

简介:这套基于MATLAB实现的MIMO-OFDM仿真程序,面向通信工程与信号处理方向的初学者和研究人员,是理解多天线正交频分复用系统原理及仿真的实用资料。压缩包共31个文件,含15个m函数文件(完成信道建模、调制解调、OFDM收…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬