質問編集履歴
4
誤字脱字の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -94,31 +94,197 @@
|
|
94
94
|
|
95
95
|
↓コンソール
|
96
96
|
|
97
|
+
```
|
98
|
+
|
99
|
+
The type of base64Image is String // 画像がString型になっている
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
### 【できなかったこと】
|
106
|
+
|
107
|
+
iOSアプリから起動したiPhoneカメラで撮ったものを、base64でSrting型に変換したものをAlamofireを使ってAPIリクエストした。
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
実行前・実行中(runtime)エラーのいずれもなく(エラーが何も出てこない)、Build Succeedとなり、正常にアプリが起動する。
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
しかし全くの無反応で、403や404などのエラーも返ってきません。
|
116
|
+
|
117
|
+
|
118
|
+
|
97
119
|
```Swift
|
98
120
|
|
121
|
+
let parameters: Parameters = [
|
122
|
+
|
123
|
+
"requests": [
|
124
|
+
|
99
|
-
|
125
|
+
"image": ["content": base64EncodedImage],
|
126
|
+
|
100
|
-
|
127
|
+
"features": [
|
128
|
+
|
129
|
+
"type": "TEXT_DETECTION",
|
130
|
+
|
131
|
+
"maxResults": 1]
|
132
|
+
|
133
|
+
]
|
134
|
+
|
135
|
+
]
|
136
|
+
|
101
|
-
```
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
|
102
|
-
|
141
|
+
Google Cloud Platformコンソール画面に行ってAPIリクエストが届いているか確認しても、APIリクエストの数が増えていないので、リクエスト自体が、届いていないと思われます。
|
103
|
-
|
104
|
-
|
142
|
+
|
143
|
+
|
144
|
+
|
105
|
-
### 【
|
145
|
+
### 【その他のコード】
|
106
|
-
|
107
|
-
|
146
|
+
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
実行前・実行中(runtime)エラーのいずれもなく(エラーが何も出てこない)、Build Succeedとなり、正常にアプリが起動する。
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
し
|
147
|
+
### カメラを起動し、写真を撮るコード
|
116
|
-
|
117
|
-
|
118
148
|
|
119
149
|
```Swift
|
120
150
|
|
151
|
+
//
|
152
|
+
|
153
|
+
//
|
154
|
+
|
155
|
+
// CameraViewController.swift
|
156
|
+
|
157
|
+
//
|
158
|
+
|
159
|
+
//
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
// MARK: AVCapturePhotoCaptureDelegateデリゲートメソッド
|
164
|
+
|
165
|
+
extension CameraViewController: AVCapturePhotoCaptureDelegate{
|
166
|
+
|
167
|
+
// 撮影した画像データが生成されたときに呼び出されるデリゲートメソッド
|
168
|
+
|
169
|
+
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
|
170
|
+
|
171
|
+
if let imageData = photo.fileDataRepresentation() {
|
172
|
+
|
173
|
+
// Data型をUIImageオブジェクトに変換
|
174
|
+
|
175
|
+
let uiImage = UIImage(data: imageData)
|
176
|
+
|
177
|
+
// 写真ライブラリに画像を保存
|
178
|
+
|
179
|
+
UIImageWriteToSavedPhotosAlbum(uiImage!, nil,nil,nil) // 正常にライブラリに保存されることを確認済みです
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
// こっから下はGoogle Cloud Vision APIのdetectBoundingBoxes()を呼び出している
|
184
|
+
|
185
|
+
GoogleCloudOCR().detect(from: uiImage!) { ocrResult in
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
// self.activityIndicator.stopAnimating() //読み込み中画面を使う場合はコメントアウトを消す
|
190
|
+
|
191
|
+
guard let ocrResult = ocrResult else {
|
192
|
+
|
193
|
+
fatalError("Did not recognize any text in this image")
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
print(ocrResult)
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
### 【Google CloudにAPIリクエストするコード】
|
214
|
+
|
215
|
+
```Swift
|
216
|
+
|
217
|
+
//
|
218
|
+
|
219
|
+
// GoogleCloudOCR.swift
|
220
|
+
|
221
|
+
//
|
222
|
+
|
223
|
+
//
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
import Foundation
|
228
|
+
|
229
|
+
import Alamofire
|
230
|
+
|
231
|
+
import UIKit
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
class GoogleCloudOCR {
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
// Google Cloud Vision API
|
240
|
+
|
241
|
+
var apiKey = "<My API Key>"
|
242
|
+
|
243
|
+
var apiURL: URL {
|
244
|
+
|
245
|
+
return URL(string: "https://vision.googleapis.com/v1/images:annotate?key=(apiKey)")!
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
func detect(from image: UIImage, completion: @escaping (OCRResult?) -> Void) {
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
guard let base64Image = base64EncodeImage(image) else {
|
256
|
+
|
257
|
+
completion(nil)
|
258
|
+
|
259
|
+
return
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
callGoogleVisionAPI(with: base64Image, completion: completion)
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
print ("The type of base64Image is (type(of: base64Image))")
|
268
|
+
|
269
|
+
print ("callGoogleVisionAPI(with: base64Image, completion: completion) has been called")
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
private func callGoogleVisionAPI(
|
276
|
+
|
277
|
+
with base64EncodedImage: String, completion: @escaping (OCRResult?) -> Void) {
|
278
|
+
|
279
|
+
print ("func callGoogleVisionAPI() has been called")
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
// APIリクエストが送れなかったparameters(【できなかったこと】)
|
284
|
+
|
285
|
+
/*
|
286
|
+
|
121
|
-
let parameters: Parameters = [
|
287
|
+
let parameters: Parameters = [
|
122
288
|
|
123
289
|
"requests": [
|
124
290
|
|
@@ -134,69 +300,207 @@
|
|
134
300
|
|
135
301
|
]
|
136
302
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
303
|
+
*/
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
// 以下のparametersならAPIリクエストが成功し、画像に含まれる文字列が返された(【できたこと①】)
|
308
|
+
|
309
|
+
let parameters: Parameters = [
|
310
|
+
|
311
|
+
"requests": [
|
312
|
+
|
313
|
+
"image": [
|
314
|
+
|
315
|
+
"source": ["imageUri": "https://1.bp.blogspot.com/-DS_mPoCrYrQ/XVDZ7TPubmI/AAAAAAAASyI/Z7Dl29IkmQwtoiQc5g8a_eqILvpihY9RQCLcBGAs/s1600/fullsizeoutput_5bf.jpeg"
|
316
|
+
|
317
|
+
]
|
318
|
+
|
319
|
+
],
|
320
|
+
|
321
|
+
"features": [
|
322
|
+
|
323
|
+
"type": "TEXT_DETECTION",
|
324
|
+
|
325
|
+
"maxResults": 1
|
326
|
+
|
327
|
+
]
|
328
|
+
|
329
|
+
]
|
330
|
+
|
331
|
+
]
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
let headers: HTTPHeaders = [
|
336
|
+
|
337
|
+
"Content-Type": "application/json",
|
338
|
+
|
339
|
+
"X-Ios-Bundle-Identifier": Bundle.main.bundleIdentifier ?? ""]
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
Alamofire.request(
|
344
|
+
|
345
|
+
apiURL,
|
346
|
+
|
347
|
+
method: .post,
|
348
|
+
|
349
|
+
parameters: parameters,
|
350
|
+
|
351
|
+
encoding: JSONEncoding.default,
|
352
|
+
|
353
|
+
headers: headers)
|
354
|
+
|
355
|
+
.responseJSON { response in
|
356
|
+
|
357
|
+
if response.result.isFailure {
|
358
|
+
|
359
|
+
completion(nil)
|
360
|
+
|
361
|
+
return
|
194
362
|
|
195
363
|
}
|
196
364
|
|
365
|
+
print(response.result.debugDescription)
|
366
|
+
|
197
|
-
|
367
|
+
debugPrint(response)
|
198
|
-
|
368
|
+
|
199
|
-
|
369
|
+
}
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
private func base64EncodeImage(_ image: UIImage) -> String? {
|
376
|
+
|
377
|
+
return image.pngData()?.base64EncodedString(options: .endLineWithCarriageReturn)
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
struct Vertex: Codable {
|
384
|
+
|
385
|
+
let x: Int?
|
386
|
+
|
387
|
+
let y: Int?
|
388
|
+
|
389
|
+
enum CodingKeys: String, CodingKey {
|
390
|
+
|
391
|
+
case x = "x", y = "y"
|
392
|
+
|
393
|
+
}
|
394
|
+
|
395
|
+
init(from decoder: Decoder) throws {
|
396
|
+
|
397
|
+
let container = try decoder.container(keyedBy: CodingKeys.self)
|
398
|
+
|
399
|
+
x = try container.decodeIfPresent(Int.self, forKey: .x)
|
400
|
+
|
401
|
+
y = try container.decodeIfPresent(Int.self, forKey: .y)
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
func toCGPoint() -> CGPoint {
|
408
|
+
|
409
|
+
return CGPoint(x: x ?? 0, y: y ?? 0)
|
410
|
+
|
411
|
+
}
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
struct BoundingBox: Codable {
|
418
|
+
|
419
|
+
let vertices: [Vertex]
|
420
|
+
|
421
|
+
enum CodingKeys: String, CodingKey {
|
422
|
+
|
423
|
+
case vertices = "vertices"
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
init(from decoder: Decoder) throws {
|
428
|
+
|
429
|
+
let container = try decoder.container(keyedBy: CodingKeys.self)
|
430
|
+
|
431
|
+
vertices = try container.decode([Vertex].self, forKey: .vertices)
|
432
|
+
|
433
|
+
}
|
434
|
+
|
435
|
+
}
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
struct Annotation: Codable {
|
440
|
+
|
441
|
+
let text: String
|
442
|
+
|
443
|
+
let boundingBox: BoundingBox
|
444
|
+
|
445
|
+
enum CodingKeys: String, CodingKey {
|
446
|
+
|
447
|
+
case text = "description"
|
448
|
+
|
449
|
+
case boundingBox = "boundingPoly"
|
450
|
+
|
451
|
+
}
|
452
|
+
|
453
|
+
init(from decoder: Decoder) throws {
|
454
|
+
|
455
|
+
let container = try decoder.container(keyedBy: CodingKeys.self)
|
456
|
+
|
457
|
+
text = try container.decode(String.self, forKey: .text)
|
458
|
+
|
459
|
+
boundingBox = try container.decode(BoundingBox.self, forKey: .boundingBox)
|
460
|
+
|
461
|
+
}
|
462
|
+
|
463
|
+
}
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
struct OCRResult: Codable {
|
468
|
+
|
469
|
+
let annotations: [Annotation]
|
470
|
+
|
471
|
+
enum CodingKeys: String, CodingKey {
|
472
|
+
|
473
|
+
case annotations = "textAnnotations"
|
474
|
+
|
475
|
+
}
|
476
|
+
|
477
|
+
init(from decoder: Decoder) throws {
|
478
|
+
|
479
|
+
let container = try decoder.container(keyedBy: CodingKeys.self)
|
480
|
+
|
481
|
+
annotations = try container.decode([Annotation].self, forKey: .annotations)
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
}
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
struct GoogleCloudOCRResponse: Codable {
|
490
|
+
|
491
|
+
let responses: [OCRResult]
|
492
|
+
|
493
|
+
enum CodingKeys: String, CodingKey {
|
494
|
+
|
495
|
+
case responses = "responses"
|
496
|
+
|
497
|
+
}
|
498
|
+
|
499
|
+
init(from decoder: Decoder) throws {
|
500
|
+
|
501
|
+
let container = try decoder.container(keyedBy: CodingKeys.self)
|
502
|
+
|
503
|
+
responses = try container.decode([OCRResult].self, forKey: .responses)
|
200
504
|
|
201
505
|
}
|
202
506
|
|
@@ -207,307 +511,3 @@
|
|
207
511
|
|
208
512
|
|
209
513
|
```
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
### 【Google CloudにAPIリクエストするコード】
|
214
|
-
|
215
|
-
```Swift
|
216
|
-
|
217
|
-
//
|
218
|
-
|
219
|
-
// GoogleCloudOCR.swift
|
220
|
-
|
221
|
-
//
|
222
|
-
|
223
|
-
//
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
import Foundation
|
228
|
-
|
229
|
-
import Alamofire
|
230
|
-
|
231
|
-
import UIKit
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
class GoogleCloudOCR {
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
// Google Cloud Vision API
|
240
|
-
|
241
|
-
var apiKey = "<My API Key>"
|
242
|
-
|
243
|
-
var apiURL: URL {
|
244
|
-
|
245
|
-
return URL(string: "https://vision.googleapis.com/v1/images:annotate?key=(apiKey)")!
|
246
|
-
|
247
|
-
}
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
func detect(from image: UIImage, completion: @escaping (OCRResult?) -> Void) {
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
guard let base64Image = base64EncodeImage(image) else {
|
256
|
-
|
257
|
-
completion(nil)
|
258
|
-
|
259
|
-
return
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
callGoogleVisionAPI(with: base64Image, completion: completion)
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
print ("The type of base64Image is (type(of: base64Image))")
|
268
|
-
|
269
|
-
print ("callGoogleVisionAPI(with: base64Image, completion: completion) has been called")
|
270
|
-
|
271
|
-
}
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
private func callGoogleVisionAPI(
|
276
|
-
|
277
|
-
with base64EncodedImage: String, completion: @escaping (OCRResult?) -> Void) {
|
278
|
-
|
279
|
-
print ("func callGoogleVisionAPI() has been called")
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
// APIリクエストが送れなかったparameters(【できなかったこと】)
|
284
|
-
|
285
|
-
/*
|
286
|
-
|
287
|
-
let parameters: Parameters = [
|
288
|
-
|
289
|
-
"requests": [
|
290
|
-
|
291
|
-
"image": ["content": base64EncodedImage],
|
292
|
-
|
293
|
-
"features": [
|
294
|
-
|
295
|
-
"type": "TEXT_DETECTION",
|
296
|
-
|
297
|
-
"maxResults": 1]
|
298
|
-
|
299
|
-
]
|
300
|
-
|
301
|
-
]
|
302
|
-
|
303
|
-
*/
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
// 以下のparametersならAPIリクエストが成功し、画像に含まれる文字列が返された(【できたこと①】)
|
308
|
-
|
309
|
-
let parameters: Parameters = [
|
310
|
-
|
311
|
-
"requests": [
|
312
|
-
|
313
|
-
"image": [
|
314
|
-
|
315
|
-
"source": ["imageUri": "https://1.bp.blogspot.com/-DS_mPoCrYrQ/XVDZ7TPubmI/AAAAAAAASyI/Z7Dl29IkmQwtoiQc5g8a_eqILvpihY9RQCLcBGAs/s1600/fullsizeoutput_5bf.jpeg"
|
316
|
-
|
317
|
-
]
|
318
|
-
|
319
|
-
],
|
320
|
-
|
321
|
-
"features": [
|
322
|
-
|
323
|
-
"type": "TEXT_DETECTION",
|
324
|
-
|
325
|
-
"maxResults": 1
|
326
|
-
|
327
|
-
]
|
328
|
-
|
329
|
-
]
|
330
|
-
|
331
|
-
]
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
let headers: HTTPHeaders = [
|
336
|
-
|
337
|
-
"Content-Type": "application/json",
|
338
|
-
|
339
|
-
"X-Ios-Bundle-Identifier": Bundle.main.bundleIdentifier ?? ""]
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
Alamofire.request(
|
344
|
-
|
345
|
-
apiURL,
|
346
|
-
|
347
|
-
method: .post,
|
348
|
-
|
349
|
-
parameters: parameters,
|
350
|
-
|
351
|
-
encoding: JSONEncoding.default,
|
352
|
-
|
353
|
-
headers: headers)
|
354
|
-
|
355
|
-
.responseJSON { response in
|
356
|
-
|
357
|
-
if response.result.isFailure {
|
358
|
-
|
359
|
-
completion(nil)
|
360
|
-
|
361
|
-
return
|
362
|
-
|
363
|
-
}
|
364
|
-
|
365
|
-
print(response.result.debugDescription)
|
366
|
-
|
367
|
-
debugPrint(response)
|
368
|
-
|
369
|
-
}
|
370
|
-
|
371
|
-
}
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
private func base64EncodeImage(_ image: UIImage) -> String? {
|
376
|
-
|
377
|
-
return image.pngData()?.base64EncodedString(options: .endLineWithCarriageReturn)
|
378
|
-
|
379
|
-
}
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
struct Vertex: Codable {
|
384
|
-
|
385
|
-
let x: Int?
|
386
|
-
|
387
|
-
let y: Int?
|
388
|
-
|
389
|
-
enum CodingKeys: String, CodingKey {
|
390
|
-
|
391
|
-
case x = "x", y = "y"
|
392
|
-
|
393
|
-
}
|
394
|
-
|
395
|
-
init(from decoder: Decoder) throws {
|
396
|
-
|
397
|
-
let container = try decoder.container(keyedBy: CodingKeys.self)
|
398
|
-
|
399
|
-
x = try container.decodeIfPresent(Int.self, forKey: .x)
|
400
|
-
|
401
|
-
y = try container.decodeIfPresent(Int.self, forKey: .y)
|
402
|
-
|
403
|
-
}
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
func toCGPoint() -> CGPoint {
|
408
|
-
|
409
|
-
return CGPoint(x: x ?? 0, y: y ?? 0)
|
410
|
-
|
411
|
-
}
|
412
|
-
|
413
|
-
}
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
struct BoundingBox: Codable {
|
418
|
-
|
419
|
-
let vertices: [Vertex]
|
420
|
-
|
421
|
-
enum CodingKeys: String, CodingKey {
|
422
|
-
|
423
|
-
case vertices = "vertices"
|
424
|
-
|
425
|
-
}
|
426
|
-
|
427
|
-
init(from decoder: Decoder) throws {
|
428
|
-
|
429
|
-
let container = try decoder.container(keyedBy: CodingKeys.self)
|
430
|
-
|
431
|
-
vertices = try container.decode([Vertex].self, forKey: .vertices)
|
432
|
-
|
433
|
-
}
|
434
|
-
|
435
|
-
}
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
struct Annotation: Codable {
|
440
|
-
|
441
|
-
let text: String
|
442
|
-
|
443
|
-
let boundingBox: BoundingBox
|
444
|
-
|
445
|
-
enum CodingKeys: String, CodingKey {
|
446
|
-
|
447
|
-
case text = "description"
|
448
|
-
|
449
|
-
case boundingBox = "boundingPoly"
|
450
|
-
|
451
|
-
}
|
452
|
-
|
453
|
-
init(from decoder: Decoder) throws {
|
454
|
-
|
455
|
-
let container = try decoder.container(keyedBy: CodingKeys.self)
|
456
|
-
|
457
|
-
text = try container.decode(String.self, forKey: .text)
|
458
|
-
|
459
|
-
boundingBox = try container.decode(BoundingBox.self, forKey: .boundingBox)
|
460
|
-
|
461
|
-
}
|
462
|
-
|
463
|
-
}
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
struct OCRResult: Codable {
|
468
|
-
|
469
|
-
let annotations: [Annotation]
|
470
|
-
|
471
|
-
enum CodingKeys: String, CodingKey {
|
472
|
-
|
473
|
-
case annotations = "textAnnotations"
|
474
|
-
|
475
|
-
}
|
476
|
-
|
477
|
-
init(from decoder: Decoder) throws {
|
478
|
-
|
479
|
-
let container = try decoder.container(keyedBy: CodingKeys.self)
|
480
|
-
|
481
|
-
annotations = try container.decode([Annotation].self, forKey: .annotations)
|
482
|
-
|
483
|
-
}
|
484
|
-
|
485
|
-
}
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
struct GoogleCloudOCRResponse: Codable {
|
490
|
-
|
491
|
-
let responses: [OCRResult]
|
492
|
-
|
493
|
-
enum CodingKeys: String, CodingKey {
|
494
|
-
|
495
|
-
case responses = "responses"
|
496
|
-
|
497
|
-
}
|
498
|
-
|
499
|
-
init(from decoder: Decoder) throws {
|
500
|
-
|
501
|
-
let container = try decoder.container(keyedBy: CodingKeys.self)
|
502
|
-
|
503
|
-
responses = try container.decode([OCRResult].self, forKey: .responses)
|
504
|
-
|
505
|
-
}
|
506
|
-
|
507
|
-
}
|
508
|
-
|
509
|
-
}
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
```
|
3
誤字脱字を修正しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -142,7 +142,7 @@
|
|
142
142
|
|
143
143
|
|
144
144
|
|
145
|
-
### 【その他の】
|
145
|
+
### 【その他のコード】
|
146
146
|
|
147
147
|
### カメラを起動し、写真を撮るコード
|
148
148
|
|
2
カメラを起動してから写真を撮影するまでのコードが抜けていたので、追加した。
test
CHANGED
File without changes
|
test
CHANGED
@@ -102,14 +102,6 @@
|
|
102
102
|
|
103
103
|
|
104
104
|
|
105
|
-
画像をURLで指定してAPIリクエストした際は、Google Cloud Platformコンソール画面にて、届いたAPIリクエストの数が1つ増えていることが確認できました。
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
このことからもAPIリクエストが届いていることがわかります。
|
110
|
-
|
111
|
-
|
112
|
-
|
113
105
|
### 【できなかったこと】
|
114
106
|
|
115
107
|
iOSアプリから起動したiPhoneカメラで撮ったものを、base64でSrting型に変換したものをAlamofireを使ってAPIリクエストした。
|
@@ -150,14 +142,80 @@
|
|
150
142
|
|
151
143
|
|
152
144
|
|
153
|
-
### 【
|
145
|
+
### 【その他の】
|
154
|
-
|
146
|
+
|
155
|
-
|
147
|
+
### カメラを起動し、写真を撮るコード
|
156
148
|
|
157
149
|
```Swift
|
158
150
|
|
159
151
|
//
|
160
152
|
|
153
|
+
//
|
154
|
+
|
155
|
+
// CameraViewController.swift
|
156
|
+
|
157
|
+
//
|
158
|
+
|
159
|
+
//
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
// MARK: AVCapturePhotoCaptureDelegateデリゲートメソッド
|
164
|
+
|
165
|
+
extension CameraViewController: AVCapturePhotoCaptureDelegate{
|
166
|
+
|
167
|
+
// 撮影した画像データが生成されたときに呼び出されるデリゲートメソッド
|
168
|
+
|
169
|
+
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
|
170
|
+
|
171
|
+
if let imageData = photo.fileDataRepresentation() {
|
172
|
+
|
173
|
+
// Data型をUIImageオブジェクトに変換
|
174
|
+
|
175
|
+
let uiImage = UIImage(data: imageData)
|
176
|
+
|
177
|
+
// 写真ライブラリに画像を保存
|
178
|
+
|
179
|
+
UIImageWriteToSavedPhotosAlbum(uiImage!, nil,nil,nil) // 正常にライブラリに保存されることを確認済みです
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
// こっから下はGoogle Cloud Vision APIのdetectBoundingBoxes()を呼び出している
|
184
|
+
|
185
|
+
GoogleCloudOCR().detect(from: uiImage!) { ocrResult in
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
// self.activityIndicator.stopAnimating() //読み込み中画面を使う場合はコメントアウトを消す
|
190
|
+
|
191
|
+
guard let ocrResult = ocrResult else {
|
192
|
+
|
193
|
+
fatalError("Did not recognize any text in this image")
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
print(ocrResult)
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
### 【Google CloudにAPIリクエストするコード】
|
214
|
+
|
215
|
+
```Swift
|
216
|
+
|
217
|
+
//
|
218
|
+
|
161
219
|
// GoogleCloudOCR.swift
|
162
220
|
|
163
221
|
//
|
1
見出しの追加と、説明の補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
【ゴール】
|
1
|
+
### 【ゴール】
|
2
2
|
|
3
3
|
Alamofireを用いてGoogle Cloud Vision APIに画像をPOSTし、画像から検出されて返される文字列を取得すること。
|
4
4
|
|
@@ -8,9 +8,9 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
【できたこと①】
|
11
|
+
### 【できたこと①】
|
12
|
-
|
12
|
+
|
13
|
-
・以下のように画像URLを渡すと、APIリクエストが成功し、画像に含まれる文字列を認識してちゃんと返してくれました。
|
13
|
+
・以下のように"source"に画像URLを渡すと、APIリクエストが成功し、画像に含まれる文字列を認識してちゃんと返してくれました。
|
14
14
|
|
15
15
|
```Swift
|
16
16
|
|
@@ -42,13 +42,27 @@
|
|
42
42
|
|
43
43
|
```
|
44
44
|
|
45
|
-
【できたこと②】
|
45
|
+
### 【できたこと②】
|
46
46
|
|
47
47
|
・iPhoneカメラで撮った画像をbase64でString型に変換することも、できていました。
|
48
48
|
|
49
49
|
```Swift
|
50
50
|
|
51
|
+
// 画像をbae64でエンコードする
|
52
|
+
|
53
|
+
private func base64EncodeImage(_ image: UIImage) -> String? {
|
54
|
+
|
55
|
+
return image.pngData()?.base64EncodedString(options: .endLineWithCarriageReturn)
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
```Swift
|
62
|
+
|
63
|
+
// 画像に含まれるテキストを検出する
|
64
|
+
|
51
|
-
|
65
|
+
func detect(from image: UIImage, completion: @escaping (OCRResult?) -> Void) {
|
52
66
|
|
53
67
|
|
54
68
|
|
@@ -60,6 +74,10 @@
|
|
60
74
|
|
61
75
|
}
|
62
76
|
|
77
|
+
|
78
|
+
|
79
|
+
// Google Vision APIに、base64でエンコード済みのデータを渡す
|
80
|
+
|
63
81
|
callGoogleVisionAPI(with: base64Image, completion: completion)
|
64
82
|
|
65
83
|
|
@@ -78,7 +96,7 @@
|
|
78
96
|
|
79
97
|
```Swift
|
80
98
|
|
81
|
-
The type of base64Image is String
|
99
|
+
The type of base64Image is String // 画像がString型になっている
|
82
100
|
|
83
101
|
```
|
84
102
|
|
@@ -92,7 +110,7 @@
|
|
92
110
|
|
93
111
|
|
94
112
|
|
95
|
-
【できなかったこと】
|
113
|
+
### 【できなかったこと】
|
96
114
|
|
97
115
|
iOSアプリから起動したiPhoneカメラで撮ったものを、base64でSrting型に変換したものをAlamofireを使ってAPIリクエストした。
|
98
116
|
|
@@ -132,7 +150,7 @@
|
|
132
150
|
|
133
151
|
|
134
152
|
|
135
|
-
【コード全文】
|
153
|
+
### 【コード全文】
|
136
154
|
|
137
155
|
参考までに、コード全文を載せさせていただきます。
|
138
156
|
|