This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double potentialEnergy(double mass, double height) { | |
return mass * 9.81 * height; | |
} | |
//becomes this | |
static final double GRAVITATIONAL_CONSTANT = 9.81; | |
double potentialEnergy(double mass, double height) { | |
return mass * GRAVITATIONAL_CONSTANT * height; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Firebase | |
class ChooseImageViewController: UIViewController { | |
let picker = UIImagePickerController() | |
lazy var vision = Vision.vision() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
picker.delegate = self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: ML Kit label detect | |
extension ChooseImageViewController{ | |
func labelImage(image : UIImage){ | |
let labelDetector = vision.labelDetector() | |
let visionImage = VisionImage(image: image) | |
labelDetector.detect(in: visionImage){ (labels, error) in | |
guard error == nil , let labels = labels, !labels.isEmpty else { | |
self.showError(errorMessage: error?.localizedDescription ?? "Something went wrong") | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//MARK: UIImagePickerController's delegate | |
extension ChooseImageViewController : UIImagePickerControllerDelegate,UINavigationControllerDelegate{ | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage { | |
self.labelImage(image: image) | |
} | |
picker.dismiss(animated: true, completion: nil) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//MARK: UI | |
extension ChooseImageViewController{ | |
func showError(errorMessage : String){ | |
let alertController = UIAlertController(title: "Error", message: errorMessage, preferredStyle: .alert) | |
let action = UIAlertAction(title: "dismiss", style: .default) | |
alertController.addAction(action) | |
self.present(alertController, animated: true, completion: nil) | |
} | |
func showResultScreen(image : UIImage,resultString : String){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Represents a label for an image. | |
*/ | |
NS_SWIFT_NAME(VisionLabel) | |
@interface FIRVisionLabel : NSObject | |
/** | |
* The rectangle that holds the discovered label relative to the detected image in the view | |
* coordinate system. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun onImageSelected(uri: Uri) { | |
var image: FirebaseVisionImage | |
try { | |
image = FirebaseVisionImage.fromFilePath(context!!, uri) | |
val detector = FirebaseVision.getInstance().getVisionLabelDetector() | |
detector.detectInImage(image) | |
.addOnSuccessListener { | |
var output = "" | |
it.forEach { | |
output += it.label + " " + it.confidence + "\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun showResultScreen(uri : Uri, label : String){ | |
val direction = ChooseImageFragmentDirections | |
.action_chooseImageFragment_to_resultFragment(uri.toString(),label) | |
Navigation.findNavController(activity!!,R.id.my_nav_host_fragment) | |
.navigate(direction) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
arguments?.let { | |
val args = ResultFragmentArgs.fromBundle(it) | |
imageView.setImageURI(args.uri.toUri()) | |
textView.text = args.output | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation 'com.google.firebase:firebase-core:15.0.0' | |
implementation 'com.google.firebase:firebase-ml-vision:15.0.0' | |
implementation 'com.google.firebase:firebase-ml-vision-image-label-model:15.0.0' |
OlderNewer