-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuntitled.SWIFT
166 lines (140 loc) · 4.84 KB
/
untitled.SWIFT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
///////////////////
// NOTIFICATIONS //
///////////////////
// querying permission for notifications
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.getNotificationSettings { (settings) in
// Do not schedule notifications if not authorized.
guard settings.authorizationStatus == .authorized else {return}
if settings.alertSetting == .enabled {
// Schedule an alert-only notification.
self.alertOnlyFunc()
}
else {
// Schedule a notification with a badge and sound.
self.badgeAndSoundFunc()
}
}
// creating the notofication
// first, content
let content = UNNotificationContent()
content.title = "Weekly Staff Meeting"
content.body = "Every Tuesday at 2pm"
// let content = UNMutableNotificationContent()
// NSString.localizedUserNotificationStringForKey("Hello!", arguments: nil)
// in case the contents change between now and later
content.sound = UNNotificationSound.default()
// second, trigger
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
// third, assign to request
// identifier can be used to remove notifications
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)
// finnally, schedule
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
if error != nil {
// Handle any errors
}
}
////////////////////
// TEXT DETECTION //
////////////////////
// add firebase https://firebase.google.com/docs/ios/setup
// add to podfile:
pod 'Firebase/Core'
pod 'Firebase/MLVision'
# If using an on-device API:
pod 'Firebase/MLVisionTextModel'
// back to SWIFT
import Firebase
let vision = Vision.vision()
let textRecognizer = vision.cloudTextRecognizer()
//let textRecognizer = vision.onDeviceTextRecognizer()
// OPTIONAL language hints
let options = VisionCloudTextRecognizerOptions()
options.languageHints = ["en", "hi"]
let textRecognizer = vision.cloudTextRecognizer(options: options)
// first rotate the UIImage
let image = VisionImage(image: uiImage)
// recognise
textRecognizer.process(visionImage) { result, error in
guard error == nil, let result = res else {
// ...
let resultText = res.text
for block in res.blocks {
let blockText = block.text
let blockConfidence = block.confidence
let blockLanguages = block.recognizedLanguages
let blockCornerPoints = block.cornerPoints
let blockFrame = block.frame
for line in block.lines {
let lineText = line.text
let lineConfidence = line.confidence
let lineLanguages = line.recognizedLanguages
let lineCornerPoints = line.cornerPoints
let lineFrame = line.frame
for element in line.elements {
let elementText = element.text
let elementConfidence = element.confidence
let elementLanguages = element.recognizedLanguages
let elementCornerPoints = element.cornerPoints
let elementFrame = element.frame
}
}
}
}
}
///////////////
// HTTP POST //
///////////////
// Use image name from bundle to create NSData
let image : UIImage = UIImage(named:"imageNameHere")!
// Now use image to create into NSData format
let imageData:NSData = UIImagePNGRepresentation(image)!
// converting img to base 64 str
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
// POST https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY
{
"requests":[
{
"image":
{
"content":strBase64
},
"features":[
{
"type":"TEXT_DETECTION",
"maxResults":1
}]
]
}
}
// processing result
import Foundation
let data: Data // received from a network request, for example
let json = try? JSONSerialization.jsonObject(with: data, options: [])
let dictionary = json as? [String: Any]
let response = dictionary["responses"] as? [String: Any]
let array = response as? [Any]
let responseDict = array.first as? [String: Any]
let textAnnotations = responseDict["textAnnotations"] as? [String: Any]
let annotationsArray = textAnnotations as? [Any]
var str: String = ""
for object in annotationsArray {
// access all objects in array
var dict = object as? [String: Any]
var description = dict["description"] as? [String: Any]
str.append(description)
}
// access individual object in array
//
//if let dictionary = json as? [String: Any] {
// if let response = dictionary["responses"] as? [String: Any] {
// if let array = jsonWithArrayRoot as? [Any] {
// if let firstObject = array.first {
// // access individual object in array
// }
// }
// }
//}