-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
585 lines (458 loc) · 21.2 KB
/
ViewController.swift
File metadata and controls
585 lines (458 loc) · 21.2 KB
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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//
// ViewController.swift
// BitGrader2
//
// Created by American Code on 10/29/18.
// Copyright © 2018 American Code and Dev LLC. All rights reserved.
//
import UIKit
import AVFoundation
import Vision
import CoreLocation
import Foundation
import MessageUI
extension CGRect{
init(_ x:CGFloat,_ y:CGFloat,_ width:CGFloat,_ height:CGFloat) {
self.init(x:x,y:y,width:width,height:height)
}
}
extension CGSize{
init(_ width:CGFloat,_ height:CGFloat) {
self.init(width:width,height:height)
}
}
extension CGPoint{
init(_ x:CGFloat,_ y:CGFloat) {
self.init(x:x,y:y)
}
}
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, CLLocationManagerDelegate, MFMailComposeViewControllerDelegate, UITextFieldDelegate {
let label: UILabel = {
let label = UILabel()
label.textColor = .white
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Press Grade to Classify Bit"
label.font = label.font.withSize(60)
return label
}()
let label2: UILabel = {
let label2 = UILabel()
label2.textColor = .white
label2.translatesAutoresizingMaskIntoConstraints = false
label2.text = "Test"
label2.font = label2.font.withSize(15)
return label2
}()
let label3: UILabel = {
let label3 = UILabel()
label3.textColor = .white
label3.translatesAutoresizingMaskIntoConstraints = false
label3.text = "Email?"
label3.font = label3.font.withSize(15)
return label3
}()
@objc func buttonClicked() {
print("Button Clicked")
// if view.tag == 3 {
// view.removeFromSuperview()
// }
setupCaptureSession()
view.addSubview(label)
setupLabel()
// label.font = label.font.withSize(60)
// setupLabel3()
}
@objc func buttonClicked2() {
// view.removeFromSuperView()
self.label3.textColor = .green
self.label2.textColor = .green
self.label.textColor = .green
assignbackground2()
view.addSubview(label)
view.addSubview(button)
view.addSubview(button2)
view.addSubview(button3)
view.addSubview(button4)
view.addSubview(label2)
view.addSubview(label3)
setupLabel()
setupLabel2()
setupLabel3()
button2.backgroundColor = .red
button3.backgroundColor = .red
}
@objc func buttonClicked3() {
// view.removeFromSuperView()
self.label3.textColor = .white
self.label2.textColor = .white
self.label.textColor = .white
assignbackground()
view.addSubview(label)
view.addSubview(button)
view.addSubview(button2)
view.addSubview(button3)
view.addSubview(button4)
view.addSubview(label2)
view.addSubview(label3)
setupLabel()
setupLabel2()
setupLabel3()
button2.backgroundColor = .red
button3.backgroundColor = .red
}
@objc func buttonClicked4() {
if let url = URL(string: "https://frackenstein.github.io"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:])
}
}
let dFrom: UITextField = {
let dFrom = UITextField(frame: CGRect(x: 800, y: 200, width: 100, height: 40))
dFrom.placeholder = "Depth From"
dFrom.font = UIFont.systemFont(ofSize: 15)
dFrom.borderStyle = UITextField.BorderStyle.roundedRect
dFrom.autocorrectionType = UITextAutocorrectionType.no
dFrom.keyboardType = UIKeyboardType.default
dFrom.returnKeyType = UIReturnKeyType.done
dFrom.clearButtonMode = UITextField.ViewMode.whileEditing;
dFrom.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
return dFrom
// dFrom.delegate = self as UITextFieldDelegate
}()
let dTo: UITextField = {
let dTo = UITextField(frame: CGRect(x: 800, y: 300, width: 100, height: 40))
dTo.placeholder = "Depth To"
dTo.font = UIFont.systemFont(ofSize: 15)
dTo.borderStyle = UITextField.BorderStyle.roundedRect
dTo.autocorrectionType = UITextAutocorrectionType.no
dTo.keyboardType = UIKeyboardType.default
dTo.returnKeyType = UIReturnKeyType.done
dTo.clearButtonMode = UITextField.ViewMode.whileEditing;
dTo.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
return dTo
// dTo.delegate = self as UITextFieldDelegate
}()
let button: UIButton = {
let button = UIButton(frame: CGRect(x: 125, y: 80, width: 100, height: 50))
button.backgroundColor = .red
//button.setTitle("Upload", for: .normal)
button.setTitle("Grade", for: [])
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
return button
//button.addTarget(self, action:#selector(buttonClicked), for: .touchUpInside)
}()
let button2: UIButton = {
let button2 = UIButton(frame: CGRect(x: 800, y: 500, width: 100, height: 50))
button2.backgroundColor = .red
//button.setTitle("Upload", for: .normal)
button2.setTitle("Dark?", for: [])
button2.addTarget(self, action: #selector(buttonClicked2), for: .touchUpInside)
return button2
//button.addTarget(self, action:#selector(buttonClicked), for: .touchUpInside)
}()
let button3: UIButton = {
let button3 = UIButton(frame: CGRect(x: 800, y: 570, width: 100, height: 50))
button3.backgroundColor = .red
//button.setTitle("Upload", for: .normal)
button3.setTitle("Light?", for: [])
button3.addTarget(self, action: #selector(buttonClicked3), for: .touchUpInside)
return button3
//button.addTarget(self, action:#selector(buttonClicked), for: .touchUpInside)
}()
let button4: UIButton = {
let button4 = UIButton(frame: CGRect(x: 900, y: 650, width: 70, height: 50))
button4.backgroundColor = .blue
//button.setTitle("Upload", for: .normal)
button4.setTitle("Info", for: [])
button4.addTarget(self, action: #selector(buttonClicked4), for: .touchUpInside)
return button4
//button.addTarget(self, action:#selector(buttonClicked), for: .touchUpInside)
}()
func assignbackground(){
let background1 = UIImage(named: "green-grass-field.jpg")
var imageView : UIImageView!
imageView = UIImageView(frame: view.bounds)
imageView.contentMode = UIView.ContentMode.scaleAspectFill
imageView.clipsToBounds = true
imageView.image = background1
imageView.center = view.center
view.addSubview(imageView)
// self.view.sendSubviewToBack(imageView)
self.view.addSubview(dFrom)
self.view.addSubview(dTo)
var msg = UserDefaults.standard.bool(forKey: "msg")
let switchDemo=UISwitch(frame:CGRect(x: self.view.bounds.width - 100, y: 100, width: 30, height: 10))
switchDemo.addTarget(self, action: #selector(ViewController.switchStateDidChange(_:)), for: .valueChanged)
if msg == true{
switchDemo.setOn(true, animated: false)
}
if msg == false{
switchDemo.setOn(false, animated: false)
}
view.addSubview(switchDemo)
}
func assignbackground2(){
let background = UIImage(named: "dark.jpg")
var imageView2 : UIImageView!
imageView2 = UIImageView(frame: view.bounds)
imageView2.contentMode = UIView.ContentMode.scaleAspectFill
imageView2.clipsToBounds = true
imageView2.image = background
imageView2.center = view.center
view.addSubview(imageView2)
self.view.addSubview(dFrom)
self.view.addSubview(dTo)
self.view.addSubview(button4)
var msg = UserDefaults.standard.bool(forKey: "msg")
let switchDemo=UISwitch(frame:CGRect(x: self.view.bounds.width - 100, y: 100, width: 30, height: 10))
switchDemo.addTarget(self, action: #selector(ViewController.switchStateDidChange(_:)), for: .valueChanged)
if msg == true{
switchDemo.setOn(true, animated: false)
}
if msg == false{
switchDemo.setOn(false, animated: false)
}
view.addSubview(switchDemo)
}
override func viewDidLoad() {
super.viewDidLoad()
var msg = UserDefaults.standard.bool(forKey: "msg")
// var msg2 = UserDefaults.standard.bool(forKey: "drk")
// var returnValue: [Bool]? = UserDefaults.standard.object(forKey: "msg") as? [Bool]
// self.view.backgroundColor = UIColor(patternImage: UIImage(named: "metal.jpg")!)
assignbackground()
let switchDemo=UISwitch(frame:CGRect(x: self.view.bounds.width - 100, y: 100, width: 30, height: 10))
switchDemo.addTarget(self, action: #selector(ViewController.switchStateDidChange(_:)), for: .valueChanged)
if msg == true{
switchDemo.setOn(true, animated: false)
}
if msg == false{
switchDemo.setOn(false, animated: false)
}
view.addSubview(switchDemo)
// let switchDemo2=UISwitch(frame:CGRect(x: self.view.bounds.width - 100, y: 200, width: 30, height: 10))
// switchDemo2.addTarget(self, action: #selector(ViewController.switchStateDidChange(_:)), for: .valueChanged)
// if msg2 == true{
// switchDemo2.setOn(true, animated: false)
// assignbackground2()
// }
// if msg2 == false{
// switchDemo2.setOn(false, animated: false)
//// viewDidLoad()
// }
// view.addSubview(switchDemo2)
self.view.addSubview(dFrom)
self.view.addSubview(dTo)
// let imageName = "Icon-76@2x.png"
// let image = UIImage(named: imageName)
// let imageView = UIImageView(image: image!)
// imageView.frame = CGRect(x: self.view.bounds.width/2 - 38, y: self.view.bounds.height/2 - 38, width: 76, height: 76)
// imageView.tag = 3
// view.addSubview(imageView)
let locationManager = CLLocationManager()
// Ask for Authorisation from the User.
locationManager.requestAlwaysAuthorization()
// For use in foreground
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
let user_lat = String(format: "%f", locationManager.location!.coordinate.latitude)
let user_long = String(format: "%f", locationManager.location!.coordinate.longitude)
let first = user_lat + ", " + user_long
//let second = "\(user_lat), \(user_long)"
label2.text = first
locationManager.stopUpdatingLocation()
}
view.addSubview(label)
view.addSubview(button)
view.addSubview(button2)
view.addSubview(button3)
view.addSubview(button4)
view.addSubview(label2)
view.addSubview(label3)
setupLabel()
setupLabel2()
setupLabel3()
}
@objc func switchStateDidChange(_ sender:UISwitch!)
{
if (sender.isOn == true){
print("UISwitch state is now ON")
UserDefaults.standard.set(true, forKey: "msg") //Bool
}
else{
print("UISwitch state is now Off")
UserDefaults.standard.set(false, forKey: "msg") //Bool
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
label2.text = "locations = \(locValue.latitude) \(locValue.longitude)"
}
func setupCaptureSession() {
let captureSession = AVCaptureSession()
// search for available capture devices
let availableDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices
// setup capture device, add input to our capture session
do {
if let captureDevice = availableDevices.first {
let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)
captureSession.addInput(captureDeviceInput)
//--------------------------------------------------------------------------------------
}
} catch {
print(error.localizedDescription)
}
// setup output, add output to our capture session
let captureOutput = AVCaptureVideoDataOutput()
captureOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
captureSession.addOutput(captureOutput)
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.frame
view.layer.addSublayer(previewLayer)
//Add Rectangle
let cgRect = CGRect(x: 375, y: 200, width: self.view.bounds.width/4, height: self.view.bounds.height/2)
let myView = UIImageView()
myView.frame = cgRect
myView.backgroundColor = UIColor.clear
myView.isOpaque = false
myView.layer.cornerRadius = 10
myView.layer.borderColor = UIColor.yellow.cgColor
myView.layer.borderWidth = 3
myView.layer.masksToBounds = true
previewLayer.addSublayer(myView.layer)
captureSession.startRunning()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { // change 5 to desired number of seconds
// Your code with delay
self.takeScreenshot(true)
captureSession.stopRunning()
//self.sendMail()
}
// DispatchQueue.main.asyncAfter(deadline: .now() + 6) { // change 5 to desired number of seconds
// // Your code with delay
// self.takeScreenshot(true)
// }
}
// called everytime a frame is captured
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let model = try? VNCoreMLModel(for: BitClassifier().model) else {return}
var readings1:NSMutableArray = []
var readings2:NSMutableArray = []
let request = VNCoreMLRequest(model: model) { (finishedRequest, error) in
let textView = UITextView(frame: CGRect(x: 40.0, y: 300.0, width: 250.0, height: 400.0))
//self.automaticallyAdjustsScrollViewInsets = false
//textView.center = self.view.center
textView.textAlignment = NSTextAlignment.justified
textView.textColor = UIColor.red
textView.backgroundColor = UIColor.black
//self.view.layer.removeFromSuperlayer()
self.view.addSubview(textView)
guard let results = finishedRequest.results as? [VNClassificationObservation] else { return }
guard let Observation = results.first else { return }
// var sum1 = 0.0 as! Float
// var sum2 = 0.0 as! Float
DispatchQueue.main.async(execute: {
self.label.text = "\(Observation.identifier)"
let temp = "\(Observation.identifier)"
let scoreArray = temp.components(separatedBy: "-")
//cummulate readings here
let numberFormatter = NumberFormatter()
let number1 = numberFormatter.number(from: scoreArray[0])
let value0 = Float(scoreArray[0]) ?? 0.0
let number1FloatValue = number1?.floatValue
let number2 = numberFormatter.number(from: scoreArray[1])
let value1 = Float(scoreArray[1]) ?? 0.0
let number2FloatValue = number2?.floatValue
readings1.add(number1FloatValue)
readings2.add(number2FloatValue)
// sum1 = value0 + sum1
// sum2 = value0 + sum2
// var sum = 0 as! Float
// var counter = 0
// while counter < readings1.count {
// if let str = readings1[counter] as? String, let i = Int(str) {
// // do what you want with i
// var newValue = readings1[counter] as! Float
// sum = newValue + sum
// counter += 1
// }
//
// }
// print(sum)
let count1 = readings1.count
let count2 = readings2.count
textView.text = "Scan Complete! \n Location: \n \(self.label2.text!) \n Inner: \(readings1[0]) \n Outer: \(readings2[0]) \n Depth From: \(self.dFrom.text!) \n Depth To: \(self.dTo.text!)"
print(readings1)
print(readings2)
})
}
guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
// executes request
try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
}
func setupLabel() {
label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
}
// x: self.view.bounds.width - 100, y: 100
func setupLabel3() {
label3.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 360).isActive = true
label3.bottomAnchor.constraint(equalTo: view.centerYAnchor, constant: -260).isActive = true
// label3.center = CGPoint(x: 300, y: 100)
}
func setupLabel2() {
label2.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -330).isActive = true
label2.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -600).isActive = true
}
func takeScreenshot(_ shouldSave: Bool = true) -> UIImage? {
var screenshotImage :UIImage?
let layer = UIApplication.shared.keyWindow!.layer
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
guard let context = UIGraphicsGetCurrentContext() else {return nil}
layer.render(in:context)
screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let image = screenshotImage, shouldSave {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
var msg = UserDefaults.standard.bool(forKey: "msg")
// var returnValue: [Bool]? = UserDefaults.standard.object(forKey: "msg") as? [Bool]
if msg == true{
sendMail(imageView: screenshotImage!)
}
return screenshotImage
}
func sendMail(imageView: UIImage) {
//func sendMail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self;
mail.setCcRecipients(["drilling@dvn.com"])
mail.setSubject("Updated Drill Bit Grading \(self.label2.text!)")
mail.setMessageBody("Grading: \(self.label.text!) \n Depth From: \(self.dFrom.text!) \n Depth To: \(self.dTo.text!)", isHTML: false)
let imageData: NSData = imageView.pngData()! as NSData
mail.addAttachmentData(imageData as Data, mimeType: "image/png", fileName: "Test.png")
self.present(mail, animated: true, completion: nil)
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result.rawValue {
case MFMailComposeResult.cancelled.rawValue :
print("Cancelled")
case MFMailComposeResult.failed.rawValue :
print("Failed")
case MFMailComposeResult.saved.rawValue :
print("Saved")
case MFMailComposeResult.sent.rawValue :
print("Sent")
default: break
}
self.dismiss(animated: true, completion: nil)
}
}