Skip to content

Commit

Permalink
use texture2d to crop
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-ninja committed Oct 25, 2016
1 parent 9d9487a commit 4857734
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
27 changes: 6 additions & 21 deletions Assets/Scripts/GazeGestureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,6 @@ private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
}
}

byte[] Crop(byte[] src, int srcWidth, int xStart, int xEnd, int yStart, int yEnd)
{
var width = xEnd - xStart;
var height = yEnd - yStart;

var dst = new byte[width * height * 4];

for (var iY = yStart; iY < yEnd; iY++)
{
Array.Copy(src, GetIndex(srcWidth, xStart, iY), dst, GetIndex(width, 0, iY - yStart), width * 4);
}

return dst;
}

int GetIndex(int width, int x, int y)
{
return (x + (width * y)) * 4;
}

IEnumerator<object> PostToFaceAPI(byte[] imageData, Matrix4x4 cameraToWorldMatrix, Matrix4x4 pixelToCameraMatrix) {

var url = "https://api.projectoxford.ai/face/v1.0/detect?returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses";
Expand Down Expand Up @@ -135,7 +115,12 @@ IEnumerator<object> PostToFaceAPI(byte[] imageData, Matrix4x4 cameraToWorldMatri

try
{
byte[] justThisFace = Crop(imageData, cameraResolution.width, (int)p.GetField("left").i, (int)p.GetField("left").i + (int)p.GetField("width").i, (int)p.GetField("top").i, (int)p.GetField("height").i);
var source = new Texture2D(0, 0);
source.LoadImage(imageData);
var dest = new Texture2D((int)p["width"].i, (int)p["height"].i);
dest.SetPixels(source.GetPixels((int)p["left"].i, (int)p["top"].i, (int)p["width"].i, (int)p["height"].i));
byte[] justThisFace = dest.EncodeToPNG();
File.WriteAllBytes("cropped.png", justThisFace);
recognitionJobs[id] = new WWW(OpenFaceUrl, justThisFace);
} catch (Exception e) {
Debug.LogError(e);
Expand Down
18 changes: 18 additions & 0 deletions Assets/Scripts/ImageCrop_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using UnityEngine;
using System.Collections;
using System;

public class ImageCrop_Test : MonoBehaviour {

void Start()
{
var bytes = System.IO.File.ReadAllBytes("example.jpg");
var source = new Texture2D(0,0);
source.LoadImage(bytes);
var dest = new Texture2D(50, 50);
dest.SetPixels(source.GetPixels(200, 200, 50, 50));
var cropped = dest.EncodeToJPG();
Debug.Log(cropped);
System.IO.File.WriteAllBytes("cropped.jpg", cropped);
}
}
12 changes: 12 additions & 0 deletions Assets/Scripts/ImageCrop_Test.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/main.unity
Binary file not shown.

0 comments on commit 4857734

Please sign in to comment.