import { View, Text, DeviceEventEmitter, Dimensions, EmitterSubscription, ViewStyle, } from "react-native"; import React, { useEffect, useState } from "react"; import { GuideProps, GuideTrigger, GuideUpdate } from "../components/guides/GuideManager"; import Animated, { runOnJS, useAnimatedStyle } from "react-native-reanimated"; import { useSelector } from "react-redux"; import { RootState } from "../redux/store"; const COCO_CLASSES = [ "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush", ]; const SmartGuide = (props: GuideProps) => { const cameraViewDimensions = useSelector((state: RootState) => state.Util.cameraViewDimensions); const [bboxes, setBboxes] = useState([]); const [bboxStyle, setBboxStyle] = useState({}); const [eventSubscription, setEventSubscription] = useState([]); const { triggers, updates } = props; const updateUI = (data: any[]) => { let boxList = data.filter((d) => triggers.map((t) => t.target).includes(getClassName(d.class))); triggers.forEach((trigger) => { if (evaluateTarget(trigger, boxList)) { DeviceEventEmitter.emit(trigger.eventWhenTrue); let updateEventsToFire = updates.filter( (update) => update.trigger === trigger.triggerTitle ); updateEventsToFire.forEach((update) => { DeviceEventEmitter.emit(`@s20/sequence-event/action-event-${update.targetId}`, update); }); } }); try { if (boxList.length) { setBboxes(boxList); } else { setBboxes([]); // console.log("Could not parse result or no objects detected."); } } catch (error) { console.error("Error while setting label:", error); } }; const updater = () => { const subscribedEvent = DeviceEventEmitter.addListener( `@s20/sequence-event/action-event-${props.guide._id}`, (data: GuideUpdate) => { handleUpdate(data); } ); setEventSubscription((subscribedEvents) => [...subscribedEvents, subscribedEvent]); }; const handleUpdate = (update: GuideUpdate) => { if (update.action.property == "BBOX") { if (update.action.type === "SHOW") { if (update.action.styleUpdate) setBboxStyle(update.action.styleUpdate); } else if (update.action.type === "DONT_SHOW") { setBboxStyle({ display: "none" }); } } }; const evaluateTarget = (trigger: GuideTrigger, data: any[]) => { const { evalString, target } = trigger; switch (evalString) { case "BBOX_CENTER": let isCenter = checkBoundingBoxInCenter(data, target as string, cameraViewDimensions); return isCenter; case "DETECTED": if (data.length) { return true; } return false; case "NOT_DETECTED": if (data.length === 0) { return true; } return false; default: return false; } }; useEffect(() => { DeviceEventEmitter.emit("@s20/smart-guide/frame-processor/start", "objectDetectionPlugin"); updater(); const fpBBoxSub = DeviceEventEmitter.addListener( "@s20/smart-guide/frame-processor/bbox-update", (data) => { runOnJS(updateUI)(data); } ); return () => { fpBBoxSub.remove(); DeviceEventEmitter.emit("@s20/smart-guide/frame-processor/stop", "objectDetectionPlugin"); eventSubscription.forEach((subscription) => subscription.remove()); }; }, []); const BoundingBox = (props: any) => { const { box, boxClass } = props; const boxStyle = useAnimatedStyle(() => { if (box) { const x = box[0]; const y = box[1]; const width = box[2]; const height = box[3]; // Calculate actual width and height of the box const actualWidth = width * cameraViewDimensions.width; const actualHeight = height * cameraViewDimensions.height; return { position: "absolute", left: x * cameraViewDimensions.width - actualWidth / 2, top: y * cameraViewDimensions.height - actualHeight / 2, width: actualWidth, height: actualHeight, borderWidth: 2, borderColor: "red", zIndex: 100, }; } return {}; }); return ( {boxClass} ); }; return ( {bboxes?.map((d, i) => ( ))} ); }; export default SmartGuide; function getClassName(index: any) { return COCO_CLASSES[index]; } const checkBoundingBoxInCenter = function ( boundingBoxes: any[], target: string, containerDimensions: { width: number; height: number } ): boolean { let box = boundingBoxes.find((d) => getClassName(d.class) === target); if (!box) return false; // else return true; const [x, y, width, height] = box.box; const boxCenterX = x * containerDimensions.width; const boxCenterY = y * containerDimensions.height; const screenCenterX = containerDimensions.width / 2; const screenCenterY = containerDimensions.height / 2; // Check if the bounding box center is close to the screen center if (Math.abs(boxCenterX - screenCenterX) <= 20 && Math.abs(boxCenterY - screenCenterY) <= 30) { return true; // Print 'Test' when a bounding box is in the center } return false; };