- Home /
Unity Opencv
Hi there, I spent the whole day looking for a way to get opencv to work with unity and I wasn't able to get it to work.Some people were able to get it to work successfully. So can anyone share the method he used or a sample project if he can upload it ?
Answer by unity_Fh_3Ok4CaXlg-w · Feb 27, 2019 at 08:20 AM
im using opencv with unity.there is an object following the movements of face detection circle on x,y,z axises.and i wanna prevent y,z axises and just move only for x axis here is the code;
using UnityEngine;
public class PositionAtFaceScreenSpace : MonoBehaviour { private float _camDistance;
void Start() { _camDistance = Vector3.Distance(Camera.main.transform.position, transform.position); }
void Update() { if (OpenCVFaceDetection.NormalizedFacePositions.Count == 0) return;
transform.position = Camera.main.ViewportToWorldPoint(new Vector3( OpenCVFaceDetection.NormalizedFacePositions[0].x, OpenCVFaceDetection.NormalizedFacePositions[0].y, OpenCVFaceDetection.NormalizedFacePositions[0].z)); } },how do i prevent y,z axises. i wanna move sphere only for x axis. here is the movement code. using UnityEngine;
public class PositionAtFaceScreenSpace : MonoBehaviour { private float _camDistance;
void Start()
{
_camDistance = Vector3.Distance(Camera.main.transform.position, transform.position);
}
void Update()
{
if (OpenCVFaceDetection.NormalizedFacePositions.Count == 0)
return;
transform.position = Camera.main.ViewportToWorldPoint(new Vector3( OpenCVFaceDetection.NormalizedFacePositions[0].x, OpenCVFaceDetection.NormalizedFacePositions[0].x, OpenCVFaceDetection.NormalizedFacePositions[0].x));
}
}
You should probably explain where you get OpenCVFaceDetection
from. Without that, this won't help anyone.
using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine;
public class OpenCVFaceDetection : $$anonymous$$onoBehaviour {
public static List<Vector3> NormalizedFacePositions { get; private set; }
public static Vector2 CameraResolution;
public float DepthDevider = 10f;
public float DepthAdder = 10f;
private const int DetectionDownScale = 1;
private bool _ready;
private int _maxFaceDetectCount = 5;
private CvCircle[] _faces;
void Start()
{
int camWidth = 0, camHeight = 0;
int result = OpenCVInterop.Init(ref camWidth, ref camHeight);
if (result < 0)
{
if (result == -1)
{
Debug.LogWarningFormat("[{0}] Failed to find cascades definition.", GetType());
}
else if (result == -2)
{
Debug.LogWarningFormat("[{0}] Failed to open camera stream.", GetType());
}
return;
}
CameraResolution = new Vector2(camWidth, camHeight);
_faces = new CvCircle[_maxFaceDetectCount];
NormalizedFacePositions = new List<Vector3>();
OpenCVInterop.SetScale(DetectionDownScale);
_ready = true;
}
void OnApplicationQuit()
{
if (_ready)
{
OpenCVInterop.Close();
}
}
void Update()
{
if (!_ready)
return;
int detectedFaceCount = 0;
unsafe
{
fixed (CvCircle* outFaces = _faces)
{
OpenCVInterop.Detect(outFaces, _maxFaceDetectCount, ref detectedFaceCount);
}
}
NormalizedFacePositions.Clear();
for (int i = 0; i < detectedFaceCount; i++)
{
NormalizedFacePositions.Add(new Vector3((_faces[i].X * DetectionDownScale) / CameraResolution.x, 1f - ((_faces[i].Y * DetectionDownScale) / CameraResolution.y), 1f - (_faces[i].Radius*DetectionDownScale)/DepthDevider + DepthAdder));
}
}
}
internal static class OpenCVInterop { [DllImport("OpenCV$$anonymous$$Part2Try1")] internal static extern int Init(ref int outCameraWidth, ref int outCameraHeight);
[DllImport("OpenCV$$anonymous$$Part2Try1")]
internal static extern int Close();
[DllImport("OpenCV$$anonymous$$Part2Try1")]
internal static extern int SetScale(int downscale);
[DllImport("OpenCV$$anonymous$$Part2Try1")]
internal unsafe static extern void Detect(CvCircle* outFaces, int maxOutFacesCount, ref int outDetectedFacesCount);
}
[StructLayout(Layout$$anonymous$$ind.Sequential, Size = 12)] public struct CvCircle { public int X, Y, Radius; }
So, it looks like you've compiled your own native DLL with some OpenCV functionality in it. Surely someone who can't get OpenCV working in Unity at all is going to need help with doing that. I can't see how showing them the unity code that uses the DLL is going to help.
Anyway, this question is 4 years old. These days there are quite a lot of helpful resources out there if someone just googles for Unity, using/compiling native DLLS, and OpenCV.
Your answer
![](https://koobas.hobune.stream/wayback/20220613185046im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to crop image using opencv 1 Answer
Image Stitching without external libraries 0 Answers
Using of Texture2D.LoadImage 0 Answers
import video from camera 3 Answers