尧图网络 高端网站定制 · 原创设计
免费咨询热线
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/11/9 0:18:15
如何构建一个基于CNN+BiLSTM+Attention模型来处理滚动轴承的故障诊断和寿命预测问题 结合CNN(卷积神经网络)、BiLSTM(双向长短期记忆网络)以及注意力机制Attention

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

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

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

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

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

📅 2026/10/8 14:54:01
MORE NEWS

更多资讯

📰

西宁市高压电工证考试太难?良心建议助你高效通关

西宁市高压电工证考试太难?良心建议助你高效通关 工地太忙根本没时间复习考试,这几乎是每个在一线作业的电工都面临的现实。尤其是西宁市高压电工证考试,内容繁杂、时间紧迫,很多人一想到要花时间复习,就打退堂鼓。但别急,本文给你一份 良心建议…

📰

电工证的补贴怎么申请多少钱 电工证补贴申请全攻略

电工证的补贴怎么申请多少钱 电工证补贴申请全攻略 找工作必须要有特种作业证,尤其是电工证,没有它连面试机会都没有。你现在是不是正愁不知道怎么申请电工证的补贴,又怕流程太复杂、 多少钱…

📰

在烟溪怎么考电工证值不值得考,工地太忙也能搞定

在烟溪怎么考电工证值不值得考,工地太忙也能搞定 工地太忙根本没时间复习考试,这几乎是每个在烟溪从事建筑或工程类工作的电工都面临的现实。你可能还在纠结 在烟溪怎么考电工证 ,也可能会犹豫这个证到底 值不值得考…

📰

湖南耒阳的电工证有效期多久?工地太忙根本没时间复习考试

湖南耒阳的电工证有效期多久?工地太忙根本没时间复习考试 工地太忙根本没时间复习考试,这几乎是所有一线电工的真实写照。尤其是湖南耒阳地区的建筑工地,施工节奏快、任务重,很多电工师傅都面临着“边干边学”的压力。但 湖南耒阳的电工证…

📰

五级电工证有哪些?内部消息告诉你怎么稳过实操考试

五级电工证有哪些?内部消息告诉你怎么稳过实操考试 实操考试心里没底怕挂科?别慌,五级电工证作为电工行业入门的“敲门砖”,其实没有你想象中那么难。但关键在于你有没有掌握正确的备考方法和考试技巧。尤其是新乡的应届工程类毕业生,很多人在报考后才发…

📰

低压电工证怎么辨真假 怎么报名才能顺利上岗

低压电工证怎么辨真假 怎么报名才能顺利上岗 你是不是正在找工作,但卡在了低压电工证怎么辨真假这个坎上?别急,今天就给你讲清楚怎么报名、怎么避坑、怎么判断证书真伪,确保你拿到的是能用的证,而不是一张废纸。…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

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

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

📞 💬