<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rumesh Buddhike on Medium]]></title>
        <description><![CDATA[Stories by Rumesh Buddhike on Medium]]></description>
        <link>https://medium.com/@rumesh.b.k?source=rss-c0113ae44d11------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ag5CbutT0xJAiOa-O9WgMA.jpeg</url>
            <title>Stories by Rumesh Buddhike on Medium</title>
            <link>https://medium.com/@rumesh.b.k?source=rss-c0113ae44d11------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 19:43:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rumesh.b.k/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Image Recognition With CoreML And Swift]]></title>
            <link>https://medium.com/@rumesh.b.k/image-recognition-with-coreml-and-swift-f336a18804bd?source=rss-c0113ae44d11------2</link>
            <guid isPermaLink="false">https://medium.com/p/f336a18804bd</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[image-recognition]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[coreml]]></category>
            <dc:creator><![CDATA[Rumesh Buddhike]]></dc:creator>
            <pubDate>Wed, 18 Aug 2021 16:01:00 GMT</pubDate>
            <atom:updated>2021-08-18T16:01:00.882Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to work with CoreML framework using Xcode 12 and swift</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XcmU1JhBQbMqrSX2" /><figcaption>Photo by <a href="https://unsplash.com/@ikukevk?utm_source=medium&amp;utm_medium=referral">Kevin Ku</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>CoreML framework is Apple’s one of the framework that allows iOS developers to achieve machine learning capabilities locally on the app , so developersdo not need to connect with a server to process data and get a result back . The other framework is CreateML and which allows you to create your own Modal . In this particular article will focus on CoreML framework which has lots of Models for your usage.</p><h4>What we’re gonna do?</h4><p>We will be making a small app that will capture an image of your surrounding or you can make it as an image from photo library, that will process locally on the Apple device using CoreML framework and Vision, and let you have the results as the identification of the image. We will be using MobileNetV2 model .So let’s dig in.</p><h4>First , Let’s create user interface</h4><p>As user interface, will go for a simple one, which just have a UIImageView to hold the image we capture , UIButton for the photo library or capture an image and finally a label to print the processed results. There would be additional UIViews for holding the outlets and for constraints purposes. So I will add three UIOutlets and initialise them with things like UIColor , textAlignment etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mpTDt4xLGxBUpN09FzYpyw.png" /></figure><h4>Setting the Constraints programmatically using AutoLayout</h4><p>Using AutoLayout you can set the constraints as you would like , here I set the constraints programmatically inside setupUI method and second image shows you how the UI seen on the emulator. As you can see in the below image there are two UIViews, upperView and bottomView to hold the outlets and upperView holds the imageView and label and the bottom view holds the button , you can find out how constraints are done with the image below for this particular UI .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dzfr-XQYQBMhMhFPTCVyWw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WP-GzAI3bNAnHijwOzN9RA.png" /></figure><h4>Making image picker to capture or choose image</h4><p>After setting up the UI and set constraints let’s move onto the image picker which allows to choose an image from photoLibrary or capture one using camera. So first we would have to extend <strong>UIImagePickerControllerDelegate</strong> and <strong>UINavigationControllerDelegate</strong> (<em>no 1 in the image</em>). After that create an object using <strong>UIImagePickerController()</strong>class (<em>no 2 in the image</em>) and set delegate to the current view controller which is ViewController using self keyword and then you have to set imagePicker source type as .<strong>camera</strong> or .<strong>photoLibrary</strong> as per your choice (<em>no 3 in the image</em>) , if you are using .<strong>camera</strong> which means you are accessing device camera so you have to add Privacy settings using <strong>info.plist</strong> file which exists in project navigator panel as per seen in the second image below</p><p>After setting those things let’s move onto add a target or action to the button in order to show photoLibrary or camera to take an image , for this use <strong>addTarget()</strong> to the button element and set selector method name, then set action as .<strong>touchUpInside</strong> as shown in the <em>no 4 in below image</em>.</p><p>After setting the action method let’s perform imagepicker using present view controller .(<em>no 5 in the image</em>) Now you can see the photoLibrary or camera will popup regarding of what you set to the source type and you can choose an image or capture an image now (Third image below)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P9eqr8yk4owjUWIpuJjylw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KxqpF_IxJqtt-vobX8U1Sw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*L8rXoqS8uHDpwBn0HKjhWQ.png" /></figure><h4>Use UIImagePickerControllerDelegate method to get captured image</h4><p>After choose or capture image the image will be set at <em>didFinishPickingMediaWithInfo </em>delegate method which comes from <em>UIImagePickerControllerDelegate()</em> protocol. Using this delegate method we have to take the image from <em>info</em>object array. Don’t forget to dismiss the imagePicker inside the method.</p><p>There after take the image to a variable using [.originalImage] and don’t forget to downcast into UIImage in order to set the image in the imageView and same time bind the statement using guard statement as it is an optional value. After that set the image to the imageView as it shown in the below image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ttdmgeC1qVSesH2Yk89Ew.png" /></figure><p><strong>Convert UIImage to CIImage in order to work with coreML framework</strong></p><p>In order to do this you have to import <strong>Vision</strong> framework. It will support CIImage. Same time make sure to import <strong>CoreML </strong>framework as well as shown in below image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p5kAV8gvBItvnResCaDFDw.png" /></figure><p>After that just convert the image into a CIImage like in the below image and same time create a method as processImage and pass the converted CIImage to that method, so that we can process image using CoreML framwork.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nu58IeCp4xsSgq1AJKoScw.png" /></figure><p><strong>Adding CoreML model to the project</strong></p><p>We have done picking image and convert it into a CIImage. Now we have to put a coreML model to the project in order to process the image. For this, Apple has made numerous models. We can pick a model and have so many machine learning capabilities to ensure we have right pick.</p><p>For image recognition part we have to pick one of image classification models and Apple has developed some image classification models for us. Among those we have to choose small model for our scenario and this it will be <a href="https://developer.apple.com/machine-learning/models/"><strong>MobileNetV2</strong></a> image classification model considering light on weight.</p><p><a href="https://developer.apple.com/machine-learning/models/">Models - Machine Learning - Apple Developer</a></p><p>You can compare and see the models Apple has developed for us by clicking above link and also do not forget to download <a href="https://developer.apple.com/machine-learning/models/"><strong>MobileNetV2</strong></a> model as shown in the below image in order to continue this project .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y4kPDe6QutumBw3azJHIUQ.png" /></figure><p>After download the model you have to put it to the project simply by drag and drop into the project navigator. Then build the project after that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qQy5DpdO0ZDmaegoD2O1sg.png" /></figure><h4>Let’s Process Image using MobileNetV2 Model</h4><p>Now it’s time to work with model we downloaded, first let’s demonstrate briefly what we have to do in order to take a result of the image.</p><p>First you have to create instance of the model (<em>no 1 in the below image</em>) class you downloaded using <strong>VNCoreMLModel() </strong>and create a request using <strong>VNCoreMLRequest()</strong>(<em>no 2 in the below image</em>).<strong> </strong>Use the model we created and give the model instance to the request . After that create a handler using <strong>VNImageRequestHandler() </strong>to perform the request (<em>no 3 in the below image</em>)</p><h4>Take the results and show</h4><p>After perform the request, if success, the results contain <strong>VNClassificationObservation </strong>objects that will show possible classifications from the CoreML model. As in below code make sure to handle the errors accordingly. (<em>no 4 in the below image</em>)</p><p>After taking the results back we have to sort out the results objects. From the coreML model it will prompt series of results according to the confidence towords the image , meaning that the model process the image and with the data it has, it will sort out the results as its confidents towords the certain objects. Model send its results with confidents level from 0 to 1. The highest confidence level object come first in the <strong>VNClassificationObservation</strong> object array. So what we have to do is get the first result and its identification and set them into the label to show the result or image identification to the user. (<em>no 5 in the below</em>)</p><p>You can refer apple documentation and their sample code <a href="https://developer.apple.com/documentation/vision/classifying_images_with_vision_and_core_ml">here</a> , they explain how you should access the MobileNetV2 model , you can always refer apple documentations to understand their frameworks.</p><p><a href="https://developer.apple.com/documentation/vision/classifying_images_with_vision_and_core_ml">Classifying Images with Vision and Core ML | Apple Developer Documentation</a></p><h4>Finally the Output</h4><p>So after arranging the code to get results from CoreML model we can get a fine output for the images we input to the model , as we can see below image, the CoreML model here can identify the objects correctly , but some times these outputs might not be accurate as it would like it to be.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wODLn4Wkr5DJQ-ldwfc2kg.png" /></figure><p>I hope you find this article usefull and hope you guys use CoreML models for your upcoming projects.</p><p>You can have the full project here :</p><p><a href="https://github.com/RumeshBuddhike/coreml_image_identifier">GitHub - RumeshBuddhike/coreml_image_identifier: This is a iOS app project that used coreML MobileNetV2 model to identifying images</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f336a18804bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>