Skip to content

Commit 52a75a0

Browse files
committed
Add image type detection module and update response interfaces
1 parent 242f7ba commit 52a75a0

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

src/checkSightEngineAPI.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export interface SightengineResponse {
2828
ideogram?: number;
2929
};
3030
deepfake?: number;
31+
illustration?: number;
32+
photo?: number;
3133
};
3234
quality?: {
3335
score?: number;

src/detections/DetectImageType.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { SettingsFormField } from "@devvit/public-api";
2+
import { DetectionBase } from "./DetectionBase.js";
3+
import { SightengineResponse } from "../checkSightEngineAPI.js";
4+
5+
enum ModuleSetting {
6+
ImageTypeToDetect = "ImageType_ImageTypeToDetect",
7+
}
8+
9+
export class DetectImageType extends DetectionBase {
10+
public name = "ImageType";
11+
public friendlyName = "Image Type Detection";
12+
public helpText = "Detects photographs vs. illustrations. Useful for subs that prefer one type over another.";
13+
public sightengineType = "type";
14+
15+
public moduleSettings: SettingsFormField[] = [
16+
{
17+
type: "select",
18+
name: ModuleSetting.ImageTypeToDetect,
19+
label: "Image Type to Detect",
20+
options: [
21+
{ value: "photo", label: "Photo" },
22+
{ value: "illustration", label: "Illustration" },
23+
],
24+
defaultValue: ["photo"],
25+
helpText: "The type of image to detect. The app will flag posts that contain this type of image.",
26+
multiSelect: false,
27+
onValidate: ({ value }) => {
28+
if (!value || value.length === 0) {
29+
return "You must select an image type to detect.";
30+
}
31+
},
32+
},
33+
];
34+
35+
override defaultEnabledForMenu = false;
36+
37+
override enabledForProactive (): boolean {
38+
return super.enabledForProactive() && this.getReportableImageType() !== undefined;
39+
}
40+
41+
override enabledForMenu (): boolean {
42+
return super.enabledForMenu() && this.getReportableImageType() !== undefined;
43+
}
44+
45+
private getImageType (sightEngineResponse: SightengineResponse): "photo" | "illustration" | undefined {
46+
const threshold = 0.7;
47+
const isPhoto = sightEngineResponse.type?.photo !== undefined && sightEngineResponse.type.photo > threshold;
48+
const isIllustration = sightEngineResponse.type?.illustration !== undefined && sightEngineResponse.type.illustration > threshold;
49+
50+
if (isPhoto && isIllustration) {
51+
return undefined; // Both types detected, ambiguous
52+
} else if (isPhoto) {
53+
return "photo";
54+
} else if (isIllustration) {
55+
return "illustration";
56+
}
57+
}
58+
59+
private getReportableImageType (): "photo" | "illustration" | undefined {
60+
const selectedType = this.getSetting<string[]>(ModuleSetting.ImageTypeToDetect, []);
61+
if (selectedType.length === 0) {
62+
return undefined;
63+
}
64+
return selectedType[0] as "photo" | "illustration";
65+
}
66+
67+
public detectProactive (sightEngineResponse: SightengineResponse): string | undefined {
68+
const detectedType = this.getImageType(sightEngineResponse);
69+
const reportableType = this.getReportableImageType();
70+
71+
if (detectedType && detectedType !== reportableType) {
72+
return `Detected image type: ${detectedType}`;
73+
}
74+
}
75+
76+
public detectByMenu (sightEngineResponse: SightengineResponse): string | undefined {
77+
const detectedType = this.getImageType(sightEngineResponse);
78+
79+
return `Detected image type: ${detectedType ?? "uncertain"}`;
80+
}
81+
}

src/detections/allDetections.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DetectOffensiveContent } from "./DetectOffensiveContent.js";
88
import { DetectQRCodeSpam } from "./DetectQRCodes.js";
99
import { DetectTextSpam } from "./DetectTextSpam.js";
1010
import { DetectDrugs } from "./DetectDrugs.js";
11+
import { DetectImageType } from "./DetectImageType.js";
1112

1213
export const ALL_DETECTIONS: (new (settings: SettingsValues) => DetectionBase)[] = [
1314
DetectGenAI,
@@ -18,6 +19,7 @@ export const ALL_DETECTIONS: (new (settings: SettingsValues) => DetectionBase)[]
1819
DetectQRCodeSpam,
1920
DetectTextSpam,
2021
DetectDrugs,
22+
DetectImageType,
2123
];
2224

2325
export function getModels (detectors: (new (settings: SettingsValues) => DetectionBase)[]): string[] {

0 commit comments

Comments
 (0)