Spaces:
Runtime error
Runtime error
Commit Β·
d2f86ce
1
Parent(s): a0e7842
WIP
Browse files11 unmodified lines12131415161718192046 unmodified lines676869676869707170717273747576777879777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721894952192202212222232242252262272282292302312322332342352362372382392409924124224324424524624724824925025125225325425525625711 unmodified linesfrom diffusers import FluxControlNetPipeline, FluxControlNetModelfrom diffusers.models import FluxMultiControlNetModel
import warningsfrom typing import Tuple
"""FLUXβ1 ControlNet demo----------------------46 unmodified lines# --------------------------------------------------MODE_MAPPING = { "canny": 0, "depth": 1, "openpose": 2, "gray": 3, "blur": 4, "tile": 5, "tile": 1, "depth": 2, "blur": 3, "pose": 4, "gray": 5, "low quality": 6,}
MAX_SEED = 100
# --------------------------------------------------# Helper: quickβnβdirty Canny preview (only for UI display)# --------------------------------------------------# -----------------------------------------------------------------------------# Preview helpers βΒ one small, selfβcontained function per mode# -----------------------------------------------------------------------------
def _preview_canny( pil_img: Image.Image, canny_threshold_1: int, canny_threshold_2: int) -> Image.Image: """Fast Cannyβedge preview (already implemented)."""
arr = np.array(pil_img.convert("RGB")) edges = cv2.Canny(arr, threshold1=canny_threshold_1, threshold2=canny_threshold_2) edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB) return Image.fromarray(edges_rgb)
# βββ tile ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ #
def _preview_tile(pil_img: Image.Image, grid: Tuple[int, int] = (2, 2)) -> Image.Image: """Replicates *pil_img* into an *nΓm* tiled grid (default 2Γ2).
This offers a quick visual hint of what a *tiling* control mode will do (repeatable textures, etc.)."""
cols, rows = grid img_rgb = pil_img.convert("RGB") w, h = img_rgb.size tiled = Image.new("RGB", (w * cols, h * rows)) for c in range(cols): for r in range(rows): tiled.paste(img_rgb, (c * w, r * h)) return tiled
# βββ depth βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ #
def _preview_depth(pil_img: Image.Image) -> Image.Image: """Very rough *depth* proxy using the Laplacian and a colormap.
βΈ Convert to gray βΈ Run Laplacian to highlight depthβlike gradients βΈ Apply a TURBO colormap to mimic depth heatβmap appearance"""
gray = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2GRAY) lap = cv2.Laplacian(gray, cv2.CV_16S, ksize=3) depth = cv2.convertScaleAbs(lap) depth_color = cv2.applyColorMap(depth, cv2.COLORMAP_TURBO) return Image.fromarray(depth_color)
# βββ blur βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ #
def _preview_blur(pil_img: Image.Image, ksize: int = 15) -> Image.Image: """Gaussian blur preview. A single, relatively large kernel is enough for UI illustration."""
if ksize % 2 == 0: ksize += 1 # kernel must be odd blurred = cv2.GaussianBlur(np.array(pil_img), (ksize, ksize), sigmaX=0) return Image.fromarray(blurred)
# βββ pose ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ #
def _preview_pose(pil_img: Image.Image) -> Image.Image: """Attempt a lightweight 2βD pose overlay using *mediapipe* if available.
If *mediapipe* is not installed (or CPU inference fails), we gracefully fallback to an edgeβmap preview so the UI never crashes."""
try: import mediapipe as mp # type: ignore
mp_pose = mp.solutions.pose mp_drawing = mp.solutions.drawing_utils
img_bgr = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR) with mp_pose.Pose(static_image_mode=True) as pose_estimator: results = pose_estimator.process( img_bgr[..., ::-1] ) # Mediapipe expects RGB
annotated = img_bgr.copy() if results.pose_landmarks: mp_drawing.draw_landmarks( annotated, results.pose_landmarks, mp_pose.POSE_CONNECTIONS ) annotated_rgb = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB) return Image.fromarray(annotated_rgb)
except Exception as exc: # pragma: no cover βΒ any import / runtime error warnings.warn( f"Pose preview failed ({exc!s}); falling back to Canny.", RuntimeWarning ) # Return an edge map as a sensible fallback rather than exploding the UI return _preview_canny(pil_img, 100, 200)
# βββ gray βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ #
def _preview_gray(pil_img: Image.Image) -> Image.Image: """Simple grayscale conversion, but keep a 3βchannel RGB image so the UI widget pipeline stays consistent."""
gray = cv2.cvtColor(np.array(pil_img.convert("RGB")), cv2.COLOR_RGB2GRAY) gray_rgb = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB) return Image.fromarray(gray_rgb)
# βββ low quality βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ #
def _preview_low_quality(pil_img: Image.Image, factor: int = 8) -> Image.Image: """Mimic a lowβquality thumbnail: aggressively downsample then upscale.
The default *factor* (8Γ) is chosen to make artefacts obvious."""
img_rgb = pil_img.convert("RGB") w, h = img_rgb.size small = img_rgb.resize((max(1, w // factor), max(1, h // factor)), Image.BILINEAR) low_q = small.resize( (w, h), Image.NEAREST ) # upsample w/ Nearest to exaggerate blocks return low_q
# -----------------------------------------------------------------------------# Master dispatch# -----------------------------------------------------------------------------
def _make_preview( control_image: Image.Image, mode: str, canny_threshold_1: int, canny_threshold_2: int, canny_threshold_1: int = 100, canny_threshold_2: int = 200,) -> Image.Image: """Return a *quickβnβdirty* preview image for the requested *mode*.
Parameters ---------- control_image : PIL.Image The input image selected by the user. mode : str One of the keys of :data:`MODE_MAPPING`. canny_threshold_1 / 2 : int, optional Only used if *mode* is "canny" (passed straight to OpenCV Canny). """
mode = mode.lower() if mode not in MODE_MAPPING: warnings.warn(f"Unknown preview mode '{mode}'. Returning untouched image.") return control_image
if mode == "canny": return _preview_canny(control_image, canny_threshold_1, canny_threshold_2) # For other modes you can plug in your own visualiser later if mode == "tile": return _preview_tile(control_image) if mode == "depth": return _preview_depth(control_image) if mode == "blur": return _preview_blur(control_image) if mode == "pose": return _preview_pose(control_image) if mode == "gray": return _preview_gray(control_image) if mode == "low quality": return _preview_low_quality(control_image)
# Fallback βΒ should never happen due to early mode check return control_image
app.py+166-11