Skip to content

Commit 13123da

Browse files
blessedcoolanthipsterusername
authored andcommitted
feat: Add DepthAnything to Linear UI
1 parent c859eb8 commit 13123da

File tree

7 files changed

+877
-82
lines changed

7 files changed

+877
-82
lines changed

invokeai/frontend/web/public/locales/en.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
"amult": "a_mult",
225225
"autoConfigure": "Auto configure processor",
226226
"balanced": "Balanced",
227+
"base": "Base",
227228
"beginEndStepPercent": "Begin / End Step Percentage",
228229
"bgth": "bg_th",
229230
"canny": "Canny",
@@ -237,6 +238,8 @@
237238
"controlMode": "Control Mode",
238239
"crop": "Crop",
239240
"delete": "Delete",
241+
"depthAnything": "Depth Anything",
242+
"depthAnythingDescription": "Depth map generation using the Depth Anything technique",
240243
"depthMidas": "Depth (Midas)",
241244
"depthMidasDescription": "Depth map generation using Midas",
242245
"depthZoe": "Depth (Zoe)",
@@ -256,6 +259,7 @@
256259
"colorMapTileSize": "Tile Size",
257260
"importImageFromCanvas": "Import Image From Canvas",
258261
"importMaskFromCanvas": "Import Mask From Canvas",
262+
"large": "Large",
259263
"lineart": "Lineart",
260264
"lineartAnime": "Lineart Anime",
261265
"lineartAnimeDescription": "Anime-style lineart processing",
@@ -268,6 +272,7 @@
268272
"minConfidence": "Min Confidence",
269273
"mlsd": "M-LSD",
270274
"mlsdDescription": "Minimalist Line Segment Detector",
275+
"modelSize": "Model Size",
271276
"none": "None",
272277
"noneDescription": "No processing applied",
273278
"normalBae": "Normal BAE",
@@ -288,6 +293,7 @@
288293
"selectModel": "Select a model",
289294
"setControlImageDimensions": "Set Control Image Dimensions To W/H",
290295
"showAdvanced": "Show Advanced",
296+
"small": "Small",
291297
"toggleControlNet": "Toggle this ControlNet",
292298
"w": "W",
293299
"weight": "Weight",

invokeai/frontend/web/src/features/controlAdapters/components/ControlAdapterProcessorComponent.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { memo } from 'react';
55
import CannyProcessor from './processors/CannyProcessor';
66
import ColorMapProcessor from './processors/ColorMapProcessor';
77
import ContentShuffleProcessor from './processors/ContentShuffleProcessor';
8+
import DepthAnyThingProcessor from './processors/DepthAnyThingProcessor';
89
import HedProcessor from './processors/HedProcessor';
910
import LineartAnimeProcessor from './processors/LineartAnimeProcessor';
1011
import LineartProcessor from './processors/LineartProcessor';
@@ -48,6 +49,16 @@ const ControlAdapterProcessorComponent = ({ id }: Props) => {
4849
);
4950
}
5051

