- Home /
acquiring images from simulation?
Hi, i have a problem with acquiring images from my simulation. I think the problem is in the script i use to manage this, as it works fine and is fluid until i enable a SaveVideo button. Below i write the script i use for this task:
using UnityEngine; using System.Collections.Generic; public class CamerasManager : MonoBehaviour { public bool enableVideoSave; public string folder = "AcquisitionVideo"; public int frameRate = 25; public List cameras = new List();
// initialisation void Start() {
if(enableVideoSave) {
Time.captureFramerate = frameRate;
if(cameras.Count > 0) {
// create one main folder which will contain a sub folder for each camera
folder = folder + System.DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss");
if(System.IO.Directory.Exists(folder)) {
System.IO.Directory.Delete(folder, true);
}
System.IO.Directory.CreateDirectory(folder);
// create sub folder for each camera
for(int i = 0; i < cameras.Count; ++i) {
if(cameras[i].Camera) {
cameras[i].Folder = string.Format("{0}/{1}_{2}x{3}_{4}fps",
folder,
cameras[i].Name,
cameras[i].Camera.rect.width,
cameras[i].Camera.rect.height,
frameRate);
System.IO.Directory.CreateDirectory(cameras[i].Folder);
}
}
}
}
}
//function called after Update() void LateUpdate() {
if(enableVideoSave) {
// for each camera
for(int i = 0; i < cameras.Count; ++i) {
if(cameras[i].Camera) {
RenderTexture rt = new RenderTexture((int) cameras[i].Camera.rect.width, (int)
cameras[i].Camera.rect.height, 24);
cameras[i].Camera.targetTexture = rt;
rt.antiAliasing = 8;
Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
cameras[i].Camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
cameras[i].Camera.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = string.Format("{0}/frame_{1:D04}.png", cameras[i].Folder, Time.frameCount);
System.IO.File.WriteAllBytes(filename, bytes);
}
}
}
} }
[System.Serializable]
public class CameraType {
public string Name;
public Camera Camera;
public string Folder;
public CameraType(string _Name) {
Name = _Name;
} }
I also attach a screen with the errors. Thanks in advance
as you can see from image the problem clearly is that at a certain point it generates black shadows ins$$anonymous$$d of real people. This problem doesn't appear when i run the simulation without acquiring images from cameras
Answer by Bunny83 · Oct 17, 2017 at 12:15 PM
You don't destroy your "screenShot" textures you create in that loop. So you create a new texture every iteration and finally run out of memory.
Really thank, but then i have another problem. Adding the command "Destroy(screenShot)" it creates people correctly, but almost immediately (from frame 97 out of 900), this warning appear "failed to create RenderTexture( 1920x1080 fmt 27 aa 8), error 0x800700e", and all the following captured images are completely grey. I really don't get why the program works perfectly and then it suddenly stops working as it should.