|
| 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 | +} |
0 commit comments