/// <summary> /// Updates the skeleton data from ZEDCamera call and send it to Skeleton Handler script. /// </summary> private void updateSkeletonData(DetectionFrame dframe) { #if FAKEMODE if (avatarControlList.ContainsKey(0)) { SkeletonHandler handler = avatarControlList[0]; handler.setFakeTest(indexFakeTest); } else { SkeletonHandler handler = ScriptableObject.CreateInstance <SkeletonHandler>(); handler.Create(Avatar, Vector3.zero); avatarControlList.Add(0, handler); } #else List <int> remainingKeyList = new List <int>(avatarControlList.Keys); List <DetectedObject> newobjects = dframe.GetFilteredObjectList(showON, showSEARCHING, showOFF); /*if (dframe.rawObjectsFrame.detectionModel!= sl.DETECTION_MODEL.HUMAN_BODY_ACCURATE && * dframe.rawObjectsFrame.detectionModel!= sl.DETECTION_MODEL.HUMAN_BODY_FAST) * { * Debug.Log("Wrong model selected : " + dframe.rawObjectsFrame.detectionModel); * return; * }*/ foreach (DetectedObject dobj in newobjects) { int person_id = dobj.rawObjectData.id; //Avatar controller already exist --> update position if (avatarControlList.ContainsKey(person_id)) { SkeletonHandler handler = avatarControlList[person_id]; UpdateAvatarControl(handler, dobj.rawObjectData, useAvatar); // remove keys from list remainingKeyList.Remove(person_id); } else { SkeletonHandler handler = ScriptableObject.CreateInstance <SkeletonHandler>(); Vector3 spawnPosition = zedManager.GetZedRootTansform().TransformPoint(dobj.rawObjectData.rootWorldPosition); handler.Create(Avatar, spawnPosition); handler.initSkeleton(person_id); avatarControlList.Add(person_id, handler); UpdateAvatarControl(handler, dobj.rawObjectData, useAvatar); } } foreach (int index in remainingKeyList) { SkeletonHandler handler = avatarControlList[index]; handler.Destroy(); avatarControlList.Remove(index); } #endif }
/// <summary> /// Given a frame of object detections, positions a GameObject to represent every visible object /// in that object's actual 3D location within the world. /// <para>Called from ZEDManager.OnObjectDetection each time there's a new detection frame available.</para> /// </summary> private void Visualize3DBoundingBoxes(DetectionFrame dframe) { //Get a list of all active IDs from last frame, and we'll remove each box that's visible this frame. //At the end, we'll clear the remaining boxes, as those are objects no longer visible to the ZED. List <int> activeids = liveBBoxes.Keys.ToList(); List <DetectedObject> newobjects = dframe.GetFilteredObjectList(showONTracked, showSEARCHINGTracked, showOFFTracked); foreach (DetectedObject dobj in newobjects) { Bounds objbounds = dobj.Get3DWorldBounds(); //Make sure the object is big enough to count. We filter out very small boxes. if (objbounds.size.x < minimumWidthToDisplay) { continue; } //Remove the ID from the list we'll use to clear no-longer-visible boxes. if (activeids.Contains(dobj.id)) { activeids.Remove(dobj.id); } //Get the box and update its distance value. GameObject bbox = GetBBoxForObject(dobj); //Move the box into position. bbox.transform.position = dobj.Get3DWorldPosition(); if (floorBBoxPosition) { bbox.transform.position = new Vector3(bbox.transform.position.x, 0, bbox.transform.position.z); } bbox.transform.rotation = dobj.Get3DWorldRotation(boxesFaceCamera); //Rotate them. //Transform the box if desired. if (transformBoxScale) { //We'll scale the object assuming that it's mesh is the default Unity cube, or something sized equally. if (transformBoxToTouchFloor) { Vector3 startscale = objbounds.size; float distfromfloor = bbox.transform.position.y - (objbounds.size.y / 2f); bbox.transform.localScale = new Vector3(objbounds.size.x, objbounds.size.y + distfromfloor, objbounds.size.z); Vector3 newpos = bbox.transform.position; newpos.y -= (distfromfloor / 2f); bbox.transform.position = newpos; } else { bbox.transform.localScale = objbounds.size; } } //Now that we've adjusted position, tell the handler on the prefab to adjust distance display.. BBox3DHandler boxhandler = bbox.GetComponent <BBox3DHandler>(); if (boxhandler) { float disttobox = Vector3.Distance(dobj.detectingZEDManager.GetLeftCameraTransform().position, dobj.Get3DWorldPosition()); boxhandler.SetDistance(disttobox); boxhandler.UpdateBoxUVScales(); boxhandler.UpdateLabelScaleAndPosition(); } //DrawDebugBox(dobj); } //Remove boxes for objects that the ZED can no longer see. foreach (int id in activeids) { ReturnBoxToPool(id, liveBBoxes[id]); } }
/// <summary> /// Given a frame of object detections, positions a canvas object to represent every visible object /// to encompass the object within the 2D image from the ZED. /// <para>Called from ZEDManager.OnObjectDetection each time there's a new detection frame available.</para> /// </summary> public void Visualize2DBoundingBoxes(DetectionFrame dframe) { //Clear any masks that were displayed last frame, to avoid memory leaks. DestroyLastFrameMaskTextures(); //Debug.Log("Received frame with " + dframe.detectedObjects.Count + " objects."); //Get a list of all active IDs from last frame, and we'll remove each box that's visible this frame. //At the end, we'll clear the remaining boxes, as those are objects no longer visible to the ZED. List <int> activeids = liveBBoxes.Keys.ToList(); List <DetectedObject> newobjects = dframe.GetFilteredObjectList(showONTracked, showSEARCHINGTracked, showOFFTracked); //Test just setting box to first available. foreach (DetectedObject dobj in newobjects) { //Remove the ID from the list we'll use to clear no-longer-visible boxes. if (activeids.Contains(dobj.id)) { activeids.Remove(dobj.id); } //Get the relevant box. This function will create a new one if it wasn't designated yet. RectTransform bbox = GetBBoxForObject(dobj); BBox2DHandler idtext = bbox.GetComponentInChildren <BBox2DHandler>(); if (idtext) { float disttobox = Vector3.Distance(dobj.detectingZEDManager.GetLeftCameraTransform().position, dobj.Get3DWorldPosition()); idtext.SetDistance(disttobox); } #if UNITY_2018_3_OR_NEWER float xmod = canvas.GetComponent <RectTransform>().rect.width / zedManager.zedCamera.ImageWidth; Rect objrect = dobj.Get2DBoundingBoxRect(xmod); #else Rect objrect = dobj.Get2DBoundingBoxRect(); #endif //Adjust the size of the RectTransform to encompass the object. bbox.sizeDelta = new Vector2(objrect.width, objrect.height); bbox.anchoredPosition = new Vector2(objrect.x, objrect.y); /* #if UNITY_2018_3_OR_NEWER * float xmod = canvas.GetComponent<RectTransform>().rect.width / zedManager.zedCamera.ImageWidth; * bbox.anchoredPosition = new Vector2(bbox.anchoredPosition.x * xmod, bbox.anchoredPosition.y); * bbox.sizeDelta *= xmod; #endif */ //Apply the mask. if (showObjectMask) { //Make a new image for this new mask. Texture2D maskimage; if (dobj.GetMaskTexture(out maskimage, false)) { idtext.SetMaskImage(maskimage); //Apply to 2D bbox. lastFrameMasks.Add(maskimage); //Cache the texture so it's deleted next time we update our objects. } } } //Remove boxes for objects that the ZED can no longer see. foreach (int id in activeids) { ReturnBoxToPool(id, liveBBoxes[id]); } SortActiveObjectsByDepth(); //Sort all object transforms so that ones with further depth appear behind objects that are closer. }