public async UniTask <VideoPacket> GetVideo() { await UniTask.WaitForEndOfFrame(); var request = AsyncGPUReadback.Request(localRenderTexture); await UniTask.WaitUntil(() => request.done); rawData = request.GetData <Color32>().ToArray(); await UniTask.SwitchToThreadPool(); await UniTask.Run(() => { jpgData = ImageConversion.EncodeArrayToJPG(rawData, GraphicsFormat.R8G8B8A8_UNorm, width, height); }); byte[] data = jpgData; VideoPacket packet = new VideoPacket(); packet.Id = ConfigManager.LOCAL_ID; packet.Width = width; packet.Height = height; packet.Timestamp = ConvertDateTimeToLong(DateTime.Now); packet.Data = data; return(packet); }
/// <summary> /// Encode the input data as per provided image format. /// </summary> /// <param name="data">An array of data to be encoded.</param> /// <param name="width">Image width.</param> /// <param name="height">Image height.</param> /// <param name="format">Graphics format used by the render texture.</param> /// <param name="imageFormat">Format for encoding the data.</param> /// <param name="additionalParam">Additional flags to be passed for the encoding.</param> /// <returns></returns> /// <exception cref="NotSupportedException"></exception> public static Array EncodeArray(Array data, int width, int height, GraphicsFormat format, ImageFormat imageFormat, int additionalParam = 0) { switch (imageFormat) { case ImageFormat.Raw: return(data); #if UNITY_2019_3_OR_NEWER case ImageFormat.Png: return(ImageConversion.EncodeArrayToPNG(data, format, (uint)width, (uint)height, 0)); case ImageFormat.Exr: return(ImageConversion.EncodeArrayToEXR(data, format, (uint)width, (uint)height, 0, /*EXRFlags*/ (Texture2D.EXRFlags)additionalParam)); case ImageFormat.Tga: return(ImageConversion.EncodeArrayToTGA(data, format, (uint)width, (uint)height, 0)); #endif case ImageFormat.Jpg: #if USIM_USE_BUILTIN_JPG_ENCODER return(ImageConversion.EncodeArrayToJPG(data, format, (uint)width, (uint)height, 0, /*quality*/ additionalParam > 0 ? (int)additionalParam : 75)); #else return(JpegEncoder.Encode(ArrayUtilities.Cast <byte>(data), width, height, GraphicsUtilities.GetBlockSize(format), format, /*quality*/ additionalParam > 0 ? (int)additionalParam : 75)); #endif default: throw new NotSupportedException("ImageFormat is not supported"); } }
private void SendFrame(TcpClient client, FrameUpdatedEvent ev) { var stream = client.GetStream(); var encodedFrame = ImageConversion.EncodeArrayToJPG(ev.Data, GraphicsFormat.R8G8B8A8_UNorm, (uint)ev.Resolution, (uint)ev.Resolution, 0, 75); WriteString(GetImageHeaders(encodedFrame), stream); stream.Write(encodedFrame, 0, encodedFrame.Length); WriteString("\r\n", stream); }
private void OnChangeColor(DirectoryInfo material) { var texture = new Texture2D(1024, 1024); Debug.Log(material.FullName); var bytes = File.ReadAllBytes(material.FullName); var bytesTGA = ImageConversion.EncodeArrayToJPG(bytes, GraphicsFormat.R8G8B8_SInt, 1024, 1024); texture.LoadImage(bytesTGA); player.ChangeColor(texture); }
private void StreamVideo(TcpClient client, string applicationPath) { using (client) using (var stream = client.GetStream()) { const string headers = "HTTP/1.1 200 OK\r\n" + "Content-Type: multipart/x-mixed-replace; boundary=--boundary\r\n"; WriteString(headers, stream); while (true) { try { if (_latestStartedFrameTimestamp < LatestFrameAt) { _latestStartedFrameTimestamp = LatestFrameAt; var encodedFrame = ImageConversion.EncodeArrayToJPG(LatestFrame, GraphicsFormat.R8G8B8A8_UNorm, Resolution, Resolution); WriteString(GetImageHeaders(encodedFrame), stream); stream.Write(encodedFrame, 0, encodedFrame.Length); WriteString("\r\n", stream); latestSentFrameTimestamp = _latestStartedFrameTimestamp; } Thread.Sleep(5); } catch (Exception ex) { // Disconnected? Debug.Log($"Encountered error: {ex.Message}"); break; } } }; }
public async Task <List <DetectedObject> > DetectAsync(CameraFrame frame) { if (frame == null) { throw new ArgumentNullException(nameof(frame)); } Imgcodecs.imwrite(Application.persistentDataPath + "/testB.jpg", frame.Mat); try { Debug.Log($"Enter PredictImageAsync with mat {frame.Mat}"); var imageWidth = frame.Width; var imageHeight = frame.Height; Mat rgb = new Mat(imageWidth, imageHeight, CvType.CV_8UC3); if (frame.Format == ColorFormat.Grayscale) { Imgproc.cvtColor(frame.Mat, rgb, Imgproc.COLOR_GRAY2RGB); Debug.Log($"Converted gray2rgb to {rgb}"); } else { frame.Mat.copyTo(rgb); } //Mat rgba = new Mat(); //Imgproc.cvtColor(rgb, rgba, Imgproc.COLOR_RGB2RGBA); float newHeight = 416.0f / imageWidth * imageHeight; Mat resized = new Mat(416, 416, CvType.CV_8UC3); Imgproc.resize(rgb, resized, new Size(416, newHeight), 0.5, 0.5, Imgproc.INTER_LINEAR); //Imgproc.resize(rgb, resized, new Size(targetWidth, targetHeight), 0.5, 0.5, Imgproc.INTER_LINEAR); Debug.Log($"Resized {resized}"); Mat resizedBorder = new Mat(); Core.copyMakeBorder(resized, resizedBorder, 0, (int)(416 - newHeight), 0, 0, Core.BORDER_CONSTANT, new Scalar(0, 0, 0)); /*Mat rgba = new Mat(); * Imgproc.cvtColor(resizedBorder, rgba, Imgproc.COLOR_RGB2RGBA);*/ Texture2D texture = new Texture2D(416, 416, TextureFormat.RGB24, false); Utils.matToTexture2D(resizedBorder, texture, true); //texture.Apply(); Color32[] pixels32 = texture.GetPixels32(); byte[] encodeArrayToJPG = ImageConversion.EncodeArrayToJPG(pixels32, GraphicsFormat.R8G8B8A8_UInt, 416, 416); File.WriteAllBytes(Application.persistentDataPath + "/testA.jpg", encodeArrayToJPG); using (var tensor = TransformInput(pixels32, ImageNetSettings.imageWidth, ImageNetSettings.imageHeight)) { var inputs = new Dictionary <string, Tensor>(); inputs.Add(ModelSettings.ModelInput, tensor); //yield return StartCoroutine(worker.StartManualSchedule(inputs)); //var output = engine.Execute(inputs).PeekOutput(); var output = engine.Execute(inputs).PeekOutput(ModelSettings.ModelOutput); var results = outputParser.ParseOutputs(output, MINIMUM_CONFIDENCE); var boxes = outputParser.FilterBoundingBoxes(results, 10, MINIMUM_CONFIDENCE); foreach (var box in boxes) { Debug.Log($"{box.tagName}, {box.probability}, {box.boundingBox.left},{box.boundingBox.top},{box.boundingBox.width},{box.boundingBox.height},"); } List <DetectedObject> detectedObjects = boxes.Select(prediction => CreateDetectedObject(frame, prediction, (int)newHeight)).ToList(); int count = 0; foreach (var detectedObject in detectedObjects) { count++; Mat clone = frame.Mat.clone(); Imgproc.rectangle(clone, detectedObject.Rect.tl(), detectedObject.Rect.br(), new Scalar(255, 255, 255), 10, 1, 0); Imgcodecs.imwrite(Application.persistentDataPath + "/clone-" + count + ".jpg", clone); } } } catch (Exception e) { Debug.LogException(e); throw e; } return(new List <DetectedObject>()); }
// Build voxel object public override float Build(Storage voxels, Bounds bounds, Informer informer, object parameter) { processor.superSamplingCount = superSamplingCount; processor.powerOfTwo = powerOfTwo; processor.expandEdges = expandEdges; processor.backgroundColor = backgroundColor; processor.hdr = fileFormat == FileFormat.EXR; // Execute real build-up method float progress = processor.Build(voxels, bounds); // Check if processing has been finished if (progress >= 1) { // Store file, if it is specified if (fileStoring && filePath != null && filePath.Length > 0 && Texture != null) { try { // Build target path switch (fileFormat) { #if UNITY_2020_2_OR_NEWER case FileFormat.JPG: case FileFormat.PNG: case FileFormat.TGA: case FileFormat.EXR: var source = processor.Texture; if (source) { var sourceData = source.GetPixelData <byte>(0); if (sourceData != null) { var length = source.width * source.height * source.depth; //var textureWidth = Mathf.CeilToInt(Mathf.Sqrt(length)); var textureWidth = (int)Math.Pow(2, Math.Ceiling(Math.Log(Mathf.Sqrt(length)) / Math.Log(2))); var columnsCount = Mathf.CeilToInt((float)textureWidth / source.width); var rowsCount = Mathf.CeilToInt((float)source.depth / columnsCount); textureWidth = source.width * columnsCount; var textureHeight = source.height * rowsCount; var pixelSize = processor.hdr ? 8 : 4; var lineSize = source.width * pixelSize; var buffer = new byte[lineSize]; var targetData = new byte[textureWidth * textureHeight * pixelSize]; if (targetData != null) { var sourceOffset = 0; // Process depth slices for (int slice = 0; slice < source.depth; ++slice) { // Calculate column and row of the target sub image var column = slice % columnsCount; var row = rowsCount - slice / columnsCount - 1; // Process lines of the current slice for (int line = 0; line < source.height; ++line) { // Get data for line into the cache sourceData.GetSubArray(sourceOffset, lineSize).CopyTo(buffer); // Copy it to the target buffer Array.Copy(buffer, 0, targetData, (column + (line + row * source.height) * columnsCount) * lineSize, lineSize); sourceOffset += lineSize; } } byte[] data; switch (fileFormat) { case FileFormat.JPG: // Convert image data to JPEG data data = ImageConversion.EncodeArrayToJPG(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight, 0, 100); break; case FileFormat.PNG: // Convert image data to PNG data data = ImageConversion.EncodeArrayToPNG(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight); break; case FileFormat.TGA: // Convert image data to TGA data data = ImageConversion.EncodeArrayToTGA(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight); break; case FileFormat.EXR: // Convert image data to EXR data data = ImageConversion.EncodeArrayToEXR(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight, 0, UnityEngine.Texture2D.EXRFlags.CompressZIP); break; default: data = null; break; } if (data != null) { // Make sure the target folders exist var path = System.IO.Path.Combine(Application.dataPath, filePath); if (Helper.CreateDirectory(path, true)) { #if UNITY_EDITOR // Remove existing asset file if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } // Update database UnityEditor.AssetDatabase.Refresh(); #endif // Save data to file System.IO.File.WriteAllBytes(path, data); #if UNITY_EDITOR // Store columns and rows for the asset to be applied later voxelTexturePaths.Add(System.IO.Path.GetFullPath(path), new Vector2Int(columnsCount, rowsCount)); // Update database to enable changing import settings UnityEditor.AssetDatabase.Refresh(); #endif } } } } } break; #endif default: // Save texture as asset file Helper.StoreAsset(processor.Texture, filePath, null); break; } } catch (System.Exception exception) { Debug.Log(exception.Message); } } #if UNITY_EDITOR // Add object creation undo operation if (!Application.isPlaying) { UnityEditor.Undo.RegisterCreatedObjectUndo(processor.Texture, "\"VoxelTexture3D\" Creation"); } #endif // Execute informer callback informer?.Invoke(new UnityEngine.Object[] { processor.Texture }, parameter); } return(progress); }