|
27 | 27 | /// </summary>
|
28 | 28 | public class WebCamRecognition : MonoBehaviour
|
29 | 29 | {
|
30 |
| - #region Private Data |
31 |
| - [SerializeField] |
32 |
| - private WebCamWidget m_WebCamWidget; |
33 |
| - [SerializeField] |
34 |
| - private WebCamDisplayWidget m_WebCamDisplayWidget; |
| 30 | + #region Private Data |
| 31 | + [SerializeField] |
| 32 | + private WebCamWidget m_WebCamWidget; |
| 33 | + [SerializeField] |
| 34 | + private WebCamDisplayWidget m_WebCamDisplayWidget; |
35 | 35 |
|
36 |
| - VisualRecognition m_VisualRecognition; |
37 |
| - #endregion |
| 36 | + VisualRecognition m_VisualRecognition; |
| 37 | + #endregion |
| 38 | + |
| 39 | + void Start() |
| 40 | + { |
| 41 | + m_VisualRecognition = new VisualRecognition(); |
| 42 | + } |
| 43 | + |
| 44 | + #region Public Functions |
| 45 | + public void SaveFile() |
| 46 | + { |
| 47 | + Log.Debug("WebCamRecognition", "Attempting to save file!"); |
| 48 | + Runnable.Run(SaveImageFile()); |
| 49 | + } |
| 50 | + |
| 51 | + public void SaveFile(string filePath = default(string), string fileName = default(string)) |
| 52 | + { |
| 53 | + Log.Debug("WebCamRecognition", "Attempting to save file!"); |
| 54 | + Runnable.Run(SaveImageFile(filePath, fileName)); |
| 55 | + } |
| 56 | + |
| 57 | + public void Classify() |
| 58 | + { |
| 59 | + Log.Debug("WebCamRecognition", "Attempting to classify image!"); |
| 60 | + Runnable.Run(ClassifyImage()); |
| 61 | + } |
| 62 | + |
| 63 | + public void DetectFaces() |
| 64 | + { |
| 65 | + Log.Debug("WebCamRecognition", "Attempting to detect faces!"); |
| 66 | + Runnable.Run(DetectFacesInImage()); |
| 67 | + } |
| 68 | + |
| 69 | + public void RecognizeText() |
| 70 | + { |
| 71 | + Log.Debug("WebCamRecognition", "Attempting to recognize text!"); |
| 72 | + Runnable.Run(RecognizeTextInImage()); |
| 73 | + } |
| 74 | + #endregion |
| 75 | + |
| 76 | + |
| 77 | + #region Private Functions |
| 78 | + private IEnumerator SaveImageFile(string filePath = default(string), string fileName = default(string)) |
| 79 | + { |
| 80 | + yield return new WaitForEndOfFrame(); |
| 81 | + |
| 82 | + if (filePath == default(string)) |
| 83 | + filePath = Application.dataPath + "/../"; |
| 84 | + if (fileName == default(string)) |
| 85 | + fileName = "WebCamImage.png"; |
| 86 | + Texture2D image = new Texture2D(m_WebCamWidget.WebCamTexture.width, m_WebCamWidget.WebCamTexture.height, TextureFormat.RGB24, false); |
| 87 | + image.SetPixels32(m_WebCamWidget.WebCamTexture.GetPixels32()); |
| 88 | + image.Apply(); |
| 89 | + |
| 90 | + File.WriteAllBytes(filePath + fileName, image.EncodeToPNG()); |
| 91 | + |
| 92 | + Log.Debug("WebCamRecognition", "File writeen to {0}{1}", filePath, fileName); |
| 93 | + } |
| 94 | + |
| 95 | + private IEnumerator ClassifyImage() |
| 96 | + { |
| 97 | + yield return new WaitForEndOfFrame(); |
| 98 | + |
| 99 | + //Color32[] colors = m_WebCamWidget.WebCamTexture.GetPixels32(); |
| 100 | + //byte[] rawImageData = Utility.Color32ArrayToByteArray(colors); |
| 101 | + |
| 102 | + Texture2D image = new Texture2D(m_WebCamWidget.WebCamTexture.width, m_WebCamWidget.WebCamTexture.height, TextureFormat.RGB24, false); |
| 103 | + image.SetPixels32(m_WebCamWidget.WebCamTexture.GetPixels32()); |
| 104 | + |
| 105 | + byte[] imageData = image.EncodeToPNG(); |
| 106 | + |
| 107 | + m_VisualRecognition.Classify(OnClassify, imageData); |
| 108 | + } |
| 109 | + |
| 110 | + private IEnumerator DetectFacesInImage() |
| 111 | + { |
| 112 | + yield return new WaitForEndOfFrame(); |
| 113 | + |
| 114 | + Texture2D image = new Texture2D(m_WebCamWidget.WebCamTexture.width, m_WebCamWidget.WebCamTexture.height, TextureFormat.RGB24, false); |
| 115 | + image.SetPixels32(m_WebCamWidget.WebCamTexture.GetPixels32()); |
| 116 | + |
| 117 | + byte[] imageData = image.EncodeToPNG(); |
| 118 | + |
| 119 | + m_VisualRecognition.DetectFaces(OnDetectFaces, imageData); |
| 120 | + } |
| 121 | + |
| 122 | + private IEnumerator RecognizeTextInImage() |
| 123 | + { |
| 124 | + yield return new WaitForEndOfFrame(); |
| 125 | + |
| 126 | + Texture2D image = new Texture2D(m_WebCamWidget.WebCamTexture.width, m_WebCamWidget.WebCamTexture.height, TextureFormat.RGB24, false); |
| 127 | + image.SetPixels32(m_WebCamWidget.WebCamTexture.GetPixels32()); |
| 128 | + |
| 129 | + byte[] imageData = image.EncodeToPNG(); |
| 130 | + |
| 131 | + m_VisualRecognition.RecognizeText(OnRecognizeText, imageData); |
| 132 | + } |
| 133 | + #endregion |
38 | 134 |
|
39 |
| - void Start() |
| 135 | + #region Event Handlers |
| 136 | + private void OnClassify(ClassifyTopLevelMultiple classify, string data) |
| 137 | + { |
| 138 | + if (classify != null) |
40 | 139 | {
|
41 |
| - m_VisualRecognition = new VisualRecognition(); |
42 |
| - } |
43 |
| - |
44 |
| - #region Public Functions |
45 |
| - public void SaveFile() |
46 |
| - { |
47 |
| - Log.Debug("WebCamRecognition", "Attempting to save file!"); |
48 |
| - Runnable.Run(SaveImageFile()); |
49 |
| - } |
50 |
| - |
51 |
| - public void SaveFile(string filePath = default(string), string fileName = default(string)) |
52 |
| - { |
53 |
| - Log.Debug("WebCamRecognition", "Attempting to save file!"); |
54 |
| - Runnable.Run(SaveImageFile(filePath, fileName)); |
55 |
| - } |
56 |
| - |
57 |
| - public void Classify() |
58 |
| - { |
59 |
| - Log.Debug("WebCamRecognition", "Attempting to classify image!"); |
60 |
| - Runnable.Run(ClassifyImage()); |
61 |
| - } |
62 |
| - |
63 |
| - public void DetectFaces() |
64 |
| - { |
65 |
| - Log.Debug("WebCamRecognition", "Attempting to detect faces!"); |
66 |
| - Runnable.Run(DetectFacesInImage()); |
67 |
| - } |
68 |
| - |
69 |
| - public void RecognizeText() |
70 |
| - { |
71 |
| - Log.Debug("WebCamRecognition", "Attempting to recognize text!"); |
72 |
| - Runnable.Run(RecognizeTextInImage()); |
73 |
| - } |
74 |
| - #endregion |
75 |
| - |
76 |
| - |
77 |
| - #region Private Functions |
78 |
| - private IEnumerator SaveImageFile(string filePath = default(string), string fileName = default(string)) |
79 |
| - { |
80 |
| - yield return new WaitForEndOfFrame(); |
81 |
| - |
82 |
| - if (filePath == default(string)) |
83 |
| - filePath = Application.dataPath + "/../"; |
84 |
| - if (fileName == default(string)) |
85 |
| - fileName = "WebCamImage.png"; |
86 |
| - Texture2D image = new Texture2D(m_WebCamWidget.WebCamTexture.width, m_WebCamWidget.WebCamTexture.height, TextureFormat.RGB24, false); |
87 |
| - image.SetPixels32(m_WebCamWidget.WebCamTexture.GetPixels32()); |
88 |
| - image.Apply(); |
89 |
| - |
90 |
| - File.WriteAllBytes(filePath + fileName, image.EncodeToPNG()); |
91 |
| - |
92 |
| - Log.Debug("WebCamRecognition", "File writeen to {0}{1}", filePath, fileName); |
93 |
| - } |
94 |
| - |
95 |
| - private IEnumerator ClassifyImage() |
96 |
| - { |
97 |
| - yield return new WaitForEndOfFrame(); |
98 |
| - |
99 |
| - //Color32[] colors = m_WebCamWidget.WebCamTexture.GetPixels32(); |
100 |
| - //byte[] rawImageData = Utility.Color32ArrayToByteArray(colors); |
101 |
| - |
102 |
| - Texture2D image = new Texture2D(m_WebCamWidget.WebCamTexture.width, m_WebCamWidget.WebCamTexture.height, TextureFormat.RGB24, false); |
103 |
| - image.SetPixels32(m_WebCamWidget.WebCamTexture.GetPixels32()); |
104 |
| - |
105 |
| - byte[] imageData = image.EncodeToPNG(); |
106 |
| - |
107 |
| - m_VisualRecognition.Classify(OnClassify, imageData); |
108 |
| - } |
109 |
| - |
110 |
| - private IEnumerator DetectFacesInImage() |
111 |
| - { |
112 |
| - yield return new WaitForEndOfFrame(); |
113 |
| - |
114 |
| - Texture2D image = new Texture2D(m_WebCamWidget.WebCamTexture.width, m_WebCamWidget.WebCamTexture.height, TextureFormat.RGB24, false); |
115 |
| - image.SetPixels32(m_WebCamWidget.WebCamTexture.GetPixels32()); |
116 |
| - |
117 |
| - byte[] imageData = image.EncodeToPNG(); |
118 |
| - |
119 |
| - m_VisualRecognition.DetectFaces(OnDetectFaces, imageData); |
| 140 | + Log.Debug("WebCamRecognition", "images processed: " + classify.images_processed); |
| 141 | + foreach (ClassifyTopLevelSingle image in classify.images) |
| 142 | + { |
| 143 | + Log.Debug("WebCamRecognition", "\tsource_url: " + image.source_url + ", resolved_url: " + image.resolved_url); |
| 144 | + foreach (ClassifyPerClassifier classifier in image.classifiers) |
| 145 | + { |
| 146 | + Log.Debug("WebCamRecognition", "\t\tclassifier_id: " + classifier.classifier_id + ", name: " + classifier.name); |
| 147 | + foreach (ClassResult classResult in classifier.classes) |
| 148 | + Log.Debug("WebCamRecognition", "\t\t\tclass: " + classResult.m_class + ", score: " + classResult.score + ", type_hierarchy: " + classResult.type_hierarchy); |
| 149 | + } |
| 150 | + } |
120 | 151 | }
|
121 |
| - |
122 |
| - private IEnumerator RecognizeTextInImage() |
| 152 | + else |
123 | 153 | {
|
124 |
| - yield return new WaitForEndOfFrame(); |
125 |
| - |
126 |
| - Texture2D image = new Texture2D(m_WebCamWidget.WebCamTexture.width, m_WebCamWidget.WebCamTexture.height, TextureFormat.RGB24, false); |
127 |
| - image.SetPixels32(m_WebCamWidget.WebCamTexture.GetPixels32()); |
128 |
| - |
129 |
| - byte[] imageData = image.EncodeToPNG(); |
130 |
| - |
131 |
| - m_VisualRecognition.RecognizeText(OnRecognizeText, imageData); |
| 154 | + Log.Debug("WebCamRecognition", "Classification failed!"); |
132 | 155 | }
|
133 |
| - #endregion |
| 156 | + } |
134 | 157 |
|
135 |
| - #region Event Handlers |
136 |
| - private void OnClassify(ClassifyTopLevelMultiple classify, string data) |
| 158 | + private void OnDetectFaces(FacesTopLevelMultiple multipleImages, string data) |
| 159 | + { |
| 160 | + if (multipleImages != null) |
137 | 161 | {
|
138 |
| - if (classify != null) |
139 |
| - { |
140 |
| - Log.Debug("WebCamRecognition", "images processed: " + classify.images_processed); |
141 |
| - foreach (ClassifyTopLevelSingle image in classify.images) |
142 |
| - { |
143 |
| - Log.Debug("WebCamRecognition", "\tsource_url: " + image.source_url + ", resolved_url: " + image.resolved_url); |
144 |
| - foreach (ClassifyPerClassifier classifier in image.classifiers) |
145 |
| - { |
146 |
| - Log.Debug("WebCamRecognition", "\t\tclassifier_id: " + classifier.classifier_id + ", name: " + classifier.name); |
147 |
| - foreach (ClassResult classResult in classifier.classes) |
148 |
| - Log.Debug("WebCamRecognition", "\t\t\tclass: " + classResult.m_class + ", score: " + classResult.score + ", type_hierarchy: " + classResult.type_hierarchy); |
149 |
| - } |
150 |
| - } |
151 |
| - } |
152 |
| - else |
| 162 | + Log.Debug("WebCamRecognition", "images processed: {0}", multipleImages.images_processed); |
| 163 | + foreach (FacesTopLevelSingle faces in multipleImages.images) |
| 164 | + { |
| 165 | + Log.Debug("WebCamRecognition", "\tsource_url: {0}, resolved_url: {1}", faces.source_url, faces.resolved_url); |
| 166 | + foreach (OneFaceResult face in faces.faces) |
153 | 167 | {
|
154 |
| - Log.Debug("WebCamRecognition", "Classification failed!"); |
| 168 | + if (face.face_location != null) |
| 169 | + Log.Debug("WebCamRecognition", "\t\tFace location: {0}, {1}, {2}, {3}", face.face_location.left, face.face_location.top, face.face_location.width, face.face_location.height); |
| 170 | + if (face.gender != null) |
| 171 | + Log.Debug("WebCamRecognition", "\t\tGender: {0}, Score: {1}", face.gender.gender, face.gender.score); |
| 172 | + if (face.age != null) |
| 173 | + Log.Debug("WebCamRecognition", "\t\tAge Min: {0}, Age Max: {1}, Score: {2}", face.age.min, face.age.max, face.age.score); |
| 174 | + |
| 175 | + if (face.identity != null) |
| 176 | + Log.Debug("WebCamRecognition", "\t\tName: {0}, Score: {1}, Type Heiarchy: {2}", face.identity.name, face.identity.score, face.identity.type_hierarchy); |
155 | 177 | }
|
| 178 | + } |
156 | 179 | }
|
157 |
| - |
158 |
| - private void OnDetectFaces(FacesTopLevelMultiple multipleImages, string data) |
| 180 | + else |
159 | 181 | {
|
160 |
| - if (multipleImages != null) |
161 |
| - { |
162 |
| - Log.Debug("WebCamRecognition", "images processed: {0}", multipleImages.images_processed); |
163 |
| - foreach (FacesTopLevelSingle faces in multipleImages.images) |
164 |
| - { |
165 |
| - Log.Debug("WebCamRecognition", "\tsource_url: {0}, resolved_url: {1}", faces.source_url, faces.resolved_url); |
166 |
| - foreach (OneFaceResult face in faces.faces) |
167 |
| - { |
168 |
| - if(face.face_location != null) |
169 |
| - Log.Debug("WebCamRecognition", "\t\tFace location: {0}, {1}, {2}, {3}", face.face_location.left, face.face_location.top, face.face_location.width, face.face_location.height); |
170 |
| - if(face.gender != null) |
171 |
| - Log.Debug("WebCamRecognition", "\t\tGender: {0}, Score: {1}", face.gender.gender, face.gender.score); |
172 |
| - if(face.age != null) |
173 |
| - Log.Debug("WebCamRecognition", "\t\tAge Min: {0}, Age Max: {1}, Score: {2}", face.age.min, face.age.max, face.age.score); |
174 |
| - |
175 |
| - if(face.identity != null) |
176 |
| - Log.Debug("WebCamRecognition", "\t\tName: {0}, Score: {1}, Type Heiarchy: {2}", face.identity.name, face.identity.score, face.identity.type_hierarchy); |
177 |
| - } |
178 |
| - } |
179 |
| - } |
180 |
| - else |
181 |
| - { |
182 |
| - Log.Debug("WebCamRecognition", "Detect faces failed!"); |
183 |
| - } |
| 182 | + Log.Debug("WebCamRecognition", "Detect faces failed!"); |
184 | 183 | }
|
| 184 | + } |
185 | 185 |
|
186 |
| - private void OnRecognizeText(TextRecogTopLevelMultiple multipleImages, string data) |
| 186 | + private void OnRecognizeText(TextRecogTopLevelMultiple multipleImages, string data) |
| 187 | + { |
| 188 | + if (multipleImages != null) |
187 | 189 | {
|
188 |
| - if (multipleImages != null) |
189 |
| - { |
190 |
| - Log.Debug("WebCamRecognition", "images processed: {0}", multipleImages.images_processed); |
191 |
| - foreach (TextRecogTopLevelSingle texts in multipleImages.images) |
192 |
| - { |
193 |
| - Log.Debug("WebCamRecognition", "\tsource_url: {0}, resolved_url: {1}", texts.source_url, texts.resolved_url); |
194 |
| - Log.Debug("WebCamRecognition", "\ttext: {0}", texts.text); |
195 |
| - foreach (TextRecogOneWord text in texts.words) |
196 |
| - { |
197 |
| - Log.Debug("WebCamRecognition", "\t\ttext location: {0}, {1}, {2}, {3}", text.location.left, text.location.top, text.location.width, text.location.height); |
198 |
| - Log.Debug("WebCamRecognition", "\t\tLine number: {0}", text.line_number); |
199 |
| - Log.Debug("WebCamRecognition", "\t\tword: {0}, Score: {1}", text.word, text.score); |
200 |
| - } |
201 |
| - } |
202 |
| - } |
203 |
| - else |
| 190 | + Log.Debug("WebCamRecognition", "images processed: {0}", multipleImages.images_processed); |
| 191 | + foreach (TextRecogTopLevelSingle texts in multipleImages.images) |
| 192 | + { |
| 193 | + Log.Debug("WebCamRecognition", "\tsource_url: {0}, resolved_url: {1}", texts.source_url, texts.resolved_url); |
| 194 | + Log.Debug("WebCamRecognition", "\ttext: {0}", texts.text); |
| 195 | + foreach (TextRecogOneWord text in texts.words) |
204 | 196 | {
|
205 |
| - Log.Debug("WebCamRecognition", "RecognizeText failed!"); |
| 197 | + Log.Debug("WebCamRecognition", "\t\ttext location: {0}, {1}, {2}, {3}", text.location.left, text.location.top, text.location.width, text.location.height); |
| 198 | + Log.Debug("WebCamRecognition", "\t\tLine number: {0}", text.line_number); |
| 199 | + Log.Debug("WebCamRecognition", "\t\tword: {0}, Score: {1}", text.word, text.score); |
206 | 200 | }
|
| 201 | + } |
| 202 | + } |
| 203 | + else |
| 204 | + { |
| 205 | + Log.Debug("WebCamRecognition", "RecognizeText failed!"); |
207 | 206 | }
|
208 |
| - #endregion |
| 207 | + } |
| 208 | + #endregion |
209 | 209 | }
|
0 commit comments