You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
usingSystem;usingSystem.IO;usingSystem.Linq;usingUnityEngine;usingUnityEngine.Windows.WebCam;publicclassMyPhotoCapture:MonoBehaviour{PhotoCapturephotoCaptureObject=null;internalstringfilePath=string.Empty;internalboolcaptureIsActive;privatevoidStart(){// Clean up the LocalState folder of this application from all photos storedDirectoryInfoinfo=newDirectoryInfo(Application.persistentDataPath);varfileInfo=info.GetFiles();foreach(varfileinfileInfo){try{file.Delete();}catch(Exception){Debug.LogFormat("Cannot delete file: ",file.Name);}}//StartCapture();}publicvoidStartCapture(){if(!captureIsActive){captureIsActive=true;PhotoCapture.CreateAsync(false,OnPhotoCaptureCreated);}else{captureIsActive=false;}}voidOnPhotoCaptureCreated(PhotoCapturecaptureObject){photoCaptureObject=captureObject;varcameraResolution=PhotoCapture.SupportedResolutions.OrderByDescending((res)=>res.width*res.height).First();varcameraParams=newCameraParameters(){hologramOpacity=0f,cameraResolutionWidth=cameraResolution.width,cameraResolutionHeight=cameraResolution.height,pixelFormat=CapturePixelFormat.BGRA32};captureObject.StartPhotoModeAsync(cameraParams,OnPhotoModeStarted);}privatevoidOnPhotoModeStarted(PhotoCapture.PhotoCaptureResultresult){if(result.success){stringfilename=string.Format(@"CapturedImage_{0}.jpg",DateTime.Now.ToString("yyyyMMddHHmmss"));filePath=Path.Combine(Application.persistentDataPath,filename);photoCaptureObject.TakePhotoAsync(filePath,PhotoCaptureFileOutputFormat.JPG,OnCapturedPhotoToDisk);}else{Debug.LogError("Unable to start photo mode!");}}voidOnCapturedPhotoToDisk(PhotoCapture.PhotoCaptureResultresult){if(result.success){Debug.Log("Saved Photo to disk!");photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);}else{Debug.Log("Failed to save Photo to disk");}}voidOnStoppedPhotoMode(PhotoCapture.PhotoCaptureResultresult){photoCaptureObject.Dispose();photoCaptureObject=null;captureIsActive=false;StartCoroutine(CustomVisionAnalyser.Instance.AnalyseLastImageCaptured(filePath));}}
usingSystem.Collections;usingSystem.IO;usingUnityEngine;usingUnityEngine.Networking;publicclassCustomVisionAnalyser:MonoBehaviour{publicstaticCustomVisionAnalyserInstance;privatestringpredictionEndpoint="http://192.168.0.103:5000/upload";/// <summary>/// Byte array of the image to submit for analysis/// </summary>[HideInInspector]publicbyte[]imageBytes;privatevoidAwake(){Instance=this;}/// <summary>/// Call the Computer Vision Service to submit the image./// </summary>publicIEnumeratorAnalyseLastImageCaptured(stringimagePath){WWWFormwebForm=newWWWForm();// Gets a byte array out of the saved imageimageBytes=GetImageAsByteArray(imagePath);// 将图片byte数组添加进表单webForm.AddBinaryData("file",imageBytes,"photo.jpg");// 将图片上传到服务器using(UnityWebRequestunityWebRequest=UnityWebRequest.Post(predictionEndpoint,webForm)){// The download handler will help receiving the analysis from AzureunityWebRequest.downloadHandler=newDownloadHandlerBuffer();// Send the requestyieldreturnunityWebRequest.SendWebRequest();if(unityWebRequest.isHttpError||unityWebRequest.isNetworkError){Debug.Log(unityWebRequest.error);}else{stringresponse=unityWebRequest.downloadHandler.text;Debug.Log(response);}}}/// <summary>/// Returns the contents of the specified image file as a byte array./// </summary>staticbyte[]GetImageAsByteArray(stringimageFilePath){FileStreamfileStream=newFileStream(imageFilePath,FileMode.Open,FileAccess.Read);BinaryReaderbinaryReader=newBinaryReader(fileStream);returnbinaryReader.ReadBytes((int)fileStream.Length);}}