Implementation of Python in swift using TensorFlow
Updated 26 February 2021
Save
Python is an interpreted, high-level and general-purpose programming language. Python was created in the late 1980s, and first released in 1991, by Guido van Rossum as a successor to the ABC programming language. Mainly Python used for machine learning in data science.
Apple doesn’t support Python directly. TensorFlow provides a next-generation platform for machine learning. TensorFlow provides the framework to Implement interpreted python in ios both Swift & Objective-C. Please visit the TensorFlow website to know more about this framework.
In this blog, I will show you how can match 2 faces using python. To implement this we need a .tflite file where we have already implement python code and compressed it. I will share my blog to create .tflite from TensorFlow.
Let’s Start
Swift version: 5.2
iOS version: 14
Xcode: 12.0
First Create a project from Xcode.
File ▸ New ▸ Project…. Choose the iOS ▸ Application ▸ Single View App template and create a new project.
Second, add TensorFlowLiteSwift to your project.
1
pod'TensorFlowLiteSwift'
Add MobileFace.tflite in your project. This file I will share below.
Now create a class with the name MobileFace.
import TensorFlowLite in this class.
Now add some variable which is used in our class.
1
2
3
4
5
6
privatevarinterpreter:Interpreter?
privatevarfileName="MobileFace"
privatevarfileType="tflite"
privatevarimageWidth=128
privatevarimageHeight=128
privatevarsize=192
Now initialize the Interpreter object.
1
2
3
4
5
6
7
8
9
init(){
guard let pPath=Bundle.main.path(forResource:fileName,ofType:fileType)else{
Specializes in Core Data, GCD, Swift and Storyboards. Expert in Firebase Push Notifications, threading and performance tuning. Proficient in managing Apple Developer Account and optimizing iOS app performance.
Be the first to comment.