52+
if (processorNode.type === 'depth_anything_image_processor') {
53+
return (
54+
<DepthAnyThingProcessor
55+
controlNetId={id}
56+
processorNode={processorNode}
57+
isEnabled={isEnabled}
58+
/>
59+
);
60+
}
61+
5162
if (processorNode.type === 'hed_image_processor') {
5263
return (
5364
<HedProcessor
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type { ComboboxOnChange } from '@invoke-ai/ui';
2+
import { Combobox, FormControl, FormLabel } from '@invoke-ai/ui';
3+
import { useProcessorNodeChanged } from 'features/controlAdapters/components/hooks/useProcessorNodeChanged';
4+
import { CONTROLNET_PROCESSORS } from 'features/controlAdapters/store/constants';
5+
import type {
6+
DepthAnythingModelSize,
7+
RequiredDepthAnythingImageProcessorInvocation,
8+
} from 'features/controlAdapters/store/types';
9+
import { isDepthAnythingModelSize } from 'features/controlAdapters/store/types';
10+
import { memo, useCallback, useMemo } from 'react';
11+
import { useTranslation } from 'react-i18next';
12+
13+
import ProcessorWrapper from './common/ProcessorWrapper';
14+
15+
const DEFAULTS = CONTROLNET_PROCESSORS.midas_depth_image_processor
16+
.default as RequiredDepthAnythingImageProcessorInvocation;
17+
18+
type Props = {
19+
controlNetId: string;
20+
processorNode: RequiredDepthAnythingImageProcessorInvocation;
21+
isEnabled: boolean;
22+
};
23+
24+
const DepthAnythingProcessor = (props: Props) => {
25+
const { controlNetId, processorNode, isEnabled } = props;
26+
const { model_size } = processorNode;
27+
const processorChanged = useProcessorNodeChanged();
28+
29+
const { t } = useTranslation();
30+
31+
const handleModelSizeChange = useCallback<ComboboxOnChange>(
32+
(v) => {
33+
if (!isDepthAnythingModelSize(v?.value)) {
34+
return;
35+
}
36+
processorChanged(controlNetId, {
37+
model_size: v.value,
38+
});
39+
},
40+
[controlNetId, processorChanged]
41+
);
42+
43+
const options: { label: string; value: DepthAnythingModelSize }[] = useMemo(
44+
() => [
45+
{ label: t('controlnet.large'), value: 'large' },
46+
{ label: t('controlnet.base'), value: 'base' },
47+
{ label: t('controlnet.small'), value: 'small' },
48+
],
49+
[t]
50+
);
51+
52+
const value = useMemo(
53+
() => options.filter((o) => o.value === model_size)[0],
54+
[options, model_size]
55+
);
56+
57+
return (
58+
<ProcessorWrapper>
59+
<FormControl isDisabled={!isEnabled}>
60+
<FormLabel>{t('controlnet.modelSize')}</FormLabel>
61+
<Combobox
62+
value={value}
63+
defaultInputValue={DEFAULTS.model_size}
64+
options={options}
65+
onChange={handleModelSizeChange}
66+
/>
67+
</FormControl>
68+
</ProcessorWrapper>
69+
);
70+
};
71+
72+
export default memo(DepthAnythingProcessor);

invokeai/frontend/web/src/features/controlAdapters/store/constants.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ export const CONTROLNET_PROCESSORS: ControlNetProcessorsDict = {
8383
f: 256,
8484
},
8585
},
86+
depth_anything_image_processor: {
87+
type: 'depth_anything_image_processor',
88+
get label() {
89+
return i18n.t('controlnet.depthAnything');
90+
},
91+
get description() {
92+
return i18n.t('controlnet.depthAnythingDescription');
93+
},
94+
default: {
95+
id: 'depth_anything_image_processor',
96+
type: 'depth_anything_image_processor',
97+
model_size: 'large',
98+
offload: false,
99+
},
100+
},
86101
hed_image_processor: {
87102
type: 'hed_image_processor',
88103
get label() {

invokeai/frontend/web/src/features/controlAdapters/store/types.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
CannyImageProcessorInvocation,
1111
ColorMapImageProcessorInvocation,
1212
ContentShuffleImageProcessorInvocation,
13+
DepthAnythingImageProcessorInvocation,
1314
HedImageProcessorInvocation,
1415
LineartAnimeImageProcessorInvocation,
1516
LineartImageProcessorInvocation,
@@ -31,6 +32,7 @@ export type ControlAdapterProcessorNode =
3132
| CannyImageProcessorInvocation
3233
| ColorMapImageProcessorInvocation
3334
| ContentShuffleImageProcessorInvocation
35+
| DepthAnythingImageProcessorInvocation
3436
| HedImageProcessorInvocation
3537
| LineartAnimeImageProcessorInvocation
3638
| LineartImageProcessorInvocation
@@ -73,6 +75,20 @@ export type RequiredContentShuffleImageProcessorInvocation = O.Required<
7375
'type' | 'detect_resolution' | 'image_resolution' | 'w' | 'h' | 'f'
7476
>;
7577

78+
/**
79+
* The DepthAnything processor node, with parameters flagged as required
80+
*/
81+
export type RequiredDepthAnythingImageProcessorInvocation = O.Required<
82+
DepthAnythingImageProcessorInvocation,
83+
'type' | 'model_size' | 'offload'
84+
>;
85+
86+
export const zDepthAnythingModelSize = z.enum(['large', 'base', 'small']);
87+
export type DepthAnythingModelSize = z.infer<typeof zDepthAnythingModelSize>;
88+
export const isDepthAnythingModelSize = (
89+
v: unknown
90+
): v is DepthAnythingModelSize => zDepthAnythingModelSize.safeParse(v).success;
91+
7692
/**
7793
* The HED processor node, with parameters flagged as required
7894
*/
@@ -161,6 +177,7 @@ export type RequiredControlAdapterProcessorNode =
161177
| RequiredCannyImageProcessorInvocation
162178
| RequiredColorMapImageProcessorInvocation
163179
| RequiredContentShuffleImageProcessorInvocation
180+
| RequiredDepthAnythingImageProcessorInvocation
164181
| RequiredHedImageProcessorInvocation
165182
| RequiredLineartAnimeImageProcessorInvocation
166183
| RequiredLineartImageProcessorInvocation
@@ -219,6 +236,22 @@ export const isContentShuffleImageProcessorInvocation = (
219236
return false;
220237
};
221238

239+
/**
240+
* Type guard for DepthAnythingImageProcessorInvocation
241+
*/
242+
export const isDepthAnythingImageProcessorInvocation = (
243+
obj: unknown
244+
): obj is DepthAnythingImageProcessorInvocation => {
245+
if (
246+
isObject(obj) &&
247+
'type' in obj &&
248+
obj.type === 'depth_anything_image_processor'
249+
) {
250+
return true;
251+
}
252+
return false;
253+
};
254+
222255
/**
223256
* Type guard for HedImageprocessorInvocation
224257
*/

0 commit comments

Comments
 (0)